forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimObjectives.lua
1988 lines (1733 loc) · 66.6 KB
/
SimObjectives.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/SimObjectives.lua
-- Summary : Sim side objectives
-- Copyright © 2006 Gas Powered Games, Inc. All rights reserved.
-----------------------------------------------------------------
-- SUPPORTED OBJECTIVE TYPES:
-- Kill
-- Capture
-- KillOrCapture
-- Reclaim
-- ReclaimProp
-- Locate
-- SpecificUnitsInArea
-- CategoriesInArea
-- ArmyStatCompare
-- UnitStatCompare
-- CategoryStatCompare
-- Protect
-- Timer
-- Unknown
-- Camera
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local Triggers = import('/lua/scenariotriggers.lua')
local VizMarker = import('/lua/sim/VizMarker.lua').VizMarker
local objNum = 0 -- Used to create unique tags for objectives
local DecalLOD = 4000
local objectiveDecal = '/env/utility/decals/objective_debug_albedo.dds'
local SavedList = {}
-- Return one of the human players in a game, using this instead of GetFocusArmy()
-- to get a deterministic army index in coop.
local playerArmy
function GetPlayerArmy()
if not playerArmy then
for _, v in ArmyBrains do
if v.BrainType == 'Human' then
playerArmy = v:GetArmyIndex()
break
end
end
end
return playerArmy
end
-- Camera objective by roates
-- Creates markers that satisfy the objective when they are all inside of the camera viewport
-- Camera(objectiveType, completeState, title, description, positionTable)
-- objectiveType = 'primary' or 'bonus' etc...
-- completeState = 'complete' or 'incomplete'
-- title = title string table from map's string file
-- description = description string table from map's string file
-- positionTable = table of position tables where markers will be created. {{x1, y1, z1}, {x2, y2, z2}} format
function Camera(objectiveType, completeState, title, description, positionTable)
local numMarkers = 0
local curMarkers = 0
local objective = AddObjective(objectiveType, completeState, title, description, nil, positionTable)
local RemoveMarker = function(mark)
mark:Destroy()
curMarkers = curMarkers + 1
UpdateObjective(title, 'Progress', '('..curMarkers..'/'..numMarkers..')', objective.Tag)
objective:OnProgress(curMarkers, numMarkers)
if curMarkers == numMarkers then
objective.Active = false
UpdateObjective(title, 'complete', 'complete', objective.Tag)
objective:OnResult(true)
end
end
for i, v in positionTable do
numMarkers = numMarkers + 1
local newMark = import('/lua/simcameramarkers.lua').AddCameraMarker(v)
newMark:AddCallback(RemoveMarker)
end
objective:OnProgress(curMarkers, numMarkers)
UpdateObjective(title, 'Progress', '('..curMarkers..'/'..numMarkers..')', objective.Tag)
return objective
end
-- ControlGroup
-- Complete when specified units matching the target blueprint types are in
-- a control group. We don't care exactly which units they are (pre-built or
-- newly constructed), as long as the requirements are ment. We just check
-- the area for what units are in control groups and look at the blueprints (and optionally
-- match the army, use -1 for don't care).
-- Target = {
-- Requirements = {
-- {Category=<cat1>, CompareOp=<op>, Value=<x>, [ArmyIndex=<index>]},
-- {Category=<cat2>, CompareOp=<op>, Value=<y>, [ArmyIndex=<index>]},
-- {Category=<cat3>, CompareOp=<op>, Value=<z>, [ArmyIndex=<index>]},
-- }
-- }
-- op is one of: '<=', '>=', '<', '>', or '=='
function ControlGroup(Type, Complete, Title, Description, Target)
local image = GetActionIcon('group')
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
local lastReqsMet = -1
-- Call ManualResult
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
local function WatchGroups(requirements)
local totalReqs = table.getn(requirements)
while objective.Active do
local reqsMet = 0
for i, requirement in requirements do
local units = ScenarioInfo.ControlGroupUnits
local cnt = 0
if units then
for _, unit in units do
if not requirement.ArmyIndex or (requirement.ArmyIndex == unit.Army) then
if EntityCategoryContains(requirement.Category, unit) then
if not unit.Marked and objective.MarkUnits then
unit.Marked = true
local ObjectiveArrow = import('objectiveArrow.lua').ObjectiveArrow
local arrow = ObjectiveArrow {AttachTo = unit}
objective:AddUnitTarget(unit)
end
cnt = cnt + 1
end
end
end
end
if not requirement.CompareFunc then
requirement.CompareFunc = GetCompareFunc(requirement.CompareOp)
end
if requirement.CompareFunc(cnt, requirement.Value) then
reqsMet = reqsMet +1
end
end
if lastReqsMet ~= reqsMet then
local progress = string.format('(%s/%s)', reqsMet, totalReqs)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
objective:OnProgress(reqsMet, totalReqs)
lastReqsMet = reqsMet
end
if reqsMet == totalReqs then
objective.Active = false
objective:OnResult(true)
UpdateObjective(Title, 'complete', 'complete', objective.Tag)
return
end
WaitTicks(10)
end
end
UpdateObjective(Title, 'Progress', '(0/0)', objective.Tag)
ForkThread(WatchGroups, Target.Requirements)
return objective
end
-- CreateGroup
-- Takes list of objective tables which are produced by the objective creation
-- functions such as Kill, Protect, Capture, etc.
-- UserCallback is executed when all objectives in the list are complete
function CreateGroup(name, userCallback, numRequired)
local objectiveGroup = {
Name = name,
Active = true,
Objectives = {},
NumRequired = numRequired,
NumCompleted = 0,
AddObjective = function(self, objective) end, -- Defined later
RemoveObjective = function(self, objective) end, -- Defined later
OnComplete = userCallback,
}
local function OnResult(result)
if not objectiveGroup.Active then
return
end
if result then
objectiveGroup.NumCompleted = objectiveGroup.NumCompleted + 1
end
if objectiveGroup.NumRequired then
if objectiveGroup.NumCompleted < objectiveGroup.NumRequired then
return
end
else
if objectiveGroup.Objectives then
for _, v in objectiveGroup.Objectives do
if v.Active then
return
end
end
end
end
objectiveGroup.Active = false
objectiveGroup.OnComplete()
end
objectiveGroup.AddObjective = function(self, objective)
table.insert(self.Objectives, objective)
objective:AddResultCallback(OnResult)
end
objectiveGroup.RemoveObjective = function(self, objective)
table.removeByValue(self.Objectives, objective)
end
return objectiveGroup
end
-- Kill units
function Kill(Type, Complete, Title, Description, Target)
Target.killed = 0
Target.total = table.getn(Target.Units)
local image = GetActionIcon('kill')
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
-- Call ManualResult
objective.ManualResult = function(self, result)
objective.Active = false
objective:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, objective.Tag)
end
objective.OnUnitKilled = function(unit)
if not objective.Active then
return
end
Target.killed = Target.killed + 1
local progress = string.format('(%s/%s)', Target.killed, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
objective:OnProgress(Target.killed, Target.total)
if Target.killed == Target.total then
UpdateObjective(Title, 'complete', "complete", objective.Tag)
objective.Active = false
objective:OnResult(true, unit)
end
end
objective.OnUnitGiven = function(unit, newUnit)
if not objective.Active then
return
end
OnUnitGivenBase(objective, Target, unit, newUnit, (Target.MarkUnits == nil) or Target.MarkUnits)
CreateTriggers(newUnit, objective, true) -- Reclaiming is same as killing for our purposes
end
for _, unit in Target.Units do
if not unit.Dead then
-- Mark the units unless MarkUnits == false
if Target.MarkUnits == nil or Target.MarkUnits then
local ObjectiveArrow = import('objectiveArrow.lua').ObjectiveArrow
local arrow = ObjectiveArrow {AttachTo = unit}
end
if Target.FlashVisible then
FlashViz(unit)
end
CreateTriggers(unit, objective, true) -- Reclaiming is same as killing for our purposes
else
objective.OnUnitKilled(unit)
end
end
local progress = string.format('(%s/%s)', Target.killed, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
return objective
end
-- Capture units
-- Target = {
-- Units = <units>,
-- NumRequired = <x>,
-- }
function Capture(Type, Complete, Title, Description, Target)
Target.captured = 0
Target.total = table.getn(Target.Units)
local required = Target.NumRequired or Target.total
local returnUnits = {}
local image = GetActionIcon('capture')
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
objective.OnUnitCaptured = function(unit, captor)
table.insert(returnUnits, unit)
if not objective.Active then
return
end
Target.captured = Target.captured + 1
local progress = string.format('(%s/%s)', Target.captured, required)
objective:OnProgress(Target.captured, required)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
if Target.captured >= required then
objective.Active = false
objective:OnResult(true, returnUnits)
UpdateObjective(Title, 'complete', "complete", objective.Tag)
end
end
objective.OnUnitKilled = function(unit)
if not objective.Active then
return
end
Target.total = Target.total - 1
if Target.total < required then
objective.Active = false
objective:OnResult(false)
UpdateObjective(Title, 'complete', 'failed', objective.Tag)
end
end
objective.OnUnitGiven = function(unit, newUnit)
if not objective.Active then
return
end
OnUnitGivenBase(objective, Target, unit, newUnit, (Target.MarkUnits == nil) or Target.MarkUnits)
CreateTriggers(newUnit, objective, true) -- Reclaiming is same as killing for our purposes
end
for _, unit in Target.Units do
if not unit.Dead then
-- Mark the units unless MarkUnits == false
if Target.MarkUnits == nil or Target.MarkUnits then
local ObjectiveArrow = import('objectiveArrow.lua').ObjectiveArrow
local arrow = ObjectiveArrow {AttachTo = unit}
end
CreateTriggers(unit, objective, true) -- Reclaiming is same as killing for our purposes
if Target.FlashVisible then
FlashViz(unit)
end
else
objective.OnUnitKilled(unit)
end
end
local progress = string.format('(%s/%s)', Target.captured, required)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
return objective
end
-- Kill or Capture units
function KillOrCapture(Type, Complete, Title, Description, Target)
local KilledOrCaptured = 0
local Total = table.getn(Target.Units)
local PercentRequired = Target.PercentRequired or 100
local NumRequired = math.ceil(Total * (PercentRequired / 100))
local image = GetActionIcon('KillOrCapture')
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
objective.UpdateProgress = function()
local progress
if Target.PercentProgress then
progress = string.format('(%s%%/%s%%)', math.floor(((Total - (Total - KilledOrCaptured)) / Total) * 100), PercentRequired)
elseif Target.ShowProgress == nil or Target.ShowProgress then
progress = string.format('(%s/%s)', KilledOrCaptured, NumRequired)
end
UpdateObjective(Title, 'Progress', progress, objective.Tag)
end
-- Keep track of captured units so subsequent kills dont get counted
local captured = {}
objective.OnUnitKilled = function(unit)
if not objective.Active then
return
end
for _, v in captured do
if v == unit then
-- Ignore units already captured
return
end
end
KilledOrCaptured = KilledOrCaptured + 1
objective:OnProgress(KilledOrCaptured, NumRequired)
objective:UpdateProgress()
if KilledOrCaptured == NumRequired then
objective.Active = false
objective:OnResult(true, unit)
UpdateObjective(Title, 'complete', "complete", objective.Tag)
end
end
objective.OnUnitCaptured = function(unit)
if not objective.Active then
return
end
table.insert(captured, unit)
KilledOrCaptured = KilledOrCaptured + 1
objective:OnProgress(KilledOrCaptured, NumRequired)
objective:UpdateProgress()
if KilledOrCaptured == NumRequired then
objective.Active = false
objective:OnResult(true, unit)
UpdateObjective(Title, 'complete', "complete", objective.Tag)
end
end
objective.OnUnitReclaimed = function(unit)
if not objective.Active then
return
end
KilledOrCaptured = KilledOrCaptured + 1
objective:OnProgress(KilledOrCaptured, NumRequired)
objective:UpdateProgress()
if KilledOrCaptured == NumRequired then
objective.Active = false
objective:OnResult(true, unit)
UpdateObjective(Title, 'complete', "complete", objective.Tag)
end
end
objective.OnUnitGiven = function(unit, newUnit)
if not objective.Active then
return
end
for _, cUnit in captured do
if cUnit == unit then
table.insert(captured, newUnit)
break
end
end
OnUnitGivenBase(objective, Target, unit, newUnit, (Target.MarkUnits == nil) or Target.MarkUnits)
CreateTriggers(newUnit, objective)
end
for _, unit in Target.Units do
if not unit.Dead then
-- Mark the units unless MarkUnits == false
if Target.MarkUnits == nil or Target.MarkUnits then
local ObjectiveArrow = import('objectiveArrow.lua').ObjectiveArrow
local arrow = ObjectiveArrow {AttachTo = unit}
end
if Target.FlashVisible then
FlashViz(unit)
end
CreateTriggers(unit, objective)
else
objective.OnUnitKilled(unit)
end
end
objective:UpdateProgress()
return objective
end
-- Reclaim units
function Reclaim(Type, Complete, Title, Description, Target)
Target.reclaimed = 0
Target.total = table.getn(Target.Units)
local image = GetActionIcon("reclaim")
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
objective.OnUnitReclaimed = function(unit)
if not objective.Active then
return
end
Target.reclaimed = Target.reclaimed + 1
local progress = string.format('(%s/%s)', Target.reclaimed, Target.total)
objective:OnProgress(Target.reclaimed, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
if Target.reclaimed == Target.total then
objective.Active = false
objective:OnResult(true)
UpdateObjective(Title, 'complete', "complete", objective.Tag)
end
end
objective.OnUnitKilled = function(unit)
if not objective.Active then
return
end
objective.Active = false
objective:OnResult(false)
UpdateObjective(Title, 'complete', 'failed', objective.Tag)
end
-- If the unit is captured it can still be reclaimed to complete the
-- objective, so track the new unit created on a capture.
objective.OnUnitCaptured = function(newUnit, captor)
if not objective.Active then
return
end
OnUnitGivenBase(objective, Target, nil, newUnit, true)
CreateTriggers(newUnit, objective)
end
objective.OnUnitGiven = function(unit, newUnit)
if not objective.Active then
return
end
OnUnitGivenBase(objective, Target, unit, newUnit, true)
CreateTriggers(newUnit, objective)
end
for _, unit in Target.Units do
local ObjectiveArrow = import('objectiveArrow.lua').ObjectiveArrow
local arrow = ObjectiveArrow {AttachTo = unit}
CreateTriggers(unit, objective)
end
local progress = string.format('(%s/%s)', Target.reclaimed, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
return objective
end
-- ReclaimProp
function ReclaimProp(Type, Complete, Title, Description, Target)
Target.reclaimed = 0
Target.total = table.getn(Target.Wrecks)
local image = GetActionIcon("reclaim")
local objective = AddObjective(Type, Complete, Title, Description, image)
-- Call ManualResult
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
local function OnPropKilled(unit)
objective.Active = false
objective:OnResult(false)
UpdateObjective(Title, 'complete', 'failed', objective.Tag)
end
local function OnPropReclaimed(unit)
if not objective.Active then
return
end
Target.reclaimed = Target.reclaimed + 1
local progress = string.format('(%s/%s)', Target.reclaimed, Target.total)
objective:OnProgress(Target.reclaimed, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
if Target.reclaimed == Target.total then
objective.Active = false
objective:OnResult(true)
UpdateObjective(Title, 'complete', "complete", objective.Tag)
end
end
for _, wreck in Target.Wrecks do
-- Mark the units if MarkUnits == true
if Target.MarkUnits then
local ObjectiveArrow = import('objectiveArrow.lua').ObjectiveArrow
local arrow = ObjectiveArrow {AttachTo = unit}
end
Triggers.CreatePropReclaimedTrigger(OnPropReclaimed, wreck)
Triggers.CreatePropKilledTrigger(OnPropKilled, wreck)
end
local progress = string.format('(%s/%s)', Target.reclaimed, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
return objective
end
-- Locate units
function Locate(Type, Complete, Title, Description, Target)
Target.located = 0
Target.total = table.getn(Target.Units)
local isLocated = {}
local image = GetActionIcon("locate")
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
objective.OnUnitLocated = function(unit)
if isLocated[unit] or not objective.Active then
return
end
Target.located = Target.located + 1
isLocated[unit] = true
local progress = string.format('(%s/%s)', Target.located, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
objective:OnProgress(Target.located, Target.total)
if Target.located == Target.total then
objective.Active = false
objective:OnResult(true)
UpdateObjective(Title, 'complete', "complete", objective.Tag)
end
end
objective.OnUnitGiven = function(unit, newUnit)
if isLocated[unit] or not objective.Active then
return
end
OnUnitGivenBase(objective, Target, unit, newUnit, false)
isLocated[newUnit] = CreateIntelTriggers(newUnit, objective, isLocated[unit])
end
for _, unit in Target.Units do
CreateIntelTriggers(unit, objective)
end
local progress = string.format('(%s/%s)', Target.located, Target.total)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
return objective
end
-- SpecificUnitsInArea
-- Complete when specified units are in the target area. We don't care how
-- they got there (cheat teleport, etc), we just check if they're in there
-- ShowProgress, optional:
function SpecificUnitsInArea(Type, Complete, Title, Description, Target)
local image = GetActionIcon('Move')
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
local total = table.getn(Target.Units)
local numRequired = Target.NumRequired or total
Target.Count = 0
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
local function WatchArea(units, rect)
while objective.Active do
local cnt = 0
for _, unit in units do
if not unit.Dead then
if ScenarioUtils.InRect(unit:GetPosition(), rect) then
cnt = cnt + 1
end
end
end
if cnt ~= Target.Count then
Target.Count = cnt
local progress = string.format('(%s/%s)', Target.Count, numRequired)
objective:OnProgress(Target.Count, numRequired)
if Target.ShowProgress then
UpdateObjective(Title, 'Progress', progress, objective.Tag)
end
end
if cnt >= numRequired then
objective.Active = false
objective:OnResult(true)
UpdateObjective(Title, 'complete', 'complete', objective.Tag)
return
end
WaitTicks(5)
end
end
local rect = ScenarioUtils.AreaToRect(Target.Area)
local w = rect.x1 - rect.x0
local h = rect.y1 - rect.y0
local x = rect.x0 + (w / 2.0)
local z = rect.y0 + (h / 2.0)
if Target.MarkArea then
objective.Decals[Target.Area] = CreateObjectiveDecal(x, z, w, h)
end
local watchThread = ForkThread(WatchArea, Target.Units, rect)
objective.OnUnitKilled = function(unit)
total = total - 1
if objective.Active and total < numRequired then
objective.Active = false
objective:OnResult(false)
UpdateObjective(Title, 'complete', 'failed', objective.Tag)
KillThread(watchThread)
end
end
objective.OnUnitGiven = function(unit, newUnit)
if not objective.Active then
return
end
OnUnitGivenBase(objective, Target, unit, newUnit, Target.MarkUnits)
CreateTriggers(newUnit, objective, true)
end
for _, unit in Target.Units do
CreateTriggers(unit, objective, true)
end
if Target.ShowProgress then
local progress = string.format('(%s/%s)', Target.Count, numRequired)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
end
return objective
end
-- CategoriesInArea
-- Complete when specified units matching the target blueprint types are in
-- the target area. We don't care exactly which units they are (pre-built or
-- newly constructed) or how they got there (cheat teleport, etc). We just check
-- the area for what units are inside and look at the blueprints (and optionally
-- match the army, use -1 for don't care)
-- New: Add a table with armies instead of a single ArmyIndex. ex: {'Army_1', 'Army_2', ...}
-- Use 'HumanPlayers' when you want to use all Human controlled armies.
--
-- Target = {
-- Requirements = {
-- {Area = <areaName>, Category=<cat1>, CompareOp=<op>, Value=<x>, [ArmyIndex=<index>], [Armies=<armyTable>]},
-- {Area = <areaName>, Category=<cat2>, CompareOp=<op>, Value=<y>, [ArmyIndex=<index>], [Armies=<armyTable>]},
-- ...
-- {Area = <areaName>, Category=<cat3>, CompareOp=<op>, Value=<z>, [ArmyIndex=<index>], [Armies=<armyTable>]},
-- }
-- }
-- op is one of: '<=', '>=', '<', '>', or '=='
function CategoriesInArea(Type, Complete, Title, Description, Action, Target)
local image = GetActionIcon(Action)
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
local lastReqsMet = 0
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
local function WatchArea(requirements)
local totalReqs = table.getn(requirements)
while objective.Active do
local reqsMet = 0
for i, requirement in requirements do
local units = GetUnitsInRect(requirement.Rect)
local cnt = 0
local ArmiesList = CreateArmiesList(requirement.Armies)
if units then
for _, unit in units do
if not unit.Dead and not unit:IsBeingBuilt() then
if not (requirement.ArmyIndex or requirement.Armies) or (requirement.ArmyIndex == unit.Army) or ArmiesList[unit.Army] then
if EntityCategoryContains(requirement.Category, unit) then
if not unit.Marked and objective.MarkUnits then
unit.Marked = true
local ObjectiveArrow = import('objectiveArrow.lua').ObjectiveArrow
local arrow = ObjectiveArrow {AttachTo = unit}
objective:AddUnitTarget(unit)
end
cnt = cnt + 1
end
end
end
end
end
if requirement.CompareFunc(cnt, requirement.Value) then
reqsMet = reqsMet +1
end
end
if lastReqsMet ~= reqsMet then
local progress = string.format('(%s/%s)', reqsMet, totalReqs)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
objective:OnProgress(reqsMet, totalReqs)
lastReqsMet = reqsMet
end
if reqsMet == totalReqs then
objective.Active = false
objective:OnResult(true)
UpdateObjective(Title, 'complete', 'complete', objective.Tag)
return
end
WaitTicks(10)
end
end
for _, requirement in Target.Requirements do
local rect = ScenarioUtils.AreaToRect(requirement.Area)
local w = rect.x1 - rect.x0
local h = rect.y1 - rect.y0
local x = rect.x0 + (w / 2.0)
local z = rect.y0 + (h / 2.0)
if Target.MarkArea and not objective.Decals[requirement.Area] then
local decal = CreateObjectiveDecal(x, z, w, h)
objective.Decals[requirement.Area] = decal
elseif Target.FlashVisible then
FlashViz(requirement.Area)
end
if Target.MarkUnits then
objective.MarkUnits = true
end
local reqRef = requirement
reqRef.Rect = rect
reqRef.CompareFunc = GetCompareFunc(requirement.CompareOp)
end
UpdateObjective(Title, 'Progress', string.format('(0/%d)', table.getsize(Target.Requirements)), objective.Tag)
ForkThread(WatchArea, Target.Requirements)
return objective
end
function CreateArmiesList(armies)
if not armies then
return {}
end
local armiesList = {}
for _, armyName in armies do
if type(armyName) ~= 'string' then
error('SimObjectives error: Armies in requirements need to be of type string, provided type: ' .. type(armyName))
end
if armyName == 'HumanPlayers' then
local tblArmy = ListArmies()
for iArmy, strArmy in pairs(tblArmy) do
if ScenarioInfo.ArmySetup[strArmy].Human then
armiesList[ScenarioInfo.ArmySetup[strArmy].ArmyIndex] = true
end
end
elseif ScenarioInfo.ArmySetup[armyName] then
armiesList[ScenarioInfo.ArmySetup[armyName].ArmyIndex] = true
else
error('SimObjectives error: Army doesnt exist: ' .. armyName)
end
end
return armiesList
end
-- ArmyStatCompare
-- Army stat is compared <=, >=, >, <, or == to some value.
-- Target = {
-- Army=<index>,
-- Armies=[<string>, <string>, ...] --table of army names and HumanPlayers
-- StatName=<name>,
-- CompareOp=<op>, -- op is one of: '<=', '>=', '<', '>', or '=='
-- Value=<value>,
-- [Category=<category>], -- optional to compare to a blueprint stat
-- ShowProgress, optional: shows --/--. may not make sense for all compare types
-- }
-- Note: Be careful when using '==' as the stat is only checked every 5 ticks.
function ArmyStatCompare(Type, Complete, Title, Description, Action, Target)
local image = GetActionIcon(Action)
local objective = AddObjective(Type, Complete, Title, Description, image, Target)
local armyBrainsList = MakeListFromTarget(Target)
local function WatchStat(statName, aibrains, compareFunc, value, category)
local oldVal
while objective.Active do
local result = false
local testVal = 0
for brain, _ in aibrains do
if category then
testVal = testVal + brain:GetBlueprintStat(statName, category)
else
testVal = testVal + brain:GetArmyStat(statName, value).Value
end
end
if Target.ShowProgress then
if testVal ~= oldVal then
local progress = string.format('(%s/%s)', testVal, value)
UpdateObjective(Title, 'Progress', progress, objective.Tag)
oldVal = testVal
end
end
result = compareFunc(testVal, value)
if result then
objective.Active = false
objective:OnResult(true)
UpdateObjective(Title, 'complete', 'complete', objective.Tag)
return
end
WaitTicks(5)
end
end
local op = GetCompareFunc(Target.CompareOp)
if op then
ForkThread(WatchStat, Target.StatName, armyBrainsList, op, Target.Value, Target.Category)
end
objective.ManualResult = function(self, result)
self.Active = false
self:OnResult(result)
local resultStr
if result then
resultStr = 'complete'
else
resultStr = 'failed'
end
UpdateObjective(Title, 'complete', resultStr, self.Tag)
end
return objective
end
function MakeListFromTarget(Target)
local resultList = {}
if Target.Army then
resultList[GetArmyBrain(Target.Army)] = true
end
if Target.Armies then
local tblArmy = ListArmies()
for _, armyName in Target.Armies do
if armyName == "HumanPlayers" then
for iArmy, strArmy in pairs(tblArmy) do
if ScenarioInfo.ArmySetup[strArmy].Human then
resultList[GetArmyBrain(iArmy)] = true