forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScenarioPlatoonAI.lua
2462 lines (2293 loc) · 91.9 KB
/
ScenarioPlatoonAI.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
------------------------------------------------------------------------
--- File : /lua/ai/ScenarioPlatoonAI.lua
--- Author(s): Drew Staltman
--- Summary : Houses a number of AI threads that are used in operations
--- Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
------------------------------------------------------------------------
local AIBuildStructures = import("/lua/ai/aibuildstructures.lua")
local ScenarioFramework = import("/lua/scenarioframework.lua")
local StructureTemplates = import("/lua/buildingtemplates.lua")
local ScenarioUtils = import("/lua/sim/scenarioutilities.lua")
--- Retrieves all human brains that are hostile to the given army index
---@param armyIndex number Army index to check alliance with
---@return table AIBrain[]
function GetHumanEnemies(armyIndex)
humans = {}
for i, brain in ArmyBrains do
if brain.BrainType == 'Human' and IsEnemy(armyIndex, brain:GetArmyIndex()) then
table.insert(humans, brain)
end
end
return humans
end
---@param platoon Platoon
function BuildOnce(platoon)
local aiBrain = platoon:GetBrain()
if aiBrain.HasPlatoonList then
aiBrain:PBMSetPriority(platoon, 0)
else
platoon.BuilderHandle:SetPriority(0)
end
end
---@param platoon Platoon
function DefaultOSBasePatrol(platoon)
local aiBrain = platoon:GetBrain()
local master = string.sub(platoon.PlatoonData.BuilderName, 11)
local chain = false
if platoon.PlatoonData.LocationType and Scenario.Chains[aiBrain.Name .. '_' .. platoon.PlatoonData.LocationType .. '_BasePatrolChain'] then
chain = aiBrain.Name .. '_' .. platoon.PlatoonData.LocationType .. '_BasePatrolChain'
elseif Scenario.Chains[master .. '_BasePatrolChain'] then
chain = master .. '_BasePatrolChain'
elseif Scenario.Chains[aiBrain.Name .. '_BasePatrolChain'] then
chain = aiBrain.Name .. '_BasePatrolChain'
end
if chain then
platoon.PlatoonData.PatrolChain = chain
PatrolThread(platoon)
end
end
--- Uses OrderName - Name of the Order from the editor & Target - Handle to Unit used in orders that require a target (OPTIONAL)
---@param platoon Platoon
---@return boolean
function PlatoonAssignOrders(platoon)
platoon:Stop()
local data = platoon.PlatoonData
if not data.OrderName then
error('*SCENARIO PLATOON AI ERROR: No OrderName given to PlatoonAssignOrders AI Function', 2)
return false
end
ScenarioUtils.AssignOrders(data.OrderName, platoon, data.Target)
end
--- Attacks the closest unit the AIBrain is aware of, based on intel
---@param platoon Platoon
function PlatoonAttackClosestUnit(platoon)
local aiBrain = platoon:GetBrain()
local target
while not target do
target = platoon:FindClosestUnit('Attack', 'Enemy', true, categories.ALLUNITS)
WaitSeconds(3)
end
platoon:Stop()
local cmd = platoon:AggressiveMoveToLocation(target:GetPosition())
while aiBrain:PlatoonExists(platoon) do
if target ~= nil then
if target.Dead or not platoon:IsCommandsActive(cmd) then
target = platoon:FindClosestUnit('Attack', 'Enemy', true, categories.ALLUNITS-categories.WALL)
if target and not target.Dead then
platoon:Stop()
cmd = platoon:AggressiveMoveToLocation(target:GetPosition())
end
end
else
target = platoon:FindClosestUnit('Attack', 'Enemy', true, categories.ALLUNITS)
end
WaitSeconds(17)
end
end
--- Attacks the closest unit the AIBrain is aware of, based on intel
---@param platoon Platoon
function PlatoonAttackHighestThreat(platoon)
local patrol = false
local aiBrain = platoon:GetBrain()
local location, threat = aiBrain:GetHighestThreatPosition(1, true)
platoon:Stop()
local cmd = platoon:AggressiveMoveToLocation(location)
while aiBrain:PlatoonExists(platoon) do
if not platoon:IsCommandsActive(cmd) then
location, threat = aiBrain:GetHighestThreatPosition(1, true)
if threat > 0 then
platoon:Stop()
cmd = platoon:AggressiveMoveToLocation(location)
end
end
WaitSeconds(13)
end
end
--- Attack moves to a specific location on the map, once arrived it will attack the highest threat
---@param platoon Platoon
function PlatoonAttackLocation(platoon)
platoon:Stop()
local data = platoon.PlatoonData
if not data.Location then
error('*SCENARIO PLATOON AI ERROR: PlatoonAttackLocation requires a Location to operate', 2)
end
local location = data.Location
if type(location) == 'string' then
location = ScenarioUtils.MarkerToPosition(location)
end
local aiBrain = platoon:GetBrain()
local cmd = platoon:AggressiveMoveToLocation(location)
local threat = 0
while aiBrain:PlatoonExists(platoon) do
if not platoon:IsCommandsActive(cmd) then
location, threat = platoon:GetBrain():GetHighestThreatPosition(1, true)
platoon:Stop()
cmd = platoon:AggressiveMoveToLocation(location)
end
WaitSeconds(13)
end
end
--- # PlatoonAttackLocationList
--- Attack moves to a location chosen from a list. Location can be the highest threat or the lowest non-negative threat.
--- After reaching location will attack next location from the list.
--- ## PlatoonData:
--- - LocationList - (REQUIRED) location on the map to attack move to
--- - LocationChain - (REQUIRED) Chain on the map to attack move to
--- - High - true will attack highest threats first, false lowest - defaults to false/lowest
---@param platoon Platoon
function PlatoonAttackLocationList(platoon)
platoon:Stop()
local location = nil
local aiBrain = platoon:GetBrain()
local data = platoon.PlatoonData
if not data.LocationList and not data.LocationChain then
error('*SCENARIO PLATOON AI ERROR: PlatoonAttackLocationList requires a LocationList or LocationChain in PlatoonData to operate', 2)
end
local positions = {}
if data.LocationChain then
positions = ScenarioUtils.ChainToPositions(data.LocationChain)
else
for _, v in data.LocationList do
if type(v) == 'string' then
table.insert(positions, ScenarioUtils.MarkerToPosition(v))
else
table.insert(positions, v)
end
end
end
if data.High then
location = PlatoonChooseHighest(platoon:GetBrain(), positions, 1)
else
location = PlatoonChooseLowestNonNegative(platoon:GetBrain(), positions, 1)
end
local cmd
if location then
cmd = platoon:AggressiveMoveToLocation(location)
end
while aiBrain:PlatoonExists(platoon) do
if not location or not platoon:IsCommandsActive(cmd) then
if data.High then
location = PlatoonChooseHighest(platoon:GetBrain(), positions, 1, location)
else
location = PlatoonChooseLowestNonNegative(platoon:GetBrain(), positions, 1, location)
end
if location then
platoon:Stop()
cmd = platoon:AggressiveMoveToLocation(location)
end
end
WaitSeconds(13)
end
end
--- Assigns units in platoon to TransportPool platoon for other platoons to use and moves to to specified location if specified.
--- - TransportMoveLocation - Location to move transport to before assigning to transport pool
--- - MoveRoute - List of locations to move to
--- - MoveChain - Chain of locations to move
---@param platoon Platoon
function TransportPool(platoon)
local aiBrain = platoon:GetBrain()
local data = platoon.PlatoonData
-- Default transport platoon to grab from
local poolName = 'TransportPool'
local BaseName = data.BaseName
-- If base name is specified in platoon data, use that instead
if BaseName then
poolName = BaseName .. '_TransportPool'
end
local tPool = aiBrain:GetPlatoonUniquelyNamed(poolName)
if not tPool then
tPool = aiBrain:MakePlatoon('', '')
tPool:UniquelyNamePlatoon(poolName)
end
if data.TransportMoveLocation then
if type(data.TransportMoveLocation) == 'string' then
data.MoveRoute = {ScenarioUtils.MarkerToPosition(data.TransportMoveLocation)}
else
data.MoveRoute = {data.TransportMoveLocation}
end
end
if data.MoveChain or data.MoveRoute then
MoveToThread(platoon)
end
aiBrain:AssignUnitsToPlatoon(tPool, platoon:GetPlatoonUnits(), 'Scout', 'GrowthFormation')
end
--- Grabs a specific number of transports from the transports pool and loads units into the transport. Once ready a scenario variable can be set. Can wait on another scenario variable. Attempts to land at the location with the least threat and uses the accompanying attack chain for the units that have landed.
--- - ReadyVariable - `ScenarioInfo.VarTable[ReadyVariable]` Variable set when units are on transports
--- - WaitVariable - `ScenarioInfo.VarTable[WaitVariable]` Variable checked before transports leave
--- - LandingList - (REQUIRED or LandingChain) List of possible locations for transports to unload units
--- - LandingChain - (REQUIRED or LandingList) Chain of possible landing locations
--- - TransportReturn - Location for transports to return to (they will attack with land units if this isn't set)
--- - TransportChain - (or TransportRoute) Route to move along the transports before dropping the units.
--- - AttackPoints - (REQUIRED or AttackChain or PatrolChain) List of locations to attack. The platoon attacks the highest threat first
--- - AttackChain - (REQUIRED or AttackPoints or PatrolChain) Marker Chain of postitions to attack
--- - PatrolChain - (REQUIRED or AttackChain or AttackPoints) Chain of patrolling
--- - RandomPatrol - Bool if you want the patrol things to be random rather than in order
---@param platoon Platoon
function LandAssaultWithTransports(platoon)
local aiBrain = platoon:GetBrain()
local data = platoon.PlatoonData
if not data.AttackPoints and not data.AttackChain and not data.AssaultChains then
error('*SCENARIO PLATOON AI ERROR: LandAssaultWithTransports requires AttackPoints in PlatoonData to operate', 2)
elseif not data.LandingList and not data.LandingChain and not data.AssaultChains then
error('*SCENARIO PLATOON AI ERROR: LandAssaultWithTransports requires LandingList in PlatoonData to operate', 2)
end
-- Fix wrongly named variable from the GPG era.
if data.MovePath then
data.TransportChain = data.MovePath
end
local assaultAttackChain, assaultLandingChain
if data.AssaultChains then
local tempChains = {}
local tempNum = 0
for landingChain, attackChain in data.AssaultChains do
for num, pos in ScenarioUtils.ChainToPositions(attackChain) do
if aiBrain:GetThreatAtPosition(pos, 1, true) > 0 then
tempChains[landingChain] = attackChain
tempNum = tempNum + 1
break
end
end
end
local pickNum = Random(1, tempNum)
tempNum = 0
for landingChain, attackChain in tempChains do
tempNum = tempNum + 1
if tempNum == pickNum then
assaultAttackChain = attackChain
assaultLandingChain = landingChain
break
end
end
end
-- Make attack positions out of chain, markers, or marker names
local attackPositions = {}
if data.AttackChain then
attackPositions = ScenarioUtils.ChainToPositions(data.AttackChain)
elseif assaultAttackChain then
attackPositions = ScenarioUtils.ChainToPositions(assaultAttackChain)
else
for _, v in data.AttackPoints do
if type(v) == 'string' then
table.insert(attackPositions, ScenarioUtils.MarkerToPosition(v))
else
table.insert(attackPositions, v)
end
end
end
-- Make landing positions out of chain, markers, or marker names
local landingPositions = {}
if data.LandingChain then
landingPositions = ScenarioUtils.ChainToPositions(data.LandingChain)
elseif assaultLandingChain then
landingPositions = ScenarioUtils.ChainToPositions(assaultLandingChain)
else
for _, v in data.LandingList do
if type(v) == 'string' then
table.insert(landingPositions, ScenarioUtils.MarkerToPosition(v))
else
table.insert(landingPositions, v)
end
end
end
platoon:Stop()
-- Load transports
if not GetLoadTransports(platoon) then
return
end
if not ReadyWaitVariables(data) then
return
end
-- Move the transports along desired route
if data.TransportRoute then
ScenarioFramework.PlatoonMoveRoute(platoon, data.TransportRoute)
elseif data.TransportChain then
ScenarioFramework.PlatoonMoveChain(platoon, data.TransportChain)
end
-- Find landing location and unload units at right spot
local landingLocation = PlatoonChooseRandomNonNegative(aiBrain, landingPositions, 1)
local cmd = platoon:UnloadAllAtLocation(landingLocation)
-- Wait until the units are dropped
while platoon:IsCommandsActive(cmd) do
WaitSeconds(1)
if not aiBrain:PlatoonExists(platoon) then
return
end
end
-- Send transports back to base if desired
if platoon.PlatoonData.TransportReturn then
ReturnTransportsToPool(platoon, data)
end
if data.PatrolChain then
if data.RandomPatrol then
ScenarioFramework.PlatoonPatrolRoute(platoon, GetRandomPatrolRoute(ScenarioUtils.ChainToPositions(data.PatrolChain)))
else
ScenarioFramework.PlatoonPatrolChain(platoon, data.PatrolChain)
end
else
-- Patrol attack route by creating attack route
local attackRoute = PlatoonChooseHighestAttackRoute(aiBrain, attackPositions, 1)
ScenarioFramework.PlatoonPatrolRoute(platoon, attackRoute)
end
for num, unit in platoon:GetPlatoonUnits() do
if EntityCategoryContains(categories.ENGINEER, unit) then
platoon:CaptureAI()
break
end
end
end
--- Platoon moves to a set of locations
--- - MoveRoute - List of locations to move to |
--- - MoveChain - Chain of locations to move |
--- - UseTransports - Boolean, if true, use transports to move |
---@param platoon Platoon
function MoveToThread(platoon)
local data = platoon.PlatoonData
if data then
if data.MoveRoute or data.MoveChain then
local movePositions = {}
if data.MoveChain then
movePositions = ScenarioUtils.ChainToPositions(data.MoveChain)
else
for _, v in data.MoveRoute do
if type(v) == 'string' then
table.insert(movePositions, ScenarioUtils.MarkerToPosition(v))
else
table.insert(movePositions, v)
end
end
end
if data.UseTransports then
for _, v in movePositions do
platoon:MoveToLocation(v, data.UseTransports)
end
else
for _, v in movePositions do
platoon:MoveToLocation(v, false)
end
end
else
error('*SCENARIO PLATOON AI ERROR: MoveToRoute or MoveChain not defined', 2)
end
else
error('*SCENARIO PLATOON AI ERROR: PlatoonData not defined', 2)
end
end
--- Platoon patrols a set of locations
--- - PatrolRoute - List of locations to patrol
--- - PatrolChain - Chain of locations to patrol
---@param platoon Platoon
function PatrolThread(platoon)
local data = platoon.PlatoonData
platoon:Stop()
if data then
if data.PatrolRoute or data.PatrolChain then
if data.PatrolChain then
ScenarioFramework.PlatoonPatrolRoute(platoon, ScenarioUtils.ChainToPositions(data.PatrolChain))
else
for _, v in data.PatrolRoute do
if type(v) == 'string' then
platoon:Patrol(ScenarioUtils.MarkerToPosition(v))
else
platoon:Patrol(v)
end
end
end
else
error('*SCENARIO PLATOON AI ERROR: PatrolRoute or PatrolChain not defined', 2)
end
else
error('*SCENARIO PLATOON AI ERROR: PlatoonData not defined', 2)
end
end
--- Platoon patrols a random set of locations
--- - PatrolRoutes - List of locations to patrol
--- - PatrolChains - Chain of locations to patrol
---@param platoon Platoon
function RandomPatrolThread(platoon)
local data = platoon.PlatoonData
platoon:Stop()
if data then
if data.PatrolRoute or data.PatrolChain then
if data.PatrolChain then
ScenarioFramework.PlatoonPatrolRoute(platoon,
GetRandomPatrolRoute(ScenarioUtils.ChainToPositions(data.PatrolChain)))
else
local route = {}
for _, v in data.PatrolRoute do
if type(v) == 'string' then
table.insert(route, ScenarioUtils.MarkerToPosition(v))
else
table.insert(route, v)
end
end
ScenarioFramework.PlatoonPatrolRoute(platoon, GetRandomPatrolRoute(route))
end
else
error('*SCENARIO PLATOON AI ERROR: PatrolRoute or PatrolChain not defined', 2)
end
else
error('*SCENARIO PLATOON AI ERROR: PlatoonData not defined', 2)
end
end
--- Gives a platoon a random patrol path from a set of locations
--- - PatrolChain - Chain of locations to patrol
---@param platoon Platoon
function RandomDefensePatrolThread(platoon)
local data = platoon.PlatoonData
platoon:Stop()
if data then
if data.PatrolChain then
for _, v in platoon:GetPlatoonUnits() do
ScenarioFramework.GroupPatrolRoute({v}, GetRandomPatrolRoute(ScenarioUtils.ChainToPositions(data.PatrolChain)))
end
else
error('*SCENARIO PLATOON AI ERROR: PatrolChain not defined', 2)
end
else
error('*SCENARIO PLATOON AI ERROR: PlatoonData not defined', 2)
end
end
--- Gives a platoon a random patrol path from a set of chains
--- - PatrolChains - Chains of locations to patrol
---@param platoon Platoon
function PatrolChainPickerThread(platoon)
local data = platoon.PlatoonData
platoon:Stop()
if data then
if data.PatrolChains then
local chain = Random(1, table.getn(data.PatrolChains))
ScenarioFramework.PlatoonPatrolRoute(platoon, ScenarioUtils.ChainToPositions(data.PatrolChains[chain]))
else
error('*SCENARIO PLATOON AI ERROR: PatrolChains not defined', 2)
end
else
error('*SCENARIO PLATOON AI ERROR: PlatoonData not defined', 2)
end
end
--- Gives a random patrol chain form the list to each unit of the platoon
--- - PatrolChains - List of chains to choose from
---@param platoon Platoon
function SplitPatrolThread(platoon)
local data = platoon.PlatoonData
platoon:Stop()
if data then
if data.PatrolChains then
local num = table.getn(data.PatrolChains)
for _, v in platoon:GetPlatoonUnits() do
local chain = Random(1, num)
ScenarioFramework.GroupPatrolChain({v}, data.PatrolChains[chain])
end
else
error('*SCENARIO PLATOON AI ERROR: PatrolChains not defined', 2)
end
else
error('*SCENARIO PLATOON AI ERROR: PlatoonData not defined', 2)
end
end
--- The default engineer build platoon
---@param platoon Platoon
function EngineersBuildPlatoon(platoon)
local aiBrain = platoon:GetBrain()
local platoonUnits = platoon:GetPlatoonUnits()
local data = platoon.PlatoonData
local platoonName = data.PlatoonName
local eng = false
local engTable = {}
local buildingPlatoon = false
local buildingData
local unitBeingBuilt = false
local busy = false
local buildingTemplate = StructureTemplates.BuildingTemplates[aiBrain:GetFactionIndex()]
if not data.PlatoonsTable then
error('*SCENARIO PLATOON AI ERROR: EngineersBuildPlatoon requires PlatoonsTable', 2)
end
-- Find all engineers in platoon
for _, v in platoonUnits do
if EntityCategoryContains(categories.CONSTRUCTION, v) then
if not eng then
eng = v
else
table.insert(engTable, v)
end
end
end
if not eng then
error('*SCENARIO PLATOON AI ERROR: No Engineers found in platoon using EngineersBuildPlatoon', 2)
end
-- Wait for eng to stop moving
while eng:IsUnitState('Moving') do
WaitSeconds(3)
if not aiBrain:PlatoonExists(platoon) then
return
end
end
-- Have all engineers guard main engineer
if not table.empty(engTable) then
if eng.Dead then -- Must check if a death occured since platoon was forked
for num, unit in engTable do
if not unit.Dead then
eng = table.remove(engTable, num)
if not table.empty(engTable) then
IssueGuard(engTable, eng)
end
break
end
end
else
IssueGuard(engTable, eng)
end
end
if not aiBrain.EngBuiltPlatoonList then
aiBrain.EngBuiltPlatoonList = {}
end
while aiBrain:PlatoonExists(platoon) do
-- Set new primary eng
if eng.Dead then
return
end
if not buildingPlatoon then
for _, v in data.PlatoonsTable do
if not aiBrain.EngBuiltPlatoonList[v.PlatoonName] then
buildingPlatoon = v.PlatoonName
buildingData = v
break
else
local plat = aiBrain.EngBuiltPlatoonList[v.PlatoonName]
if not aiBrain:PlatoonExists(plat) then
buildingPlatoon = v.PlatoonName
buildingData = v
break
end
end
end
end
if not eng:IsUnitState('Patrolling') and (eng:IsUnitState('Reclaiming') or eng:IsUnitState('Building') or eng:IsUnitState('Upgrading') or eng:IsUnitState('Repairing')) then
busy = true
end
if not busy and buildingPlatoon then
local newPlatoonUnits = {}
local unitGroup = ScenarioUtils.FlattenTreeGroup(aiBrain.Name, buildingPlatoon)
local plat
for strName, tblData in unitGroup do
if eng and aiBrain:CanBuildStructureAt(tblData.type, tblData.Position) then
IssueStop({eng})
IssueClearCommands({eng})
local result = aiBrain:BuildStructure(eng, tblData.type, {tblData.Position[1], tblData.Position[3], 0}, false)
unitBeingBuilt = false
repeat
WaitSeconds(5)
if eng.Dead then
eng, engTable = AssistOtherEngineer(eng, engTable, unitBeingBuilt)
else
if not unitBeingBuilt then
unitBeingBuilt = eng.UnitBeingBuilt
if unitBeingBuilt then
table.insert(newPlatoonUnits, unitBeingBuilt)
end
end
end
until not eng or eng.Dead or eng:IsIdleState()
if not aiBrain.EngBuiltPlatoonList[buildingPlatoon] then
plat = aiBrain:MakePlatoon('', '')
aiBrain.EngBuiltPlatoonList[buildingPlatoon] = plat
plat.EngBuildName = buildingPlatoon
plat:AddDestroyCallback(function(aiBrain, plat)
aiBrain.EngBuiltPlatoonList[plat.EngBuildName] = false
end
)
end
aiBrain:AssignUnitsToPlatoon(aiBrain.EngBuiltPlatoonList[buildingPlatoon], {unitBeingBuilt}, 'Attack', 'NoFormation')
end
end
buildingPlatoon = false
if not table.empty(plat:GetPlatoonUnits()) then
if buildingData.PlatoonData then
plat.PlatoonData = buildingData.PlatoonData
end
if plat.PlatoonData.AMPlatoons then
for _, v in plat.PlatoonData.AMPlatoons do
plat:SetPartOfAttackForce()
break
end
end
if buildingData.ScenPlatoonAI then
plat:ForkAIThread(import("/lua/scenarioplatoonai.lua")[buildingData.ScenPlatoonAI])
elseif buildingData.PlatoonAI then
plat:ForkAIThread(import("/lua/platoon.lua")[buildingData.PlatoonAI])
elseif buildingData.LocalFunction and buildingData.ScenName then
plat:ForkAIThread(import('/maps/'..buildingData.ScenName..'/'..buildingData.ScenName..'_script.lua')[LocalFunction])
end
end
newPlatoonUnits = {}
-- Disband if desired
if aiBrain:PlatoonExists(platoon) and data.DisbandAfterBuilding then
aiBrain:DisbandPlatoon(platoon)
end
end
if not eng:IsUnitState('Patrolling') and data.PatrolChain then
for _, v in ScenarioUtils.ChainToPositions(data.PatrolChain) do
platoon:Patrol(v)
end
end
WaitSeconds(11)
end
end
--- Sends out units to hunt and attack Experimental Air units (Soul Ripper, Czar, etc). It cheats to find the air units. This should *NOT* ever be used in skirmish. This platoon only seeks out PLAYER experimentals. It won't register experimentals from other AIs
--- - CategoryList - The categories we are going to find and attack
---@param platoon Platoon
function CategoryHunterPlatoonAI(platoon)
local aiBrain = platoon:GetBrain()
local platoonUnits = platoon:GetPlatoonUnits()
local data = platoon.PlatoonData
local target = false
while aiBrain:PlatoonExists(platoon) do
-- Find nearest enemy category to this platoon
-- Cheat to find the focus army's units
local newTarget = false
local platPos = platoon:GetPlatoonPosition()
local enemies = table.shuffle(GetHumanEnemies(aiBrain:GetArmyIndex()))
for i, enemy in enemies do
for catNum, category in platoon.PlatoonData.CategoryList do
local unitList = enemy:GetListOfUnits(category, false, false)
if not table.empty(unitList) then
local distance = 100000
for _, v in unitList do
if not v.Dead then
local currDist = VDist3(platPos, v:GetPosition())
if currDist < distance then
newTarget = v
distance = currDist
end
end
end
-- If the target has changed, attack new target
if newTarget ~= target then
platoon:Stop()
platoon:AttackTarget(newTarget)
end
end
if newTarget then
break
end
end
end
-- If there are no targets, seek out and fight nearest enemy the platoon can find; no cheeting here
if not newTarget then
target = platoon:FindClosestUnit('Attack', 'Enemy', true, categories.ALLUNITS-categories.WALL)
if target and not target.Dead then
platoon:Stop()
platoon:AggressiveMoveToLocation(target:GetPosition())
-- If we still cant find a target, go to the highest threat position on the map
else
platoon:Stop()
local pos, threat = aiBrain:GetHighestThreatPosition(1, true)
platoon:AggressiveMoveToLocation(pos)
end
end
WaitSeconds(Random(73, 181) * .1)
end
end
--- Upgrades / retrieves an engineer of the desired tech. Then moves the engineer to a location, possibly with a transport. Builds the specific buildings found in PlatoonData.Construction, and then maintains or patrols the base depending on what is defined.
--- | Platoon data value | Description |
--- | --------------------- | ------------- |
--- | ReadyVariable | (optional) `ScenarioInfo.VarTable[ReadyVariable]` Variable set when units are on transports
--- | WaitVariable | (optional) `ScenarioInfo.VarTable[WaitVariable]` Variable checked before transports leave
--- | LandingLocation | (optional) Location for transports to drop engineers
--- | MoveBeacon | (optional) TransportBeacon to use to move engineer to location
--- | Construction | A table, see _Construction Data_ below for more information
--- | BuildBaseTemplate | BaseTemplate of the base to build once
--- | MaintainBaseTemplate | BaseTemplate of base to build any non-existing buildings for.
--- | AssistFactories | Bool; will assist factories in a Location Type when set true; break off and rebuild if maintain
--- | LocationType | Platoon base manager location type to have the engineers assist build factories in
--- | BuildingTemplate | building template found in BaseTemplates.lua (only required if trying to build different factions buildings)
--- | PatrolRoute | Route of locations to patrol while maintaining or after platoon disbanded
--- | PatrolChain | Chain of locations to patrol while maintaining or after platoon disbanded
--- | TransportRoute | List of locations for the transport to use to get to the location
--- | TransportChain | Chain of locations for the transport to use to get to landing location
--- | DisbandAfterPatrol | bool, if true, platoon will disband if its not maintaining a base and given a patrol order
--- | RandomPatrol | bool, if true, platoon will sort PatrolRoute randomly
--- | UseTransports | bool, if true, platoons will use transports to move
--- | NamedUnitBuild | table of unit names; platoon will build these specific units and only build them once
--- | GroupBuildOnce | name of a group to build each thing in the group only once
---
--- | Construction data value | Description |
--- | ------------------------- | ------------- |
--- | BuildingTemplate | building template found in BaseTemplates.lua (only required if trying to build different factions buildings)
--- | BaseTemplate | Name of base template to use. This template is generated from bases made in the editor
--- | BuildClose | Bool if you want to have unit pick closest of next building to build
--- | BuildStructures | List of buildings to build in order, ex:T1AirFactory, T2ShieldDefense, etc
---
--- Order of events:
--- - Grab transports
--- - set ready variable
--- - wait for wait variable
--- - travel using transports
--- - travel using beacon
--- - build structures in Construction block
--- - build base using BuildBaseTemplate
--- - the platoon will assist factories if assigned to do so, they will break off and rebuild if Maintain is set
--- - maintain a base using MaintainBaseTemplate (will patrol here if PatrolRoute given)
--- - patrol using PatrolRoute, platoon can disband if given a patrol and is not maintaining a base
---@param platoon Platoon
function StartBaseEngineerThread(platoon)
local aiBrain = platoon:GetBrain()
local platoonUnits = platoon:GetPlatoonUnits()
local data = platoon.PlatoonData
local baseName
-- Check Construction table for bad variables
if data.Construction then
for varName, varData in data.Construction do
if not(varName == 'BuildingTemplate' or varName == 'BaseTemplate' or varName == 'BuildClose' or
varName == 'BuildStructures') then
error('*SCENARIO PLATOON AI ERROR: StartBaseEngineerThread does not accept Construction Table variable named-'..varName, 2)
end
end
end
-- Set BaseTemplats in brain if not existing already
if data then
baseName = data.MaintainBaseTemplate
if baseName then
if not aiBrain.BaseTemplates[baseName] then
AIBuildStructures.CreateBuildingTemplate(aiBrain, aiBrain.Name, baseName)
end
end
if data.BuildBaseTemplate then
if not aiBrain.BaseTemplates[data.BuildBaseTemplate] then
AIBuildStructures.CreateBuildingTemplate(aiBrain, aiBrain.Name, data.BuildBaseTemplate)
end
end
if data.Construction then
if data.Construction.BaseTemplate then
if not aiBrain.BaseTemplates[data.Construction.BaseTemplate] then
AIBuildStructures.CreateBuildingTemplate(aiBrain, aiBrain.Name, data.Construction.BaseTemplate)
end
end
end
end
local eng = false
local engTable = {}
local cmd
local unitBeingBuilt
-- Find all engineers in platoon
for _, v in platoonUnits do
if EntityCategoryContains(categories.CONSTRUCTION, v) then
if not eng then
eng = v
else
table.insert(engTable, v)
end
end
end
if not eng then
error('*SCENARIO PLATOON AI ERROR: No Engineers found in platoon using StartBaseEngineer', 2)
end
-- Wait for eng to stop moving
while not eng.Dead and eng:IsUnitState('Moving') do
WaitSeconds(3)
if not aiBrain:PlatoonExists(platoon) then
return
end
end
platoon:Stop()
-- If platoon needs transports get em
if data.UseTransports then
if not GetLoadTransports(platoon) then
return
end
end
-- Set Ready and hold for Wait variable
if not ReadyWaitVariables(data) then
return
end
-- Move and unload units
if not StartBaseTransports(platoon, data, aiBrain) then
return
end
-- Have all engineers guard main engineer
if not table.empty(engTable) then
if eng.Dead then -- Must check if a death occured since platoon was forked
for num, unit in engTable do
if not unit.Dead then
eng = table.remove(engTable, num)
if not table.empty(engTable) then
IssueGuard(engTable, eng)
end
break
end
end
else
IssueGuard(engTable, eng)
end
end
-- Construction Block building
if not StartBaseConstruction(eng, engTable, data, aiBrain) then
return
end
-- Build specific units
if not StartBaseBuildUnits(eng, engTable, data, aiBrain) then
return
end
-- Build group unit once thing
if not StartBaseGroupOnceBuild(eng, engTable, data, aiBrain) then
return
end
-- BuildBaseTemplate building
if not StartBaseBuildBase(eng, engTable, data, aiBrain) then
return
end
-- Factory assisting
if data.LocationType and data.AssistFactories then
ForkThread(EngineersAssistFactories, platoon, data.LocationType)
end
-- MaintainBaseTemplate
if ScenarioInfo.Options.Difficulty >= (data.MaintainDiffLevel or 2) then
if not StartBaseMaintainBase(platoon, eng, engTable, data, aiBrain) then
return
end
end
-- Send engineers on patrol
if aiBrain:PlatoonExists(platoon) then
EngPatrol(eng, engTable, data)
end
-- Disband if desired
if aiBrain:PlatoonExists(platoon) and data.DisbandAfterPatrol then
aiBrain:DisbandPlatoon(platoon)
end
end
--- Utility Function
--- Gets engineers using StartBaseEngineers to their location
---@param platoon Platoon
---@param data table
---@param aiBrain AIBrain
---@return boolean
function StartBaseTransports(platoon, data, aiBrain)
-- Move the unit using transports
if data.UseTransports then
if data.TransportRoute then
for _, v in data.TransportRoute do
if type(v) == 'string' then
platoon:MoveToLocation(ScenarioUtils.MarkerToPosition(v), false, 'Scout')
else
platoon:MoveToLocation(v, false, 'Scout')
end
end
elseif data.TransportChain then
local transPositionChain = {}
transPositionChain = ScenarioUtils.ChainToPositions(data.TransportChain)
for _, v in transPositionChain do
platoon:MoveToLocation(v, false, 'Scout')
end
end
-- Unload transports
if type(data.LandingLocation) == 'string' then
cmd = platoon:UnloadAllAtLocation(ScenarioUtils.MarkerToPosition(data.LandingLocation))
else
cmd = platoon:UnloadAllAtLocation(data.LandingLocation)
end
-- Wait for unload to end
while platoon:IsCommandsActive(cmd) do
WaitSeconds(3)
if not aiBrain:PlatoonExists(platoon) then
return false
end
end
if data.TransportReturn then
ReturnTransportsToPool(platoon, data)
end
end
-- Move unit if needed - USING FERRYS
if data.MoveBeacon then
while not ScenarioInfo.VarTable[data.MoveBeacon] do
WaitSeconds(3)
if not aiBrain:PlatoonExists(platoon) then
return false
end
end
cmd = platoon:UseFerryBeacon(categories.ALLUNITS, ScenarioInfo.VarTable[data.MoveBeacon])
while platoon:IsCommandsActive(cmd) do
WaitSeconds(3)
if not aiBrain:PlatoonExists(platoon) then
return false
end
end
end
return true
end
--- Utility Function
--- Takes transports in platoon, returns them to pool, flys them back to return location
---@param platoon Platoon
---@param data table
function ReturnTransportsToPool(platoon, data)
-- Put transports back in TPool