forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformations.lua
1682 lines (1500 loc) · 67.4 KB
/
formations.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 : /cdimage/lua/formations.lua
-- ** Author(s):
-- **
-- ** Summary :
-- **
-- ** Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
-- ****************************************************************************
--
-- Basic create formation scripts
---@alias UnitFormations 'AttackFormation' | 'GrowthFormation' | 'NoFormation' | 'None' | 'none'
SurfaceFormations = {
'AttackFormation',
'GrowthFormation',
}
AirFormations = {
'AttackFormation',
'GrowthFormation',
}
ComboFormations = {
'AttackFormation',
'GrowthFormation',
}
local FormationPos = {} -- list to be returned
local FormationCache = {}
local MaxCacheSize = 30
---@param formationUnits Unit[]
---@param formationType UnitFormations
---@return boolean
function GetCachedResults(formationUnits, formationType)
local cache = FormationCache[formationType]
if not cache then
return false
end
local unitCount = table.getn(formationUnits)
for _, data in cache do
if data.UnitCount == unitCount then
local match = true
for i = 0, unitCount - 1, 1 do -- These indices are 0-based.
if data.Units[i] ~= formationUnits[i] then
match = false
break
end
end
if match then
return data.Results
end
end
end
return false
end
---@param results TLaserBotProjectile
---@param formationUnits Unit[]
---@param formationType UnitFormations
function CacheResults(results, formationUnits, formationType)
if not FormationCache[formationType] then
FormationCache[formationType] = {}
end
local cache = FormationCache[formationType]
if table.getn(cache) >= MaxCacheSize then
table.remove(cache)
end
table.insert(cache, 1, {Results = results, Units = formationUnits, UnitCount = table.getn(formationUnits)})
end
-- =========================================
-- ================ LAND DATA ==============
-- =========================================
local RemainingCategory = { 'RemainingCategory', }
-- === LAND CATEGORIES ===
local DirectFire = (categories.DIRECTFIRE - (categories.CONSTRUCTION + categories.SNIPER)) * categories.LAND
local Sniper = categories.SNIPER * categories.LAND
local Artillery = (categories.ARTILLERY + categories.INDIRECTFIRE - categories.SNIPER) * categories.LAND
local AntiAir = (categories.ANTIAIR - (categories.EXPERIMENTAL + categories.DIRECTFIRE + categories.SNIPER + Artillery)) * categories.LAND
local Construction = ((categories.COMMAND + categories.CONSTRUCTION + categories.ENGINEER) - (DirectFire + Sniper + Artillery)) * categories.LAND
local UtilityCat = (((categories.RADAR + categories.COUNTERINTELLIGENCE) - categories.DIRECTFIRE) + categories.SCOUT) * categories.LAND
local ShieldCat = categories.uel0307 + categories.ual0307 + categories.xsl0307
-- === TECH LEVEL LAND CATEGORIES ===
local LandCategories = {
Shields = ShieldCat,
Bot1 = (DirectFire * categories.TECH1) * categories.BOT - categories.SCOUT,
Bot2 = (DirectFire * categories.TECH2) * categories.BOT - categories.SCOUT,
Bot3 = (DirectFire * categories.TECH3) * categories.BOT - categories.SCOUT,
Bot4 = (DirectFire * categories.EXPERIMENTAL) * categories.BOT - categories.SCOUT,
Tank1 = (DirectFire * categories.TECH1) - categories.BOT - categories.SCOUT,
Tank2 = (DirectFire * categories.TECH2) - categories.BOT - categories.SCOUT,
Tank3 = (DirectFire * categories.TECH3) - categories.BOT - categories.SCOUT,
Tank4 = (DirectFire * categories.EXPERIMENTAL) - categories.BOT - categories.SCOUT,
Sniper1 = (Sniper * categories.TECH1) - categories.SCOUT,
Sniper2 = (Sniper * categories.TECH2) - categories.SCOUT,
Sniper3 = (Sniper * categories.TECH3) - categories.SCOUT,
Sniper4 = (Sniper * categories.EXPERIMENTAL) - categories.SCOUT,
Art1 = Artillery * categories.TECH1,
Art2 = Artillery * categories.TECH2,
Art3 = Artillery * categories.TECH3,
Art4 = Artillery * categories.EXPERIMENTAL,
AA1 = AntiAir * categories.TECH1,
AA2 = AntiAir * categories.TECH2,
AA3 = AntiAir * categories.TECH3,
Com1 = Construction * categories.TECH1,
Com2 = Construction * categories.TECH2,
Com3 = Construction - (categories.TECH1 + categories.TECH2 + categories.EXPERIMENTAL),
Com4 = Construction * categories.EXPERIMENTAL,
Util1 = (UtilityCat * categories.TECH1) + categories.OPERATION,
Util2 = UtilityCat * categories.TECH2,
Util3 = UtilityCat * categories.TECH3,
Util4 = UtilityCat * categories.EXPERIMENTAL,
RemainingCategory = categories.LAND - (DirectFire + Sniper + Construction + Artillery + AntiAir + UtilityCat + ShieldCat)
}
-- === SUB GROUP ORDERING ===
local Bots = { 'Bot4', 'Bot3', 'Bot2', 'Bot1', }
local Tanks = { 'Tank4', 'Tank3', 'Tank2', 'Tank1', }
local DF = { 'Tank4', 'Bot4', 'Tank3', 'Bot3', 'Tank2', 'Bot2', 'Tank1', 'Bot1', }
local Art = { 'Art4', 'Sniper4', 'Art3', 'Sniper3', 'Art2', 'Sniper2', 'Art1', 'Sniper1', }
local T1Art = { 'Sniper1', 'Art1', 'Sniper2', 'Art2', 'Sniper3', 'Art3', 'Sniper4', 'Art4', }
local AA = { 'AA3', 'AA2', 'AA1', }
local Util = { 'Util4', 'Util3', 'Util2', 'Util1', }
local Com = { 'Com4', 'Com3', 'Com2', 'Com1', }
local Shield = { 'Shields', }
-- === LAND BLOCK TYPES =
local DFFirst = { DF, T1Art, AA, Shield, Com, Util, RemainingCategory }
local ShieldFirst = { Shield, AA, DF, T1Art, Com, Util, RemainingCategory }
local AAFirst = { AA, DF, T1Art, Shield, Com, Util, RemainingCategory }
local ArtFirst = { Art, DF, AA, Shield, Com, Util, RemainingCategory }
local T1ArtFirst = { T1Art, DF, AA, Shield, Com, Util, RemainingCategory }
local UtilFirst = { Util, AA, Shield, DF, T1Art, Com, RemainingCategory }
-- === LAND BLOCKS ===
-- === 3 Wide Attack Block / 3 Units ===
local ThreeWideAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, },
}
-- === 4 Wide Attack Block / 12 Units ===
local FourWideAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, },
-- second row
{ UtilFirst, ShieldFirst, ShieldFirst, UtilFirst, },
-- third Row
{ AAFirst, ArtFirst, ArtFirst, AAFirst, },
}
-- === 5 Wide Attack Block ===
local FiveWideAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, },
-- second row
{ DFFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, },
-- third row
{ UtilFirst, ShieldFirst, DFFirst, ShieldFirst, UtilFirst, },
-- fourth row
{ AAFirst, DFFirst, ArtFirst, DFFirst, AAFirst, },
}
-- === 6 Wide Attack Block ===
local SixWideAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, },
-- second row
{ DFFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, DFFirst, },
-- third row
{ UtilFirst, AAFirst, DFFirst, DFFirst, AAFirst, UtilFirst, },
-- fourth row
{ AAFirst, ShieldFirst, ArtFirst, ArtFirst, ShieldFirst, AAFirst, },
-- fifth row
{ DFFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, DFFirst, },
}
-- === 7 Wide Attack Block ===
local SevenWideAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, },
-- second Row
{ DFFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, },
-- third row
{ DFFirst, UtilFirst, AAFirst, DFFirst, AAFirst, UtilFirst, DFFirst, },
-- fourth row
{ DFFirst, ShieldFirst, AAFirst, T1ArtFirst, AAFirst, ShieldFirst, DFFirst, },
-- fifth row
{ DFFirst, T1ArtFirst, AAFirst, ShieldFirst, AAFirst, T1ArtFirst, DFFirst, },
-- sixth row
{ ArtFirst, UtilFirst, ArtFirst, AAFirst, ArtFirst, UtilFirst, ArtFirst, },
}
-- === 8 Wide Attack Block ===
local EightWideAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, },
-- second Row
{ DFFirst, ShieldFirst, DFFirst, ShieldFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, },
-- third row
{ DFFirst, UtilFirst, AAFirst, DFFirst, DFFirst, AAFirst, UtilFirst, DFFirst, },
-- fourth row
{ DFFirst, ShieldFirst, T1ArtFirst, AAFirst, AAFirst, T1ArtFirst, ShieldFirst, DFFirst, },
-- fifth row
{ DFFirst, T1ArtFirst, AAFirst, T1ArtFirst, T1ArtFirst, AAFirst, T1ArtFirst, DFFirst, },
-- sixth row
{ DFFirst, ShieldFirst, UtilFirst, ShieldFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, },
-- seventh row
{ DFFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, DFFirst, },
}
-- === 2 Row Attack Block - 8 units wide ===
local TwoRowAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst },
-- second row
{ AAFirst, UtilFirst, ShieldFirst, ArtFirst, ArtFirst, ShieldFirst, UtilFirst, AAFirst },
}
-- === 3 Row Attack Block - 10 units wide ===
local ThreeRowAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst },
-- second row
{ AAFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, AAFirst },
-- third row
{ DFFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, DFFirst },
}
-- === 4 Row Attack Block - 12 units wide ===
local FourRowAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst },
-- second row
{ DFFirst, ShieldFirst, DFFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, DFFirst, ShieldFirst, DFFirst },
-- third row
{ DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst },
-- fourth row
{ AAFirst, ShieldFirst, DFFirst, ArtFirst, ShieldFirst, ArtFirst, ArtFirst, ShieldFirst, ArtFirst, DFFirst, ShieldFirst, AAFirst },
}
-- === 5 Row Attack Block - 14 units wide ===
local FiveRowAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst },
-- second row
{ UtilFirst, ShieldFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, ShieldFirst, UtilFirst },
-- third row
{ DFFirst, AAFirst, DFFirst, AAFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, AAFirst, DFFirst, AAFirst, DFFirst },
-- fourth row
{ AAFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, ShieldFirst, AAFirst, AAFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, ShieldFirst, AAFirst },
-- five row
{ ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst },
}
-- === 6 Row Attack Block - 16 units wide ===
local SixRowAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst },
-- second row
{ UtilFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, DFFirst, ShieldFirst, DFFirst, ShieldFirst, UtilFirst },
-- third row
{ DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst },
-- fourth row
{ AAFirst, ShieldFirst, AAFirst, DFFirst, AAFirst, AAFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, AAFirst, AAFirst, DFFirst, AAFirst, ShieldFirst, AAFirst },
-- fifth row
{ DFFirst, AAFirst, DFFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, AAFirst, AAFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, DFFirst, AAFirst, DFFirst },
-- sixth row
{ AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst },
}
-- === 7 Row Attack Block - 18 units wide ===
local SevenRowAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst },
-- second row
{ UtilFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, ShieldFirst, DFFirst, ShieldFirst, UtilFirst },
-- third row
{ DFFirst, DFFirst, AAFirst, DFFirst, AAFirst, DFFirst, DFFirst, DFFirst, AAFirst, AAFirst, DFFirst, DFFirst, DFFirst, AAFirst, DFFirst, AAFirst, DFFirst, DFFirst },
-- fourth row
{ DFFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, DFFirst, AAFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, AAFirst, DFFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, DFFirst },
-- fifth row
{ UtilFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, UtilFirst },
-- sixth row
{ DFFirst, ShieldFirst, AAFirst, DFFirst, ShieldFirst, UtilFirst, DFFirst, ShieldFirst, AAFirst, AAFirst, ShieldFirst, DFFirst, UtilFirst, ShieldFirst, DFFirst, AAFirst, ShieldFirst, DFFirst },
-- seventh row
{ ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst },
}
-- === 8 Row Attack Block - 20 units wide ===
local EightRowAttackFormationBlock = {
-- first row
{ DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst, DFFirst },
-- second row
{ DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, UtilFirst, ShieldFirst, DFFirst },
-- third row
{ DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst },
-- fourth row
{ DFFirst, ShieldFirst, DFFirst, AAFirst, ShieldFirst, DFFirst, ShieldFirst, AAFirst, ShieldFirst, DFFirst, DFFirst, ShieldFirst, AAFirst, ShieldFirst, DFFirst, ShieldFirst, AAFirst, DFFirst, ShieldFirst, DFFirst },
-- fifth row
{ DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, AAFirst, AAFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst, DFFirst, AAFirst, DFFirst },
-- sixth row
{ UtilFirst, ShieldFirst, ArtFirst, ShieldFirst, ArtFirst, UtilFirst, ShieldFirst, ArtFirst, ShieldFirst, AAFirst, AAFirst, ShieldFirst, ArtFirst, ShieldFirst, UtilFirst, ArtFirst, ShieldFirst, ArtFirst, ShieldFirst, UtilFirst },
-- seventh row
{ DFFirst, AAFirst, DFFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, ArtFirst, ArtFirst, AAFirst, ArtFirst, DFFirst, AAFirst, DFFirst },
-- eight row
{ AAFirst, ShieldFirst, ArtFirst, AAFirst, ShieldFirst, ArtFirst, ShieldFirst, AAFirst, ShieldFirst, ArtFirst, ArtFirst, ShieldFirst, AAFirst, ShieldFirst, ArtFirst, ShieldFirst, AAFirst, ArtFirst, ShieldFirst, AAFirst },
}
-- =========================================
-- ================ AIR DATA ===============
-- =========================================
-- === AIR CATEGORIES ===
local GroundAttackAir = (categories.AIR * categories.GROUNDATTACK) - categories.ANTIAIR
local TransportationAir = categories.AIR * categories.TRANSPORTATION - categories.GROUNDATTACK
local BomberAir = categories.AIR * categories.BOMBER
local AAAir = categories.AIR * categories.ANTIAIR
local AntiNavyAir = categories.AIR * categories.ANTINAVY
local IntelAir = categories.AIR * (categories.SCOUT + categories.RADAR)
local ExperimentalAir = categories.AIR * categories.EXPERIMENTAL
local EngineerAir = categories.AIR * categories.ENGINEER
-- === TECH LEVEL AIR CATEGORIES ===
local AirCategories = {
Ground1 = GroundAttackAir * categories.TECH1,
Ground2 = GroundAttackAir * categories.TECH2,
Ground3 = GroundAttackAir * categories.TECH3,
Trans1 = TransportationAir * categories.TECH1,
Trans2 = TransportationAir * categories.TECH2,
Trans3 = TransportationAir* categories.TECH3,
Bomb1 = BomberAir * categories.TECH1,
Bomb2 = BomberAir * categories.TECH2,
Bomb3 = BomberAir * categories.TECH3,
AA1 = AAAir * categories.TECH1,
AA2 = AAAir * categories.TECH2,
AA3 = AAAir * categories.TECH3,
AN1 = AntiNavyAir * categories.TECH1,
AN2 = AntiNavyAir * categories.TECH2,
AN3 = AntiNavyAir * categories.TECH3,
AIntel1 = IntelAir * categories.TECH1,
AIntel2 = IntelAir * categories.TECH2,
AIntel3 = IntelAir * categories.TECH3,
AExper = ExperimentalAir,
AEngineer = EngineerAir,
RemainingCategory = categories.AIR - (GroundAttackAir + TransportationAir + BomberAir + AAAir + AntiNavyAir + IntelAir + ExperimentalAir + EngineerAir)
}
-- === SUB GROUP ORDERING ===
local GroundAttack = { 'Ground3', 'Ground2', 'Ground1', }
local Transports = { 'Trans3', 'Trans2', 'Trans1', }
local Bombers = { 'Bomb3', 'Bomb2', 'Bomb1', }
local T3Bombers = {'Bomb3',}
local AntiAir = { 'AA3', 'AA2', 'AA1', }
local AntiNavy = { 'AN3', 'AN2', 'AN1', }
local Intel = { 'AIntel3', 'AIntel2', 'AIntel1', }
local ExperAir = { 'AExper', }
local EngAir = { 'AEngineer', }
-- === Air Block Arrangement ===
local ChevronSlot = { AntiAir, ExperAir, AntiNavy, GroundAttack, Bombers, Intel, Transports, EngAir, RemainingCategory }
local StratSlot = { T3Bombers }
local AttackChevronBlock = {
RepeatAllRows = false,
HomogenousBlocks = true,
{ ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, }, -- 1 -> 3 at 20 units
{ ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, }, -- 3 -> 5 at 60 units
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, }, -- 5 -> 7 at 170 units
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, }, -- 7 -> 9 at 390 units
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, }, -- 9 -> 11 at 760 units
}
local GrowthChevronBlock = {
RepeatAllRows = false,
HomogenousBlocks = true,
{ ChevronSlot, },
{ ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, }, -- 1 -> 3 at 25 units
{ ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, }, -- 3 -> 5 at 95 units
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, }, -- 5 -> 7 at 255 units
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, },
{ ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, ChevronSlot, }, -- 7 -> 9 at 545 units
}
-- =========================================
-- ============== NAVAL DATA ===============
-- =========================================
local LightAttackNaval = categories.LIGHTBOAT
local FrigateNaval = categories.FRIGATE
local SubNaval = categories.T1SUBMARINE + categories.T2SUBMARINE + (categories.NUKESUB * categories.ANTINAVY - categories.NUKE)
local DestroyerNaval = categories.DESTROYER
local CruiserNaval = categories.CRUISER
local BattleshipNaval = categories.BATTLESHIP
local CarrierNaval = categories.NAVALCARRIER
local NukeSubNaval = categories.NUKESUB - SubNaval
local MobileSonar = categories.MOBILESONAR
local DefensiveBoat = categories.DEFENSIVEBOAT
local RemainingNaval = categories.NAVAL - (LightAttackNaval + FrigateNaval + SubNaval + DestroyerNaval + CruiserNaval + BattleshipNaval +
CarrierNaval + NukeSubNaval + DefensiveBoat + MobileSonar)
-- === TECH LEVEL LAND CATEGORIES ===
local NavalCategories = {
LightCount = LightAttackNaval,
FrigateCount = FrigateNaval,
CruiserCount = CruiserNaval,
DestroyerCount = DestroyerNaval,
BattleshipCount = BattleshipNaval,
CarrierCount = CarrierNaval,
NukeSubCount = NukeSubNaval,
MobileSonarCount = MobileSonar + DefensiveBoat,
RemainingCategory = RemainingNaval,
}
local SubCategories = {
SubCount = SubNaval,
}
-- === SUB GROUP ORDERING ===
local Frigates = { 'FrigateCount', 'LightCount', }
local Destroyers = { 'DestroyerCount', }
local Cruisers = { 'CruiserCount', }
local Battleships = { 'BattleshipCount', }
local Subs = { 'SubCount', }
local NukeSubs = { 'NukeSubCount', }
local Carriers = { 'CarrierCount', }
local Sonar = {'MobileSonarCount', }
-- === NAVAL BLOCK TYPES =
local FrigatesFirst = { Frigates, Destroyers, Battleships, Cruisers, Carriers, NukeSubs, Sonar, RemainingCategory }
local DestroyersFirst = { Destroyers, Frigates, Battleships, Cruisers, Carriers, NukeSubs, Sonar, RemainingCategory }
local CruisersFirst = { Cruisers, Carriers, Battleships, Destroyers, Frigates, NukeSubs, Sonar, RemainingCategory }
local LargestFirstDF = { Battleships, Carriers, Destroyers, Cruisers, Frigates, NukeSubs, Sonar, RemainingCategory }
local SmallestFirstDF = { Frigates, Destroyers, Cruisers, Sonar, Battleships, Carriers, NukeSubs, RemainingCategory }
local LargestFirstAA = { Carriers, Battleships, Cruisers, Destroyers, Frigates, NukeSubs, Sonar, RemainingCategory }
local SmallestFirstAA = { Cruisers, Frigates, Destroyers, Sonar, Carriers, Battleships, NukeSubs, RemainingCategory }
local Subs = { Subs, NukeSubs, RemainingCategory }
local SonarFirst = { Sonar, Carriers, Cruisers, Battleships, Destroyers, Frigates, NukeSubs, Sonar, RemainingCategory }
-- === NAVAL BLOCKS ===
-- === Three Naval Growth Formation Block ==
local ThreeNavalGrowthFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst },
-- second row
{ LargestFirstDF, SonarFirst, LargestFirstDF },
-- third row
{ DestroyersFirst, CruisersFirst, DestroyersFirst },
}
-- === Five Naval Growth Formation Block ==
local FiveNavalGrowthFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst },
-- second row
{ FrigatesFirst, LargestFirstDF, DestroyersFirst, LargestFirstDF, FrigatesFirst },
-- third row
{ DestroyersFirst, SmallestFirstDF, SonarFirst, SmallestFirstDF, DestroyersFirst },
-- fourth row
{ DestroyersFirst, LargestFirstAA, CruisersFirst, LargestFirstAA, DestroyersFirst },
-- fifth row
{ DestroyersFirst, SmallestFirstAA, CruisersFirst, SmallestFirstAA, DestroyersFirst },
}
-- === Seven Naval Growth Formation Block ==
local SevenNavalGrowthFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst },
-- second row
{ FrigatesFirst, SmallestFirstDF, DestroyersFirst, DestroyersFirst, DestroyersFirst, SmallestFirstDF, FrigatesFirst },
-- third row
{ DestroyersFirst, DestroyersFirst, LargestFirstDF, SonarFirst, LargestFirstDF, DestroyersFirst, DestroyersFirst },
-- fourth row
{ DestroyersFirst, SmallestFirstAA, SmallestFirstAA, CruisersFirst, SmallestFirstAA, SmallestFirstAA, DestroyersFirst },
-- fifth row
{ DestroyersFirst, CruisersFirst, LargestFirstAA, DestroyersFirst, LargestFirstAA, CruisersFirst, DestroyersFirst },
-- sixth row
{ DestroyersFirst, SmallestFirstAA, SmallestFirstAA, SonarFirst, SmallestFirstAA, SmallestFirstAA, DestroyersFirst },
-- seventh row
{ DestroyersFirst, CruisersFirst, LargestFirstDF, CruisersFirst, LargestFirstDF, CruisersFirst, DestroyersFirst },
}
-- === Nine Naval Growth Formation Block ==
local NineNavalGrowthFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst },
-- second row
{ FrigatesFirst, LargestFirstDF, SonarFirst, LargestFirstDF, DestroyersFirst, LargestFirstDF, SonarFirst, LargestFirstDF, FrigatesFirst },
-- third row
{ SmallestFirstDF, DestroyersFirst, SmallestFirstAA, SmallestFirstAA, SonarFirst, SmallestFirstAA, SmallestFirstAA, DestroyersFirst, SmallestFirstDF },
-- fourth row
{ DestroyersFirst, LargestFirstAA, CruisersFirst, LargestFirstAA, CruisersFirst, LargestFirstAA, CruisersFirst, LargestFirstAA, DestroyersFirst },
-- fifth row
{ DestroyersFirst, DestroyersFirst, SmallestFirstAA, SmallestFirstAA, CruisersFirst, SmallestFirstAA, SmallestFirstAA, DestroyersFirst, DestroyersFirst },
}
-- ==============================================
-- ============ Naval Attack Formation===========
-- ==============================================
-- === Five Wide Naval Attack Formation Block ==
local FiveWideNavalAttackFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst},
-- second row
{ DestroyersFirst, LargestFirstDF, CruisersFirst, LargestFirstDF, DestroyersFirst},
}
-- === Seven Wide Naval Attack Formation Block ==
local SevenWideNavalAttackFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst },
-- second row
{ DestroyersFirst, SonarFirst, LargestFirstDF, DestroyersFirst, LargestFirstDF, SonarFirst, DestroyersFirst },
-- third row
{ SmallestFirstDF, SmallestFirstAA, SmallestFirstDF, CruisersFirst, SmallestFirstDF, SmallestFirstAA, SmallestFirstDF },
}
-- === Nine Wide Naval Attack Formation Block ==
local NineWideNavalAttackFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst },
-- second row
{ DestroyersFirst, LargestFirstDF, SonarFirst, LargestFirstDF, DestroyersFirst, LargestFirstDF, SonarFirst, LargestFirstDF, DestroyersFirst },
-- third row
{ SmallestFirstDF, DestroyersFirst, SmallestFirstAA, SmallestFirstAA, SonarFirst, SmallestFirstAA, SmallestFirstAA, DestroyersFirst, SmallestFirstDF },
-- fourth row
{ DestroyersFirst, SmallestFirstDF, CruisersFirst, LargestFirstAA, CruisersFirst, LargestFirstAA, CruisersFirst, SmallestFirstDF, DestroyersFirst },
}
-- === Eleven Wide Naval Attack Formation Block ==
local ElevenWideNavalAttackFormation = {
LineBreak = 0.5,
-- first row
{ FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst, FrigatesFirst },
-- second row
{ DestroyersFirst, DestroyersFirst, LargestFirstDF, SmallestFirstDF, LargestFirstDF, DestroyersFirst, LargestFirstDF, SmallestFirstDF, LargestFirstDF, DestroyersFirst, DestroyersFirst },
-- third row
{ SmallestFirstDF, DestroyersFirst, SmallestFirstAA, SmallestFirstAA, SmallestFirstAA, SonarFirst, SmallestFirstAA, SmallestFirstAA, SmallestFirstAA, DestroyersFirst, SmallestFirstDF },
-- fourth row
{ DestroyersFirst, SmallestFirstAA, LargestFirstDF, SonarFirst, LargestFirstAA, CruisersFirst, LargestFirstAA, SonarFirst, LargestFirstDF, SmallestFirstAA, DestroyersFirst },
}
-- ==============================================
-- ============ Sub Growth Formation===========
-- ==============================================
-- === Four Wide Growth Subs Formation ===
local FourWideSubGrowthFormation = {
LineBreak = 0.5,
{ Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs },
}
-- === Six Wide Subs Formation ===
local SixWideSubGrowthFormation = {
LineBreak = 0.5,
{ Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs },
}
-- === Eight Wide Subs Formation ===
local EightWideSubGrowthFormation = {
LineBreak = 0.5,
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
}
-- ==============================================
-- ============ Sub Attack Formation===========
-- ==============================================
-- === Four Wide Subs Formation ===
local FourWideSubAttackFormation = {
LineBreak = 0.5,
{ Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs },
}
-- === Six Wide Subs Formation ===
local SixWideSubAttackFormation = {
LineBreak = 0.5,
{ Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs },
}
-- === Eight Wide Subs Formation ===
local EightWideSubAttackFormation = {
LineBreak = 0.5,
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
}
-- === Ten Wide Subs Formation ===
local TenWideSubAttackFormation = {
LineBreak = 0.5,
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
{ Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs, Subs },
}
-- ============ Formation Pickers ============
---@param typeName string
---@param distance Vector
---@return number
function PickBestTravelFormationIndex(typeName, distance)
if typeName == 'AirFormations' then
return 0;
else
return 1;
end
end
---@param typeName string
---@param distance Vector
---@return number
function PickBestFinalFormationIndex(typeName, distance)
return -1;
end
-- ================ THE GUTS ====================
-- ============ Formation Functions =============
-- ==============================================
---@param formationUnits Unit[]
---@return table
function AttackFormation(formationUnits)
local cachedResults = GetCachedResults(formationUnits, 'AttackFormation')
if cachedResults then
return cachedResults
end
FormationPos = {}
local unitsList = CategorizeUnits(formationUnits)
local landUnitsList = unitsList.Land
local landBlock
if landUnitsList.AreaTotal <= 16 then -- 8 wide
landBlock = TwoRowAttackFormationBlock
elseif landUnitsList.AreaTotal <= 30 then -- 10 wide
landBlock = ThreeRowAttackFormationBlock
elseif landUnitsList.AreaTotal <= 48 then -- 12 wide
landBlock = FourRowAttackFormationBlock
elseif landUnitsList.AreaTotal <= 70 then -- 14 wide
landBlock = FiveRowAttackFormationBlock
elseif landUnitsList.AreaTotal <= 96 then -- 16 wide
landBlock = SixRowAttackFormationBlock
elseif landUnitsList.AreaTotal <= 126 then -- 18 wide
landBlock = SevenRowAttackFormationBlock
else -- 20 wide
landBlock = EightRowAttackFormationBlock
end
BlockBuilderLand(landUnitsList, landBlock, LandCategories, 1)
local seaUnitsList = unitsList.Naval
local subUnitsList = unitsList.Subs
local seaArea = seaUnitsList.AreaTotal
local subArea = subUnitsList.AreaTotal
local seaBlock
local subBlock
if seaArea <= 10 and subArea <= 8 then
seaBlock = FiveWideNavalAttackFormation
subBlock = FourWideSubAttackFormation
elseif seaArea <= 21 and subArea <= 18 then
seaBlock = SevenWideNavalAttackFormation
subBlock = SixWideSubAttackFormation
elseif seaArea <= 36 and subArea <= 32 then
seaBlock = NineWideNavalAttackFormation
subBlock = EightWideSubAttackFormation
else
seaBlock = ElevenWideNavalAttackFormation
subBlock = TenWideSubAttackFormation
end
BlockBuilderLand(seaUnitsList, seaBlock, NavalCategories, 1)
BlockBuilderLand(subUnitsList, subBlock, SubCategories, 1)
BlockBuilderAir(unitsList.Air, AttackChevronBlock, 1)
CacheResults(FormationPos, formationUnits, 'AttackFormation')
return FormationPos
end
---@param formationUnits Unit[]
---@return table
function GrowthFormation(formationUnits)
local cachedResults = GetCachedResults(formationUnits, 'GrowthFormation')
if cachedResults then
return cachedResults
end
FormationPos = {}
local unitsList = CategorizeUnits(formationUnits)
local landUnitsList = unitsList.Land
local landBlock
if landUnitsList.AreaTotal <= 3 then
landBlock = ThreeWideAttackFormationBlock
elseif landUnitsList.AreaTotal <= 12 then
landBlock = FourWideAttackFormationBlock
elseif landUnitsList.AreaTotal <= 20 then
landBlock = FiveWideAttackFormationBlock
elseif landUnitsList.AreaTotal <= 30 then
landBlock = SixWideAttackFormationBlock
elseif landUnitsList.AreaTotal <= 42 then
landBlock = SevenWideAttackFormationBlock
else
landBlock = EightWideAttackFormationBlock
end
BlockBuilderLand(landUnitsList, landBlock, LandCategories, 1)
local seaUnitsList = unitsList.Naval
local subUnitsList = unitsList.Subs
local seaArea = seaUnitsList.AreaTotal
local subArea = subUnitsList.AreaTotal
local seaBlock
local subBlock
if seaArea <= 9 and subArea <= 12 then
seaBlock = ThreeNavalGrowthFormation
subBlock = FourWideSubGrowthFormation
elseif seaArea <= 25 and subArea <= 20 then
seaBlock = FiveNavalGrowthFormation
subBlock = FourWideSubGrowthFormation
elseif seaArea <= 49 and subArea <= 42 then
seaBlock = SevenNavalGrowthFormation
subBlock = SixWideSubGrowthFormation
else
seaBlock = NineNavalGrowthFormation
subBlock = EightWideSubGrowthFormation
end
BlockBuilderLand(seaUnitsList, seaBlock, NavalCategories, 1)
BlockBuilderLand(subUnitsList, subBlock, SubCategories, 1)
if unitsList.Air.Bomb3[1] then
local count = unitsList.Air.Bomb3[1].Count
local oldAirArea = unitsList.Air.AreaTotal
local oldUnitTotal = unitsList.Air.UnitTotal
unitsList.Air.AreaTotal = count
unitsList.Air.UnitTotal = count
BlockBuilderAirT3Bombers(unitsList.Air, 1.5) --strats formation
--strats are already in formation so we remove them from table and adjust all parameters.
unitsList.Air.Bomb3 = {}
unitsList.Air.AreaTotal = oldAirArea - count
unitsList.Air.UnitTotal = oldUnitTotal - count
BlockBuilderAir(unitsList.Air, GrowthChevronBlock, 1)
else
BlockBuilderAir(unitsList.Air, GrowthChevronBlock, 1)
end
CacheResults(FormationPos, formationUnits, 'GrowthFormation')
return FormationPos
end
---@param formationUnits Unit[]
---@return table
function GuardFormation(formationUnits)
-- Not worth caching GuardFormation because it's almost never called repeatedly with the same units.
local FormationPos = {}
local shieldCategory = ShieldCat
local nonShieldCategory = categories.ALLUNITS - shieldCategory
local footprintCounts = {}
local remainingUnits = table.getn(formationUnits)
local remainingShields = 0
for _, u in formationUnits do
if EntityCategoryContains(ShieldCat, u) then
remainingShields = remainingShields + 1
end
local fs = u.FootPrintSize
footprintCounts[fs] = (footprintCounts[fs] or 0) + 1
end
local numSizes = 0
for _ in footprintCounts do
numSizes = numSizes + 1
end
local largestFootprint = 0
local smallestFootprint = 9999
local minCount = remainingUnits / numSizes -- This could theoretically divide by 0, but it wouldn't be a problem because the result would never be used.
for fs, count in footprintCounts do
largestFootprint = math.max(largestFootprint, fs)
if count >= minCount then
smallestFootprint = math.min(smallestFootprint, fs)
end
end
local ringSpacing = (smallestFootprint + 2) / (largestFootprint + 2) -- A distance of 1 in formation coordinates is translated to (largestFootprint + 2) world units.
local rotate = false
local sizeMult = 0
local ringChange = 0
local ringCount = 1
local unitCount = 1
local shieldsInRing = 0
local unitsPerShield = 0
local nextShield = 0
-- Form concentric circles around the assisted unit
-- Most of the numbers after this point are arbitrary. Don't go looking for the significance of 0.19 or the like because there is none.
while remainingUnits > 0 do
if unitCount > ringChange then
unitCount = 1
ringCount = ringCount + 1
sizeMult = ringCount * ringSpacing
ringChange = ringCount * 6
if remainingUnits < ringChange * 1.167 then
ringChange = remainingUnits -- It looks better to squeeze a few more units into the last ring than add a ring with only one or two units.
end
if ringCount == 2 or remainingShields >= (remainingUnits + ringChange + 6) * 0.19 then
shieldsInRing = math.min(ringChange / 2, remainingShields)
elseif remainingShields >= (remainingUnits + ringChange + 6) * 0.13 then
shieldsInRing = math.min(ringChange / 3, remainingShields)
else
shieldsInRing = 0
end
shieldsInRing = math.max(shieldsInRing, remainingShields - (remainingUnits - ringChange))
if shieldsInRing > 0 then
unitsPerShield = ringChange / shieldsInRing
nextShield = unitsPerShield - 0.01 -- Rounding error could result in missing a shield if nextShield is supposed to equal ringChange.
end
end
local ringPosition = unitCount / ringChange * math.pi * 2.0
offsetX = sizeMult * math.sin(ringPosition)
offsetY = -sizeMult * math.cos(ringPosition)
if shieldsInRing > 0 and unitCount >= nextShield then
table.insert(FormationPos, { offsetX, offsetY, shieldCategory, 0, rotate })
remainingShields = remainingShields - 1
nextShield = nextShield + unitsPerShield
else
table.insert(FormationPos, { offsetX, offsetY, nonShieldCategory, 0, rotate })
end
unitCount = unitCount + 1
remainingUnits = remainingUnits - 1
end
return FormationPos
end
-- =========== LAND BLOCK BUILDING =================
---@param unitsList table
---@param formationBlock any
---@param categoryTable EntityCategory[]
---@param spacing? number defaults to 1
---@return table
function BlockBuilderLand(unitsList, formationBlock, categoryTable, spacing)
spacing = (spacing or 1) * unitsList.Scale
local numRows = table.getn(formationBlock)
local rowNum = 1
local whichRow = 1
local whichCol = 1
local currRowLen = table.getn(formationBlock[whichRow])
local rowModifier = GetLandRowModifer(unitsList, categoryTable, currRowLen)
currRowLen = currRowLen - rowModifier
local evenRowLen = math.mod(currRowLen, 2) == 0
local rowType = false
local formationLength = 0
local inserted = false
local occupiedSpaces = {}
while unitsList.UnitTotal > 0 do
if whichCol > currRowLen then
rowNum = rowNum + 1
if whichRow == numRows then
whichRow = 1
else
whichRow = whichRow + 1
end
formationLength = formationLength + 1 + (formationBlock.LineBreak or 0)
whichCol = 1
rowType = false
currRowLen = table.getn(formationBlock[whichRow])
if occupiedSpaces[rowNum] then
rowModifier = 0
else
rowModifier = GetLandRowModifer(unitsList, categoryTable, currRowLen)
end
currRowLen = currRowLen - rowModifier
evenRowLen = math.mod(currRowLen, 2) == 0
end
if occupiedSpaces[rowNum] and occupiedSpaces[rowNum][whichCol] then
whichCol = whichCol + 1
continue
end
local currColSpot = GetColSpot(currRowLen + rowModifier, whichCol + rowModifier) -- Translate whichCol to correct spot in row
local currSlot = formationBlock[whichRow][currColSpot]
for _, type in currSlot do
if inserted then
break
end
for _, group in type do
if not formationBlock.HomogenousRows or (rowType == false or rowType == type) then
local fs = 0
local size = 0
local evenSize = true
local groupData = nil
for k, v in unitsList[group] do
size = unitsList.FootprintSizes[k]
evenSize = math.mod(size, 2) == 0
if v.Count > 0 then
if size > 1 and IsLandSpaceOccupied(occupiedSpaces, size, rowNum, whichCol, currRowLen, unitsList.UnitTotal) then
continue
end
fs = k
groupData = v
break
end
end
if groupData then
local offsetX = 0