forked from ciribob/DCS-CSAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmist.lua
6116 lines (5261 loc) · 201 KB
/
mist.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
--[[
Links:
ED Forum Thread: http://forums.eagle.ru/showthread.php?t=98616
Github
Development: https://github.com/mrSkortch/MissionScriptingTools/tree/development
Official Release: https://github.com/mrSkortch/MissionScriptingTools/tree/master
]]
--MiST Mission Scripting Tools
mist = {}
-- don't change these
mist.majorVersion = 4
mist.minorVersion = 1
mist.build = 61
--------------------------------------------------------------------------------------------------------------
-- the main area
do
local coroutines = {}
local tempSpawnedUnits = {} -- birth events added here
local mistAddedObjects = {} -- mist.dynAdd unit data added here
local mistAddedGroups = {} -- mist.dynAdd groupdata added here
local writeGroups = {}
local lastUpdateTime = 0
local function update_alive_units() -- coroutine function
local lalive_units = mist.DBs.aliveUnits -- local references for faster execution
local lunits = mist.DBs.unitsByNum
local ldeepcopy = mist.utils.deepCopy
local lUnit = Unit
local lremovedAliveUnits = mist.DBs.removedAliveUnits
local updatedUnits = {}
if #lunits > 0 then
local units_per_run = math.ceil(#lunits/20)
if units_per_run < 5 then
units_per_run = 5
end
for i = 1, #lunits do
if lunits[i].category ~= 'static' then -- can't get statics with Unit.getByName :(
local unit = lUnit.getByName(lunits[i].unitName)
if unit then
--print('unit named ' .. lunits[i].unitName .. ' alive!')
local pos = unit:getPosition()
local newtbl = ldeepcopy(lunits[i])
if pos then
newtbl['pos'] = pos.p
end
newtbl['unit'] = unit
--newtbl['rt_id'] = unit.id_
lalive_units[unit.id_] = newtbl
updatedUnits[unit.id_] = true
end
end
if i%units_per_run == 0 then
--print('yielding at: ' .. tostring(i))
coroutine.yield()
--print('resuming at: ' .. tostring(i))
end
end
-- All units updated, remove any "alive" units that were not updated- they are dead!
for unit_id, unit in pairs(lalive_units) do
if not updatedUnits[unit_id] then
lremovedAliveUnits[unit_id] = unit
lalive_units[unit_id] = nil
end
end
end
end
local function dbUpdate(event)
local newTable = {}
newTable['startTime'] = 0
if type(event) == 'string' then -- if name of an object.
local newObject
local newType = 'group'
if Group.getByName(event) then
newObject = Group.getByName(event)
elseif StaticObject.getByName(event) then
newObject = StaticObject.getByName(event)
newType = 'static'
-- env.info('its static')
else
env.info('WTF')
return false
end
newTable.name = newObject:getName()
newTable.groupId = tonumber(newObject:getID())
newTable.groupName = newObject:getName()
local unitOneRef
if newType == 'static' then
unitOneRef = newObject
newTable.countryId = tonumber(newObject:getCountry())
newTable.coalitionId = tonumber(newObject:getCoalition())
newTable.category = 'static'
else
unitOneRef = newObject:getUnits()
newTable.countryId = tonumber(unitOneRef[1]:getCountry())
newTable.coalitionId = tonumber(unitOneRef[1]:getCoalition())
newTable.category = tonumber(newObject:getCategory())
end
for countryData, countryId in pairs(country.id) do
if newTable.country and string.upper(countryData) == string.upper(newTable.country) or countryId == newTable.countryId then
newTable['countryId'] = countryId
newTable['country'] = string.lower(countryData)
for coaData, coaId in pairs(coalition.side) do
if coaId == coalition.getCountryCoalition(countryId) then
newTable['coalition'] = string.lower(coaData)
end
end
end
end
for catData, catId in pairs(Unit.Category) do
if newType == 'group' and Group.getByName(newTable.groupName):isExist() then
if catId == Group.getByName(newTable.groupName):getCategory() then
newTable['category'] = string.lower(catData)
end
elseif newType == 'static' and StaticObject.getByName(newTable.groupName):isExist() then
if catId == StaticObject.getByName(newTable.groupName):getCategory() then
newTable['category'] = string.lower(catData)
end
end
end
local gfound = false
for index, data in pairs(mistAddedGroups) do
if mist.stringMatch(data.name, newTable.groupName) == true then
gfound = true
newTable.task = data.task
newTable.modulation = data.modulation
newTable.uncontrolled = data.uncontrolled
newTable.radioSet = data.radioSet
newTable.hidden = data.hidden
newTable.startTime = data.start_time
mistAddedGroups[index] = nil
end
end
if gfound == false then
newTable.uncontrolled = false
newTable.hidden = false
end
newTable.units = {}
if newType == 'group' then
for unitId, unitData in pairs(unitOneRef) do
newTable.units[unitId] = {}
newTable.units[unitId].unitName = unitData:getName()
newTable.units[unitId].x = mist.utils.round(unitData:getPosition().p.x)
newTable.units[unitId].y = mist.utils.round(unitData:getPosition().p.z)
newTable.units[unitId].point = {}
newTable.units[unitId].point.x = newTable.units[unitId].x
newTable.units[unitId].point.y = newTable.units[unitId].y
newTable.units[unitId].alt = mist.utils.round(unitData:getPosition().p.y)
newTable.units[unitId].speed = mist.vec.mag(unitData:getVelocity())
newTable.units[unitId].heading = mist.getHeading(unitData, true)
newTable.units[unitId].type = unitData:getTypeName()
newTable.units[unitId].unitId = tonumber(unitData:getID())
newTable.units[unitId].groupName = newTable.groupName
newTable.units[unitId].groupId = newTable.groupId
newTable.units[unitId].countryId = newTable.countryId
newTable.units[unitId].coalitionId = newTable.coalitionId
newTable.units[unitId].coalition = newTable.coalition
newTable.units[unitId].country = newTable.country
local found = false
for index, data in pairs(mistAddedObjects) do
if mist.stringMatch(data.name, newTable.units[unitId].unitName) == true then
found = true
newTable.units[unitId].livery_id = data.livery_id
newTable.units[unitId].skill = data.skill
newTable.units[unitId].alt_type = data.alt_type
newTable.units[unitId].callsign = data.callsign
newTable.units[unitId].psi = data.psi
mistAddedObjects[index] = nil
end
if found == false then
newTable.units[unitId].skill = "High"
newTable.units[unitId].alt_type = "BARO"
end
end
end
else -- its a static
newTable.category = 'static'
newTable.units[1] = {}
newTable.units[1].unitName = newObject:getName()
newTable.units[1].category = 'static'
newTable.units[1].x = mist.utils.round(newObject:getPosition().p.x)
newTable.units[1].y = mist.utils.round(newObject:getPosition().p.z)
newTable.units[1].point = {}
newTable.units[1].point.x = newTable.units[1].x
newTable.units[1].point.y = newTable.units[1].y
newTable.units[1].alt = mist.utils.round(newObject:getPosition().p.y)
newTable.units[1].heading = mist.getHeading(newObject, true)
newTable.units[1].type = newObject:getTypeName()
newTable.units[1].unitId = tonumber(newObject:getID())
newTable.units[1].groupName = newTable.name
newTable.units[1].groupId = newTable.groupId
newTable.units[1].countryId = newTable.countryId
newTable.units[1].country = newTable.country
newTable.units[1].coalitionId = newTable.coalitionId
newTable.units[1].coalition = newTable.coalition
if newObject:getCategory() == 6 and newObject:getCargoDisplayName() then
local mass = newObject:getCargoDisplayName()
mass = string.gsub(mass, ' ', '')
mass = string.gsub(mass, 'kg', '')
newTable.units[1].mass = tonumber(mass)
newTable.units[1].categoryStatic = 'Cargos'
newTable.units[1].canCargo = true
newTable.units[1].shape_name = 'ab-212_cargo'
end
----- search mist added objects for extra data if applicable
for index, data in pairs(mistAddedObjects) do
if mist.stringMatch(data.name, newTable.units[1].unitName) == true then
newTable.units[1].shape_name = data.shape_name -- for statics
newTable.units[1].livery_id = data.livery_id
newTable.units[1].airdromeId = data.airdromeId
newTable.units[1].mass = data.mass
newTable.units[1].canCargo = data.canCargo
newTable.units[1].categoryStatic = data.categoryStatic
newTable.units[1].type = 'cargo1'
mistAddedObjects[index] = nil
end
end
end
end
--mist.debug.writeData(mist.utils.serialize,{'msg', newTable}, timer.getAbsTime() ..'Group.lua')
newTable['timeAdded'] = timer.getAbsTime() -- only on the dynGroupsAdded table. For other reference, see start time
--mist.debug.dumpDBs()
--end
return newTable
end
local function checkSpawnedEvents()
if #tempSpawnedUnits > 0 then
local groupsToAdd = {}
local added = false
local ltemp = tempSpawnedUnits
local ltable = table
local updatesPerRun = math.ceil(#tempSpawnedUnits/20)
if updatesPerRun < 5 then
updatesPerRun = 5
end
for x = 1, #tempSpawnedUnits do
local spawnedObj = ltemp[x]
if spawnedObj and spawnedObj:isExist() then
local found = false
for name, val in pairs(groupsToAdd) do
if spawnedObj:getCategory() == 1 then -- normal groups
if mist.stringMatch(spawnedObj:getGroup():getName(), name) == true then
found = true
break
end
elseif spawnedObj:getCategory() == 3 or spawnedObj:getCategory() == 6 then -- static objects
if mist.stringMatch(spawnedObj:getName(), name) == true then
found = true
break
end
end
end
-- for some reason cargo objects are returning as category == 6.
if found == false then
added = true
if spawnedObj:getCategory() == 1 then -- normal groups
groupsToAdd[spawnedObj:getGroup():getName()] = true
elseif spawnedObj:getCategory() == 3 or spawnedObj:getCategory() == 6 then -- static objects
groupsToAdd[spawnedObj:getName()] = true
end
end
end
table.remove(ltemp, x)
if x%updatesPerRun == 0 then
coroutine.yield()
end
end
if added == true then
for groupName, val in pairs(groupsToAdd) do
local dataChanged = false
if mist.DBs.groupsByName[groupName] then
for _index, data in pairs(mist.DBs.groupsByName[groupName]) do
if data.unitName ~= spawnedObj:getName() and data.unitId ~= spawnedObj:getID() and data.type ~= spawnedObj:getTypeName() then
dataChanged = true
break
end
end
if dataChanged == false then
groupsToAdd[groupName] = false
end
end
if groupsToAdd[groupName] == true or not mist.DBs.groupsByName[groupName] then
writeGroups[#writeGroups + 1] = dbUpdate(groupName)
end
end
end
end
end
local function updateDBTables()
local i = 0
for index, newTable in pairs(writeGroups) do
i = i + 1
end
local savesPerRun = math.ceil(i/10)
if savesPerRun < 5 then
savesPerRun = 5
end
if i > 0 then
local ldeepCopy = mist.utils.deepCopy
for x = 1, i do
local newTable = writeGroups[x]
local mistCategory
if type(newTable.category) == 'string' then
mistCategory = string.lower(newTable.category)
end
if string.upper(newTable['category']) == 'GROUND_UNIT' then
mistCategory = 'vehicle'
newTable['category'] = mistCategory
elseif string.upper(newTable['category']) == 'AIRPLANE' then
mistCategory = 'plane'
newTable['category'] = mistCategory
elseif string.upper(newTable['category']) == 'HELICOPTER' then
mistCategory = 'helicopter'
newTable['category'] = mistCategory
elseif string.upper(newTable['category']) == 'SHIP' then
mistCategory = 'ship'
newTable['category'] = mistCategory
end
for newId, newUnitData in pairs(newTable.units) do
newUnitData.category = mistCategory
if newUnitData.unitId then
mist.DBs.unitsById[tonumber(newUnitData.unitId)] = ldeepCopy(newUnitData)
end
mist.DBs.unitsByName[newUnitData.unitName] = ldeepCopy(newUnitData)
mist.DBs.unitsByCat[mistCategory][#mist.DBs.unitsByCat[mistCategory] + 1] = ldeepCopy(newUnitData)
mist.DBs.unitsByNum[#mist.DBs.unitsByNum + 1] = ldeepCopy(newUnitData)
end
-- this is a really annoying DB to populate. Gotta create new tables in case its missing
if not mist.DBs.units[newTable.coalition] then
mist.DBs.units[newTable.coalition] = {}
end
if not mist.DBs.units[newTable.coalition][newTable.country] then
mist.DBs.units[newTable.coalition][(newTable.country)] = {}
mist.DBs.units[newTable.coalition][(newTable.country)]['countryId'] = newTable.countryId
end
if not mist.DBs.units[newTable.coalition][newTable.country][mistCategory] then
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] = {}
end
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][#mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] + 1] = ldeepCopy(newTable)
if newTable.groupId then
mist.DBs.groupsById[newTable.groupId] = ldeepCopy(newTable)
end
mist.DBs.groupsByName[newTable.name] = ldeepCopy(newTable)
mist.DBs.dynGroupsAdded[#mist.DBs.dynGroupsAdded + 1] = ldeepCopy(newTable)
writeGroups[x] = nil
if x%savesPerRun == 0 then
coroutine.yield()
end
end
if timer.getTime() > lastUpdateTime then
lastUpdateTime = timer.getTime()
end
end
end
local update_alive_units_counter = 0
local write_DB_table_counter = 0
local check_spawn_events_counter = 0
-- THE MAIN FUNCTION -- Accessed 100 times/sec.
mist.main = function()
timer.scheduleFunction(mist.main, {}, timer.getTime() + 0.01) --reschedule first in case of Lua error
----------------------------------------------------------------------------------------------------------
--area to add new stuff in
write_DB_table_counter = write_DB_table_counter + 1
if write_DB_table_counter == 10 then
write_DB_table_counter = 0
if not coroutines.updateDBTables then
coroutines['updateDBTables'] = coroutine.create(updateDBTables)
end
coroutine.resume(coroutines.updateDBTables)
if coroutine.status(coroutines.updateDBTables) == 'dead' then
coroutines.updateDBTables = nil
end
end
check_spawn_events_counter = check_spawn_events_counter + 1
if check_spawn_events_counter == 10 then
check_spawn_events_counter = 0
if not coroutines.checkSpawnedEvents then
coroutines['checkSpawnedEvents'] = coroutine.create(checkSpawnedEvents)
end
coroutine.resume(coroutines.checkSpawnedEvents)
if coroutine.status(coroutines.checkSpawnedEvents) == 'dead' then
coroutines.checkSpawnedEvents = nil
end
end
-----------------------------------------------------------------------------------------------------------
--updating alive units
update_alive_units_counter = update_alive_units_counter + 1
if update_alive_units_counter == 5 then
update_alive_units_counter = 0
if not coroutines.update_alive_units then
coroutines['update_alive_units'] = coroutine.create(update_alive_units)
end
coroutine.resume(coroutines.update_alive_units)
if coroutine.status(coroutines.update_alive_units) == 'dead' then
coroutines.update_alive_units = nil
end
end
mist.do_scheduled_functions()
end -- end of mist.main
--------------------------------------------
------------ mist dyn add stuff for coroutines
local mistGpId = 7000
local mistUnitId = 7000
local mistDynAddIndex = 1
mist.nextGroupId = 1
mist.nextUnitId = 1
mist.getNextUnitId = function()
mist.nextUnitId = mist.nextUnitId + 1
if mist.nextUnitId > 6900 then
mist.nextUnitId = 14000
end
return mist.nextUnitId
end
mist.getNextGroupId = function()
mist.nextGroupId = mist.nextGroupId + 1
if mist.nextGroupId > 6900 then
mist.nextGroupId = 14000
end
return mist.nextGroupId
end
mist.getLastDBUpdateTime = function()
return lastUpdateTime
end
local function groupSpawned(event)
if event.id == world.event.S_EVENT_BIRTH and timer.getTime0() < timer.getAbsTime()then -- dont need to add units spawned in at the start of the mission if mist is loaded in init line
table.insert(tempSpawnedUnits,(event.initiator))
end
end
mist.dynAddStatic = function(staticObj)
local newObj = {}
newObj.groupId = staticObj.groupId
newObj.category = staticObj.category
newObj.type = staticObj.type
newObj.unitId = staticObj.unitId
newObj.y = staticObj.y
newObj.x = staticObj.x
newObj.heading = staticObj.heading
newObj.name = staticObj.name
newObj.dead = staticObj.dead
newObj.country = staticObj.country
newObj.countryId = staticObj.countryId
newObj.clone = staticObj.clone
newObj.shape_name = staticObj.shape_name
newObj.canCargo = staticObj.canCargo
newObj.mass = staticObj.mass
newObj.categoryStatic = staticObj.categoryStatic
if staticObj.units then -- if its mist format
newObj.groupId = staticObj.units[1].groupId
newObj.category = staticObj.units[1].category
newObj.type = staticObj.units[1].type
newObj.unitId = staticObj.units[1].unitId
newObj.y = staticObj.units[1].y
newObj.x = staticObj.units[1].x
newObj.heading = staticObj.units[1].heading
newObj.name = staticObj.units[1].name
newObj.dead = staticObj.units[1].dead
newObj.country = staticObj.units[1].country
newObj.countryId = staticObj.units[1].countryId
newObj.shape_name = staticObj.units[1].shape_name
newObj.canCargo = staticObj.units[1].canCargo
newObj.mass = staticObj.units[1].mass
newObj.categoryStatic = staticObj.units[1].categoryStatic
end
if not newObj.country then
return false
end
local newCountry = newObj.country
if newObj.countryId then
newCountry = newObj.countryId
end
for countryId, countryName in pairs(country.name) do
if type(newObj.country) == 'string' then
if tostring(countryName) == string.upper(newObj.country) then
newCountry = countryName
end
elseif type(newObj.country) == 'number' then
if countryId == newObj.country then
newCountry = countryName
end
end
end
if newObj.clone or not newObj.groupId then
mistGpId = mistGpId + 1
newObj.groupId = mistGpId
end
if newObj.clone or not newObj.unitId then
mistUnitId = mistUnitId + 1
newObj.unitId = mistUnitId
end
if newObj.clone or not newObj.name then
mistDynAddIndex = mistDynAddIndex + 1
newObj.name = (country.name[newCountry] .. ' static ' .. mistDynAddIndex)
end
if not newObj.dead then
newObj.dead = false
end
if not newObj.heading then
newObj.heading = math.random(360)
end
if newObj.categoryStatic then
newObj.category = newObj.categoryStatic
end
if newObj.mass then
newObj.category = 'Cargos'
end
mistAddedObjects[#mistAddedObjects + 1] = mist.utils.deepCopy(newObj)
if newObj.x and newObj.y and newObj.type and type(newObj.x) == 'number' and type(newObj.y) == 'number' and type(newObj.type) == 'string' then
coalition.addStaticObject(country.id[newCountry], newObj)
return newObj
end
return false
end
mist.dynAdd = function(newGroup) -- same as coalition.add function in SSE. checks the passed data to see if its valid.
--Will generate groupId, groupName, unitId, and unitName if needed
--
--mist.debug.writeData(mist.utils.serialize,{'msg', newGroup}, 'newGroupOrig.lua')
local cntry = newGroup.country
if newGroup.countryId then
cntry = newGroup.countryId
end
local groupType = newGroup.category
local newCountry = ''
-- validate data
for countryId, countryName in pairs(country.name) do
if type(cntry) == 'string' then
cntry = cntry:gsub("%s+", "_")
if tostring(countryName) == string.upper(cntry) then
newCountry = countryName
end
elseif type(cntry) == 'number' then
if countryId == cntry then
newCountry = countryName
end
end
end
if newCountry == '' then
return false
end
local newCat = ''
for catName, catId in pairs(Unit.Category) do
if type(groupType) == 'string' then
if tostring(catName) == string.upper(groupType) then
newCat = catName
end
elseif type(groupType) == 'number' then
if catId == groupType then
newCat = catName
end
end
if catName == 'GROUND_UNIT' and (string.upper(groupType) == 'VEHICLE' or string.upper(groupType) == 'GROUND') then
newCat = 'GROUND_UNIT'
elseif catName == 'AIRPLANE' and string.upper(groupType) == 'PLANE' then
newCat = 'AIRPLANE'
end
end
local typeName
if newCat == 'GROUND_UNIT' then
typeName = ' gnd '
elseif newCat == 'AIRPLANE' then
typeName = ' air '
elseif newCat == 'HELICOPTER' then
typeName = ' hel '
elseif newCat == 'SHIP' then
typeName = ' shp '
elseif newCat == 'BUILDING' then
typeName = ' bld '
end
if newGroup.clone or not newGroup.groupId then
mistDynAddIndex = mistDynAddIndex + 1
mistGpId = mistGpId + 1
newGroup.groupId = mistGpId
end
if newGroup.groupName or newGroup.name then
if newGroup.groupName then
newGroup['name'] = newGroup.groupName
elseif newGroup.name then
newGroup['name'] = newGroup.name
end
end
if newGroup.clone and mist.DBs.groupsByName[newGroup.name] or not newGroup.name then
newGroup['name'] = tostring(tostring(country.name[cntry]) .. tostring(typeName) .. mistDynAddIndex)
end
if not newGroup.hidden then
newGroup.hidden = false
end
if not newGroup.visible then
newGroup.visible = false
end
if (newGroup.start_time and type(newGroup.start_time) ~= 'number') or not newGroup.start_time then
if newGroup.startTime then
newGroup.start_time = mist.utils.round(newGroup.startTime)
else
newGroup.start_time = 0
end
end
for unitIndex, unitData in pairs(newGroup.units) do
local originalName = newGroup.units[unitIndex].unitName or newGroup.units[unitIndex].name
if newGroup.clone or not unitData.unitId then
mistUnitId = mistUnitId + 1
newGroup.units[unitIndex]['unitId'] = mistUnitId
end
if newGroup.units[unitIndex].unitName or newGroup.units[unitIndex].name then
if newGroup.units[unitIndex].unitName then
newGroup.units[unitIndex].name = newGroup.units[unitIndex].unitName
elseif newGroup.units[unitIndex].name then
newGroup.units[unitIndex].name = newGroup.units[unitIndex].name
end
end
if newGroup.clone or not unitData.name then
newGroup.units[unitIndex].name = tostring(newGroup.name .. ' unit' .. unitIndex)
end
if not unitData.skill then
newGroup.units[unitIndex].skill = 'Random'
end
if not unitData.alt then
if newCat == 'AIRPLANE' then
newGroup.units[unitIndex].alt = 2000
newGroup.units[unitIndex].alt_type = 'RADIO'
newGroup.units[unitIndex].speed = 150
elseif newCat == 'HELICOPTER' then
newGroup.units[unitIndex].alt = 500
newGroup.units[unitIndex].alt_type = 'RADIO'
newGroup.units[unitIndex].speed = 60
else
--[[env.info('check height')
newGroup.units[unitIndex].alt = land.getHeight({x = newGroup.units[unitIndex].x, y = newGroup.units[unitIndex].y})
newGroup.units[unitIndex].alt_type = 'BARO']]
end
end
if newCat == 'AIRPLANE' or newCat == 'HELICOPTER' then
if newGroup.units[unitIndex].alt_type and newGroup.units[unitIndex].alt_type ~= 'BARO' or not newGroup.units[unitIndex].alt_type then
newGroup.units[unitIndex].alt_type = 'RADIO'
end
if not unitData.speed then
if newCat == 'AIRPLANE' then
newGroup.units[unitIndex].speed = 150
elseif newCat == 'HELICOPTER' then
newGroup.units[unitIndex].speed = 60
end
end
if not unitData.payload then
newGroup.units[unitIndex].payload = mist.getPayload(originalName)
end
end
mistAddedObjects[#mistAddedObjects + 1] = mist.utils.deepCopy(newGroup.units[unitIndex])
end
mistAddedGroups[#mistAddedGroups + 1] = mist.utils.deepCopy(newGroup)
if newGroup.route and not newGroup.route.points then
if not newGroup.route.points and newGroup.route[1] then
local copyRoute = newGroup.route
newGroup.route = {}
newGroup.route.points = copyRoute
end
end
newGroup.country = newCountry
--mist.debug.writeData(mist.utils.serialize,{'msg', newGroup}, 'newGroup.lua')
-- sanitize table
newGroup.groupName = nil
newGroup.clone = nil
newGroup.category = nil
newGroup.country = nil
newGroup.tasks = {}
for unitIndex, unitData in pairs(newGroup.units) do
newGroup.units[unitIndex].unitName = nil
end
coalition.addGroup(country.id[newCountry], Unit.Category[newCat], newGroup)
return newGroup
end
---------------------------------------------------------------------------------------------
--Modified Slmod task scheduler, superior to timer.scheduleFunction
local Tasks = {}
local task_id = 0
--[[ mist.scheduleFunction:
int id = mist.schedule_task(f function, vars table, t number, rep number, st number)
id - integer id of this function task
f - function to run
vars - table of vars for that function
t - time to run function
rep - time between repetitions of this function (OPTIONAL)
st - time when repetitions of this function will stop automatically (OPTIONAL)
]]
mist.scheduleFunction = function(f, vars, t, rep, st)
--verify correct types
assert(type(f) == 'function', 'variable 1, expected function, got ' .. type(f))
assert(type(vars) == 'table' or vars == nil, 'variable 2, expected table or nil, got ' .. type(f))
assert(type(t) == 'number', 'variable 3, expected number, got ' .. type(t))
assert(type(rep) == 'number' or rep == nil, 'variable 4, expected number or nil, got ' .. type(rep))
assert(type(st) == 'number' or st == nil, 'variable 5, expected number or nil, got ' .. type(st))
if not vars then
vars = {}
end
task_id = task_id + 1
table.insert(Tasks, {f = f, vars = vars, t = t, rep = rep, st = st, id = task_id})
return task_id
end
-- removes a scheduled function based on the function's id. returns true if successful, false if not successful.
mist.removeFunction = function(id)
local i = 1
while i <= #Tasks do
if Tasks[i].id == id then
table.remove(Tasks, i)
else
i = i + 1
end
end
end
--------------------------------------------------------------------------------------------------------------------
-- not intended for users to use this function.
mist.do_scheduled_functions = function()
local i = 1
while i <= #Tasks do
if not Tasks[i].rep then -- not a repeated process
if Tasks[i].t <= timer.getTime() then
local Task = Tasks[i] -- local reference
table.remove(Tasks, i)
local err, errmsg = pcall(Task.f, unpack(Task.vars, 1, table.maxn(Task.vars)))
if not err then
env.info('mist.scheduleFunction, error in scheduled function: ' .. errmsg)
end
--Task.f(unpack(Task.vars, 1, table.maxn(Task.vars))) -- do the task, do not increment i
else
i = i + 1
end
else
if Tasks[i].st and Tasks[i].st <= timer.getTime() then --if a stoptime was specified, and the stop time exceeded
table.remove(Tasks, i) -- stop time exceeded, do not execute, do not increment i
elseif Tasks[i].t <= timer.getTime() then
local Task = Tasks[i] -- local reference
Task.t = timer.getTime() + Task.rep --schedule next run
local err, errmsg = pcall(Task.f, unpack(Task.vars, 1, table.maxn(Task.vars)))
if not err then
env.info('mist.scheduleFunction, error in scheduled function: ' .. errmsg)
end
--Tasks[i].f(unpack(Tasks[i].vars, 1, table.maxn(Tasks[i].vars))) -- do the task
i = i + 1
else
i = i + 1
end
end
end
end
local idNum = 0
--Simplified event handler
mist.addEventHandler = function(f) --id is optional!
local handler = {}
idNum = idNum + 1
handler.id = idNum
handler.f = f
handler.onEvent = function(self, event)
self.f(event)
end
world.addEventHandler(handler)
return handler.id
end
mist.removeEventHandler = function(id)
for key, handler in pairs(world.eventHandlers) do
if handler.id and handler.id == id then
world.eventHandlers[key] = nil
return true
end
end
return false
end
mist.addEventHandler(groupSpawned)
-- mist.scheduleFunction(checkSpawnedEvents, {}, timer.getTime() + 5, 1)
end
------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
-- Utils- conversion, Lua utils, etc.
mist.utils = {}
mist.utils.toDegree = function(angle)
return angle*180/math.pi
end
mist.utils.toRadian = function(angle)
return angle*math.pi/180
end
mist.utils.metersToNM = function(meters)
return meters/1852
end
mist.utils.metersToFeet = function(meters)
return meters/0.3048
end
mist.utils.NMToMeters = function(NM)
return NM*1852
end
mist.utils.feetToMeters = function(feet)
return feet*0.3048
end
mist.utils.mpsToKnots = function(mps)
return mps*3600/1852
end
mist.utils.mpsToKmph = function(mps)
return mps*3.6
end
mist.utils.knotsToMps = function(knots)
return knots*1852/3600
end
mist.utils.kmphToMps = function(kmph)
return kmph/3.6
end
function mist.utils.makeVec2(Vec3)
if Vec3.z then
return {x = Vec3.x, y = Vec3.z}
else
return {x = Vec3.x, y = Vec3.y} -- it was actually already vec2.
end
end
function mist.utils.makeVec3(Vec2, y)
if not Vec2.z then
if Vec2.alt and not y then
y = Vec2.alt
elseif not y then
y = 0
end
return {x = Vec2.x, y = y, z = Vec2.y}
else
return {x = Vec2.x, y = Vec2.y, z = Vec2.z} -- it was already Vec3, actually.
end
end
function mist.utils.makeVec3GL(Vec2, offset)
local adj = offset or 0
if not Vec2.z then
return {x = Vec2.x, y = (land.getHeight(Vec2) + adj), z = Vec2.y}
else
return {x = Vec2.x, y = (land.getHeight({x = Vec2.x, y = Vec2.z}) + adj), z = Vec2.z}
end
end
mist.utils.zoneToVec3 = function(zone)
local new = {}
if type(zone) == 'table' and zone.point then
new.x = zone.point.x
new.y = zone.point.y
new.z = zone.point.z
return new
elseif type(zone) == 'string' then
zone = trigger.misc.getZone(zone)
if zone then
new.x = zone.point.x
new.y = zone.point.y
new.z = zone.point.z
return new
end
end
end
-- gets heading-error corrected direction from point along vector vec.
function mist.utils.getDir(vec, point)
local dir = math.atan2(vec.z, vec.x)
if point then
dir = dir + mist.getNorthCorrection(point)
end
if dir < 0 then
dir = dir + 2*math.pi -- put dir in range of 0 to 2*pi
end
return dir
end
-- gets distance in meters between two points (2 dimensional)
function mist.utils.get2DDist(point1, point2)
point1 = mist.utils.makeVec3(point1)
point2 = mist.utils.makeVec3(point2)
return mist.vec.mag({x = point1.x - point2.x, y = 0, z = point1.z - point2.z})
end
-- gets distance in meters between two points (3 dimensional)
function mist.utils.get3DDist(point1, point2)
return mist.vec.mag({x = point1.x - point2.x, y = point1.y - point2.y, z = point1.z - point2.z})
end