-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCICAP.lua
1748 lines (1568 loc) · 60.5 KB
/
GCICAP.lua
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
--[[
Copyright (c) 2016 Snafu, Stonehouse, Rivvern, Chameleon Silk, lukrop.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software and the Software shall not be
included in whole or part in any sort of paid for software or paid for downloadable
content (DLC) without the express permission of the copyright holders.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
--[[--
## Overview
Autonomous GCI and CAP script for DCS: World.
The script provides an autonomous model of combat air patrols and ground controlled
interceptors for use with DCS World by mission builders.
After minimal setup the script will automatically spawn CAP and GCI flights for two
sides and give them patrol and intercept tasks as well as returning them to base when
threats cease to be detected.
Originally created by Snafu, enhanced and further modified by Stonehouse,
Rivvern, Chameleon Silk.
Rewritten by lukrop.
## Links
Github repository: <https://github.com/lukrop/GCICAP>
@script GCICAP
@author Snafu
@author Stonehouse
@author Rivvern
@author Chameleon Silk
@author lukrop
@copyright 2016 Snafu, Stonehouse, Rivvern, Chameleon Silk, lukrop.
@license Modified MIT. See LICENSE file.
]]
gcicap = {}
gcicap.red = {}
gcicap.red.gci = {}
gcicap.red.cap = {}
gcicap.blue = {}
gcicap.blue.gci = {}
gcicap.blue.cap = {}
gcicap.cap = {}
gcicap.gci = {}
--- Sets how verbose the log output will be.
-- Possible values are "none", "info", "warning" and "error".
-- I recommend "error" for production.
gcicap.log_level = "info"
--- Interval, in seconds, of main function.
-- Default 30 seconds.
gcicap.interval = 30
--- Interval, in seconds, GCI flights get vectors on targets.
-- AI GCI flights don't use their radar, to be as stealth as
-- possible, relying on those vectors.
-- Default 15 seconds.
gcicap.vector_interval = 15
--- How far does a target have to move before an intercept is revectored
gcicap.revector_threshold = 15000
--- Initial spawn delay between CAPs
-- Default 30 seconds.
gcicap.initial_spawn_delay = 30
--- Enable/disable borders for the red side.
-- CAP units only engage if enemy units intrude their airspace
gcicap.red.borders_enabled = false
--- Enable/disable borders for the blue side.
-- CAP units only engage if enemy units intrude their airspace
gcicap.blue.borders_enabled = false
--- CAP minimum altitudes in meters.
-- Default 4500
gcicap.cap.min_alt = 4500
--- CAP maximum altitudes in meters.
-- Default 7500
gcicap.cap.max_alt = 7500
--- Speed for CAP flights on their CAP route.
-- speed is in m/s. Default 220.
gcicap.cap.speed = 220
--- Speed for GCI flights on intercept
-- speed is in m/s. Default 300.
gcicap.gci.speed = 300
--- Maximum engage distance for CAP flights as long as they are on patrol.
-- this might be overruled by an intercept vector given from
-- ground control (EWR). Default 15000.
gcicap.cap.max_engage_distance = 15000
--- Minimum red CAP VUL time in minutes.
-- Minimum time the red CAP flight will orbit on station.
gcicap.red.cap.vul_time_min = 25
--- Maximum red CAP VUL time in minutes.
-- Maximum time the red CAP flight will orbit on station.
gcicap.red.cap.vul_time_max = 40
--- Minimum blue CAP VUL time in minutes.
gcicap.blue.cap.vul_time_min = 25
--- Maximum blue CAP VUL time in minutes.
gcicap.blue.cap.vul_time_max = 30
--- Use race-track orbit for CAP flights
-- If true CAPs will use a race-track pattern for orbit
-- between two points in the CAP zone.
gcicap.cap.race_track_orbit = false
--[[ INOP at the time
--- Minimum leg length for red CAP orbits in meters.
gcicap.red.cap.leg_min = 10000
--- Maximum leg length for red CAP orbits in meters.
gcicap.red.cap.leg_min = 20000
--- Minimum leg length for blue CAP orbits in meters.
gcicap.blue.cap.leg_min = 10000
--- Maximum leg length for blue CAP orbits in meters.
gcicap.blue.cap.leg_min = 20000
]]--
--- Enable/disable red CAP flights airborne start.
-- set to true for CAP flight to start airborne at script initialisation
-- (mission start), false for taking off from the airfield.
-- Default true.
gcicap.red.cap.start_airborne = true
--- Enable/disable blue CAP flights airborne start.
gcicap.blue.cap.start_airborne = true
--- Amount of red CAP zones.
-- placed with triggerzones in the ME.
gcicap.red.cap.zones_count = 3
--- Amount of blue CAP zones.
gcicap.blue.cap.zones_count = 3
--- Amount of red CAP groups concurrently in the air.
gcicap.red.cap.groups_count = 3
--- Amount of blue CAP groups concurrently in the air.
gcicap.blue.cap.groups_count = 3
--- Group size of red CAP flights.
-- Can be "2", "4" or "randomized"
--
-- If "2" it consists of 2 planes, if "4" it consists of 4 planes
-- if "randomized", the CAP groups consist of either 2 or 4 planes
gcicap.red.cap.group_size = "2"
--- Group size of blue CAP flights.
-- See @{gcicap.red.cap.group_size}
gcicap.blue.cap.group_size = "2"
--- Maximum amount of concurrent red intercepts.
gcicap.red.gci.groups_count = 2
--- Maximum amount of concurrent blue intercepts.
gcicap.blue.gci.groups_count = 2
--- Group size of red GCI flights.
-- Can be "2", "4" or "dynamic"
--
-- If "2" it consists of 2 planes, if "4" it consists of 4 planes
-- if "dynamic", the GCI groups consist of as much aircrafts
-- as the intruder group.
gcicap.red.gci.group_size = "dynamic"
--- Group size of blue GCI flights.
-- See @{gcicap.red.gci.group_size}
gcicap.blue.gci.group_size = "dynamic"
--- Enable/disable GCI messages for red
gcicap.red.gci.messages = true
--- Enable/disable GCI messages for blue
gcicap.blue.gci.messages = true
--- How long a GCI message will be shown in seconds.
gcicap.gci.message_time = 5
--- Display GCI messages with metric measurment for red.
-- If false the imperial system is used.
gcicap.red.gci.messages_metric = true
--- Display GCI messages with metric measurment for blue.
-- If false the imperial system is used.
gcicap.blue.gci.messages_metric = false
--- Names of red groups which will receive GCI messages.
-- Leave blank for all groups of coalition
-- @usage gcicap.red.gci.messages_to = { "my group 1", "GCI Flight" }
gcicap.red.gci.messages_to = {}
--- Names of blue groups which will receive GCI messages.
-- See @{gcicap.red.gci.messages_to} for format.
gcicap.blue.gci.messages_to = {}
--- How red CAP flights are spawned.
-- can be "parking", "takeoff" or "air" and defines the way the fighters spawn
-- takeoff is NOT RECOMMENDED currently since their occur timing issues with tasking
-- if a flight is queued for takeoff and not already in the game world while getting tasked
--
-- Default 'parking'
gcicap.red.cap.spawn_mode = "parking"
--- How red GCI flights are spawned.
-- @see gcicap.red.cap.spawn_mode
gcicap.red.gci.spawn_mode = "parking"
--- How blue CAP flights are spawned.
-- @see gcicap.red.cap.spawn_mode
gcicap.blue.cap.spawn_mode = "parking"
--- How blue GCI flights are spawned.
-- @see gcicap.red.cap.spawn_mode
gcicap.blue.gci.spawn_mode = "parking"
--- Hide or reveal blue air units in the mission.
gcicap.blue.hide_groups = false
--- Hide or reveal red air units in the mission.
gcicap.red.hide_groups = false
--- Enable/disable red CAP flights.
gcicap.red.cap.enabled = true
--- Enable/disable blue CAP flights.
gcicap.blue.cap.enabled = true
--- Enable/disable red GCI flights.
gcicap.red.gci.enabled = true
--- Enable/disable blue GCI flights.
gcicap.blue.gci.enabled = true
--- Enabel/disable resource limitation for red.
-- If set to true limits the amount of groups a side can spawn.
gcicap.red.limit_resources = false
--- Enabel/disable resource limitation for blue.
-- @see gcicap.red.limit_resources
gcicap.blue.limit_resources = false
--- Amount of groups(!) red has at it's disposal.
-- In other words how many Groups of airplanes
-- this side can spawn.
gcicap.red.supply = 24
--- Amount of groups(!) red has at it's disposal.
-- @see gcicap.red.supply
gcicap.blue.supply = 24
--- Name of the trigger zone which defines red CAP zones.
-- This will be postfixed with the number of
-- the zone. e.g. "redCAPzone3" or "blueCAPzone1".
--
-- Default: 'redCAPzone'.
gcicap.red.cap.zone_name = 'redCAPzone'
--- Name of the trigger zone which defines blue CAP zones.
-- Default: 'blueCAPzone'.
-- @see gcicap.red.cap.zone_name
gcicap.blue.cap.zone_name = 'blueCAPzone'
--- Name of group which waypoints define the red border.
-- Default: 'redborder'.
gcicap.red.border_group = 'redborder'
--- Name of group which waypoints define the blue border.
-- Default: 'blueborder'.
gcicap.blue.border_group = 'blueborder'
--- GCI template unit's names prefix.
gcicap.gci.template_prefix = '__GCI__'
--- CAP template unit's names prefix.
gcicap.cap.template_prefix = '__CAP__'
--- Count of template units.
-- Remember that this means you need that many
-- template units for each type. E.g. if the template_count is 2 you
-- would need two GCI and two CAP template units for each side.
gcicap.template_count = 2
--- Wether red will also acquire targets by AWACS aircraft.
-- This is is currently broken since isTargetDetected doesn't
-- seem to work with AWACS airplanes. Needs a workaround.
--
-- Default false.
gcicap.red.awacs = false
--- Wether blue will also acquire targets by AWACS aircraft.
-- @see gcicap.red.awacs
gcicap.blue.awacs = false
--- Garbage collector move timeout
-- If a unit (aircraft) is on the ground and didn't move
-- since this timeout, in seconds, it will be removed.
-- This applies only to aircraft spawned by GCICAP.
gcicap.move_timeout = 300
-- shortcut to the bullseye
gcicap.red.bullseye = coalition.getMainRefPoint(coalition.side.RED)
gcicap.blue.bullseye = coalition.getMainRefPoint(coalition.side.BLUE)
gcicap.sides = { "red", "blue" }
gcicap.tasks = { "cap", "gci" }
gcicap.log = mist.Logger:new("GCICAP", gcicap.log_level)
do
--- Flight class.
-- @type gcicap.Flight
gcicap.Flight = {}
local function getFlightIndex(group)
if type(group) ~= "string" then
if group:getName() then
group = group:getName()
else
return false
end
end
for i, side in pairs(gcicap.sides) do
for j, task in pairs(gcicap.tasks) do
for n = 1, #gcicap[side][task].flights do
if gcicap[side][task].flights[n].group_name == group then
return {side = side, task = task, index = n}
end
end
end
end
return false
end
--- Returns the flight for the given group.
-- @tparam string|Group group this can be a Group object
-- or the group name.
-- @treturn gcicap.Flight the flight for the given group.
function gcicap.Flight.getFlight(group)
f = getFlightIndex(group)
if f then
return gcicap[f.side][f.task].flights[f.index]
else
return false
end
end
--- Creates a new flight.
-- @tparam Group group group of the flight.
-- @tparam Airbase airbase homplate of the new flight.
-- @tparam string task task of the new flight. Can be "cap" or "gci".
-- @param param task parameter. This can be a zone table if it's a
-- CAP flight or it could be a target unit if it's a GCI flight.
function gcicap.Flight:new(group, airbase, task, param)
if group:isExist() then
local side = gcicap.coalitionToSide(group:getCoalition())
local f = {}
f.side = side
f.give_up = false
f.group = group
f.group_name = group:getName()
f.airbase = airbase
f.task = task
-- is the flight RTB?
f.rtb = false
f.in_zone = false
if task == "cap" then
f.zone = param
f.zone_name = param.name
f.intercepting = false
f.vul_time = math.random(gcicap[side].cap.vul_time_min,
gcicap[side].cap.vul_time_max)
else -- task should be "gci"
f.target = param
f.target_group = param.group
f.intercepting = true
f.intercept_point = { x = 0, y = 0, z = 0 }
end
-- get current timestamp
local timestamp = timer.getAbsTime()
f.units_moved = {}
-- set timestamp for each unit
-- this is later used for garbage collection checks
for u, unit in pairs(group:getUnits()) do
f.units_moved[u] = {}
f.units_moved[u].unit = unit
f.units_moved[u].last_moved = timestamp
f.units_moved[u].spawned_at = timestamp
end
setmetatable(f, self)
self.__index = self
table.insert(gcicap[side][task].flights, f)
gcicap.log:info("Registered flight: $1", f.group_name)
return f
else
return nil
end
end
--- Removes the flight
-- @tparam gcicap.Flight self flight object
function gcicap.Flight:remove()
if self.zone then
-- if we didn't already leave the zone do it now.
self:leaveCAPZone()
end
local f = getFlightIndex(self.group_name)
local r = table.remove(gcicap[f.side][f.task].flights, f.index)
if r then
gcicap.log:info("Removing flight $1 with index $2", r.group_name, f.index)
end
end
--- Decreases active flights counter in this flights zone.
-- Actually just decreases the active flights
-- counter of a zone. Does NOT task the flight itself.
function gcicap.Flight:leaveCAPZone()
if self.in_zone then
local zone = self.zone
if zone.patrol_count <= 1 then
zone.patrol_count = 0
else
zone.patrol_count = zone.patrol_count - 1
end
self.in_zone = false
-- get current time
local time_now = timer.getAbsTime()
-- get time on station by substracting vul start time from current time
-- and convert it to minutes
local time_on_station = 0
if self.vul_start then
time_on_station = (time_now - self.vul_start) / 60
end
local vul_diff = self.vul_time - time_on_station
-- set new vul time only if more than 5 minutes
if vul_diff > 5 then
self.vul_time = vul_diff
else
self.vul_time = 0
end
end
end
--- Increases active flights counter in this flights zone.
-- Actually just increases the active flights
-- counter of a zone. Does NOT task the flight itself.
function gcicap.Flight:enterCAPZone()
if not self.in_zone then
self.intercepting = false
self.in_zone = true
local zone = self.zone
zone.patrol_count = zone.patrol_count + 1
end
end
--- Tasks the flight to search and engage the target.
-- @tparam Unit intruder target unit.
-- @tparam[opt] boolean cold whether the flight should not destroy
-- the target and just follow it. Default false.
function gcicap.Flight:vectorToTarget(intruder, cold)
local target = nil
if intruder.group then
target = gcicap.getFirstActiveUnit(intruder.group)
end
if target == nil or intruder.group == nil then return end
-- check if interceptor even still exists
if self.group:isExist() then
if target:isExist() and target:inAir() and self.give_up ~= true then
local target_pos = target:getPoint()
local ctl = self.group:getController()
local gci_task = {
id = 'Mission',
params = {
route = {
points = {
[1] = {
alt = target_pos.y,
x = target_pos.x,
y = target_pos.z,
speed = gcicap.gci.speed,
action = "Turning Point",
type = "Turning Point",
task = {
id = "ComboTask",
params = {
tasks = {
[1] = {
number = 1,
key = "CAP",
id = "EngageTargets",
enabled = true,
auto = true,
params = {
targetTypes = { [1] = "Air" },
priority = 0
}
}
}
}
}
},
[2] = {
alt = target_pos.y,
x = target_pos.x,
y = target_pos.z,
speed = gcicap.gci.speed,
action = "Turning Point",
type = "Turning Point",
task = {
-- i don't really like this WrappedAction but it's needed in
-- the case the CGI completes this waypoint because of lack/loss
-- of target
id = 'WrappedAction',
params = {
action = {
id = 'Script',
params = {
command = "local group = ...\
local flight = gcicap.Flight.getFlight(group)\
if flight then\
flight.give_up = true\
if flight.zone then\
if flight.intercepting then\
flight:taskWithCAP()\
end\
else\
if not flight.target then\
flight:taskWithRTB()\
end\
end\
else\
gcicap.log:error('Could not find flight')\
end"
}
}
}
}
}
}
}
}
}
-- checkout of the patrol zone
if self.zone and not self.intercepting then
self:leaveCAPZone()
end
intruder.intercepted = true
-- only set/reset the task if the target has moved significantly since last GCI update
if mist.utils.get3DDist( target_pos, self.intercept_point ) > gcicap.revector_threshold then
-- if there's still an EWR detecting or we are responding to the initial call
-- then set the target position. do not allow revectoring if no EWR is detecting us now
if (Unit.isExist(intruder.detected_by) and intruder.detected_by:isActive()) then
self.give_up = false
self.intercept_point = mist.utils.deepCopy(target_pos)
ctl:setTask(gci_task)
gcicap.log:info("Vectoring $1 to $2 ($3)", self.group:getName(),
intruder.group:getName(), target:getName())
else
gcicap.log:info("Cannot revector $1 to $2 because no longer detecting",self.group:getName(),intruder.group:getName())
end
end
self.intercepting = true
-- taskEngageGroup provides omniscient knowledge of where the group to be attacked is, which sucks
if not cold then
--gcicap.taskEngageGroup(self.group, intruder.group)
gcicap.taskEngage(self.group, 15000)
end
-- reschedule function until either the interceptor or the intruder is dead
mist.scheduleFunction(gcicap.Flight.vectorToTarget, {self, intruder, cold},
timer.getTime() + gcicap.vector_interval)
else -- the target is dead or we had to give up, resume CAP or RTB
if self.zone then
-- send CAP back to work only if still intercepting
if self.intercepting then
self:taskWithCAP()
end
else
self.intercepting = false
-- send GCI back to homeplate
self:taskWithRTB()
end
end
else
-- our interceptor group is dead let's see if the
-- intruder is still there and set him to not beeing intercepted anymore
if target:isExist() then
intruder.intercepted = false
end
end
end
--- Tasks flight with combat air patrol.
-- Creates waypoints inside it's assigned zone and tasks
-- the flight with patroling along the route.
-- @tparam[opt] boolean cold If set to true the flight won't
-- engage any enemy unit's it detects by itself. Default false.
function gcicap.Flight:taskWithCAP(cold)
-- only task with CAP if ther is still vul time left
if self.vul_time == 0 then
-- send flight RTB if no vul time left.
gcicap.log:info("No vul time left for $1", self.group_name)
self:taskWithRTB()
else
local group = self.group
local ctl = group:getController()
local side = gcicap.coalitionToSide(group:getCoalition())
local start_pos = gcicap.getFirstActiveUnit(group):getPoint()
local leg_dist = math.random(gcicap[side].cap.leg_min, gcicap[side].cap.leg_max)
local cap_route = gcicap.buildCAPRoute(start_pos, self.zone.name, self.vul_time, leg_dist)
local cap_task = {
id = 'Mission',
params = {
route = cap_route
}
}
self.intercepting = false
self.intercept_point = { x = 0, y = 0, z = 0 }
ctl:setTask(cap_task)
self:enterCAPZone()
ctl:setOption(AI.Option.Air.id.RADAR_USING, AI.Option.Air.val.RADAR_USING.FOR_SEARCH_IF_REQUIRED)
if not cold then
gcicap.taskEngage(group)
end
gcicap.log:info("Tasking $1 with CAP in zone $2", group:getName(), self.zone.name)
end
end
--- Tasks the flight to return to it's homeplate.
-- @tparam[opt] Airbase airbase optionally use this as homeplate/airbase
-- to return to.
-- @tparam[opt] boolean cold If set to true the flight won't
-- engage any targets it detects on the way back to base.
-- Default false.
function gcicap.Flight:taskWithRTB(airbase, cold)
if not airbase then
airbase = self.airbase
end
if self.zone then
self:leaveCAPZone()
local side = self.side
-- let's try to spawn a new CAP flight as soon as the current one is tasked with RTB.
-- never spawn more than 2 x the groups_count, to prevent spam in case something ever goes wrong.
if (not gcicap[side].limit_resources or
(gcicap[side].limit_resources and gcicap[side].supply > 0))
and #gcicap[side].cap.flights < gcicap[side].cap.groups_count * 2 then
gcicap.spawnCAP(side, self.zone, gcicap[side].cap.spawn_mode)
end
end
self.rtb = true
local group = self.group
local ctl = group:getController()
local af_pos = mist.utils.makeVec2(airbase:getPoint())
local af_id = airbase:getID()
local rtb_task = {
id = 'Mission',
params = {
route = {
points = {
[1] = {
alt = gcicap.cap.min_alt,
alt_type = "BARO",
speed = gcicap.cap.speed,
x = af_pos.x,
y = af_pos.y,
aerodromeId = af_id,
type = "Land",
action = "Landing",
}
}
}
}
}
ctl:setTask(rtb_task)
if not cold then
-- only engage if enemy is inside of 10km of the leg
gcicap.taskEngage(group, 10000)
end
gcicap.log:info("Tasking $1 with RTB to $2", group:getName(), airbase:getName())
end
--- Functions
-- @section gcicap
--- Clean up inactive/stuck flights.
local function garbageCollector(side)
local timestamp = timer.getAbsTime()
for t, task in pairs(gcicap.tasks) do
for f, flight in pairs(gcicap[side][task].flights) do
if flight.group then
if flight.group:isExist() then
for u = 1, #flight.units_moved do
local unit = flight.units_moved[u].unit
-- check if unit exists
if unit then
if unit:isExist() then
-- if unit is in air we won't do anything
if not unit:inAir() then
-- check if unit is moving
local mag = mist.vec.mag(unit:getVelocity())
if mag == 0 then
-- get the last time the unit moved
local last_moved = flight.units_moved[u].last_moved
if timestamp - last_moved > gcicap.move_timeout then
gcicap.log:info("Cleaning up $1", flight.group:getName())
flight.group:destroy()
flight:remove()
end
else
flight.units_moved[u].last_moved = timestamp
end
end
end
end
end
else
flight:remove()
end
else
flight:remove()
end
end
end
end
local function checkForTemplateUnits(side)
if gcicap[side].gci.enabled then
for i = 1, gcicap.template_count do
local unit = gcicap.gci.template_prefix..side..i
if not Unit.getByName(unit) then
gcicap.log:alert("GCI template unit missing: $1", unit)
return false
end
end
end
if gcicap[side].cap.enabled then
for i = 1, gcicap.template_count do
local unit = gcicap.cap.template_prefix..side..i
if not Unit.getByName(unit) then
gcicap.log:alert("CAP template unit missing: $1", unit)
return false
end
end
end
if gcicap[side].borders_enabled then
if not Group.getByName(gcicap[side].border_group) then
gcicap.log:alert("Border group is missing: $1", gcicap[side].border_group)
return false
end
end
return true
end
local function checkForTriggerZones(side)
for i = 1, gcicap[side].cap.zones_count do
local zone_name = gcicap[side].cap.zone_name..i
if not trigger.misc.getZone(zone_name) then
gcicap.log:alert("CAP trigger zone is missing: $1", zone_name)
return false
end
end
return true
end
local function manageCAP(side)
local patroled_zones = 0
for i = 1, #gcicap[side].cap.zones do
local zone = gcicap[side].cap.zones[i]
gcicap.log:info("Zone $1 has $2 patrols", zone.name, zone.patrol_count)
-- see if we can send a new CAP into the zone
if zone.patrol_count <= 0 then
-- first check if we already hit the maximum amounts of routine CAP groups
if #gcicap[side].cap.flights < gcicap[side].cap.groups_count then
-- check if we limit resources and if we have enough supplies
-- if we don't limit resource or have enough supplies we spawn
if not gcicap[side].limit_resources or
(gcicap[side].limit_resources and gcicap[side].supply > 0) then
-- finally spawn it
gcicap.spawnCAP(side, gcicap[side].cap.zones[i], gcicap[side].cap.spawn_mode)
end
end
else
patroled_zones = patroled_zones + 1
end
end
-- if all zones are patroled and we still have cap groups left
-- send them to a random zone
if #gcicap[side].cap.flights < gcicap[side].cap.groups_count then
if not gcicap[side].limit_resources or
(gcicap[side].limit_resources and gcicap[side].supply > 0) then
local random_zone = math.random(1, #gcicap[side].cap.zones)
gcicap.spawnCAP(side, gcicap[side].cap.zones[random_zone], gcicap[side].cap.spawn_mode)
end
end
gcicap.log:info("$1 patrols in $2/$3 zones with $4 flights",
side, patroled_zones, gcicap[side].cap.zones_count, #gcicap[side].cap.flights)
end
local function handleIntrusion(side)
for i = 1, #gcicap[side].intruders do
local intruder = gcicap[side].intruders[i]
if intruder.group then
if intruder.group:isExist() then
-- check if we need to do something about him
if not intruder.intercepted then
-- first check if we have something to work with
if #gcicap[side].cap.flights > 0
or #gcicap[side].gci.flights > 0
or #gcicap[side].gci.flights < gcicap[side].gci.groups_count then
-- get closest flight to intruder if there is any
local closest = nil
local intruder_unit = gcicap.getFirstActiveUnit(intruder.group)
local closest_flights = gcicap.getClosestFlightsToUnit(side, intruder_unit)
-- we found close flights
local flight_avail = false
if closest_flights then
for j = 1, #closest_flights do
closest = closest_flights[j]
--fligh_avail = (not closest.flight.rtb) and (not closest.flight.intercepting)
flight_avail = (not closest.flight.intercepting)
if flight_avail then
gcicap.log:info("Found flight $1 which is avaliable for tasking.",
closest.flight.group:getName())
break
end
end
end
if flight_avail then
-- check if we have a airfield which is closer to the unit than the closest flight
-- but add some distance to the airfield since it takes time for a potential spawned
-- flight to take-off
local closest_af, af_distance = gcicap.getClosestAirfieldToUnit(side, intruder_unit)
af_distance = af_distance + 15000 -- add 15km
if closest.distance < af_distance or af_distance == -1 then
-- task flight with intercept
closest.flight.give_up = false
closest.flight:vectorToTarget(intruder)
return
end
if (not gcicap[side].limit_resources
or (gcicap[side].limit_resources and gcicap[side].supply > 0))
and #gcicap[side].gci.flights < gcicap[side].gci.groups_count
and gcicap[side].gci.enabled then
-- spawn CGI
gcicap.log:info("Airfield closer to intruder than flight or no flight available. Spawning GCI")
local gci = gcicap.spawnGCI(side, intruder)
end
else
if (not gcicap[side].limit_resources
or (gcicap[side].limit_resources and gcicap[side].supply > 0))
and #gcicap[side].gci.flights < gcicap[side].gci.groups_count
and gcicap[side].gci.enabled then
-- spawn CGI
gcicap.log:info("No CAP flights or already airborne GCI. Spawning GCI")
local gci = gcicap.spawnGCI(side, intruder)
end
end
end
end
end
else
-- the intruder group doesn't exist (anymore) remove it
table.remove(gcicap[side].intruders, i)
end
end
end
-- returns airfields of given side which are marked with
-- triggerzones (triggerzone name is exactly the same as airfield name).
local function getAirfields(side)
local coal_airfields = coalition.getAirbases(gcicap.sideToCoalition(side))
local gcicap_airfields = {}
-- loop over all coalition airfields
for i = 1, #coal_airfields do
-- get name of airfield
local af_name = coal_airfields[i]:getName()
if not string.match(af_name, "FARP") then
-- check if a triggerzone exists with that exact name
if mist.DBs.zonesByName[af_name] then
-- add it to our airfield list for gcicap
gcicap_airfields[#gcicap_airfields + 1] = coal_airfields[i]
end
end
end
if #gcicap_airfields == 0 then
gcicap.log:warn("No airbase for $1 found", side)
end
return gcicap_airfields
end
-- returns all currently active aircraft of the given side
-- parameter side has to be "red" or "blue"
local function getAllActiveAircrafts(side)
local filter = { "[" .. side .. "][plane]", "[" .. side .. "][helicopter]"}
local all_aircraft = mist.makeUnitTable(filter)
local active_aircraft = {}
for i = 1, #all_aircraft do
local ac = Unit.getByName(all_aircraft[i])
if ac ~= nil then
if Unit.isActive(ac) then
table.insert(active_aircraft, ac)
end
end
end
if #active_aircraft == 0 then
gcicap.log:warn("No active aircraft for $1 found", side)
end
return active_aircraft
end
-- returns all currently active EWR and AWACS units of the given side
-- parameter side has to be "red" or "blue"
local function getAllActiveEWR(side)
local filter = { "[" .. side .. "][plane]", "[" .. side .. "][vehicle]", "[" .. side .. "][ship]"}
local all_vecs = mist.makeUnitTable(filter)
local active_ewr = {}
for i = 1, #all_vecs do
local vec = Unit.getByName(all_vecs[i])
if vec ~= nil then
if Unit.isActive(vec) then
local vec_type = Unit.getTypeName(vec)
if vec_type == "55G6 EWR"
or vec_type == "1L13 EWR"
or vec_type == "Hawk sr"
or vec_type == "Patriot str" then
table.insert(active_ewr, { unit = vec, is_awacs = false} )
end
-- ED has a bug; the E-2D vehicle has type E-2C
if (vec_type == "A-50" and gcicap[side].awacs)
or (vec_type == "E-2C" and gcicap[side].awacs)
or (vec_type == "E-3A" and gcicap[side].awacs) then
table.insert(active_ewr, { unit = vec, is_awacs = true} )
end
end
end
end
if #active_ewr == 0 then
gcicap.log:warn("No active EWR for $1 found", side)
end
return active_ewr
end
local function checkForAirspaceIntrusion(side)
-- init some local vars
local border = gcicap[side].border
local active_ewr = gcicap[side].active_ewr
local intruder_count = 0