forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScenarioFramework.lua
2211 lines (1946 loc) · 73.6 KB
/
ScenarioFramework.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/scenarioFramework.lua
-- Authors : John Comes, Drew Staltman
-- Summary : Functions for use in the scenario scripts.
-- Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
-----------------------------------------------------------------
local TriggerFile = import('scenariotriggers.lua')
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local UnitUpgradeTemplates = import('/lua/upgradeTemplates.lua').UnitUpgradeTemplates
local ScenarioPlatoonAI = import('/lua/ScenarioPlatoonAI.lua')
local VizMarker = import('/lua/sim/VizMarker.lua').VizMarker
local SimCamera = import('/lua/SimCamera.lua').SimCamera
local Cinematics = import('/lua/cinematics.lua')
local SimUIVars = import('/lua/sim/SimUIState.lua')
PingGroups = import('/lua/SimPingGroup.lua')
Objectives = import('/lua/SimObjectives.lua')
-- Cause the game to exit immediately
function ExitGame()
Sync.RequestingExit = true
end
-- Call to end an operation
-- bool _success - instructs UI which dialog to show
-- bool _allPrimary - true if all primary objectives completed, otherwise, false
-- bool _allSecondary - true if all secondary objectives completed, otherwise, false
-- bool _allBonus - true if all bonus objectives completed, otherwise, false
function EndOperation(_success, _allPrimary, _allSecondary, _allBonus)
local opFile = string.gsub(ScenarioInfo.Options.ScenarioFile, 'scenario', 'operation')
local _opData = {}
if DiskGetFileInfo(opFile) then
_opData = import(opFile)
end
import('/lua/victory.lua').CallEndGame() -- We need this here to populate the score screen
ForkThread(function()
WaitSeconds(3) -- Wait for the stats to be synced
UnlockInput()
Sync.OperationComplete = {
success = _success,
difficulty = ScenarioInfo.Options.Difficulty,
allPrimary = _allPrimary,
allSecondary = _allSecondary,
allBonus = _allBonus,
faction = ScenarioInfo.LocalFaction,
opData = _opData.operationData
}
end)
end
-- Pop up a dialog to ask the user what faction they want to play
local factionCallbacks = {}
function RequestPlayerFaction(callback)
Sync.RequestPlayerFaction = true
if callback then
table.insert(factionCallbacks, callback)
end
end
-- Hook for player requested faction
-- "data" is a table containing field "Faction" which can be "cybran", "uef", or "aeon"
function OnFactionSelect(data)
if ScenarioInfo.campaignInfo then
ScenarioInfo.campaignInfo.campaignID = data.Faction
end
if table.getn(factionCallbacks) > 0 then
for index, callbackFunc in factionCallbacks do
if callbackFunc then callbackFunc(data) end
end
else
WARN('I chose ', data.Faction, ' but I dont have a callback set!')
end
end
-- Call to end an operation where the data is already provided in table form (just a wrapper for sync
function EndOperationT(opData)
Sync.OperationComplete = opData
end
-- Single Area Trigger Creation
-- This will create an area trigger around <rectangle>. It will fire when <categoy> is met of <aiBrain>.
-- onceOnly means it will not continue to run after the first time it fires.
-- invert means it will fire when units are NOT in the area. Useful for testing if someone has defeated a base.
-- number refers to the number of units it will take to fire. If not inverted. It will fire when that many are in the area-- If inverted, it will fire when less than that many are in the area
function CreateAreaTrigger(callbackFunction, rectangle, category, onceOnly, invert, aiBrain, number, requireBuilt)
return TriggerFile.CreateAreaTrigger(callbackFunction, rectangle, category, onceOnly, invert, aiBrain, number, requireBuilt)
end
-- Table of Areas Trigger Creations
-- same as above except you can supply the function with a table of Rects.
-- If you have an odd shaped area for an area trigger
function CreateMultipleAreaTrigger(callbackFunction, rectangleTable, category, onceOnly, invert, aiBrain, number, requireBuilt)
return TriggerFile.CreateMultipleAreaTrigger(callbackFunction, rectangleTable, category, onceOnly, invert, aiBrain, number, requireBuilt)
end
-- Single Line timer Trigger creation
-- Fire the <cb> function after <seconds> number of seconds.
-- you can have the function repeat <repeatNum> times which will fire every <seconds>
-- until <repeatNum> is met
local timerThread = nil
function CreateTimerTrigger(cb, seconds, displayBool)
timerThread = TriggerFile.CreateTimerTrigger(cb, seconds, displayBool)
return timerThread
end
function ResetUITimer()
if timerThread then
Sync.ObjectiveTimer = 0
KillThread(timerThread)
end
end
-- Single Line unit damaged trigger creation
-- When <unit> is damaged it will call the <callbackFunction> provided
-- If <percent> provided, will check if damaged percent EXCEEDS number provided before callback
-- function repeats up to repeatNum ... or once if not declared
function CreateUnitDamagedTrigger(callbackFunction, unit, amount, repeatNum)
TriggerFile.CreateUnitDamagedTrigger(callbackFunction, unit, amount, repeatNum)
end
function CreateUnitPercentageBuiltTrigger(callbackFunction, aiBrain, category, percent)
aiBrain:AddUnitBuiltPercentageCallback(callbackFunction, category, percent)
end
-- Single Line unit death trigger creation
-- When <unit> dies it will call the <cb> function provided
function CreateUnitDeathTrigger(cb, unit, camera)
TriggerFile.CreateUnitDeathTrigger(cb, unit)
end
function PauseUnitDeath(unit)
if unit and not unit.Dead then
unit.OnKilled = OverrideKilled
unit:SetCanBeKilled(false)
unit.DoTakeDamage = OverrideDoDamage
end
end
function OverrideDoDamage(self, instigator, amount, vector, damageType)
local preAdjHealth = self:GetHealth()
self:AdjustHealth(instigator, -amount)
local health = self:GetHealth()
if ((health <= 0) or (amount > preAdjHealth)) and not self.KilledFlag then
self.KilledFlag = true
if damageType == 'Reclaimed' then
self:Destroy()
else
local excessDamageRatio = 0.0
-- Calculate the excess damage amount
local excess = preAdjHealth - amount
local maxHealth = self:GetMaxHealth()
if excess < 0 and maxHealth > 0 then
excessDamageRatio = -excess / maxHealth
end
IssueClearCommands({self})
ForkThread(UnlockAndKillUnitThread, self, instigator, damageType, excessDamageRatio)
end
end
end
function UnlockAndKillUnitThread(self, instigator, damageType, excessDamageRatio)
self:DoUnitCallbacks('OnKilled')
WaitSeconds(2)
self:SetCanBeKilled(true)
self:Kill(instigator, damageType, excessDamageRatio)
end
function OverrideKilled(self, instigator, type, overkillRatio)
if not self.CanBeKilled then
self:DoTakeDamage(instigator, 1000000, nil, 'Normal')
return
end
self.Dead = true
local bp = self:GetBlueprint()
if self:GetCurrentLayer() == 'Water' and bp.Physics.MotionType == 'RULEUMT_Hover' then
self:PlayUnitSound('HoverKilledOnWater')
end
if self:GetCurrentLayer() == 'Land' and bp.Physics.MotionType == 'RULEUMT_AmphibiousFloating' then
self:PlayUnitSound('AmphibiousFloatingKilledOnLand')
else
self:PlayUnitSound('Killed')
end
-- If factory, destroy what I'm building if I die
if EntityCategoryContains(categories.FACTORY, self) then
if self.UnitBeingBuilt and not self.UnitBeingBuilt.Dead and self.UnitBeingBuilt:GetFractionComplete() ~= 1 then
self.UnitBeingBuilt:Kill()
end
end
if self.PlayDeathAnimation and not self:IsBeingBuilt() then
self:ForkThread(self.PlayAnimationThread, 'AnimationDeath')
self:SetCollisionShape('None')
end
self:DoUnitCallbacks('OnKilled')
self:DestroyTopSpeedEffects()
if self.UnitBeingTeleported and not self.UnitBeingTeleported.Dead then
self.UnitBeingTeleported:Destroy()
self.UnitBeingTeleported = nil
end
-- Notify instigator that you killed me.
if instigator and IsUnit(instigator) then
instigator:OnKilledUnit(self)
end
if self.DeathWeaponEnabled ~= false then
self:DoDeathWeapon()
end
self:DisableShield()
self:DisableUnitIntel('Killed')
self:ForkThread(self.DeathThread, overkillRatio , instigator)
end
function GiveUnitToArmy(unit, newArmyIndex, triggerOnGiven)
-- We need the brain to ignore army cap when transferring the unit
-- do all necessary steps to set brain to ignore, then un-ignore if necessary the unit cap
unit.IsBeingTransferred = true
SetIgnoreArmyUnitCap(newArmyIndex, true)
IgnoreRestrictions(true)
local newUnit = ChangeUnitArmy(unit, newArmyIndex)
local newBrain = ArmyBrains[newArmyIndex]
if not newBrain.IgnoreArmyCaps then
SetIgnoreArmyUnitCap(newArmyIndex, false)
end
IgnoreRestrictions(false)
if triggerOnGiven then
unit:OnGiven(newUnit)
end
return newUnit
end
-- Single Line unit death trigger creation
-- When <unit> is killed, reclaimed, or captured it will call the <cb> function provided
function CreateUnitDestroyedTrigger(cb, unit)
CreateUnitReclaimedTrigger(cb, unit)
CreateUnitCapturedTrigger(cb, nil, unit)
CreateUnitDeathTrigger(cb, unit, true)
end
-- Single Line unit given trigger creation
-- When <unit> is given it will call the <cb> function provided
function CreateUnitGivenTrigger(cb, unit)
TriggerFile.CreateUnitGivenTrigger(cb, unit)
end
-- Single Line Unit built trigger
-- Tests when <unit> builds a unit of type <category> calls <cb>
function CreateUnitBuiltTrigger(cb, unit, category)
TriggerFile.CreateUnitBuiltTrigger(cb, unit, category)
end
-- Single line unit captured trigger creation
-- When <unit> is captured cbOldUnit is called passing the old unit BEFORE it has switched armies,
-- cbNewUnit is called passing in the unit AFTER it has switched armies
function CreateUnitCapturedTrigger(cbOldUnit, cbNewUnit, unit)
TriggerFile.CreateUnitCapturedTrigger(cbOldUnit, cbNewUnit, unit)
end
-- Single line unit start being captured trigger
-- when <unit> begins to be captured function is called
function CreateUnitStartBeingCapturedTrigger(cb, unit)
TriggerFile.CreateUnitStartBeingCapturedTrigger(cb, unit)
end
-- Single line unit stop being captured trigger
-- when <unit> stops being captured, the function is called
function CreateUnitStopBeingCapturedTrigger(cb, unit)
TriggerFile.CreateUnitStopBeingCapturedTrigger(cb, unit)
end
-- Single line unit failed being captured trigger
-- when capture of <unit> fails, the function is called
function CreateUnitFailedBeingCapturedTrigger(cb, unit)
TriggerFile.CreateUnitFailedBeingCapturedTrigger(cb, unit)
end
-- Single line unit started capturing trigger creation
-- When <unit> starts capturing the <cb> function provided is called
function CreateUnitStartCaptureTrigger(cb, unit)
TriggerFile.CreateUnitStartCaptureTrigger(cb, unit)
end
-- Single line unit finished capturing trigger creation
-- When <unit> finishes capturing the <cb> function provided is called
function CreateUnitStopCaptureTrigger(cb, unit)
TriggerFile.CreateUnitStopCaptureTrigger(cb, unit)
end
-- Single line failed capturing trigger creation
-- When <unit> fails to capture a unit, the <cb> function is called
function CreateUnitFailedCaptureTrigger(cb, unit)
TriggerFile.CreateUnitFailedCaptureTrigger(cb, unit)
end
-- Single line unit has been reclaimed trigger creation
-- When <unit> has been reclaimed the <cb> function provided is called
function CreateUnitReclaimedTrigger(cb, unit)
TriggerFile.CreateUnitReclaimedTrigger(cb, unit)
end
-- Single line unit started reclaiming trigger creation
-- When <unit> starts reclaiming the <cb> function provided is called
function CreateUnitStartReclaimTrigger(cb, unit)
TriggerFile.CreateUnitStartReclaimTrigger(cb, unit)
end
-- Single line unit finished reclaiming trigger creation
-- When <unit> finishes reclaiming the <cb> function provided is called
function CreateUnitStopReclaimTrigger(cb, unit)
TriggerFile.CreateUnitStopReclaimTrigger(cb, unit)
end
-- Single line unit veterancy trigger creation
-- When <unit> achieves veterancy, <cb> is called with parameters of the unit then level achieved
function CreateUnitVeterancyTrigger(cb, unit)
TriggerFile.CreateUnitVeterancyTrigger(cb, unit)
end
-- Single line Group Death Trigger creation
-- When all units in <group> are destroyed, <cb> function will be called
function CreateGroupDeathTrigger(cb, group)
return TriggerFile.CreateGroupDeathTrigger(cb, group)
end
-- Single line Platoon Death Trigger creation
-- When all units in <platoon> are destroyed, <cb> function will be called
function CreatePlatoonDeathTrigger(cb, platoon)
platoon:AddDestroyCallback(cb)
end
-- Single line Sub Group Death Trigger creation
-- When <num> <cat> units in <group> are destroyed, <cb> function will be called
function CreateSubGroupDeathTrigger(cb, group, num)
return TriggerFile.CreateSubGroupDeathTrigger(cb, group, num)
end
-- Checks if units of Cat are within the provided rectangle
-- Checks the <Rectangle> to see if any units of <Cat> category are in it
function UnitsInAreaCheck(Cat, Rectangle)
if type(Rectangle) == 'string' then
Rectangle = ScenarioUtils.AreaToRect(Rectangle)
end
local entities = GetUnitsInRect(Rectangle)
if not entities then
return false
end
for _, v in entities do
if EntityCategoryContains(Cat, v) then
return true
end
end
return false
end
-- Returns the number of <cat> units in <area> belonging to <brain>
function NumCatUnitsInArea(cat, area, brain)
if type(area) == 'string' then
area = ScenarioUtils.AreaToRect(area)
end
local entities = GetUnitsInRect(area)
local result = 0
if entities then
local filteredList = EntityCategoryFilterDown(cat, entities)
for _, v in filteredList do
if v:GetAIBrain() == brain then
result = result + 1
end
end
end
return result
end
-- Returns the units in <area> of <cat> belonging to <brain>
function GetCatUnitsInArea(cat, area, brain)
if type(area) == 'string' then
area = ScenarioUtils.AreaToRect(area)
end
local entities = GetUnitsInRect(area)
local result = {}
if entities then
local filteredList = EntityCategoryFilterDown(cat, entities)
for _, v in filteredList do
if v:GetAIBrain() == brain then
table.insert(result, v)
end
end
end
return result
end
-- Destroys a group
-- Goes through every unit in <group> and destroys them without explosions
function DestroyGroup(group)
for _, v in group do
v:Destroy()
end
end
-- Checks if <unitOne> and <unitTwo> are less than <distance> from each other
-- if true calls <callbackFunction>
function CreateUnitDistanceTrigger(callbackFunction, unitOne, unitTwo, distance)
TriggerFile.CreateUnitDistanceTrigger(callbackFunction, unitOne, unitTwo, distance)
end
-- Stat trigger creation
-- triggerTable spec
-- {
-- {StatType = string, -- Examples: Units_Active, Units_Killed, Enemies_Killed, Economy_Trend_Mass, Economy_TotalConsumed_Energy
-- CompareType = string, -- GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual
-- Value = integer,
-- Category = category, -- Only used with "Units" triggers
-- },
-- }
-- COMPLETE LIST OF STAT TYPES
-- "Units_Active",
-- "Units_Killed",
-- "Units_History",
-- "Enemies_Killed",
-- "Economy_TotalProduced_Energy",
-- "Economy_TotalConsumed_Energy",
-- "Economy_Income_Energy",
-- "Economy_Output_Energy",
-- "Economy_Stored_Energy",
-- "Economy_Reclaimed_Energy",
-- "Economy_MaxStorage_Energy",
-- "Economy_PeakStorage_Energy",
-- "Economy_TotalProduced_Mass",
-- "Economy_TotalConsumed_Mass",
-- "Economy_Income_Mass",
-- "Economy_Output_Mass",
-- "Economy_Stored_Mass",
-- "Economy_Reclaimed_Mass",
-- "Economy_MaxStorage_Mass",
-- "Economy_PeakStorage_Mass",
function CreateArmyStatTrigger(callbackFunction, aiBrain, name, triggerTable)
TriggerFile.CreateArmyStatTrigger(callbackFunction, aiBrain, name, triggerTable)
end
-- Fires when the threat level of <position> of size <rings> is related to <value>
-- if <greater> is true it will fire if the threat is greater than <value>
function CreateThreatTriggerAroundPosition(callbackFunction, aiBrain, posVector, rings, onceOnly, value, greater)
TriggerFile.CreateThreatTriggerAroundPosition(callbackFunction, aiBrain, posVector, rings, onceOnly, value, greater)
end
-- Fires when the threat level of <unit> of size <rings> is related to <value>
function CreateThreatTriggerAroundUnit(callbackFunction, aiBrain, unit, rings, onceOnly, value, greater)
TriggerFile.CreateThreatTriggerAroundUnit(callbackFunction, aiBrain, unit, rings, onceOnly, value, greater)
end
-- Type = 'LOSNow'/'Radar'/'Sonar'/'Omni',
-- Blip = blip handle or false if you don't care,
-- Category = category of unit to trigger off of
-- OnceOnly = run it once
-- Value = true/false, true = when you get it, false = when you first don't have it
-- <aiBrain> refers to the intelligence you are monitoring.
-- <targetAIBrain> requires that the intelligence fires on seeing a specific brain's units
function CreateArmyIntelTrigger(callbackFunction, aiBrain, reconType, blip, value, category, onceOnly, targetAIBrain)
TriggerFile.CreateArmyIntelTrigger(callbackFunction, aiBrain, reconType, blip, value, category, onceOnly, targetAIBrain)
end
function CreateArmyUnitCategoryVeterancyTrigger(callbackFunction, aiBrain, category, level)
TriggerFile.CreateArmyUnitCategoryVeterancyTrigger(callbackFunction, aiBrain, category, level)
end
-- Fires when <unit> and <marker> are less than or equal to <distance> apart
function CreateUnitToMarkerDistanceTrigger(callbackFunction, unit, marker, distance)
TriggerFile.CreateUnitToPositionDistanceTrigger(callbackFunction, unit, marker, distance)
end
-- Function that fires when <unit> is near any unit of type <category> belonging to <brain> withing <distance>
function CreateUnitNearTypeTrigger(callbackFunction, unit, brain, category, distance)
return TriggerFile.CreateUnitNearTypeTrigger(callbackFunction, unit, brain, category, distance)
end
-- tells platoon to move along table of coordinates
function PlatoonMoveRoute(platoon, route, squad)
for _, v in route do
if type(v) == 'string' then
if squad then
platoon:MoveToLocation(ScenarioUtils.MarkerToPosition(v), false, squad)
else
platoon:MoveToLocation(ScenarioUtils.MarkerToPosition(v), false)
end
else
if squad then
platoon:MoveToLocation(v, false, squad)
else
platoon:MoveToLocation(v, false)
end
end
end
end
-- commands platoon to patrol a route
function PlatoonPatrolRoute(platoon, route, squad)
for _, v in route do
if type(v) == 'string' then
if squad then
platoon:Patrol(ScenarioUtils.MarkerToPosition(v), squad)
else
platoon:Patrol(ScenarioUtils.MarkerToPosition(v))
end
else
if squad then
platoon:Patrol(v, squad)
else
platoon:Patrol(v)
end
end
end
end
function PlatoonMoveChain(platoon, chain, squad)
for _, v in ScenarioUtils.ChainToPositions(chain) do
if squad then
platoon:MoveToLocation(v, false, squad)
else
platoon:MoveToLocation(v, false)
end
end
end
-- orders a platoon to patrol all the points in a chain
function PlatoonPatrolChain(platoon, chain, squad)
for _, v in ScenarioUtils.ChainToPositions(chain) do
if squad then
platoon:Patrol(v, squad)
else
platoon:Patrol(v)
end
end
end
-- Orders a platoon to attack move through a chain
function PlatoonAttackChain(platoon, chain, squad)
local cmd = false
for _, v in ScenarioUtils.ChainToPositions(chain) do
if squad then
cmd = platoon:AggressiveMoveToLocation(v, squad)
else
cmd = platoon:AggressiveMoveToLocation(v)
end
end
return cmd
end
-- orders group to patrol a chain
function GroupPatrolChain(group, chain)
for _, v in ScenarioUtils.ChainToPositions(chain) do
IssuePatrol(group, v)
end
end
-- orders group to patrol a route
function GroupPatrolRoute(group, route)
for _, v in route do
if type(v) == 'string' then
IssuePatrol(group, ScenarioUtils.MarkerToPosition(v))
else
IssuePatrol(group, v)
end
end
end
-- orders group to patrol a route in formation
function GroupFormPatrolChain(group, chain, formation)
for _, v in ScenarioUtils.ChainToPositions(chain) do
IssueFormPatrol(group, v, formation, 0)
end
end
-- order group to attack a chain
function GroupAttackChain(group, chain)
for _, v in ScenarioUtils.ChainToPositions(chain) do
IssueAggressiveMove(group, v)
end
end
function GroupMoveChain(group, chain)
for _, v in ScenarioUtils.ChainToPositions(chain) do
IssueMove(group, v)
end
end
function GroupProgressTimer(group, time)
ForkThread(GroupProgressTimerThread, group, time)
end
function GroupProgressTimerThread(group, time)
local currTime = 0
while currTime < time do
for _, v in group do
if not v.Dead then
v:SetWorkProgress(currTime/time)
end
end
WaitSeconds(1)
currTime = currTime + 1
end
end
-- dialogueTable format
-- it's a table of 4 variables - vid, cue, text, and duration
-- ex. Hello = {
-- {vid=video, cue=false, bank=false, text='Hello World', duration = 5 },
-- }
--
-- - bank = audio bank
-- - cue = audio cue
-- - vid = video cue
-- - text: text to be displayed on the screen
-- - delay: time before begin next dialogue in table in second
function Dialogue(dialogueTable, callback, critical, speaker)
local canSpeak = true
if speaker and speaker.Dead then
canSpeak = false
end
if canSpeak then
local dTable = table.deepcopy(dialogueTable)
if callback then
dTable.Callback = callback
end
if critical then
dTable.Critical = critical
end
if ScenarioInfo.DialogueLock == nil then
ScenarioInfo.DialogueLock = false
ScenarioInfo.DialogueLockPosition = 0
ScenarioInfo.DialogueQueue = {}
ScenarioInfo.DialogueFinished = {}
end
table.insert(ScenarioInfo.DialogueQueue, dTable)
if not ScenarioInfo.DialogueLock then
ScenarioInfo.DialogueLock = true
ForkThread(PlayDialogue)
end
end
end
function FlushDialogueQueue()
if ScenarioInfo.DialogueQueue then
for _, v in ScenarioInfo.DialogueQueue do
v.Flushed = true
end
end
end
-- This function sends movie data to the sync table and saves it off for reloading in save games
function SetupMFDSync(movieTable, text)
DisplayVideoText(text)
Sync.PlayMFDMovie = {movieTable[1], movieTable[2], movieTable[3], movieTable[4] }
ScenarioInfo.DialogueFinished[movieTable[1]] = false
local tempText = LOC(text)
local tempData = {}
local nameStart = string.find(tempText, ']')
if nameStart ~= nil then
tempData.name = LOC("<LOC "..string.sub(tempText, 2, nameStart - 1)..">")
tempData.text = string.sub(tempText, nameStart + 2)
else
tempData.name = "INVALID NAME"
tempData.text = tempText
LOG("ERROR: Unable to find name in string: " .. text .. " (" .. tempText .. ")")
end
local timeSecs = GetGameTimeSeconds()
tempData.time = string.format("%02d:%02d:%02d", math.floor(timeSecs/360), math.floor(timeSecs/60), math.mod(timeSecs, 60))
tempData.color = 'ffffffff'
if movieTable[4] == 'UEF' then
tempData.color = 'ff00c1ff'
elseif movieTable[4] == 'Cybran' then
tempData.color = 'ffff0000'
elseif movieTable[4] == 'Aeon' then
tempData.color = 'ff89d300'
end
AddTransmissionData(tempData)
WaitForDialogue(movieTable[1])
end
function AddTransmissionData(entryData)
SimUIVars.SaveEntry(entryData)
end
-- The actual thread used by Dialogue
function PlayDialogue()
while table.getn(ScenarioInfo.DialogueQueue) > 0 do
local dTable = table.remove(ScenarioInfo.DialogueQueue, 1)
if not dTable then WARN('dTable is nil, ScenarioInfo.DialogueQueue len is '..repr(table.getn(ScenarioInfo.DialogueQueue))) end
if not dTable.Flushed and (not ScenarioInfo.OpEnded or dTable.Critical) then
for _, v in dTable do
if v ~= nil and not dTable.Flushed and (not ScenarioInfo.OpEnded or dTable.Critical) then
if not v.vid and v.bank and v.cue then
table.insert(Sync.Voice, {Cue = v.cue, Bank = v.bank})
if not v.delay then
WaitSeconds(5)
end
end
if v.text and not v.vid then
if not v.vid then
DisplayMissionText(v.text)
end
end
if v.vid then
local vidText = ''
local movieData = {}
if v.text then
vidText = v.text
end
if GetMovieDuration('/movies/' .. v.vid) == 0 then
movieData = {'/movies/AllyCom.sfd', v.bank, v.cue, v.faction }
else
movieData = {'/movies/' .. v.vid, v.bank, v.cue, v.faction}
end
SetupMFDSync(movieData, vidText)
end
if v.delay and v.delay > 0 then
WaitSeconds(v.delay)
end
if v.duration and v.duration > 0 then
WaitSeconds(v.duration)
end
end
end
end
if dTable.Callback then
ForkThread(dTable.Callback)
end
WaitTicks(1)
end
ScenarioInfo.DialogueLock = false
end
function WaitForDialogue(name)
while not ScenarioInfo.DialogueFinished[name] do
WaitTicks(1)
end
end
function PlayUnlockDialogue()
if Random(1, 2) == 1 then
table.insert(Sync.Voice, {Bank = 'XGG', Cue = 'Computer_Computer_UnitRevalation_01370'})
else
table.insert(Sync.Voice, {Bank = 'XGG', Cue = 'Computer_Computer_UnitRevalation_01372'})
end
end
-- Given a head and taunt number, tells the UI to play the relating taunt
function PlayTaunt(head, taunt)
Sync.MPTaunt = {head, taunt}
end
-- Mission Text
function DisplayMissionText(string)
if not Sync.MissionText then
Sync.MissionText = {}
end
table.insert(Sync.MissionText, string)
end
-- Video Text
function DisplayVideoText(string)
if not Sync.VideoText then
Sync.VideoText = {}
end
table.insert(Sync.VideoText, string)
end
-- Play an NIS
function PlayNIS(pathToMovie)
if not Sync.NISVideo then
Sync.NISVideo = pathToMovie
end
end
function PlayEndGameMovie(faction, callback)
if not Sync.EndGameMovie then
Sync.EndGameMovie = faction
end
if callback then
if not ScenarioInfo.DialogueFinished then
ScenarioInfo.DialogueFinished = {}
end
ScenarioInfo.DialogueFinished['EndGameMovie'] = false
ForkThread(EndGameWaitThread, callback)
end
end
function EndGameWaitThread(callback)
while not ScenarioInfo.DialogueFinished['EndGameMovie'] do
WaitTicks(1)
end
callback()
ScenarioInfo.DialogueFinished['EndGameMovie'] = false
end
-- Plays an XACT sound if needed - currently all VOs are videos
function PlayVoiceOver(voSound)
table.insert(Sync.Voice, voSound)
local pauseHere = nil
end
-- Set enhancement restrictions
-- Supply a table of the names of enhancements you do not want the player to build
-- Example: {"Teleport", "ResourceAllocation"}
function RestrictEnhancements(table)
local rest = {}
for _, e in table do
rest[e] = true
end
SimUIVars.SaveEnhancementRestriction(rest)
import('/lua/enhancementcommon.lua').RestrictList(rest)
Sync.EnhanceRestrict = rest
end
-- Returns true if all units in group are dead
function GroupDeathCheck(group)
for _, v in group do
if not v.Dead then
return false
end
end
return true
end
-- Iterates through objects in list. if any in list are not true ... returns false
function CheckObjectives(list)
for _, v in list do
if not v then
return false
end
end
return true
end
function SpawnCommander(brain, unit, effect, name, PauseAtDeath, DeathTrigger, enhancements)
local ACU = ScenarioUtils.CreateArmyUnit(brain, unit)
local bp = ACU:GetBlueprint()
local bonesToHide = bp.WarpInEffect.HideBones
local delay = 0
local function CreateEnhancements(unit, enhancements, delay)
if delay then
WaitSeconds(delay)
end
for _, enhancement in enhancements do
unit:CreateEnhancement(enhancement)
end
end
local function GateInEffect(unit, effect, bonesToHide)
if effect == 'Gate' then
delay = 0.75
ForkThread(FakeGateInUnit, unit, nil, bonesToHide)
elseif effect == 'Warp' then
delay = 2.1
unit:PlayCommanderWarpInEffect(bonesToHide)
else
WARN('*WARNING: Invalid effect type: ' .. effect .. '. Available types: Gate, Warp.')
end
end
if enhancements and effect then
-- Don't hide upgrade bones that we want add on the command unit
for _, enh in enhancements do
if bp.Enhancements[enh].ShowBones then
for _, bone in bp.Enhancements[enh].ShowBones do
table.removeByValue(bonesToHide, bone)
end
end
end
GateInEffect(ACU, effect, bonesToHide)
-- Creating upgrades needs to be delayed until the effect plays, else the upgrade bone would show up before the rest of the unit
ForkThread(CreateEnhancements, ACU, enhancements, delay)
elseif enhancements then
CreateEnhancements(ACU, enhancements)
elseif effect then
GateInEffect(ACU, effect)
end
-- If true is passed as parameter then it uses default name.
if name == true then
ACU:SetCustomName(GetArmyBrain(brain).Nickname)
elseif type(name) == 'string' then
ACU:SetCustomName(name)
end
if PauseAtDeath then
PauseUnitDeath(ACU)
end
if DeathTrigger then
CreateUnitDeathTrigger(DeathTrigger, ACU)
end
return ACU
end
-- FakeTeleportUnitThread
-- Run teleport effect then delete unit
function FakeTeleportUnit(unit, killUnit)
IssueStop({unit})
IssueClearCommands({unit})
unit:SetCanBeKilled(false)
unit:PlayTeleportChargeEffects(unit:GetPosition(), unit:GetOrientation())
unit:PlayUnitSound('GateCharge')
WaitSeconds(2)
unit:CleanupTeleportChargeEffects()
unit:PlayTeleportOutEffects()
unit:PlayUnitSound('GateOut')
WaitSeconds(1)
if killUnit then
unit:Destroy()
end
end
function FakeGateInUnit(unit, callbackFunction, bonesToHide)
local bp = unit:GetBlueprint()
if EntityCategoryContains(categories.COMMAND + categories.SUBCOMMANDER, unit) then
unit:HideBone(0, true)
unit:SetUnSelectable(true)
unit:SetBusy(true)
unit:PlayUnitSound('CommanderArrival')
unit:CreateProjectile('/effects/entities/UnitTeleport03/UnitTeleport03_proj.bp', 0, 1.35, 0, nil, nil, nil):SetCollision(false)
WaitSeconds(0.75)
local psm = bp.Display.WarpInEffect.PhaseShieldMesh
if psm then
unit:SetMesh(psm, true)
end
unit:ShowBone(0, true)
for _, v in bonesToHide or bp.Display.WarpInEffect.HideBones do
unit:HideBone(v, true)
end
unit:SetUnSelectable(false)
unit:SetBusy(false)
local totalBones = unit:GetBoneCount() - 1
local army = unit:GetArmy()
for _, v in import('/lua/EffectTemplates.lua').UnitTeleportSteam01 do
for bone = 1, totalBones do
CreateAttachedEmitter(unit, bone, army, v)
end
end
if psm then
WaitSeconds(2)
unit:SetMesh(bp.Display.MeshBlueprint, true)
end
else
LOG ('debug:non commander')
unit:PlayTeleportChargeEffects(unit:GetPosition(), unit:GetOrientation())
unit:PlayUnitSound('GateCharge')
WaitSeconds(2)
unit:CleanupTeleportChargeEffects()
end
if callbackFunction then
callbackFunction()
end
end
-- Upgrades unit - for use with engineers, factories, radar, and other single upgrade path units.
-- Commander upgrades are too complicated for this
function UpgradeUnit(unit)
local upgradeBP = unit:GetBlueprint().General.UpgradesTo