This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
forked from Dzsek/zoneCommander
-
Notifications
You must be signed in to change notification settings - Fork 2
/
zoneCommander.lua
3418 lines (2925 loc) · 97.9 KB
/
zoneCommander.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
--[[
~~~Description~~~
ZoneCommander provides classes and utilities for building dynamic missions, with an enemy that reacts to the changing battlefield.
Needs MIST: https://github.com/mrSkortch/MissionScriptingTools/tree/master
~~~Github~~~
Repo & documentation: https://github.com/Dzsek/zoneCommander
~~~Author~~
Dzsekeb
]]
CustomZone = {}
do
function CustomZone:getByName(name)
obj = {}
obj.name = name
local zd = nil
for _,v in ipairs(env.mission.triggers.zones) do
if v.name == name then
zd = v
break
end
end
if not zd then
return nil
end
obj.type = zd.type -- 2 == quad, 0 == circle
if obj.type == 2 then
obj.vertices = {}
for _,v in ipairs(zd.verticies) do
local vertex = {
x = v.x,
y = 0,
z = v.y
}
table.insert(obj.vertices, vertex)
end
end
obj.radius = zd.radius
obj.point = {
x = zd.x,
y = 0,
z = zd.y
}
setmetatable(obj, self)
self.__index = self
return obj
end
function CustomZone:isQuad()
return self.type==2
end
function CustomZone:isCircle()
return self.type==0
end
function CustomZone:isInside(point)
if self:isCircle() then
local dist = mist.utils.get2DDist(point, self.point)
return dist<self.radius
elseif self:isQuad() then
return mist.pointInPolygon(point, self.vertices)
end
end
function CustomZone:draw(id, border, background)
if self:isCircle() then
trigger.action.circleToAll(-1,id,self.point, self.radius,border,background,1)
elseif self:isQuad() then
trigger.action.quadToAll(-1,id,self.vertices[4], self.vertices[3], self.vertices[2], self.vertices[1],border,background,1)
end
end
function CustomZone:getRandomSpawnZone()
local spawnZones = {}
for i=1,100,1 do
local zname = self.name..'-'..i
if trigger.misc.getZone(zname) then
table.insert(spawnZones, zname)
else
break
end
end
if #spawnZones == 0 then return nil end
local choice = math.random(1, #spawnZones)
return spawnZones[choice]
end
function CustomZone:spawnGroup(grname, forceFirst)
local spname = self.name
local spawnzone = nil
if forceFirst and trigger.misc.getZone(spname..'-0')then
spawnzone = spname..'-0'
end
if not spawnzone then
spawnzone = self:getRandomSpawnZone()
end
if spawnzone then
spname = spawnzone
end
local pnt = mist.getRandomPointInZone(spname)
local vars = {
groupName = grname,
point = pnt,
action = 'clone',
disperse = true,
initTasks = true
}
local newgr = mist.teleportToPoint(vars)
if not newgr then
newgr = mist.cloneInZone(grname, spname, true, nil, {initTasks = true})
end
return newgr
end
end
Utils = {}
do
function Utils.getPointOnSurface(point)
return {x = point.x, y = land.getHeight({x = point.x, y = point.z}), z= point.z}
end
function Utils.getTableSize(tbl)
local cnt = 0
for i,v in pairs(tbl) do cnt=cnt+1 end
return cnt
end
function Utils.getBearing(fromvec, tovec)
local fx = fromvec.x
local fy = fromvec.z
local tx = tovec.x
local ty = tovec.z
local brg = math.atan2(ty - fy, tx - fx)
if brg < 0 then
brg = brg + 2 * math.pi
end
brg = brg * 180 / math.pi
return brg
end
function Utils.getAGL(object)
local pt = object:getPoint()
return pt.y - land.getHeight({ x = pt.x, y = pt.z })
end
function Utils.isLanded(unit, ignorespeed)
--return (Utils.getAGL(unit)<5 and mist.vec.mag(unit:getVelocity())<0.10)
if ignorespeed then
return not unit:inAir()
else
return (not unit:inAir() and mist.vec.mag(unit:getVelocity())<1)
end
end
function Utils.isGroupActive(group)
if group and group:getSize()>0 and group:getController():hasTask() then
return not Utils.allGroupIsLanded(group, true)
else
return false
end
end
function Utils.isInAir(unit)
--return Utils.getAGL(unit)>5
return unit:inAir()
end
function Utils.isInZone(unit, zonename)
local zn = CustomZone:getByName(zonename)
if zn then
return zn:isInside(unit:getPosition().p)
end
return false
end
function Utils.isCrateSettledInZone(crate, zonename)
local zn = CustomZone:getByName(zonename)
if zn and crate then
return (zn:isInside(crate:getPosition().p) and Utils.getAGL(crate)<1)
end
return false
end
function Utils.someOfGroupInZone(group, zonename)
for i,v in pairs(group:getUnits()) do
if Utils.isInZone(v, zonename) then
return true
end
end
return false
end
function Utils.allGroupIsLanded(group, ignorespeed)
for i,v in pairs(group:getUnits()) do
if not Utils.isLanded(v, ignorespeed) then
return false
end
end
return true
end
function Utils.someOfGroupInAir(group)
for i,v in pairs(group:getUnits()) do
if Utils.isInAir(v) then
return true
end
end
return false
end
Utils.canAccessFS = true
function Utils.saveTable(filename, variablename, data)
if not Utils.canAccessFS then
return
end
if not io then
Utils.canAccessFS = false
trigger.action.outText('Persistance disabled', 30)
return
end
local str = variablename..' = {}'
for i,v in pairs(data) do
str = str..'\n'..variablename..'[\''..i..'\'] = '..Utils.serializeValue(v)
end
File = io.open(filename, "w")
File:write(str)
File:close()
end
function Utils.serializeValue(value)
local res = ''
if type(value)=='number' or type(value)=='boolean' then
res = res..tostring(value)
elseif type(value)=='string' then
res = res..'\''..value..'\''
elseif type(value)=='table' then
res = res..'{ '
for i,v in pairs(value) do
if type(i)=='number' then
res = res..'['..i..']='..Utils.serializeValue(v)..','
else
res = res..'[\''..i..'\']='..Utils.serializeValue(v)..','
end
end
res = res:sub(1,-2)
res = res..' }'
end
return res
end
function Utils.loadTable(filename)
if not Utils.canAccessFS then
return
end
if not lfs then
Utils.canAccessFS = false
trigger.action.outText('Persistance disabled', 30)
return
end
if lfs.attributes(filename) then
dofile(filename)
end
end
end
JTAC = {}
do
JTAC.categories = {}
JTAC.categories['SAM'] = {'SAM SR', 'SAM TR', 'IR Guided SAM','SAM LL','SAM CC'}
JTAC.categories['Infantry'] = {'Infantry'}
JTAC.categories['Armor'] = {'Tanks','IFV','APC'}
JTAC.categories['Support'] = {'Unarmed vehicles','Artillery'}
--{name = 'groupname'}
function JTAC:new(obj)
obj = obj or {}
obj.lasers = {tgt=nil, ir=nil}
obj.target = nil
obj.timerReference = nil
obj.tgtzone = nil
obj.priority = nil
obj.jtacMenu = nil
obj.laserCode = 1688
obj.side = Group.getByName(obj.name):getCoalition()
setmetatable(obj, self)
self.__index = self
obj:initCodeListener()
return obj
end
function JTAC:initCodeListener()
local ev = {}
ev.context = self
function ev:onEvent(event)
if event.id == 26 then
if event.text:find('^jtac%-code:') then
local s = event.text:gsub('^jtac%-code:', '')
local code = tonumber(s)
if code>=1111 and code <= 1788 then
self.context.laserCode = code
trigger.action.outTextForCoalition(self.context.side, 'JTAC code set to '..code, 10)
trigger.action.removeMark(event.idx)
end
end
end
end
world.addEventHandler(ev)
end
function JTAC:showMenu()
local gr = Group.getByName(self.name)
if not gr then
return
end
if not self.jtacMenu then
self.jtacMenu = missionCommands.addSubMenuForCoalition(self.side, 'JTAC')
missionCommands.addCommandForCoalition(self.side, 'Target report', self.jtacMenu, function(dr)
if Group.getByName(dr.name) then
dr:printTarget(true)
else
missionCommands.removeItemForCoalition(dr.side, dr.jtacMenu)
dr.jtacMenu = nil
end
end, self)
missionCommands.addCommandForCoalition(self.side, 'Next Target', self.jtacMenu, function(dr)
if Group.getByName(dr.name) then
dr:searchTarget()
else
missionCommands.removeItemForCoalition(dr.side, dr.jtacMenu)
dr.jtacMenu = nil
end
end, self)
missionCommands.addCommandForCoalition(self.side, 'Deploy Smoke', self.jtacMenu, function(dr)
if Group.getByName(dr.name) then
local tgtunit = Unit.getByName(dr.target)
if tgtunit then
trigger.action.smoke(tgtunit:getPosition().p, 3)
trigger.action.outTextForCoalition(dr.side, 'JTAC target marked with ORANGE smoke', 10)
end
else
missionCommands.removeItemForCoalition(dr.side, dr.jtacMenu)
dr.jtacMenu = nil
end
end, self)
local priomenu = missionCommands.addSubMenuForCoalition(self.side, 'Set Priority', self.jtacMenu)
for i,v in pairs(JTAC.categories) do
missionCommands.addCommandForCoalition(self.side, i, priomenu, function(dr, cat)
if Group.getByName(dr.name) then
dr:setPriority(cat)
dr:searchTarget()
else
missionCommands.removeItemForCoalition(dr.side, dr.jtacMenu)
dr.jtacMenu = nil
end
end, drone, i)
end
missionCommands.addCommandForCoalition(self.side, "Clear", priomenu, function(dr)
if Group.getByName(dr.name) then
dr:clearPriority()
dr:searchTarget()
else
missionCommands.removeItemForCoalition(dr.side, dr.jtacMenu)
dr.jtacMenu = nil
end
end, drone)
end
end
function JTAC:setPriority(prio)
self.priority = JTAC.categories[prio]
self.prioname = prio
end
function JTAC:clearPriority()
self.priority = nil
end
function JTAC:setTarget(unit)
if self.lasers.tgt then
self.lasers.tgt:destroy()
self.lasers.tgt = nil
end
if self.lasers.ir then
self.lasers.ir:destroy()
self.lasers.ir = nil
end
local me = Group.getByName(self.name)
if not me then return end
local pnt = unit:getPoint()
self.lasers.tgt = Spot.createLaser(me:getUnit(1), { x = 0, y = 2.0, z = 0 }, pnt, self.laserCode)
self.lasers.ir = Spot.createInfraRed(me:getUnit(1), { x = 0, y = 2.0, z = 0 }, pnt)
self.target = unit:getName()
end
function JTAC:printTarget(makeitlast)
local toprint = ''
if self.target and self.tgtzone then
local tgtunit = Unit.getByName(self.target)
if tgtunit then
local pnt = tgtunit:getPoint()
local tgttype = tgtunit:getTypeName()
if self.priority then
toprint = 'Priority targets: '..self.prioname..'\n'
end
toprint = toprint..'Lasing '..tgttype..' at '..self.tgtzone.zone..'\nCode: '..self.laserCode..'\n'
local lat,lon,alt = coord.LOtoLL(pnt)
local mgrs = coord.LLtoMGRS(coord.LOtoLL(pnt))
toprint = toprint..'\nDDM: '.. mist.tostringLL(lat,lon,3)
toprint = toprint..'\nDMS: '.. mist.tostringLL(lat,lon,2,true)
toprint = toprint..'\nMGRS: '.. mist.tostringMGRS(mgrs, 5)
toprint = toprint..'\n\nAlt: '..math.floor(alt)..'m'..' | '..math.floor(alt*3.280839895)..'ft'
else
makeitlast = false
toprint = 'No Target'
end
else
makeitlast = false
toprint = 'No target'
end
local gr = Group.getByName(self.name)
if makeitlast then
trigger.action.outTextForCoalition(gr:getCoalition(), toprint, 60)
else
trigger.action.outTextForCoalition(gr:getCoalition(), toprint, 10)
end
end
function JTAC:clearTarget()
self.target = nil
if self.lasers.tgt then
self.lasers.tgt:destroy()
self.lasers.tgt = nil
end
if self.lasers.ir then
self.lasers.ir:destroy()
self.lasers.ir = nil
end
if self.timerReference then
mist.removeFunction(self.timerReference)
self.timerReference = nil
end
local gr = Group.getByName(self.name)
if gr then
gr:destroy()
missionCommands.removeItemForCoalition(self.side, self.jtacMenu)
self.jtacMenu = nil
end
end
function JTAC:searchTarget()
local gr = Group.getByName(self.name)
if gr then
if self.tgtzone and self.tgtzone.side~=0 and self.tgtzone.side~=gr:getCoalition() then
local viabletgts = {}
for i,v in pairs(self.tgtzone.built) do
local tgtgr = Group.getByName(v)
if tgtgr and tgtgr:getSize()>0 then
for i2,v2 in ipairs(tgtgr:getUnits()) do
if v2:getLife()>=1 then
table.insert(viabletgts, v2)
end
end
end
end
if self.priority then
local priorityTargets = {}
for i,v in ipairs(viabletgts) do
for i2,v2 in ipairs(self.priority) do
if v:hasAttribute(v2) and v:getLife()>=1 then
table.insert(priorityTargets, v)
break
end
end
end
if #priorityTargets>0 then
viabletgts = priorityTargets
else
self:clearPriority()
trigger.action.outTextForCoalition(gr:getCoalition(), 'JTAC: No priority targets found', 10)
end
end
if #viabletgts>0 then
local chosentgt = math.random(1, #viabletgts)
self:setTarget(viabletgts[chosentgt])
self:printTarget()
else
self:clearTarget()
end
else
self:clearTarget()
end
end
end
function JTAC:searchIfNoTarget()
if Group.getByName(self.name) then
if not self.target or not Unit.getByName(self.target) then
self:searchTarget()
elseif self.target then
local un = Unit.getByName(self.target)
if un then
if un:getLife()>=1 then
self:setTarget(un)
else
self:searchTarget()
end
end
end
else
self:clearTarget()
end
end
function JTAC:deployAtZone(zoneCom)
self.tgtzone = zoneCom
local p = CustomZone:getByName(self.tgtzone.zone).point
local vars = {}
vars.gpName = self.name
vars.action = 'respawn'
vars.point = {x=p.x, y=5000, z = p.z}
mist.teleportToPoint(vars)
mist.scheduleFunction(self.setOrbit, {self, self.tgtzone.zone, p}, timer.getTime()+1)
if not self.timerReference then
self.timerReference = mist.scheduleFunction(self.searchIfNoTarget, {self}, timer.getTime()+5, 5)
end
end
function JTAC:setOrbit(zonename, point)
local gr = Group.getByName(self.name)
if not gr then
return
end
local cnt = gr:getController()
cnt:setCommand({
id = 'SetInvisible',
params = {
value = true
}
})
cnt:setTask({
id = 'Orbit',
params = {
pattern = 'Circle',
point = {x = point.x, y=point.z},
altitude = 5000
}
})
self:searchTarget()
end
end
GlobalSettings = {}
do
GlobalSettings.blockedDespawnTime = 10*60 --used to despawn aircraft that are stuck taxiing for some reason
GlobalSettings.landedDespawnTime = 1*60
GlobalSettings.initialDelayVariance = 30 -- minutes
GlobalSettings.messages = {
grouplost = false,
captured = true,
upgraded = true,
repaired = true,
zonelost = true,
disabled = true
}
GlobalSettings.defaultRespawns = {}
GlobalSettings.defaultRespawns[1] = {
supply = { dead=35*60, hangar=20*60, preparing=5*60},
patrol = { dead=38*60, hangar=2*60, preparing=2*60},
attack = { dead=38*60, hangar=2*60, preparing=2*60}
}
GlobalSettings.defaultRespawns[2] = {
supply = { dead=35*60, hangar=20*60, preparing=5*60},
patrol = { dead=38*60, hangar=2*60, preparing=2*60},
attack = { dead=38*60, hangar=2*60, preparing=2*60}
}
GlobalSettings.respawnTimers = {}
function GlobalSettings.resetDifficultyScaling()
GlobalSettings.respawnTimers[1] = {
supply = {
dead = GlobalSettings.defaultRespawns[1].supply.dead,
hangar = GlobalSettings.defaultRespawns[1].supply.hangar,
preparing = GlobalSettings.defaultRespawns[1].supply.preparing
},
patrol = {
dead = GlobalSettings.defaultRespawns[1].patrol.dead,
hangar = GlobalSettings.defaultRespawns[1].patrol.hangar,
preparing = GlobalSettings.defaultRespawns[1].patrol.preparing
},
attack = {
dead = GlobalSettings.defaultRespawns[1].attack.dead,
hangar = GlobalSettings.defaultRespawns[1].attack.hangar,
preparing = GlobalSettings.defaultRespawns[1].attack.preparing
}
}
GlobalSettings.respawnTimers[2] = {
supply = {
dead = GlobalSettings.defaultRespawns[2].supply.dead,
hangar = GlobalSettings.defaultRespawns[2].supply.hangar,
preparing = GlobalSettings.defaultRespawns[2].supply.preparing
},
patrol = {
dead = GlobalSettings.defaultRespawns[2].patrol.dead,
hangar = GlobalSettings.defaultRespawns[2].patrol.hangar,
preparing = GlobalSettings.defaultRespawns[2].patrol.preparing
},
attack = {
dead = GlobalSettings.defaultRespawns[2].attack.dead,
hangar = GlobalSettings.defaultRespawns[2].attack.hangar,
preparing = GlobalSettings.defaultRespawns[2].attack.preparing
}
}
end
function GlobalSettings.setDifficultyScaling(value, coalition)
GlobalSettings.resetDifficultyScaling()
for i,v in pairs(GlobalSettings.respawnTimers[coalition]) do
for i2,v2 in pairs(v) do
GlobalSettings.respawnTimers[coalition][i][i2] = math.floor(GlobalSettings.respawnTimers[coalition][i][i2] * value)
end
end
end
GlobalSettings.resetDifficultyScaling()
end
BattleCommander = {}
do
BattleCommander.zones = {}
BattleCommander.indexedZones = {}
BattleCommander.connections = {}
BattleCommander.accounts = { [1]=0, [2]=0} -- 1 = red coalition, 2 = blue coalition
BattleCommander.shops = {[1]={}, [2]={}}
BattleCommander.shopItems = {}
BattleCommander.monitorROE = {}
BattleCommander.playerContributions = {[1]={}, [2]={}}
BattleCommander.playerRewardsOn = false
BattleCommander.rewards = {}
BattleCommander.creditsCap = nil
BattleCommander.difficultyModifier = 0
BattleCommander.lastDiffChange = 0
function BattleCommander:new(savepath, updateFrequency, saveFrequency, difficulty) -- difficulty = {start = 1.4, min = -0.5, max = 0.5, escalation = 0.1, fade = 0.1, fadeTime = 30*60, coalition=1} --coalition 1:red 2:blue
local obj = {}
obj.saveFile = 'zoneCommander.lua'
if savepath then
obj.saveFile = savepath
end
if not updateFrequency then updateFrequency = 10 end
if not saveFrequency then saveFrequency = 60 end
obj.difficulty = difficulty
obj.updateFrequency = updateFrequency
obj.saveFrequency = saveFrequency
setmetatable(obj, self)
self.__index = self
return obj
end
--difficulty scaling
function BattleCommander:increaseDifficulty()
self.difficultyModifier = math.max(self.difficultyModifier-self.difficulty.escalation, self.difficulty.min)
GlobalSettings.setDifficultyScaling(self.difficulty.start + self.difficultyModifier, self.difficulty.coalition)
self.lastDiffChange = timer.getAbsTime()
env.info('increasing diff: '..self.difficultyModifier)
end
function BattleCommander:decreaseDifficulty()
self.difficultyModifier = math.min(self.difficultyModifier+self.difficulty.fade, self.difficulty.max)
GlobalSettings.setDifficultyScaling(self.difficulty.start + self.difficultyModifier,self.difficulty.coalition)
self.lastDiffChange = timer.getAbsTime()
env.info('decreasing diff: '..self.difficultyModifier)
end
--end difficulty scaling
-- shops and currency functions
function BattleCommander:registerShopItem(id, name, cost, action, altAction)
self.shopItems[id] = { name=name, cost=cost, action=action, altAction = altAction }
end
function BattleCommander:addShopItem(coalition, id, ammount)
local item = self.shopItems[id]
local sitem = self.shops[coalition][id]
if item then
if sitem then
if ammount == -1 then
sitem.stock = -1
else
sitem.stock = sitem.stock+ammount
end
else
self.shops[coalition][id] = { name=item.name, cost=item.cost, stock=ammount }
self:refreshShopMenuForCoalition(coalition)
end
end
end
function BattleCommander:removeShopItem(coalition, id)
self.shops[coalition][id] = nil
self:refreshShopMenuForCoalition(coalition)
end
function BattleCommander:addFunds(coalition, ammount)
local newAmmount = math.max(self.accounts[coalition] + ammount,0)
if self.creditsCap then
newAmmount = math.min(newAmmount, self.creditsCap)
end
self.accounts[coalition] = newAmmount
end
function BattleCommander:printShopStatus(coalition)
local text = 'Credits: '..self.accounts[coalition]
if self.creditsCap then
text = text..'/'..self.creditsCap
end
text = text..'\n'
local sorted = {}
for i,v in pairs(self.shops[coalition]) do table.insert(sorted,{i,v}) end
table.sort(sorted, function(a,b) return a[2].name < b[2].name end)
for i2,v2 in pairs(sorted) do
local i = v2[1]
local v = v2[2]
text = text..'\n[Cost: '..v.cost..'] '..v.name
if v.stock ~= -1 then
text = text..' [Available: '..v.stock..']'
end
end
if self.playerContributions[coalition] then
for i,v in pairs(self.playerContributions[coalition]) do
if v>0 then
text = text..'\n\nUnclaimed credits'
break
end
end
for i,v in pairs(self.playerContributions[coalition]) do
if v>0 then
text = text..'\n '..i..' ['..v..']'
end
end
end
trigger.action.outTextForCoalition(coalition, text, 10)
end
function BattleCommander:buyShopItem(coalition, id, alternateParams)
local item = self.shops[coalition][id]
if item then
if self.accounts[coalition] >= item.cost then
if item.stock == -1 or item.stock > 0 then
local success = true
local sitem = self.shopItems[id]
if alternateParams~=nil and type(sitem.altAction)=='function' then
success = sitem:altAction(alternateParams)
elseif type(sitem.action)=='function' then
success = sitem:action()
end
if success == true or success == nil then
self.accounts[coalition] = self.accounts[coalition] - item.cost
if item.stock > 0 then
item.stock = item.stock - 1
end
if item.stock == 0 then
self.shops[coalition][id] = nil
self:refreshShopMenuForCoalition(coalition)
end
trigger.action.outTextForCoalition(coalition, 'Bought ['..item.name..'] for '..item.cost..'\n'..self.accounts[coalition]..' credits remaining',5)
if item.stock == 0 then
trigger.action.outTextForCoalition(coalition, '['..item.name..'] went out of stock',5)
end
else
if type(success) == 'string' then
trigger.action.outTextForCoalition(coalition, success,5)
else
trigger.action.outTextForCoalition(coalition, 'Not available at the current time',5)
end
return success
end
else
trigger.action.outTextForCoalition(coalition,'Not available', 5)
end
else
trigger.action.outTextForCoalition(coalition,'Can not afford ['..item.name..']', 5)
end
end
end
function BattleCommander:refreshShopMenuForCoalition(coalition)
missionCommands.removeItemForCoalition(coalition, {[1]='Support'})
local shopmenu = missionCommands.addSubMenuForCoalition(coalition, 'Support')
local sub1
local count = 0
local sorted = {}
for i,v in pairs(self.shops[coalition]) do table.insert(sorted,{i,v}) end
table.sort(sorted, function(a,b) return a[2].name < b[2].name end)
for i2,v2 in pairs(sorted) do
local i = v2[1]
local v = v2[2]
count = count +1
if count<10 then
missionCommands.addCommandForCoalition(coalition, '['..v.cost..'] '..v.name, shopmenu, self.buyShopItem, self, coalition, i)
elseif count==10 then
sub1 = missionCommands.addSubMenuForCoalition(coalition, "More", shopmenu)
missionCommands.addCommandForCoalition(coalition, '['..v.cost..'] '..v.name, sub1, self.buyShopItem, self, coalition, i)
elseif count%9==1 then
sub1 = missionCommands.addSubMenuForCoalition(coalition, "More", sub1)
missionCommands.addCommandForCoalition(coalition, '['..v.cost..'] '..v.name, sub1, self.buyShopItem, self, coalition, i)
else
missionCommands.addCommandForCoalition(coalition, '['..v.cost..'] '..v.name, sub1, self.buyShopItem, self, coalition, i)
end
end
end
-- end shops and currency
function BattleCommander:addMonitoredROE(groupname)
table.insert(self.monitorROE, groupname)
end
function BattleCommander:checkROE(groupname)
local gr = Group.getByName(groupname)
if gr then
local controller = gr:getController()
if controller:hasTask() then
controller:setOption(0, 2) -- roe = open fire
else
controller:setOption(0, 4) -- roe = weapon hold
end
end
end
--targetzoneside = 1=red, 2=blue, 0=neutral, nil = all
function BattleCommander:showTargetZoneMenu(coalition, menuname, action, targetzoneside)
local executeAction = function(act, params)
local err = act(params.zone, params.menu)
if not err then
missionCommands.removeItemForCoalition(params.coalition, params.menu)
end
end
local menu = missionCommands.addSubMenuForCoalition(coalition, menuname)
local sub1
local zones = bc:getZones()
local count = 0
for i,v in ipairs(zones) do
if targetzoneside == nil or v.side == targetzoneside then
count = count + 1
if count<10 then
missionCommands.addCommandForCoalition(coalition, v.zone, menu, executeAction, action, {zone = v.zone, menu=menu, coalition=coalition})
elseif count==10 then
sub1 = missionCommands.addSubMenuForCoalition(coalition, "More", menu)
missionCommands.addCommandForCoalition(coalition, v.zone, sub1, executeAction, action, {zone = v.zone, menu=menu, coalition=coalition})
elseif count%9==1 then
sub1 = missionCommands.addSubMenuForCoalition(coalition, "More", sub1)
missionCommands.addCommandForCoalition(coalition, v.zone, sub1, executeAction, action, {zone = v.zone, menu=menu, coalition=coalition})
else
missionCommands.addCommandForCoalition(coalition, v.zone, sub1, executeAction, action, {zone = v.zone, menu=menu, coalition=coalition})
end
end
end
return menu
end
function BattleCommander:getRandomSurfaceUnitInZone(tgtzone, myside)
local zn = self:getZoneByName(tgtzone)
local selectedUnit = nil
local units = {}
for _,v in pairs(zn.built) do
local g = Group.getByName(v)
if g and g:getCoalition() ~= myside then
for _,unit in ipairs(g:getUnits()) do
table.insert(units, unit)
end
end
end
for _,v in ipairs(zn.groups) do
local g = Group.getByName(v.name)
if g and v.type == 'surface' and v.side ~= myside then
for _,unit in ipairs(g:getUnits()) do
table.insert(units, unit)
end
end
end
if #units > 0 then
return units[math.random(1, #units)]
end
end
function BattleCommander:moveToUnit(tgtunitname, groupname)
timer.scheduleFunction(function(params, time)
local group = Group.getByName(params.groupname)
local unit = Unit.getByName(params.tgtunitname)
if not group or not unit then return end -- do not recalculate route, either target or hunter stopped existing
local pos = unit:getPoint()
local cnt = group:getController()
local task = {
id = "Mission",
params = {
airborne = false,
route = {
points = {
[1] = {
type=AI.Task.WaypointType.TURNING_POINT,
action=AI.Task.TurnMethod.FLY_OVER_POINT,
speed = 100,
x = pos.x + math.random(-100,100),
y = pos.z + math.random(-100,100)
}