Repository navigation
Expand file tree
/
Copy pathSmartLSO.lua
More file actions
3349 lines (3187 loc) · 105 KB
/
Copy pathSmartLSO.lua
File metadata and controls
3349 lines (3187 loc) · 105 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
lso = {}
lso.debug = false -- **用于调试,请勿修改**
lso.dumpTrackData = false -- **DO NOT MODIFY**
-- 航母单位名称
lso.carrierName = "Mother"
-- 使用真实无线电频率 true/false
lso.useRadioFrequency = true
-- 航母无线电单位名称
lso.carrierRadioName = "Mother Radio"
-- 启用航母自动航行 true/false
lso.carrierSailing = true
-- 航母航行区域名称
lso.carrierSailArea = "Sail Area"
-- 航母航行速度(节)
lso.carrierSpeed = 25
-- 使用无线电F10二级菜单
lso.useSubMenu = false
-- 二级菜单名称
lso.subMenuName = "Carrier"
-- 音频库
lso.Sound = {
RADIO = {"l10n/DEFAULT/radio_on.ogg", 1 },
BOLTER = {"l10n/DEFAULT/bolter.ogg", 1.7 },
CALL_THE_BALL = {"l10n/DEFAULT/call_the_ball.ogg", 1.5 },
LEFT = {"l10n/DEFAULT/come_left.ogg", 1.1 },
CUT = {"l10n/DEFAULT/cut.ogg", 0.6 },
CLIMB = {"l10n/DEFAULT/dont_climb.ogg", 1.1 },
SETTLE = {"l10n/DEFAULT/dont_settle.ogg", 1.1 },
EASY = {"l10n/DEFAULT/easy_with_it.ogg", 1.5 },
FAIR = {"l10n/DEFAULT/fair.ogg", 0.6 },
FOUR_WIRES = {"l10n/DEFAULT/four_wires.ogg", 1 },
KEEP_TURN = {"l10n/DEFAULT/keep_your_turn_in.ogg", 1.4 },
LIG = {"l10n/DEFAULT/long_in_the_groove.ogg", 3.1 },
NO_GRADE = {"l10n/DEFAULT/no_grade.ogg", 0.7 },
OK = {"l10n/DEFAULT/ok.ogg", 0.6 },
ONE_WIRE = {"l10n/DEFAULT/one_wire.ogg", 0.8 },
PADDLES_CONTACT = {"l10n/DEFAULT/paddles_contact.ogg", 1.5 },
LOW = {"l10n/DEFAULT/power.ogg", 1.1 },
TOO_LOW = {"l10n/DEFAULT/power2.ogg", 1.1 },
RIGHT = {"l10n/DEFAULT/right_for_lineup.ogg", 1.4 },
ROGER_BALL = {"l10n/DEFAULT/roger_ball.ogg", 0.9 },
THREE_WIRES = {"l10n/DEFAULT/three_wires.ogg", 0.8 },
TWO_WIRES = {"l10n/DEFAULT/two_wires.ogg", 0.8 },
WAVEOFF = {"l10n/DEFAULT/waveoff.ogg", 1.7 },
FOUL_DECK = {"l10n/DEFAULT/waveoff_foul_deck.ogg", 1.8 },
FAST = {"l10n/DEFAULT/youre_fast.ogg", 1.1 },
HIGH = {"l10n/DEFAULT/youre_high.ogg", 1.1 },
SLOW = {"l10n/DEFAULT/youre_slow.ogg", 1.3 },
}
-- 数据库模块
-- 包含了所需的固定数据
lso.data = {}
lso.data.carriers = {
["Stennis"] = {
offset = {
x = -44.54,
y = 19.06,
z = -10.49,
},
deck = 9.0,
gs = 3.5,
runway = {
length = 240.0,
width = 25.0,
},
},
}
lso.data.aircrafts = {
["FA-18C_hornet"] = {
name = "hornet",
aoa = 8.1,
},
["Su-33"] = {
name = "falcon",
aoa = 9.0,
},
}
lso.data.coalitions = {
[0] = "neutrals",
[1] = "red",
[2] = "blue",
}
function lso.data.getAircraft(unit)
if (unit) then
local typeName = unit:getTypeName()
for name, data in pairs(lso.data.aircrafts) do
if (name == typeName) then
return data
end
end
end
return nil
end
function lso.print(msg, duration, useMist, name)
if lso.debug == true then
if (mist and useMist and lso.useRadioFrequency) then
mist.message.add({
text = msg,
displayTime = duration,
msgFor = {coa = {"all"}},
name = name,
})
else
trigger.action.outText(msg, duration)
end
end
end
-- 创建枚举表
EnumObj = {
new=function(self, value)
local obj = {value=value}
setmetatable(obj, {__index=self, __eq=self.equal, __tostring=self.toString, __add=self.add, __lt=self.lt})
return obj
end,
addParam=function(self, params)
for k, v in pairs(params) do
assert(self[k] == nil, string.format("invalid Enum parameter \"%s\"", k))
self[k] = v
end
return self
end,
add=function(self, another)
if another == nil or self:equal(another) then
return self
else
return EnumObj:new(self.value + another.value)
end
end,
equal=function(self, another)
return EnumObj.band(self.value, another.value) > 0
end,
lt=function(self, another)
return self.value < another.value
end,
toString=function(self)
return self.value
end,
band=function(n1, n2)
local t1 = 0
local t2 = 0
while 2 ^ t1 < n1 do t1 = t1 + 1; end
while 2 ^ t2 < n2 do t2 = t2 + 1; end
local rlt = 0
for i = math.max(t1, t2), 0, -1 do
local ex = 2 ^ i
local b1 = 0
local b2 = 0
if (n1 >= ex) then
b1 = 1
n1 = n1 % ex
end
if (n2 >= ex) then
b2 = 1
n2 = n2 % ex
end
if (b1 == 1 and b2 == 1) then
rlt = rlt + ex
end
end
return rlt
end
}
function Enum(...)
local items = {...}
if (#items == 1 and type(items[1]) == "table") then
items = items[1]
end
local enum = {}
local index = 1
for i, v in ipairs(items) do
local val = 2 ^ (index - 1)
local obj = EnumObj:new(val)
if type(items[i+1]) == "table" then
local params = items[i+1]
obj:addParam(params)
else
index = index + 1
end
enum[v] = obj
end
return enum
end
-- 模拟 switch 语句块
function switch(value, ...)
local cases = {...}
local matched = false
for i, case in ipairs(cases) do
if (matched or (case[1] == value and type(case[2]) == "function") or (#case == 1 and type(case[1]) == "function")) then
matched = true
local code = (#case == 1 and type(case[1]) == "function") and case[1] or case[2]
if (code()) then
break
end
end
end
end
-- 检测帧实现模块
lso.frameId = 1
lso.checkFrames = {}
lso.lastCheckTime = nil
function lso.addCheckFrame(frame)
assert(type(frame) == "table", "argument expected table, got " .. type(frame))
assert(frame.onFrame ~= nil and type(frame.onFrame) == "function", "didn't implement function 'onFrame'")
local id = lso.frameId
table.insert(lso.checkFrames, {frame = frame, id = id})
lso.frameId = lso.frameId + 1
return id
end
function lso.removeCheckFrame(id)
for k, v in pairs(lso.checkFrames) do
if (v.id == id) then
lso.checkFrames[k] = nil
end
end
end
function lso.doFrame(arg, frameTime)
lso.lastCheckTime = timer.getTime()
lso.print("检查帧工作中", 5, true, "CheckFrameWorking")
local i = 1
while i <= #lso.checkFrames do
if lso.checkFrames[i] ~= nil then
local status, err = pcall(function()
lso.checkFrames[i].frame:onFrame(frameTime)
return true
end)
if (not status) then
lso.print(err, 5, true, "checkFrameError")
env.error(err)
return nil
end
end
i = i + 1
end
return timer.getTime() + 2
end
lso.resetTime = nil
function lso.autoReset(arg, frameTime)
if (lso.resetTime == nil) then
if (timer.getTime() - lso.lastCheckTime > 10) then
lso.resetTime = 5
trigger.action.outText(string.format("Smart-LSO script stop working, auto-reset in %d sec.", lso.resetTime), 5)
end
else
if (lso.resetTime > 0) then
lso.resetTime = lso.resetTime - 1
else
-- 遍历初始化所有飞机状态
for unitName, plane in pairs(lso.DB.planes) do
if (plane:updateData()) then
lso.process.initPlane(plane.unit)
end
end
timer.scheduleFunction(lso.doFrame, nil, timer.getTime() + 1)
lso.resetTime = nil
trigger.action.outText("Smart-LSO script auto-reset.", 2)
end
end
return timer.getTime() + 1
end
function lso.start()
timer.scheduleFunction(lso.doFrame, nil, timer.getTime() + 1)
timer.scheduleFunction(lso.autoReset, nil, timer.getTime() + 1)
end
-- 事件广播模块
lso.Broadcast = {}
lso.Broadcast.count = 0
lso.Broadcast.event = Enum(
"INIT_PLANE", -- plane
"REMOVE_PLANE", -- plane
"INIT_MENU", -- unit
"CHECK_IN", -- plane
"IN_SIGHT", -- plane
"INITIAL", -- plane
"BREAK", -- plane
"PADDLES_CONTACT", -- plane
"ABORT", -- plane
"DEPART", -- plane
"TURNING_START", -- nil
"TURNING_STOP", -- nil
"RECOVERY_START", -- nil
"RECOVERY_STOP", -- nil
"EMERGENCY", -- plane
"TRACK_FINISH" -- trackData, result, cause, wire
)
lso.Broadcast.listeners = {}
lso.Broadcast.queue = {}
function lso.Broadcast:send(event, ...)
if event then
table.insert(self.queue, {
event = event,
data = {...},
timestamp = timer.getTime()
})
end
end
function lso.Broadcast:remove(funcOrId)
local removed = false
if funcOrId then
if (type(funcOrId) == "number") then
for i, listener in ipairs(self.listeners) do
if listener.id == funcOrId then
table.remove(self.listeners, i)
removed = true
end
end
else
for i, listener in ipairs(self.listeners) do
if listener.callback == funcOrId then
table.remove(self.listeners, i)
removed = true
end
end
end
end
return removed
end
function lso.Broadcast:receive(events, func)
if (events and type(func) == "function") then
self.count = self.count + 1
local listenerId = self.count
table.insert(self.listeners, {
id = listenerId,
events = events,
callback = func
})
return listenerId
end
end
function lso.Broadcast.loop(args, timestamp)
while #lso.Broadcast.queue > 0 do
local item = table.remove(lso.Broadcast.queue, 1)
for i, listener in ipairs(lso.Broadcast.listeners) do
if (listener.events == item.event) then
listener.callback(item.event, item.timestamp, unpack(item.data))
end
end
end
return timer.getTime() + 0.01
end
timer.scheduleFunction(lso.Broadcast.loop, nil, timer.getTime() + 1)
-- 数据库模块
lso.DB = {}
-- 飞机集合
lso.DB.planes = {}
function lso.DB.init()
local coalition = lso.data.coalitions[lso.Carrier.unit:getCoalition()]
for coaName, coaData in pairs(env.mission.coalition) do
if (coaName == coalition) then
if (coaData.country) then
for countryID, countryData in pairs(coaData.country) do
for categoryName, categoryData in pairs(countryData) do
if (categoryName == "ship") then
if (categoryData.group and type(categoryData.group) == 'table' and #categoryData.group > 0) then
for groupID, groupData in pairs(categoryData.group) do
if (groupData.units and type(groupData.units) == 'table' and #groupData.units > 0) then
for unitID, unitData in pairs(groupData.units) do
local unitName
if env.mission.version > 7 then
unitName = env.getValueDictByKey(unitData.name)
else
unitName = unitData.name
end
if (unitName == lso.Carrier.unit:getName()) then
lso.Carrier:loadTasks(groupData)
end
end
end
end
end
elseif (categoryName == "plane") then
if (categoryData.group and type(categoryData.group) == 'table' and #categoryData.group > 0) then
for groupID, groupData in pairs(categoryData.group) do
if (groupData.units and type(groupData.units) == 'table' and #groupData.units > 0) then
for unitID, unitData in pairs(groupData.units) do
if (unitData.skill == "Player" or unitData.skill == "Client") then
if (lso.data.aircrafts[unitData.type]) then
local unitName
if env.mission.version > 7 then
unitName = env.getValueDictByKey(unitData.name)
else
unitName = unitData.name
end
local aircraftData = lso.data.aircrafts[unitData.type]
local onboardNumber = unitData.onboard_num
local plane = lso.Plane:new(unitName, aircraftData, onboardNumber)
if (plane) then
lso.DB.planes[unitName] = plane
end
end
end
end
end
end
end
end
end
end
end
end
end
end
-- 航母模块
lso.Carrier = {
recovery = false, -- 是否正在回收作业
case = 1, -- 回收作业 Case 1-3
unit, -- 航母单位
radio, -- 航母无线电单位
data, -- 航母信息
foulDeck = false,
initPoint, -- 航母初始位置 vec2
nextPoint, -- 最后一个路径点 vec2
lastHeadding, -- 最近一次记录的航向
needToTurn = false, -- 是否需要转向
turning = false,
turningTime = 0,
pointCount = 0,
inProcess = {}, -- 等待回收中的飞机
recoveryStop, -- 结束回收计划
backToCruise = false, -- 正在返回巡航区域
foulTime = {}, -- 进入 foul lines 的时间
}
function lso.Carrier:addPlane(plane)
local recoveryStarted = false
if not (lso.utils.listContains(self.inProcess, plane)) then
plane.case = self.case
table.insert(self.inProcess, plane)
if (lso.carrierSailing and #self.inProcess == 1 and self.recovery == false and self.backToCruise == false) then
local eat = lso.Carrier:getEAT()
if (eat and (eat - timer.getTime() > 15 * 60)) then
-- lso.print(string.format("Charlie %d", (eat - timer.getTime()) / 60), 5, true, "charlieTime")
self:addRoute(true)
recoveryStarted = true
end
elseif (lso.carrierSailing == false or self.recovery == true) then
recoveryStarted = true
end
if (self.recoveryStop ~= nil) then
timer.removeFunction(self.recoveryStop)
self.recoveryStop = nil
end
return true, recoveryStarted
else
return false, recoveryStarted
end
end
function lso.Carrier:removePlane(plane)
if (lso.utils.listContains(self.inProcess, plane)) then
lso.utils.listRemove(self.inProcess, plane)
if (lso.carrierSailing and #self.inProcess == 0) then
local stopRecovery = function(args, timestamp)
if (self.recovery == true) then
self:addRoute(true)
self.recovery = false
self.recoveryStop = nil
end
end
-- 3分钟内没有新飞机加入回收队列则结束回收作业
self.recoveryStop = timer.scheduleFunction(stopRecovery, nil, timer.getTime() + 180)
end
return true
else
return false
end
end
function lso.Carrier:emergency(event)
if (lso.carrierSailing and #self.inProcess > 0 and self.recovery == false) then
self:addRoute(true)
end
end
function lso.Carrier:init()
lso.Broadcast:receive(lso.Broadcast.event.EMERGENCY, function(event)
self:emergency(event)
end)
local unit = Unit.getByName(lso.carrierName)
local radioUnit = lso.carrierRadioName and Unit.getByName(lso.carrierRadioName) or nil
local radio = (lso.useRadioFrequency and radioUnit) and radioUnit or unit
if (
unit ~= nil and radio ~= nil
and unit:isExist() and radio:isExist()
) then
self.unit = unit
self.radio = radio
self.initPoint = {x=unit:getPoint().x, y=unit:getPoint().z}
self.nextPoint = {x=unit:getPoint().x, y=unit:getPoint().z}
local typeName = unit:getTypeName()
for name, data in pairs(lso.data.carriers) do
if (name == typeName) then
self.data = data
break
end
end
if (self.data ~= nil) then
if (not lso.carrierSailing) then
self.recovery = true
end
self.frameID = lso.addCheckFrame(self) -- 添加 Carrier 检测帧程序
return true
end
end
return false
end
function lso.Carrier:loadTasks(groupData)
if (lso.carrierSailing) then
local group = self.unit:getGroup()
if group then
local groupCon = group:getController()
if groupCon then
local route = groupData.route
if (#route.points > 0) then
local point = route.points[1]
if (type(point.task) == "table" and type(point.task.params) == "table" and type(point.task.params.tasks) == "table") then
for i, task in pairs(point.task.params.tasks) do
if (task.id == "WrappedAction") then
local action = task.params.action
if (action.id == "Option") then
groupCon:setOption(action.params.name, action.params.value)
else
groupCon:setCommand(action)
end
end
end
end
end
local tasks = groupData.tasks
for i, task in pairs(tasks) do
if (task.id == "WrappedAction") then
local action = task.params.action
if (action.id == "Option") then
groupCon:setOption(action.params.name, action.params.value)
else
groupCon:setCommand(action)
end
end
end
end
end
self.needToTurn = true
end
end
function lso.Carrier:addRoute(clearAll)
local nowPoint = self.unit:getPoint()
local nextPoint = nil
local zone = trigger.misc.getZone(lso.carrierSailArea)
local center, radius
if type(zone) == "table" then
center = zone.point
radius = zone.radius
else
center = self.initPoint
radius = lso.Converter.NM_M(10)
end
self.backToCruise = false
if (#self.inProcess > 0 and self.recovery == false) then
local dir, speed = lso.utils.getWindInfo(nowPoint, self.data.offset.y + 20) -- 甲板上方 20 米风
for dist = 20, 1, -1 do
local y, x = lso.math.getOffsetPoint(nowPoint.z, nowPoint.x, (math.deg(dir) + self.data.deck) % 360, lso.Converter.NM_M(dist))
local point = {x=x, y=y}
if not (lso.utils.checkLand(point, {x=nowPoint.x, y=nowPoint.z})) then
nextPoint = point
self.recovery = true
-- lso.Broadcast:send(lso.Broadcast.event.RECOVERY_START)
break
end
end
if nextPoint == nil then
return false
end
else
if (self.recovery == true) then
self.recovery = false
self.backToCruise = true
lso.Broadcast:send(lso.Broadcast.event.RECOVERY_STOP)
end
end
-- 在航行区域内随机选择下一个路径点
if (nextPoint == nil) then
local tried = 0
repeat
if (tried > 1000) then
return false
end
local point = lso.utils.getRandPointInCircle(center, radius)
local dist = lso.utils.getDistance(point.x, point.y, nowPoint.x, nowPoint.z)
if (dist > radius * math.max(0.3, math.min(6000 / radius, 0.8))) then
if not (lso.utils.checkLand(point, {x=nowPoint.x, y=nowPoint.z})) then
nextPoint = point
end
end
tried = tried + 1
until (nextPoint ~= nil)
end
local waypoint = {}
waypoint.x = nextPoint.x
waypoint.y = nextPoint.y
waypoint.x = nextPoint.x
waypoint.alt = 0
waypoint.type = "Turning Point"
waypoint.speed = lso.Converter.KNOT_MS(lso.carrierSpeed)
waypoint.action = "Turning Point"
waypoint.task = {
id = 'WrappedAction',
params = {
action = {
id = 'Script',
params = {
command = "lso.Carrier:reachPoint()",
},
},
},
}
local misTask = {
id = 'Mission',
params = {
route = {
points = {waypoint},
},
},
}
local group = self.unit:getGroup()
if group then
local groupCon = group:getController()
if groupCon then
if (clearAll) then
groupCon:setTask(misTask)
else
groupCon:pushTask(misTask)
end
end
end
self.nextPoint = nextPoint
self.pointCount = self.pointCount + 1
lso.Broadcast:send(lso.Broadcast.event.TURNING_START)
-- trigger.action.markToAll(self.pointCount, string.format("%d", self.pointCount), {x=self.nextPoint.x, y=0, z=self.nextPoint.y})
return true
end
function lso.Carrier:reachPoint()
self.needToTurn = true
end
function lso.Carrier:getRecoveryCase()
if (self.case == 1) then
return "Case I"
elseif (self.case == 2) then
return "Case II"
elseif (self.case == 3) then
return "Case III"
else
return ""
end
end
-- 获取到达下一路径点的预计时间
function lso.Carrier:getEAT()
local speed = lso.Carrier:getSpeed(self.unit)
if speed > 0 then
local point = self.unit:getPoint()
local dist = lso.utils.getDistance(point.x, point.z, self.nextPoint.x, self.nextPoint.y)
local timeInSec = dist / speed
return timer.getTime() + timeInSec
end
end
function lso.Carrier:getBRC(current)
if (current or not lso.carrierSailing) then
return lso.Carrier:getHeadding(true) or 0
else
local cPoint = self.unit:getPoint()
local nextBRC = lso.math.round(lso.math.getAzimuth(cPoint.z, cPoint.x, lso.Carrier.nextPoint.y, lso.Carrier.nextPoint.x, true))
return nextBRC
end
end
function lso.Carrier:getTemperatureAndPressure()
local point = self.unit:getPoint()
point.y = 0
local temperature, pressure = atmosphere.getTemperatureAndPressure(point)
return lso.Converter.K_C(temperature), lso.Converter.PA_INHG(pressure)
end
-- 计算当前航母的接地点坐标
-- 返回值 bx, by: 接地点 vec2 坐标
function lso.Carrier:getLandingPoint()
local carrierPoint = self.unit:getPoint()
local carrierHeadding = lso.Carrier:getHeadding(true) or 0
local landingPoint = lso.math.rotateOffsetPoint(self.data.offset, carrierHeadding)
return carrierPoint.z + landingPoint.z, carrierPoint.x + landingPoint.x
end
-- 根据距离和高度,计算出当前所处下滑道角度
-- distance: 距离
-- altitude: 高度
-- 返回值: 下滑道角度
function lso.Carrier:getGlideSlope(distance, altitude)
return math.deg(math.atan((altitude - self.data.offset.y)/distance))
end
-- 获取航母航向
-- degrees: 是否返回角度值
-- 返回值: 航母航向
function lso.Carrier:getHeadding(degrees)
local heading = lso.utils.getHeading(self.unit, true)
if degrees then
heading = math.deg(heading)
end
return heading
end
-- 计算当前进近角相对于当前着陆甲板朝向的角度偏差
-- angle: 当前进近角
-- degrees: 是否返回角度值
-- 返回值: 角度偏差
function lso.Carrier:getAngleError(angle, degrees)
local carrierHeadding = lso.Carrier:getHeadding(true)
local stdAngle = (carrierHeadding - self.data.deck) % 360
local offset = lso.math.getAzimuthError(angle, stdAngle, true)
if (degrees) then
return offset
else
return math.rad(offset)
end
end
function lso.Carrier:getSpeed()
return lso.utils.getGroundSpeed(self.unit)
end
function lso.Carrier:getCharlie()
return (not self.turning) and self.recovery and math.abs(lso.Carrier:getBRC() - (lso.Carrier:getHeadding(true) or lso.Carrier:getBRC())) < 10
end
function lso.Carrier:checkOnRunway(unit)
if (unit.__class == "Plane") then
unit = plane.unit
end
local lx, ly = lso.Carrier:getLandingPoint()
local unitPoint = unit:getPoint()
local angle = lso.math.getAzimuth(unitPoint.z, unitPoint.x, lx, ly, true)
local angleError = lso.Carrier:getAngleError(angle, true)
local unitHeading = math.deg(lso.utils.getHeading(unit, true) or 0)
local checkPoint
if (unit:getDesc().box) then
local box = unit:getDesc().box
local parts = {
nose = lso.math.rotateOffsetPoint({x=box.max.x, y=0, z=0}, unitHeading),
tail = lso.math.rotateOffsetPoint({x=box.min.x, y=0, z=0}, unitHeading),
left = lso.math.rotateOffsetPoint({x=0, y=0, z=box.min.z}, unitHeading),
right = lso.math.rotateOffsetPoint({x=0, y=0, z=box.max.z}, unitHeading),
}
checkPoints = {
{
x = unitPoint.x + parts.nose.x,
y = unitPoint.y,
z = unitPoint.z + parts.nose.z,
},
{
x = unitPoint.x + parts.tail.x,
y = unitPoint.y,
z = unitPoint.z + parts.tail.z,
},
{
x = unitPoint.x + parts.left.x,
y = unitPoint.y,
z = unitPoint.z + parts.left.z,
},
{
x = unitPoint.x + parts.right.x,
y = unitPoint.y,
z = unitPoint.z + parts.right.z,
},
}
else
checkPoints = {unitPoint}
end
local part = 0
for i, point in ipairs(checkPoints) do
local distance = lso.utils.getDistance(point.z, point.x, lx, ly)
local angle = lso.math.getAzimuth(point.z, point.x, lx, ly, true)
local angleError = lso.Carrier:getAngleError(angle, true)
local rtg = distance * math.cos(math.rad(angleError))
local offset = distance * math.sin(math.rad(angleError))
if (math.abs(rtg) < self.data.runway.length / 2) and (math.abs(offset) < self.data.runway.width / 2) and (point.y - lso.Carrier.data.offset.y < 5) then
if (part == 0) then
part = i
else
part = 5
end
end
end
return part ~= 0, part
end
function lso.Carrier:onFrame()
-- lso.print(string.format("Case %d\ninProcess %d\nrecovery %s\nbackToCruise %s", self.case, #self.inProcess, self.recovery and "true" or "false", self.backToCruise and "true" or "false"), 1, true, "carrierFrame")
if (self.needToTurn) then
if (#lso.process.getUnitsInStatus(lso.process.Status.INITIAL + lso.process.Status.BREAK + lso.process.Status.PADDLES) == 0) then
if (self:addRoute(true)) then
self.needToTurn = false
end
end
end
-- 检测航母是否在转向
local heading = lso.Carrier:getHeadding(true)
if (self.lastHeadding and math.abs(lso.math.getAzimuthError(heading, self.lastHeadding, true)) > 0.1) then
-- if (self.lastHeadding and heading ~= self.lastHeadding) then
if (not self.turning) then
self.turningTime = timer.getTime()
end
-- 转向超过 5 分钟重置路径防卡死
if (lso.carrierSailing and self.turningTime ~= nil and timer.getTime() - self.turningTime > 60 * 5) then
local group = self.unit:getGroup()
if group then
local groupCon = group:getController()
if groupCon then
groupCon:popTask()
self:addRoute()
end
end
end
self.turning = true
else
if (self.turning) then
self.turningTime = nil
if (math.abs(lso.Carrier:getBRC() - (lso.Carrier:getHeadding(true) or lso.Carrier:getBRC())) < 10) then
lso.Broadcast:send(lso.Broadcast.event.TURNING_STOP)
if (self.recovery) then
lso.Broadcast:send(lso.Broadcast.event.RECOVERY_START)
end
end
end
self.turning = false
end
self.lastHeadding = heading
-- 判断当前回收状况 Case 1-3
local case = 0
local now = lso.utils.getTime()
local ceiling = lso.Converter.M_FT(env.mission.weather.clouds.base)
local visibility = 10
if (env.mission.weather.enable_fog or env.mission.weather.fog.thickness > 0) then
visibility = lso.Converter.M_NM(env.mission.weather.fog.visibility)
end
if (now.h > 7 and now.h < 18) then
if (visibility >= 5) then
if (ceiling >= 3000) then
case = 1
elseif (ceiling >= 1000) then
case = 2
else
case = 3
end
else
case = 3
end
else
case = 3
end
if (case ~= 0) then
self.case = case
end
-- 检查飞机状态
for i, plane in ipairs(self.inProcess) do
if (not plane.unit:isExist()) then
self:removePlane(plane)
end
end
for i, unit in ipairs(
lso.process.getUnitsInStatus(
lso.process.Status.NONE
+ lso.process.Status.CHECK_IN
+ lso.process.Status.IN_SIGHT
+ lso.process.Status.INITIAL
+ lso.process.Status.BREAK
+ lso.process.Status.ABORT
)
) do
local plane = lso.Plane.get(unit)
if plane and (not plane.unit:inAir()) and (plane.groundSpeed - lso.Carrier:getSpeed()) < lso.Converter.KNOT_MS(20) then
lso.process.removePlane(plane)
end
end
-- 检查跑道入侵
local foulDeck = false
local side = self.unit:getCoalition()
for i, group in ipairs(lso.utils.listConcat(coalition.getGroups(side, Group.Category.AIRPLANE), coalition.getGroups(side, Group.Category.HELICOPTER))) do
for j, unit in ipairs(group:getUnits()) do
if unit:isExist() then
local carrierPoint = lso.Carrier.unit:getPoint()
local unitPoint = unit:getPoint()
if (lso.utils.getDistance(unitPoint.z, unitPoint.x, carrierPoint.z, carrierPoint.x) < 300) then
local onRunway, part = lso.Carrier:checkOnRunway(unit)
local plane = lso.Plane.get(unit)
if plane then
if (plane.onRunway == true and onRunway == true) then -- 在跑道
if ((plane.foulTime == nil or timer.getTime() - plane.foulTime > 10)
and lso.process.getStatus(unit) == lso.process.Status.NONE
and lso.Carrier.recovery
and #lso.process.getUnitsInStatus(lso.process.Status.PADDLES) == 0
) then
plane.foulTime = timer.getTime()
local partName = "plane"
switch(part,
{1, function()
partName = "nose"
return true
end},
{2, function()
partName = "tail"
return true
end},
{3, function()
partName = "left wing"
return true
end},
{4, function()
partName = "right wing"
return true
end},
{5, function()
partName = "plane"
return true
end}
)
lso.RadioCommand:new(string.format("%s.onRunway", plane.number), "Air Boss", string.format("%s, Move your %s out of the foul lines.", plane.number, partName), nil, 2, lso.RadioCommand.Priority.NORMAL):send()
end
elseif (plane.onRunway == true and onRunway == false) then -- 出跑道
plane.foulTime = nil
end
plane.onRunway = onRunway
end
if (onRunway) then
foulDeck = true
end
end
end
end
end
self.foulDeck = foulDeck
end
-- 飞机类
-- 包含了所需的飞行参数
lso.Plane = {__class="Plane",
case, -- 飞机正在执行的回收状况
onRunway, -- 是否在跑道上
foulTime, -- 上次检查进入跑道时间
unit, -- 飞机单位
name, -- 飞机单位名称
model, -- 飞机型号
number, -- 机身编号
point, -- 飞机位置
altitude, -- 飞机高度(米)
heading, -- 飞机航向(角度)
azimuth, -- 飞机位于航母的方位角(角度)
angle, -- 飞机到着陆点角度(角度)
angleError, -- 相对着陆甲板角度误差(角度)
distance, -- 到着陆点的平面距离(米)
rtg, -- Range-to-go 到着陆点的下滑道剩余距离(米)
gs, -- 当前下滑道角度(角度)
gsError, -- 当前下滑道相对标准下滑道误差(角度)
aoa, -- 攻角(角度)
roll, -- 侧倾角
pitch, -- 俯仰角
speed, -- 示空速(m/s)
groundSpeed, -- 地速 (m/s)
vs, -- 垂直速度(m/s)
fuel, -- 剩余油量(kg)
fuelLow, -- 油量告竭
fuelMassMax, -- 最大油量
updateTime, -- 上次更新数据的时间
}
function lso.Plane:new(unitName, aircraftData, onboardNumber)
if (unitName == nil) then
return nil
end
local unit = Unit.getByName(unitName)
local obj = {
unit = unit,
name = unitName,
model = aircraftData,
number = onboardNumber,
fuelLow = false,
fuelMassMax = unit and unit:getDesc().fuelMassMax or nil