forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScenarioUtilities.lua
More file actions
1634 lines (1469 loc) · 63.8 KB
/
ScenarioUtilities.lua
File metadata and controls
1634 lines (1469 loc) · 63.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----[ ]--
----[ File : ScenarioUtilities.lua ]--
----[ Author(s): Ivan Rumsey ]--
----[ ]--
----[ Summary : Utility functions for use with scenario save file. ]--
----[ Created from examples provided by Jeff Petkau. ]--
----[ ]--
----[ Copyright © 2005 Gas Powered Games, Inc. All rights reserved. ]--
local Entity = import('/lua/sim/Entity.lua').Entity
function EnableLoadBalance(enabled, unitThreshold) --distributeTime)
if not ScenarioInfo.LoadBalance then
ScenarioInfo.LoadBalance =
{
Accumulator = 0,
Enabled = false,
SpawnGroups = {},
PlatoonGroups = {},
}
end
if enabled == ScenarioInfo.LoadBalance.Enabled then return end
ScenarioInfo.LoadBalance.Enabled = enabled
if enabled then
ScenarioInfo.LoadBalance.UnitThreshold = unitThreshold or 50
else
ForkThread(function()
--local timePerGroup = ScenarioInfo.DistributeTime/table.getn(ScenarioInfo.LoadBalance.SpawnGroups)
--Get time
local time = GetSystemTimeSecondsOnlyForProfileUse()
--Spawn bases
while table.getn(ScenarioInfo.LoadBalance.SpawnGroups) > 0 do
local base, name, uncapturable = unpack(table.remove(ScenarioInfo.LoadBalance.SpawnGroups, 1))
base:SpawnGroup(name, uncapturable, true)
end
--Spawn units
while table.getn(ScenarioInfo.LoadBalance.PlatoonGroups) > 0 do
local strArmy, strGroup, formation, callback = unpack(table.remove(ScenarioInfo.LoadBalance.PlatoonGroups, 1))
CreateArmyGroupAsPlatoonBalanced(strArmy, strGroup, formation, callback)
end
--Report time taken
LOG("Time to spawn: " .. (GetSystemTimeSecondsOnlyForProfileUse() - time))
end)
end
end
function GetMarkers()
return Scenario.MasterChain._MASTERCHAIN_.Markers
end
function GetMarker(name)
return Scenario.MasterChain._MASTERCHAIN_.Markers[name]
end
function ChainToPositions(chainName)
local chain = Scenario.Chains[chainName]
if not chain then
error('ERROR: Invalid Chain Named- ' .. chainName, 2)
end
local positionTable = {}
for num, marker in chain.Markers do
table.insert(positionTable, Scenario.MasterChain._MASTERCHAIN_.Markers[marker]['position'])
end
return positionTable
end
----[ FindParentChain ]--
----[ ]--
----[ Gets the parent chain that the supplied marker belongs to ]--
function FindParentChain(markerName)
for cName,chain in Scenario.Chains do
for mNum,marker in chain.Markers do
if marker == markerName then
return chain
end
end
end
return nil
end
function GetMarkerChain(name)
local chain = Scenario.Chains[name]
if not chain then
error('ERROR: Invalid Chain Named- ' .. name, 2)
end
return chain
end
----[ MarkerToPosition ]--
----[ ]--
----[ Converts a marker as specified in *_save.lua file to a position. ]--
function MarkerToPosition(strMarker)
local marker = GetMarker(strMarker)
if not marker then
error('ERROR: Invalid marker name- '..strMarker)
end
return marker.position
end
----[ AreaToRect ]--
----[ ]--
----[ Converts an area as specified in *_save.lua file to a rectangle. ]--
function AreaToRect(strArea)
local area = Scenario.Areas[strArea]
if not area then
error('ERROR: Invalid area name')
end
local rectangle = area.rectangle
return Rect(rectangle[1],rectangle[2],rectangle[3],rectangle[4])
end
function InRect(vectorPos, rect)
return vectorPos[1] > rect.x0 and vectorPos[1] < rect.x1 and
vectorPos[3] > rect.y0 and vectorPos[3] < rect.y1
end
----[ AssembleUnitGroup ]--
----[ ]--
----[ Returns all units (leaf nodes) under the specified group. ]--
function AssembleUnitGroup(tblNode,tblResult)
tblResult = tblResult or {}
if nil == tblNode then
return tblResult
end
for strName, tblData in pairs(tblNode.Units) do
if 'GROUP' == tblData.type then
tblResult = AssembleUnitGroup(tblData,tblResult)
else
tblResult[strName] = tblData
end
end
return tblResult
end
----[ AssemblePlatoons ]--
----[ ]--
----[ Returns all platoon template names specified under group. ]--
function AssemblePlatoons(tblNode,tblResult)
tblResult = tblResult or {}
if nil == tblNode then
return tblResult
end
if nil ~= tblNode.platoon and '' ~= tblNode.platoon then
table.insert(tblResult,tblNode.platoon)
end
if 'GROUP' == tblNode.type then
for strName, tblData in pairs(tblNode.Units) do
tblResult = AssemblePlatoons(tblData,tblResult)
end
end
return tblResult
end
----[ FindUnit ]--
----[ ]--
----[ Finds the unit with the specified name. ]--
function FindUnit(strUnit,tblNode)
if nil == tblNode then
return nil
end
local tblResult = nil
for strName, tblData in pairs(tblNode.Units) do
if 'GROUP' == tblData.type then
tblResult = FindUnit(strUnit,tblData)
elseif strName == strUnit then
tblResult = tblData
end
if nil ~= tblResult then
break
end
end
return tblResult
end
----[ CreateArmyUnit ]--
----[ ]--
----[ Creates a named unit in an army. ]--
function CreateArmyUnit(strArmy,strUnit)
local tblUnit = FindUnit(strUnit,Scenario.Armies[strArmy].Units)
local brain = GetArmyBrain(strArmy)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
if nil ~= tblUnit then
local unit = CreateUnitHPR(
tblUnit.type,
strArmy,
tblUnit.Position[1], tblUnit.Position[2], tblUnit.Position[3],
tblUnit.Orientation[1], tblUnit.Orientation[2], tblUnit.Orientation[3]
)
if unit:GetBlueprint().Physics.FlattenSkirt then
unit:CreateTarmac(true, true, true, false, false)
end
local platoon
if tblUnit.platoon ~= nil and tblUnit.platoon ~= '' then
local i = 3
while i <= table.getn(Scenario.Platoons[tblUnit.platoon]) do
if tblUnit.Type == currTemplate[i][1] then
platoon = brain:MakePlatoon('None', 'None')
brain:AssignUnitsToPlatoon(platoon, {unit}, currTemplate[i][4], currTemplate[i][5])
break
end
i = i + 1
end
end
local armyIndex = brain:GetArmyIndex()
if ScenarioInfo.UnitNames[armyIndex] then
ScenarioInfo.UnitNames[armyIndex][strUnit] = unit
end
unit.UnitName = strUnit
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
return unit, platoon, tblUnit.platoon
end
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
return nil
end
----[ FindUnitGroup ]--
----[ ]--
----[ Finds the unit group with the specified name. ]--
function FindUnitGroup(strGroup,tblNode)
if nil == tblNode then
return nil
end
local tblResult = nil
for strName, tblData in pairs(tblNode.Units) do
if 'GROUP' == tblData.type then
if strName == strGroup then
tblResult = tblData
else
tblResult = FindUnitGroup(strGroup,tblData)
end
end
if nil ~= tblResult then
break
end
end
return tblResult
end
----[ AssembleArmyGroup ]--
----[ ]--
----[ Returns a table of units in the group owned by the specified army. ]--
function AssembleArmyGroup(strArmy,strGroup)
return AssembleUnitGroup(FindUnitGroup(strGroup,Scenario.Armies[strArmy].Units))
end
----[ CreateArmySubGroup ]--
----[ ]--
----[ Creates Army groups from a number of groups specified in order from the Units Hierarchy ]--
function CreateArmySubGroup(strArmy,strGroup,...)
local tblNode = Scenario.Armies[strArmy].Units
local tblResult = {}
local treeResult = {}
local platoonList = {}
local brain = GetArmyBrain(strArmy)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
for strName, tblData in pairs(tblNode.Units) do
if 'GROUP' == tblData.type then
if strName == strGroup then
if arg['n'] >= 1 then
platoonList, tblResult, treeResult = CreateSubGroup(tblNode.Units[strName], strArmy, unpack(arg))
else
platoonList, tblResult, treeResult = CreatePlatoons(strArmy, tblNode.Units[strName])
end
end
end
end
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
if tblResult == nil then
error('SCENARIO UTILITIES WARNING: No units found for for Army- ' .. strArmy .. ' Group- ' .. strGroup, 2)
end
return tblResult, treeResult, platoonList
end
----[ CreateSubGroup ]--
----[ ]--
----[ Used by CreateArmySubGroup ]--
function CreateSubGroup(tblNode, strArmy, strGroup, ...)
local tblResult = {}
local treeResult = {}
local platoonList = {}
for strName, tblData in pairs(tblNode.Units) do
if 'GROUP' == tblData.type then
if strName == strGroup then
if arg['n'] >= 1 then
platoonList, tblResult, treeResult = CreateSubGroup(tblNode.Units[strName], strArmy, unpack(arg))
else
platoonList, tblResult, treeResult = CreatePlatoons(strArmy, tblNode.Units[strName])
end
end
end
end
return platoonList, tblResult, treeResult
end
----[ CreateInitialArmyGroup ]--
----[ ]--
function CreateInitialArmyGroup(strArmy, createCommander)
local tblGroup = CreateArmyGroup(strArmy, 'INITIAL')
local cdrUnit = false
if createCommander and (tblGroup == nil or 0 == table.getn(tblGroup)) then
local factionIndex = GetArmyBrain(strArmy):GetFactionIndex()
local initialUnitName = import('/lua/factions.lua').Factions[factionIndex].InitialUnit
cdrUnit = CreateInitialArmyUnit(strArmy, initialUnitName)
if EntityCategoryContains(categories.COMMAND, cdrUnit) then
if ScenarioInfo.Options['PrebuiltUnits'] == 'Off' then
cdrUnit:HideBone(0, true)
ForkThread(CommanderWarpDelay, cdrUnit, 3)
end
local rotateOpt = ScenarioInfo.Options['RotateACU']
if not rotateOpt or rotateOpt == 'On' then
cdrUnit:RotateTowardsMid()
elseif rotateOpt == 'Marker' then
local marker = GetMarker(strArmy) or {}
if marker['orientation'] then
local o = EulerToQuaternion(unpack(marker['orientation']))
cdrUnit:SetOrientation(o, true)
end
end
end
end
return tblGroup, cdrUnit
end
function CommanderWarpDelay(cdrUnit, delay)
cdrUnit:SetBlockCommandQueue(true)
WaitSeconds(delay)
cdrUnit:PlayCommanderWarpInEffect()
end
----[ CreateProps ]--
----[ ]--
----[ ]--
function CreateProps()
for i, tblData in pairs(Scenario['Props']) do
CreatePropHPR(
tblData.prop,
tblData.Position[1], tblData.Position[2], tblData.Position[3],
tblData.Orientation[1], tblData.Orientation[2], tblData.Orientation[3]
)
end
end
----[ CreateResources ]--
----[ ]--
----[ ]--
function CreateResources()
local markers = GetMarkers()
for i, tblData in pairs(markers) do
if tblData.resource then
CreateResourceDeposit(
tblData.type,
tblData.position[1], tblData.position[2], tblData.position[3],
tblData.size
)
-- fixme: texture names should come from editor
local albedo, sx, sz, lod
if tblData.type == "Mass" then
albedo = "/env/common/splats/mass_marker.dds"
sx = 2
sz = 2
lod = 100
CreatePropHPR(
'/env/common/props/massDeposit01_prop.bp',
tblData.position[1], tblData.position[2], tblData.position[3],
Random(0,360), 0, 0
)
else
albedo = "/env/common/splats/hydrocarbon_marker.dds"
sx = 6
sz = 6
lod = 200
CreatePropHPR(
'/env/common/props/hydrocarbonDeposit01_prop.bp',
tblData.position[1], tblData.position[2], tblData.position[3],
Random(0,360), 0, 0
)
end
-- Decal - (position, heading, textureName1, textureName2, type, sizeX, sizeZ, lodParam, duration, army)
-- Splat - (position, heading, textureName1, textureName2, type, sizeX, sizeZ, lodParam, duration, army)
-- if not ScenarioInfo.MapData.Decals then
-- ScenarioInfo.MapData.Decals = {}
-- end
-- table.insert(ScenarioInfo.MapData.Decals, CreateDecal(
-- tblData.position, -- position
-- 0, -- heading
-- albedo, "", -- TEX1, TEX2
-- "Albedo", -- TYPE
-- sx, sz, -- SIZE
-- lod, -- LOD
-- 0, -- DURACTION
-- -1 -- ARMY
-- ))
CreateSplat(
tblData.position, -- Position
0, -- Heading (rotation)
albedo, -- Texture name for albedo
sx, sz, -- SizeX/Z
lod, -- LOD
0, -- Duration (0 == does not expire)
-1 , -- army (-1 == not owned by any single army)
0
)
end
end
end
function CreateWreckage(unit, needToRotate)
prop = unit:CreateWreckageProp(0)
if needToRotate then -- Some units like naval and air need to rotate for effect like after death in game
local roll = 0.5 + Random() - 2 * Random(0, 1) -- Random angle +-(0.5->1.5) radian
local pitch = 0.5 + Random() - 2 * Random(0, 1)
local yaw = 0
local unitRotation = unit:GetOrientation()
local rotation = EulerToQuaternion(roll, pitch, yaw)
local newOrientation = {}
-- mmm I`m love quaternions... =3
newOrientation[1] = unitRotation[4] * rotation[1] + unitRotation[1] * rotation[4] + unitRotation[2] * rotation[3] - unitRotation[3] * rotation[2]
newOrientation[2] = unitRotation[4] * rotation[2] + unitRotation[2] * rotation[4] + unitRotation[3] * rotation[1] - unitRotation[1] * rotation[3]
newOrientation[3] = unitRotation[4] * rotation[3] + unitRotation[3] * rotation[4] + unitRotation[1] * rotation[2] - unitRotation[2] * rotation[1]
newOrientation[4] = unitRotation[4] * rotation[4] - unitRotation[1] * rotation[1] - unitRotation[2] * rotation[2] - unitRotation[3] * rotation[3]
prop:SetOrientation(newOrientation, true)
end
unit:Destroy()
end
-- Animate unit death and skip it. Used for create wreckage like after death
function AnimateDeathThread(unit, deathAnim)
local animBlock = unit:ChooseAnimBlock(deathAnim)
local animator = CreateAnimator(unit)
animator:PlayAnim(animBlock.Animation)
local rate = unit.rate or 1
if animBlock.AnimationRateMax and animBlock.AnimationRateMin then
rate = Random(animBlock.AnimationRateMin * 10, animBlock.AnimationRateMax * 10) / 10
end
animator:SetRate(rate)
animator:SetAnimationTime(1000)
unit.Trash:Add(animator)
if animator then
WaitFor(animator)
end
CreateWreckage(unit, false)
end
function CreateWreckageUnit(unit)
local bp = unit:GetBlueprint()
local isStructure = bp.CategoriesHash.STRUCTURE
local isAir = bp.CategoriesHash.AIR
local isLand = bp.CategoriesHash.LAND
local isExperimental = bp.CategoriesHash.EXPERIMENTAL
local isNaval = bp.CategoriesHash.NAVAL
local layer = unit:GetCurrentLayer()
local unitPos = unit:GetPosition()
local deep = (GetSurfaceHeight(unitPos[1],unitPos[3]) - GetTerrainHeight(unitPos[1],unitPos[3]))
local deathAnim = bp.Display['AnimationDeath']
-- If unit stay on land or deep<5 and have death animation, animate this
local needAnimate = deathAnim and unit.PlayDeathAnimation and (isLand or isAir) and (layer == 'Land' or deep < 5)
-- We want to random rotate all naval and air units whats haven`t death animation
local needRotate = not (layer == 'Land' or isStructure or isLand or (isNaval and deep < 2)) or (isAir and isExperimental)
if needAnimate then
ForkThread(AnimateDeathThread, unit, deathAnim)
else
unit.PlayDeathAnimation = false -- Coz some units have broken animation and hold on surfase for ever
CreateWreckage(unit, needRotate)
end
end
----[ InitializeArmies ]--
----[ ]--
----[ ]--
function InitializeArmies()
local tblGroups = {}
local tblArmy = ListArmies()
local civOpt = ScenarioInfo.Options.CivilianAlliance
local bCreateInitial = ShouldCreateInitialArmyUnits()
for iArmy, strArmy in pairs(tblArmy) do
local tblData = Scenario.Armies[strArmy]
tblGroups[ strArmy ] = {}
if tblData then
----[ If an actual starting position is defined, overwrite the ]--
----[ randomly generated one. ]--
--LOG('*DEBUG: InitializeArmies, army = ', strArmy)
SetArmyEconomy(strArmy, tblData.Economy.mass, tblData.Economy.energy)
--GetArmyBrain(strArmy):InitializePlatoonBuildManager()
--LoadArmyPBMBuilders(strArmy)
if GetArmyBrain(strArmy).SkirmishSystems then
GetArmyBrain(strArmy):InitializeSkirmishSystems()
end
local armyIsCiv = ScenarioInfo.ArmySetup[strArmy].Civilian
if armyIsCiv and civOpt ~= 'neutral' and strArmy ~= 'NEUTRAL_CIVILIAN' then -- give enemy civilians darker color
SetArmyColor(strArmy, 255, 48, 48) -- non-player red color for enemy civs
end
if (not armyIsCiv and bCreateInitial) or (armyIsCiv and civOpt ~= 'removed') then
local commander = (not ScenarioInfo.ArmySetup[strArmy].Civilian)
local cdrUnit
tblGroups[strArmy], cdrUnit = CreateInitialArmyGroup(strArmy, commander)
if commander and cdrUnit and ArmyBrains[iArmy].Nickname then
cdrUnit:SetCustomName(ArmyBrains[iArmy].Nickname)
end
end
local wreckageGroup = FindUnitGroup('WRECKAGE', Scenario.Armies[strArmy].Units)
if wreckageGroup then
local platoonList, tblResult, treeResult = CreatePlatoons(strArmy, wreckageGroup)
for num, unit in tblResult do
CreateWreckageUnit(unit)
end
end
----[ irumsey ]--
----[ Temporary defaults. Make sure some fighting will break out. ]--
for iEnemy, strEnemy in tblArmy do
local enemyIsCiv = ScenarioInfo.ArmySetup[strEnemy].Civilian
local a, e = iArmy, iEnemy
local state = 'Enemy'
if a ~= e then
if armyIsCiv or enemyIsCiv then
if civOpt == 'neutral' or strArmy == 'NEUTRAL_CIVILIAN' or strEnemy == 'NEUTRAL_CIVILIAN' then
state = 'Neutral'
end
if ScenarioInfo.Options['RevealCivilians'] == 'Yes' and ScenarioInfo.ArmySetup[strEnemy].Human then
ForkThread(function()
WaitSeconds(.1)
local real_state = IsAlly(a, e) and 'Ally' or IsEnemy(a, e) and 'Enemy' or 'Neutral'
GetArmyBrain(e):SetupArmyIntelTrigger({
Category=categories.ALLUNITS,
Type='LOSNow',
Value=true,
OnceOnly=true,
TargetAIBrain=GetArmyBrain(a),
CallbackFunction=function()
SetAlliance(a, e, real_state)
end,
})
SetAlliance(a, e, 'Ally')
end)
end
end
if state then
SetAlliance(a, e, state)
end
end
end
end
end
return tblGroups
end
----[ InitializeScenarioArmies ]--
----[ ]--
----[ ]--
function InitializeScenarioArmies()
local tblGroups = {}
local tblArmy = ListArmies()
local factions = import('/lua/factions.lua')
local bCreateInitial = ShouldCreateInitialArmyUnits()
local armies = {}
for i, name in tblArmy do
armies[name] = i
end
ScenarioInfo.CampaignMode = true
Sync.CampaignMode = true
import('/lua/sim/simuistate.lua').IsCampaign(true)
for iArmy, strArmy in tblArmy do
local tblData = Scenario.Armies[strArmy]
tblGroups[ strArmy ] = {}
if tblData then
LOG('*DEBUG: InitializeScenarioArmies, army = ', strArmy)
SetArmyEconomy(strArmy, tblData.Economy.mass, tblData.Economy.energy)
if tblData.faction ~= nil then
if ScenarioInfo.ArmySetup[strArmy].Human or StringStartsWith(strArmy, "Player") then
local factionIndex = math.min(math.max(ScenarioInfo.ArmySetup[strArmy].Faction, 1), table.getsize(factions.Factions))
SetArmyFactionIndex(strArmy, factionIndex - 1)
else
local factionIndex = math.min(math.max(tblData.faction, 0), table.getsize(factions.Factions))
SetArmyFactionIndex(strArmy, factionIndex)
GetArmyBrain(strArmy):SetCurrentPlan()
end
end
if tblData.color ~= nil then
SetArmyColorIndex(strArmy, tblData.color)
end
if tblData.personality ~= nil then
SetArmyAIPersonality(strArmy, tblData.personality)
end
if bCreateInitial then
tblGroups[strArmy] = CreateInitialArmyGroup(strArmy)
end
local wreckageGroup = FindUnitGroup('WRECKAGE', Scenario.Armies[strArmy].Units)
if wreckageGroup then
local platoonList, tblResult, treeResult = CreatePlatoons(strArmy, wreckageGroup)
for num, unit in tblResult do
CreateWreckageUnit(unit)
end
end
----[ eemerson ]--
----[ Override alliances with custom alliance settings ]--
if tblData.Alliances ~= nil then
for army_name, state in tblData.Alliances do
if armies[army_name] and strArmy ~= army_name then
SetAllianceOneWay(strArmy, army_name, state)
end
end
end
GetArmyBrain(strArmy):InitializePlatoonBuildManager()
LoadArmyPBMBuilders(strArmy)
end
end
return tblGroups
end
----[ AssignOrders ]--
----[ ]--
----[ ]--
function AssignOrders(strQueue, tblUnit, target)
local tblOrder = Scenario.Orders[ strQueue ]
for i, order in pairs(tblOrder) do
order.cmd(tblUnit,target)
end
end
----[ SpawnPlatoon ]--
----[ Spawns unit group and assigns to platoon it is a part of ]--
function SpawnPlatoon(strArmy, strGroup)
local tblNode = FindUnitGroup(strGroup, Scenario.Armies[strArmy].Units)
if nil == tblNode then
error('SCENARIO UTILITIES WARNING: No Group found for Army- ' .. strArmy .. ' Group- ' .. strGroup, 2)
return false
end
local brain = GetArmyBrain(strArmy)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
local platoonName
if nil ~= tblNode.platoon and '' ~= tblNode.platoon then
platoonName = tblNode.platoon
end
local platoonList, tblResult, treeResult = CreatePlatoons(strArmy, tblNode)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
if tblResult == nil then
error('SCENARIO UTILITIES WARNING: No units found for for Army- ' .. strArmy .. ' Group- ' .. strGroup, 2)
end
return platoonList[platoonName], platoonList, tblResult, treeResult
end
function SpawnTableOfPlatoons(strArmy, strGroup)
local brain = GetArmyBrain(strArmy)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
local platoonList, tblResult, treeResult = CreatePlatoons(strArmy,
FindUnitGroup(strGroup, Scenario.Armies[strArmy].Units))
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
if tblResult == nil then
error('SCENARIO UTILITIES WARNING: No units found for for Army- ' .. strArmy .. ' Group- ' .. strGroup, 2)
end
return platoonList, tblResult, treeResult
end
function CountChildUnits(tblNode)
local count = 0
for k,v in pairs(tblNode.Units) do
if v.type == 'GROUP' then
count = count + CountChildUnits(v)
else
count = count + 1
end
end
tblNode.TotalUnits = count
return count
end
function CreatePlatoons(strArmy, tblNode, tblResult, platoonList, currPlatoon, treeResult, balance)
tblResult = tblResult or {}
platoonList = platoonList or {}
treeResult = treeResult or {}
currPlatoon = currPlatoon or false
local treeLocal = {}
if nil == tblNode then
return nil
end
local brain = GetArmyBrain(strArmy)
local armyIndex = brain:GetArmyIndex()
local currTemplate
local numRows
local reversePlatoon = false
local reverseRows
local reverseTemplate
if nil ~= tblNode.platoon and '' ~= tblNode.platoon and tblNode ~= currPlatoon
and not platoonList[tblNode.platoon] then
currTemplate = Scenario.Platoons[tblNode.platoon]
if currTemplate then
platoonList[tblNode.platoon] = brain:MakePlatoon('', currTemplate[2])
platoonList[tblNode.platoon].squadCounter = {}
currPlatoon = tblNode.platoon
end
end
if currPlatoon then
currTemplate = Scenario.Platoons[currPlatoon]
numRows = table.getn(currTemplate)
end
local unit = nil
--local timePerChild = nil
--[[
if timeLeft and not tblNode.TotalUnits then
--Calculate the number of units
local totalUnits = CountChildUnits(tblNode)
end
if timeLeft then
timePerChild = (timeLeft/tblNode.TotalUnits)
end
]]--
for strName, tblData in pairs(tblNode.Units) do
if 'GROUP' == tblData.type then
--[[
if timeLeft and tblData.TotalUnits > 0 then
timePerChild = (timeLeft/tblNode.TotalUnits)*tblData.TotalUnits
end
--]]--
platoonList, tblResult, treeResult[strName] = CreatePlatoons(strArmy, tblData, tblResult,
platoonList, currPlatoon, treeResult[strName], balance)
else
unit = CreateUnitHPR(tblData.type,
strArmy,
tblData.Position[1], tblData.Position[2], tblData.Position[3],
tblData.Orientation[1], tblData.Orientation[2], tblData.Orientation[3]
)
if unit:GetBlueprint().Physics.FlattenSkirt then
unit:CreateTarmac(true, true, true, false, false)
end
table.insert(tblResult, unit)
treeResult[strName] = unit
if ScenarioInfo.UnitNames[armyIndex] then
ScenarioInfo.UnitNames[armyIndex][strName] = unit
end
unit.UnitName = strName
if tblData.platoon ~= nil and tblData.platoon ~= '' and tblData.platoon ~= currPlatoon then
reversePlatoon = currPlatoon
reverseRows = numRows
reverseTemplate = currTemplate
if not platoonList[tblData.platoon] then
currTemplate = Scenario.Platoons[tblData.platoon]
platoonList[tblData.platoon] = brain:MakePlatoon('', currTemplate[2])
platoonList[tblData.platoon].squadCounter = {}
end
currPlatoon = tblData.platoon
currTemplate = Scenario.Platoons[currPlatoon]
numRows = table.getn(currTemplate)
end
if currPlatoon then
local i = 3
local inserted = false
while i <= numRows and not inserted do
if platoonList[currPlatoon].squadCounter[i] == nil then
platoonList[currPlatoon].squadCounter[i] = 0
end
if tblData.type == currTemplate[i][1] and
platoonList[currPlatoon].squadCounter[i] < currTemplate[i][3] then
platoonList[currPlatoon].squadCounter[i] = platoonList[currPlatoon].squadCounter[i] + 1
brain:AssignUnitsToPlatoon(platoonList[currPlatoon],{unit},currTemplate[i][4],currTemplate[i][5])
inserted = true
end
i = i + 1
end
if reversePlatoon then
currPlatoon = reversePlatoon
numRows = reverseRows
currTemplate = reverseTemplate
reversePlatoon = false
end
end
if balance then
--Accumulate for one tick so we don't get too much overhead from thread switching...
--ScenarioInfo.LoadBalance.Accumulator = ScenarioInfo.LoadBalance.Accumulator + timePerChild
ScenarioInfo.LoadBalance.Accumulator = ScenarioInfo.LoadBalance.Accumulator + 1
if ScenarioInfo.LoadBalance.Accumulator > ScenarioInfo.LoadBalance.UnitThreshold then
WaitSeconds(0)
ScenarioInfo.LoadBalance.Accumulator = 0
end
end
end
end
return platoonList, tblResult, treeResult
end
----[ CreateArmyGroup ]--
----[ ]--
----[ Creates the specified group in game. ]--
function CreateArmyGroup(strArmy,strGroup,wreckage, balance)
local brain = GetArmyBrain(strArmy)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
local platoonList, tblResult, treeResult = CreatePlatoons(strArmy,
FindUnitGroup(strGroup, Scenario.Armies[strArmy].Units), nil, nil, nil, nil, balance)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
if tblResult == nil and strGroup ~= 'INITIAL' then
error('SCENARIO UTILITIES WARNING: No units found for for Army- ' .. strArmy .. ' Group- ' .. strGroup, 2)
end
if wreckage then
for num, unit in tblResult do
CreateWreckageUnit(unit)
end
return
end
return tblResult, treeResult, platoonList
end
-- CreateArmyTree
--
-- Returns tree of units created by the editor. 2nd return is table of units
function CreateArmyTree(strArmy, strGroup)
local brain = GetArmyBrain(strArmy)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
local platoonList, tblResult, treeResult = CreatePlatoons(strArmy,
FindUnitGroup(strGroup, Scenario.Armies[strArmy].Units))
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
if tblResult == nil then
error('SCENARIO UTILITIES WARNING: No units found for for Army- ' .. strArmy .. ' Group- ' .. strGroup, 2)
end
return treeResult, tblResult, platoonList
end
function CreateArmyGroupAsPlatoonBalanced(strArmy, strGroup, formation, OnFinishedCallback)
ScenarioInfo.LoadBalance.Accumulator = 0
local units = CreateArmyGroupAsPlatoon(strArmy, strGroup, formation, nil, nil, true)
OnFinishedCallback(units)
end
-- CreateArmyGroupAsPlatoon
--
-- Returns a platoon that is created out of all units in a group and its sub groups.
function CreateArmyGroupAsPlatoon(strArmy, strGroup, formation, tblNode, platoon, balance)
if ScenarioInfo.LoadBalance.Enabled then
--note that tblNode in this case is actually the callback function
table.insert(ScenarioInfo.LoadBalance.PlatoonGroups, {strArmy, strGroup, formation, tblNode})
return
end
local tblNode = tblNode or FindUnitGroup(strGroup, Scenario.Armies[strArmy].Units)
if not tblNode then
error('*SCENARIO UTILS ERROR: No group named- ' .. strGroup .. ' found for army- ' .. strArmy, 2)
end
if not formation then
error('*SCENARIO UTILS ERROR: No formation given to CreateArmyGroupAsPlatoon')
end
local brain = GetArmyBrain(strArmy)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
local platoon = platoon or brain:MakePlatoon('','')
local armyIndex = brain:GetArmyIndex()
local unit = nil
for strName, tblData in pairs(tblNode.Units) do
if 'GROUP' == tblData.type then
platoon = CreateArmyGroupAsPlatoon(strArmy, strGroup, formation, tblData, platoon)
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), true)
end
else
unit = CreateUnitHPR(tblData.type,
strArmy,
tblData.Position[1], tblData.Position[2], tblData.Position[3],
tblData.Orientation[1], tblData.Orientation[2], tblData.Orientation[3]
)
if unit:GetBlueprint().Physics.FlattenSkirt then
unit:CreateTarmac(true, true, true, false, false)
end
if ScenarioInfo.UnitNames[armyIndex] then
ScenarioInfo.UnitNames[armyIndex][strName] = unit
end
unit.UnitName = strName
brain:AssignUnitsToPlatoon(platoon, {unit}, 'Attack', formation)
if balance then
ScenarioInfo.LoadBalance.Accumulator = ScenarioInfo.LoadBalance.Accumulator + 1
if ScenarioInfo.LoadBalance.Accumulator > ScenarioInfo.LoadBalance.UnitThreshold/5 then
WaitSeconds(0)
ScenarioInfo.LoadBalance.Accumulator = 0
end
end
end
end
if not brain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(brain:GetArmyIndex(), false)
end
return platoon
end
-- Creates an army group at a certain veteran level
function CreateArmyGroupAsPlatoonVeteran(strArmy, strGroup, formation, veteranLevel)
local plat = CreateArmyGroupAsPlatoon(strArmy, strGroup, formation)
veteranLevel = veteranLevel or 5