/
UnitCountBuildConditions.lua
1495 lines (1361 loc) · 51.6 KB
/
UnitCountBuildConditions.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/editor/UnitCountBuildConditions.lua
--** Author(s): Dru Staltman, John Comes
--**
--** Summary : Generic AI Platoon Build Conditions
--** Build conditions always return true or false
--**
--** Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
--****************************************************************************
local AIUtils = import("/lua/ai/aiutilities.lua")
local ScenarioFramework = import("/lua/scenarioframework.lua")
local ScenarioUtils = import("/lua/sim/scenarioutilities.lua")
local Utils = import("/lua/utilities.lua")
---@param aiBrain AIBrain
---@param numReq number
---@param category EntityCategory
---@param idleReq boolean
---@return boolean
function HaveEqualToUnitsWithCategory(aiBrain, numReq, category, idleReq)
local numUnits
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
if not idleReq then
numUnits = aiBrain:GetCurrentUnits(testCat)
else
numUnits = table.getn(aiBrain:GetListOfUnits(testCat, true))
end
return numUnits == numReq
end
---@param aiBrain AIBrain
---@param numReq number
---@param category EntityCategory
---@param idleReq boolean
---@return boolean
function HaveGreaterThanUnitsWithCategory(aiBrain, numReq, category, idleReq)
local numUnits
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
if not idleReq then
numUnits = aiBrain:GetCurrentUnits(testCat)
else
numUnits = table.getn(aiBrain:GetListOfUnits(testCat, true))
end
return numUnits > numReq
end
---@param aiBrain AIBrain
---@param numReq number
---@param category EntityCategory
---@param idleReq boolean
---@return boolean
function HaveLessThanUnitsWithCategory(aiBrain, numReq, category, idleReq)
local numUnits
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
if not idleReq then
numUnits = aiBrain:GetCurrentUnits(testCat)
else
numUnits = table.getn(aiBrain:GetListOfUnits(testCat, true))
end
return numUnits < numReq
end
---@param aiBrain AIBrain
---@param numReq number
---@param category EntityCategory
---@param area Area
---@return boolean
function HaveLessThanUnitsWithCategoryInArea(aiBrain, numReq, category, area)
local numUnits = ScenarioFramework.NumCatUnitsInArea(category, ScenarioUtils.AreaToRect(area), aiBrain)
return numUnits < numReq
end
---@param aiBrain AIBrain
---@param baseName string
---@param category EntityCategory
---@param num number
---@return boolean
function NumUnitsLessNearBase(aiBrain, baseName, category, num)
if aiBrain.BaseTemplates[baseName].Location == nil then
return false
else
local unitList = aiBrain:GetUnitsAroundPoint(category, aiBrain.BaseTemplates[baseName].Location,aiBrain.BaseTemplates[baseName].Radius, 'Ally')
local count = 0
for i, unit in unitList do
if unit:GetAIBrain() == aiBrain then
count = count + 1
end
end
return count < num
end
end
---@param aiBrain AIBrain
---@param category1 EntityCategory
---@param category2 EntityCategory
---@return boolean
function HaveLessThanUnitComparison(aiBrain, category1, category2)
local testCat1 = category1
if type(category1) == 'string' then
testCat1 = ParseEntityCategory(category1)
end
local testCat2 = category2
if type(category2) == 'string' then
testCat2 = ParseEntityCategory(category2)
end
return aiBrain:GetCurrentUnits(testCat1) < aiBrain:GetCurrentUnits(testCat2)
end
---@param aiBrain AIBrain
---@param category1 EntityCategory
---@param category2 EntityCategory
---@return boolean
function HaveGreaterThanUnitComparison(aiBrain, category1, category2)
local testCat1 = category1
if type(category1) == 'string' then
testCat1 = ParseEntityCategory(category1)
end
local testCat2 = category2
if type(category2) == 'string' then
testCat2 = ParseEntityCategory(category2)
end
return aiBrain:GetCurrentUnits(testCat1) > aiBrain:GetCurrentUnits(testCat2)
end
---@param aiBrain AIBrain
---@param varName string
---@param category EntityCategory
---@return boolean
function HaveLessThanVarTableUnitsWithCategory(aiBrain, varName, category)
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
local numUnits = aiBrain:GetCurrentUnits(testCat)
return ScenarioInfo.VarTable[varName] and (numUnits < ScenarioInfo.VarTable[varName])
end
---@param aiBrain AIBrain
---@param varName string
---@param category EntityCategory
---@return boolean
function HaveGreaterThanVarTableUnitsWithCategory(aiBrain, varName, category)
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
local numUnits = aiBrain:GetCurrentUnits(testCat)
return ScenarioInfo.VarTable[varName] and (numUnits > ScenarioInfo.VarTable[varName])
end
---@param aiBrain AIBrain
---@param varName string
---@param category EntityCategory
---@param area string
---@return boolean
function HaveLessThanVarTableUnitsWithCategoryInArea(aiBrain, varName, category, area)
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
local numUnits = ScenarioFramework.NumCatUnitsInArea(testCat, ScenarioUtils.AreaToRect(area), aiBrain)
return ScenarioInfo.VarTable[varName] and (numUnits < ScenarioInfo.VarTable[varName])
end
---@param aiBrain AIBrain
---@param varName string
---@param category EntityCategory
---@param area string
---@return boolean
function HaveGreaterThanVarTableUnitsWithCategoryInArea(aiBrain, varName, category, area)
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
local numUnits = ScenarioFramework.NumCatUnitsInArea(testCat, ScenarioUtils.AreaToRect(area), aiBrain)
return ScenarioInfo.VarTable[varName] and (numUnits > ScenarioInfo.VarTable[varName])
end
---@param aiBrain AIBrain
---@param numReq number
---@param category EntityCategory
---@param constructionCat EntityCategory
---@return boolean
function HaveGreaterThanUnitsInCategoryBeingBuilt(aiBrain, numReq, category, constructionCat)
local cat = category
if type(category) == 'string' then
cat = ParseEntityCategory(category)
end
local consCat = constructionCat
if consCat and type(consCat) == 'string' then
consCat = ParseEntityCategory(constructionCat)
end
local numUnits
if consCat then
numUnits = aiBrain:NumCurrentlyBuilding(cat, cat + categories.CONSTRUCTION + consCat)
else
numUnits = aiBrain:NumCurrentlyBuilding(cat, cat + categories.CONSTRUCTION)
end
return numUnits > numReq
end
function HaveLessThanUnitsInCategoryBeingBuilt(aiBrain, numunits, category)
--DUNCAN - rewritten, credit to Sorian
if type(category) == 'string' then
category = ParseEntityCategory(category)
end
local unitsBuilding = aiBrain:GetListOfUnits(categories.CONSTRUCTION, false)
local numBuilding = 0
for unitNum, unit in unitsBuilding do
if not unit:BeenDestroyed() and unit:IsUnitState('Building') then
local buildingUnit = unit.UnitBeingBuilt
if buildingUnit and not buildingUnit.Dead and EntityCategoryContains(category, buildingUnit) then
numBuilding = numBuilding + 1
end
end
--DUNCAN - added to pick up engineers that havent started building yet... does it work?
if not unit:BeenDestroyed() and not unit:IsUnitState('Building') then
local buildingUnit = unit.UnitBeingBuilt
if buildingUnit and not buildingUnit.Dead and EntityCategoryContains(category, buildingUnit) then
--LOG('Engi building but not in building state...')
numBuilding = numBuilding + 1
end
end
if numunits <= numBuilding then
return false
end
end
return numunits > numBuilding
end
---@param aiBrain AIBrain
---@param markerType string
---@param markerRadius number
---@param locationType string
---@param locationRadius number
---@param unitCount number
---@param unitCategory string
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function HaveLessThanUnitsAroundMarkerCategory(aiBrain, markerType, markerRadius, locationType, locationRadius,unitCount, unitCategory, threatMin, threatMax, threatRings, threatType)
local pos = aiBrain:PBMGetLocationCoords(locationType)
if not pos then
return false
end
local positions = AIUtils.AIGetMarkersAroundLocation(aiBrain, markerType, pos, locationType, threatMin, threatMax, threatRings, threatType)
for k,v in positions do
local unitTotal = table.getn(AIUtils.GetOwnUnitsAroundPoint(aiBrain, ParseEntityCategory(unitCategory), v.Position, markerRadius, threatMin,
threatMax, threatRings, threatType))
if unitTotal < unitCount then
return true
end
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function StartLocationNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
local pos, name = AIUtils.AIFindStartLocationNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
if pos then
return true
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function StartLocationsFull(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
local pos, name = AIUtils.AIFindStartLocationNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
if not pos then
return true
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function ExpansionAreaNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
local pos, name = AIUtils.AIFindExpansionAreaNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
if pos then
return true
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function NavalAreaNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
local pos, name = AIUtils.AIFindNavalAreaNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
if pos then
return true
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function NavalAreasFull(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
local pos, name = AIUtils.AIFindNavalAreaNeedsEngineer(aiBrain, locationType, locationRadius, threatMin, threatMax, threatRings, threatType)
if not pos then
return true
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param category EntityCategory
---@param markerRadius number
---@param unitMax number
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function DefensivePointNeedsStructure(aiBrain, locationType, locationRadius, category, markerRadius, unitMax, threatMin, threatMax, threatRings, threatType)
local pos, name = AIUtils.AIFindDefensivePointNeedsStructure(aiBrain, locationType, locationRadius, category, markerRadius, unitMax, threatMin, threatMax, threatRings, threatType)
if pos then
return true
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param category EntityCategory
---@param markerRadius number
---@param unitMax number
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function NavalDefensivePointNeedsStructure(aiBrain, locationType, locationRadius, category, markerRadius, unitMax, threatMin, threatMax, threatRings, threatType)
local pos, name = AIUtils.AIFindNavalDefensivePointNeedsStructure(aiBrain, locationType, locationRadius, category, markerRadius, unitMax, threatMin, threatMax, threatRings, threatType)
if pos then
return true
end
return false
end
---@param aiBrain AIBrain
---@param locationType string
---@param locationRadius number
---@param unitCount number
---@param unitCategory EntityCategory
---@param threatMin number
---@param threatMax number
---@param threatRings number
---@param threatType string
---@return boolean
function HaveAreaWithUnitsFewWalls(aiBrain, locationType, locationRadius, unitCount, unitCategory, threatMin, threatMax, threatRings, threatType)
local pos = aiBrain:PBMGetLocationCoords(locationType)
if not pos then
return false
end
local positions = {}
if aiBrain.HasPlatoonList then
for k,v in aiBrain.PBM.Locations do
if v.LocationType ~= locationType and Utils.XZDistanceTwoVectors(pos, v.Location) <= locationRadius then
table.insert(positions, v.Location)
end
end
elseif aiBrain.BuilderManagers[locationType] then
table.insert(positions, aiBrain.BuilderManagers[locationType].FactoryManager:GetLocationCoords())
end
local otherPos = AIUtils.AIGetMarkersAroundLocation(aiBrain, 'Defensive Point', pos, locationRadius, threatMin, threatMax, threatRings, threatType)
for k,v in otherPos do
table.insert(positions, v.Position)
end
for k,v in positions do
local unitTotal = table.getn(AIUtils.GetOwnUnitsAroundPoint(aiBrain, ParseEntityCategory('DEFENSE'), v, 30, threatMin,
threatMax, threatRings, threatType))
if unitTotal > unitCount then
if aiBrain:GetNumUnitsAroundPoint(categories.WALL, v, 30, 'Ally') < 15 then
return true
end
end
end
return false
end
---@param aiBrain AIBrain
---@param greater boolean
---@param numReq number
---@param category EntityCategory
---@param alliance string
---@return boolean
function HaveUnitsWithCategoryAndAlliance(aiBrain, greater, numReq, category, alliance)
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
local numUnits = aiBrain:GetNumUnitsAroundPoint(testCat, Vector(0,0,0), 100000, alliance)
return (numUnits > numReq and greater) or (numUnits < numReq and not greater)
end
---@param aiBrain AIBrain
---@param locationType string
---@return boolean
function EngineersNeedGuard(aiBrain, locationType)
local units = aiBrain:GetListOfUnits(categories.ENGINEER - categories.COMMAND, false)
for k,v in units do
if v.NeedGuard and not v.BeingGuarded then
return true
end
end
return false
end
-- =========================================== --
-- Builder Manager Generic Unit Counts
-- =========================================== --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param compareType string
---@return boolean
function HaveUnitComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, compareType)
local engineerManager = aiBrain.BuilderManagers[locationType].EngineerManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
if not engineerManager then
WARN('*AI WARNING: HaveUnitComparisonAtLocation - Invalid location - ' .. locationType)
return false
end
local numUnits = table.getn(AIUtils.GetOwnUnitsAroundPoint(aiBrain, testCat, engineerManager:GetLocationCoords(), engineerManager.Radius))
return CompareBody(numUnits, unitCount, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function UnitsLessAtLocation(aiBrain, locationType, unitCount, unitCategory)
return HaveUnitComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '<')
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function UnitsGreaterAtLocation(aiBrain, locationType, unitCount, unitCategory)
return HaveUnitComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '>')
end
-- ============================================ --
-- Builder Manager Location Pool Counts
-- ============================================ --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param compareType string
---@return boolean
function HavePoolUnitComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, compareType)
local engineerManager = aiBrain.BuilderManagers[locationType].EngineerManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
if not engineerManager then
WARN('*AI WARNING: HavePoolUnitComparisonAtLocation - Invalid location - ' .. locationType)
return false
end
local poolPlatoon = aiBrain:GetPlatoonUniquelyNamed('ArmyPool')
local numUnits = poolPlatoon:GetNumCategoryUnits(testCat, engineerManager:GetLocationCoords(), engineerManager.Radius)
return CompareBody(numUnits, unitCount, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function PoolLessAtLocation(aiBrain, locationType, unitCount, unitCategory)
return HavePoolUnitComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '<')
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function PoolGreaterAtLocation(aiBrain, locationType, unitCount, unitCategory)
return HavePoolUnitComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '>')
end
-- ======================================= --
-- Builder Manager Engineer Counts
-- ======================================= --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param compareType string
---@return boolean
function EngineerComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, compareType)
local engineerManager = aiBrain.BuilderManagers[locationType].EngineerManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
if not engineerManager then
WARN('*AI WARNING: EngineerComparisonAtLocation - Invalid location - ' .. locationType)
return false
end
local numUnits = engineerManager:GetNumCategoryUnits('Engineers', testCat)
return CompareBody(numUnits, unitCount, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function EngineerLessAtLocation(aiBrain, locationType, unitCount, unitCategory)
return EngineerComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '<')
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function EngineerGreaterAtLocation(aiBrain, locationType, unitCount, unitCategory)
return EngineerComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '>')
end
-- ====================================== --
-- Factory Manager Factory Counts
-- ====================================== --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param compareType string
---@return boolean
function FactoryComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, compareType)
local factoryManager = aiBrain.BuilderManagers[locationType].FactoryManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
if not factoryManager then
WARN('*AI WARNING: FactoryComparisonAtLocation - Invalid location - ' .. locationType)
return false
end
local numUnits = factoryManager:GetNumCategoryFactories(testCat)
return CompareBody(numUnits, unitCount, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function FactoryLessAtLocation(aiBrain, locationType, unitCount, unitCategory)
return FactoryComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '<')
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@return boolean
function FactoryGreaterAtLocation(aiBrain, locationType, unitCount, unitCategory)
return FactoryComparisonAtLocation(aiBrain, locationType, unitCount, unitCategory, '>')
end
-- ====================================== --
-- Factory Manager Factory Ratios
-- ====================================== --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCategory EntityCategory
---@param unitCategory2 EntityCategory
---@param compareType string
---@return boolean
function FactoryRatioComparisonAtLocation(aiBrain, locationType, unitCategory, unitCategory2, compareType)
local factoryManager = aiBrain.BuilderManagers[locationType].FactoryManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
local testCat2 = unitCategory2
if type(unitCategory2) == 'string' then
testCat2 = ParseEntityCategory(unitCategory2)
end
if not factoryManager then
WARN('*AI WARNING: FactoryRatioComparisonAtLocation - Invalid location - ' .. locationType)
return false
end
local numUnits = factoryManager:GetNumCategoryFactories(testCat)
local numUnits2 = factoryManager:GetNumCategoryFactories(testCat2)
return CompareBody(numUnits, numUnits2, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCategory EntityCategory
---@param unitCategory2 EntityCategory
---@return boolean
function FactoryRatioLessAtLocation(aiBrain, locationType, unitCategory, unitCategory2)
return FactoryRatioComparisonAtLocation(aiBrain, locationType, unitCategory, unitCategory2, '<')
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCategory EntityCategory
---@param unitCategory2 EntityCategory
---@return boolean
function FactoryRatioGreaterAtLocation(aiBrain, locationType, unitCategory, unitCategory2)
return FactoryRatioComparisonAtLocation(aiBrain, locationType, unitCategory, unitCategory2, '>')
end
-- ============================== --
-- Manager Builing Counts
-- ============================== --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param compareType string
---@param builderCat EntityCategory
---@return boolean
function LocationBuildingComparison(aiBrain, locationType, unitCount, unitCategory, compareType, builderCat)
local platoonFormManager = aiBrain.BuilderManagers[locationType].PlatoonFormManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
local builderTestCat = builderCat
if type(builderTestCat) == 'string' then
builderTestCat = ParseEntityCategory(builderTestCat)
end
if not platoonFormManager then
WARN('*AI WARNING: LocationBuildingComparison - Invalid location - ' .. locationType)
return false
end
local numUnits = table.getn(platoonFormManager:GetUnitsBeingBuilt(testCat, builderTestCat or categories.ALLUNITS))
return CompareBody(numUnits, unitCount, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param builderCat EntityCategory
---@return boolean
function BuildingLessAtLocation(aiBrain, locationType, unitCount, unitCategory, builderCat)
return LocationBuildingComparison(aiBrain, locationType, unitCount, unitCategory, '<', builderCat)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param builderCat EntityCategory
---@return boolean
function BuildingGreaterAtLocation(aiBrain, locationType, unitCount, unitCategory, builderCat)
return LocationBuildingComparison(aiBrain, locationType, unitCount, unitCategory, '>', builderCat)
end
-- ============================================ --
-- Factory Manager Building Unit Counts
-- ============================================ --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param compareType string
---@param facCat EntityCategory
---@return boolean
function LocationFactoriesBuildingComparison(aiBrain, locationType, unitCount, unitCategory, compareType, facCat)
local factoryManager = aiBrain.BuilderManagers[locationType].FactoryManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
if not factoryManager then
WARN('*AI WARNING: LocationFactoriesBuildingComparison - Invalid location - ' .. locationType)
return false
end
local testFac = facCat or categories.ALLUNITS
if type(facCat) == 'string' then
testFac = ParseEntityCategory(facCat)
end
local numUnits = factoryManager:GetNumCategoryBeingBuilt(testCat, testFac)
return CompareBody(numUnits, unitCount, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param facCat EntityCategory
---@return boolean
function LocationFactoriesBuildingLess(aiBrain, locationType, unitCount, unitCategory, facCat)
return LocationFactoriesBuildingComparison(aiBrain, locationType, unitCount, unitCategory, '<', facCat)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param facCat EntityCategory
---@return boolean
function LocationFactoriesBuildingGreater(aiBrain, locationType, unitCount, unitCategory, facCat)
return LocationFactoriesBuildingComparison(aiBrain, locationType, unitCount, unitCategory, '>', facCat)
end
-- ============================================= --
-- Engineer Manager Building Unit Counts
-- ============================================= --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param compareType string
---@param engCat EntityCategory
---@return boolean
function LocationEngineersBuildingComparison(aiBrain, locationType, unitCount, unitCategory, compareType, engCat)
local engineerManager = aiBrain.BuilderManagers[locationType].EngineerManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
if not engineerManager then
WARN('*AI WARNING: LocationEngineersBuildingComparison - Invalid location - ' .. locationType)
return false
end
local engCat = engCat or categories.ALLUNITS
if type(engCat) == 'string' then
engCat = ParseEntityCategory(engCat)
end
local numUnits = engineerManager:GetNumCategoryBeingBuilt(testCat, engCat)
return CompareBody(numUnits, unitCount, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param engCat EntityCategory
---@return boolean
function LocationEngineersBuildingLess(aiBrain, locationType, unitCount, unitCategory, engCat)
return LocationEngineersBuildingComparison(aiBrain, locationType, unitCount, unitCategory, '<', engCat)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param engCat EntityCategory
---@return boolean
function LocationEngineersBuildingGreater(aiBrain, locationType, unitCount, unitCategory, engCat)
return LocationEngineersBuildingComparison(aiBrain, locationType, unitCount, unitCategory, '>', engCat)
end
-- ===================================================== --
-- Engineers Wanting Assistance Build Conditions
-- ===================================================== --
---@param aiBrain AIBrain
---@param locationType string
---@param unitCategory EntityCategory
---@param compareType string
---@param engCat EntityCategory
---@return boolean
function LocationEngineersBuildingAssistanceComparison(aiBrain, locationType, unitCategory, compareType, engCat)
local engineerManager = aiBrain.BuilderManagers[locationType].EngineerManager
local testCat = unitCategory
if type(unitCategory) == 'string' then
testCat = ParseEntityCategory(unitCategory)
end
if not engineerManager then
WARN('*AI WARNING: LocationEngineersBuildingAssistanceComparison - Invalid location - ' .. locationType)
return false
end
local engCat = engCat or categories.ALLUNITS
if type(engCat) == 'string' then
engCat = ParseEntityCategory(engCat)
end
local numUnits = table.getn(engineerManager:GetEngineersWantingAssistance(testCat, engCat))
return CompareBody(numUnits, 0, compareType)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param engCat EntityCategory
---@return boolean
function LocationEngineersBuildingAssistanceLess(aiBrain, locationType, unitCount, unitCategory, engCat)
return LocationEngineersBuildingAssistanceComparison(aiBrain, locationType, unitCategory, '<', engCat)
end
---@param aiBrain AIBrain
---@param locationType string
---@param unitCount number
---@param unitCategory EntityCategory
---@param engCat EntityCategory
---@return boolean
function LocationEngineersBuildingAssistanceGreater(aiBrain, locationType, unitCount, unitCategory, engCat)
return LocationEngineersBuildingAssistanceComparison(aiBrain, locationType, unitCategory, '>', engCat)
end
-- ==================================================== --
-- Factory Manager Check Maximum Factory Number
-- ==================================================== --
---@param aiBrain AIBrain
---@param locationType string
---@param factoryType string
---@return boolean
function FactoryCapCheck(aiBrain, locationType, factoryType)
local catCheck = false
if factoryType == 'Land' then
catCheck = categories.LAND * categories.FACTORY
elseif factoryType == 'Air' then
catCheck = categories.AIR * categories.FACTORY
elseif factoryType == 'Sea' then
catCheck = categories.NAVAL * categories.FACTORY
elseif factoryType == 'Gate' then
catCheck = categories.GATE
else
WARN('*AI WARNING: Invalid factorytype - ' .. factoryType)
return false
end
local factoryManager = aiBrain.BuilderManagers[locationType].FactoryManager
if not factoryManager then
WARN('*AI WARNING: FactoryCapCheck - Invalid location - ' .. locationType)
return false
end
local numUnits = factoryManager:GetNumCategoryFactories(catCheck)
numUnits = numUnits + aiBrain:GetEngineerManagerUnitsBeingBuilt(catCheck)
if numUnits < aiBrain.BuilderManagers[locationType].BaseSettings.FactoryCount[factoryType] then
return true
end
return false
end
-- ===================================================== --
-- Engineer Manager Check Maximum Factory Number
-- ===================================================== --
---@param aiBrain AIBrain
---@param locationType string
---@param techLevel number
---@return boolean
function EngineerCapCheck(aiBrain, locationType, techLevel)
local catCheck = false
if techLevel == 'Tech1' then
catCheck = categories.TECH1
elseif techLevel == 'Tech2' then
catCheck = categories.TECH2
elseif techLevel == 'Tech3' then
catCheck = categories.TECH3
elseif techLevel == 'SCU' then
catCheck = categories.SUBCOMMANDER
else
WARN('*AI WARNING: Invalid techLevel - ' .. techLevel)
return false
end
local engineerManager = aiBrain.BuilderManagers[locationType].EngineerManager
if not engineerManager then
WARN('*AI WARNING: EngineerCapCheck - Invalid location - ' .. locationType)
return false
end
local numUnits = engineerManager:GetNumCategoryUnits('Engineers', catCheck)
if numUnits < aiBrain.BuilderManagers[locationType].BaseSettings.EngineerCount[techLevel] then
return true
end
return false
end
-- ======================================================================================= --
-- Adjacency Check - Ensures a building category can have something adjacent to it
-- ======================================================================================= --
---@param aiBrain AIBrain
---@param locationType string
---@param category EntityCategory
---@param radius number
---@param testUnit Unit
---@return boolean
function AdjacencyCheck(aiBrain, locationType, category, radius, testUnit)
local factoryManager = aiBrain.BuilderManagers[locationType].FactoryManager
if not factoryManager then
WARN('*AI WARNING: AdjacencyCheck - Invalid location - ' .. locationType)
return false
end
local testCat = category
if type(category) == 'string' then
testCat = ParseEntityCategory(category)
end
local reference = AIUtils.GetOwnUnitsAroundPoint(aiBrain, testCat, factoryManager:GetLocationCoords(), radius)
if not reference or table.empty(reference) then
return false
end
local template = {}
local unitSize = aiBrain:GetUnitBlueprint(testUnit).Physics
for k,v in reference do
if not v.Dead then
local targetSize = v:GetBlueprint().Physics
local targetPos = v:GetPosition()
targetPos[1] = targetPos[1] - (targetSize.SkirtSizeX/2)
targetPos[3] = targetPos[3] - (targetSize.SkirtSizeZ/2)
-- Top/bottom of unit
for i=0,((targetSize.SkirtSizeX/2)-1) do
local testPos = { targetPos[1] + 1 + (i * 2), targetPos[3]-(unitSize.SkirtSizeZ/2), 0 }
local testPos2 = { targetPos[1] + 1 + (i * 2), targetPos[3]+targetSize.SkirtSizeZ+(unitSize.SkirtSizeZ/2), 0 }
table.insert(template, testPos)
table.insert(template, testPos2)
end
-- Sides of unit
for i=0,((targetSize.SkirtSizeZ/2)-1) do
local testPos = { targetPos[1]+targetSize.SkirtSizeX + (unitSize.SkirtSizeX/2), targetPos[3] + 1 + (i * 2), 0 }