forked from ciribob/DCS-CTLD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mist.lua
6822 lines (6018 loc) · 220 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
--[[--
MIST Mission Scripting Tools.
## Description:
MIssion Scripting Tools (MIST) is a collection of Lua functions
and databases that is intended to be a supplement to the standard
Lua functions included in the simulator scripting engine.
MIST functions and databases provide ready-made solutions to many common
scripting tasks and challenges, enabling easier scripting and saving
mission scripters time. The table mist.flagFuncs contains a set of
Lua functions (that are similar to Slmod functions) that do not
require detailed Lua knowledge to use.
However, the majority of MIST does require knowledge of the Lua language,
and, if you are going to utilize these components of MIST, it is necessary
that you read the Simulator Scripting Engine guide on the official ED wiki.
## Links:
ED Forum Thread: <http://forums.eagle.ru/showthread.php?t=98616>
##Github:
Development <https://github.com/mrSkortch/MissionScriptingTools>
Official Releases <https://github.com/mrSkortch/MissionScriptingTools/tree/master>
@script MIST
@author Speed
@author Grimes
@author lukrop
]]
mist = {}
-- don't change these
mist.majorVersion = 4
mist.minorVersion = 3
mist.build = 74
-- forward declaration of log shorthand
local log
do -- the main scope
local coroutines = {}
local tempSpawnedUnits = {} -- birth events added here
local tempSpawnedGroups = {}
local tempSpawnGroupsCounter = 0
local mistAddedObjects = {} -- mist.dynAdd unit data added here
local mistAddedGroups = {} -- mist.dynAdd groupdata added here
local writeGroups = {}
local lastUpdateTime = 0
local updateAliveUnitsCounter = 0
local updateTenthSecond = 0
local mistGpId = 7000
local mistUnitId = 7000
local mistDynAddIndex = {[' air '] = 0, [' hel '] = 0, [' gnd '] = 0, [' bld '] = 0, [' static '] = 0, [' shp '] = 0}
local scheduledTasks = {}
local taskId = 0
local idNum = 0
mist.nextGroupId = 1
mist.nextUnitId = 1
local dbLog
local function initDBs() -- mist.DBs scope
mist.DBs = {}
mist.DBs.missionData = {}
if env.mission then
mist.DBs.missionData.startTime = env.mission.start_time
mist.DBs.missionData.theatre = env.mission.theatre
mist.DBs.missionData.version = env.mission.version
mist.DBs.missionData.files = {}
if type(env.mission.resourceCounter) == 'table' then
for fIndex, fData in pairs (env.mission.resourceCounter) do
mist.DBs.missionData.files[#mist.DBs.missionData.files + 1] = mist.utils.deepCopy(fIndex)
end
end
-- if we add more coalition specific data then bullsye should be categorized by coaliton. For now its just the bullseye table
mist.DBs.missionData.bullseye = {red = {}, blue = {}}
mist.DBs.missionData.bullseye.red.x = env.mission.coalition.red.bullseye.x --should it be point.x?
mist.DBs.missionData.bullseye.red.y = env.mission.coalition.red.bullseye.y
mist.DBs.missionData.bullseye.blue.x = env.mission.coalition.blue.bullseye.x
mist.DBs.missionData.bullseye.blue.y = env.mission.coalition.blue.bullseye.y
end
mist.DBs.zonesByName = {}
mist.DBs.zonesByNum = {}
if env.mission.triggers and env.mission.triggers.zones then
for zone_ind, zone_data in pairs(env.mission.triggers.zones) do
if type(zone_data) == 'table' then
local zone = mist.utils.deepCopy(zone_data)
zone.point = {} -- point is used by SSE
zone.point.x = zone_data.x
zone.point.y = 0
zone.point.z = zone_data.y
mist.DBs.zonesByName[zone_data.name] = zone
mist.DBs.zonesByNum[#mist.DBs.zonesByNum + 1] = mist.utils.deepCopy(zone) --[[deepcopy so that the zone in zones_by_name and the zone in
zones_by_num se are different objects.. don't want them linked.]]
end
end
end
mist.DBs.navPoints = {}
mist.DBs.units = {}
--Build mist.db.units and mist.DBs.navPoints
for coa_name, coa_data in pairs(env.mission.coalition) do
if (coa_name == 'red' or coa_name == 'blue') and type(coa_data) == 'table' then
mist.DBs.units[coa_name] = {}
-- build nav points DB
mist.DBs.navPoints[coa_name] = {}
if coa_data.nav_points then --navpoints
--mist.debug.writeData (mist.utils.serialize,{'NavPoints',coa_data.nav_points}, 'NavPoints.txt')
for nav_ind, nav_data in pairs(coa_data.nav_points) do
if type(nav_data) == 'table' then
mist.DBs.navPoints[coa_name][nav_ind] = mist.utils.deepCopy(nav_data)
mist.DBs.navPoints[coa_name][nav_ind].name = nav_data.callsignStr -- name is a little bit more self-explanatory.
mist.DBs.navPoints[coa_name][nav_ind].point = {} -- point is used by SSE, support it.
mist.DBs.navPoints[coa_name][nav_ind].point.x = nav_data.x
mist.DBs.navPoints[coa_name][nav_ind].point.y = 0
mist.DBs.navPoints[coa_name][nav_ind].point.z = nav_data.y
end
end
end
if coa_data.country then --there is a country table
for cntry_id, cntry_data in pairs(coa_data.country) do
local countryName = string.lower(cntry_data.name)
mist.DBs.units[coa_name][countryName] = {}
mist.DBs.units[coa_name][countryName].countryId = cntry_data.id
if type(cntry_data) == 'table' then --just making sure
for obj_type_name, obj_type_data in pairs(cntry_data) do
if obj_type_name == "helicopter" or obj_type_name == "ship" or obj_type_name == "plane" or obj_type_name == "vehicle" or obj_type_name == "static" then --should be an unncessary check
local category = obj_type_name
if ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then --there's a group!
mist.DBs.units[coa_name][countryName][category] = {}
for group_num, group_data in pairs(obj_type_data.group) do
if group_data and group_data.units and type(group_data.units) == 'table' then --making sure again- this is a valid group
mist.DBs.units[coa_name][countryName][category][group_num] = {}
local groupName = group_data.name
if env.mission.version > 7 then
groupName = env.getValueDictByKey(groupName)
end
mist.DBs.units[coa_name][countryName][category][group_num].groupName = groupName
mist.DBs.units[coa_name][countryName][category][group_num].groupId = group_data.groupId
mist.DBs.units[coa_name][countryName][category][group_num].category = category
mist.DBs.units[coa_name][countryName][category][group_num].coalition = coa_name
mist.DBs.units[coa_name][countryName][category][group_num].country = countryName
mist.DBs.units[coa_name][countryName][category][group_num].countryId = cntry_data.id
mist.DBs.units[coa_name][countryName][category][group_num].startTime = group_data.start_time
mist.DBs.units[coa_name][countryName][category][group_num].task = group_data.task
mist.DBs.units[coa_name][countryName][category][group_num].hidden = group_data.hidden
mist.DBs.units[coa_name][countryName][category][group_num].units = {}
mist.DBs.units[coa_name][countryName][category][group_num].radioSet = group_data.radioSet
mist.DBs.units[coa_name][countryName][category][group_num].uncontrolled = group_data.uncontrolled
mist.DBs.units[coa_name][countryName][category][group_num].frequency = group_data.frequency
mist.DBs.units[coa_name][countryName][category][group_num].modulation = group_data.modulation
for unit_num, unit_data in pairs(group_data.units) do
local units_tbl = mist.DBs.units[coa_name][countryName][category][group_num].units --pointer to the units table for this group
units_tbl[unit_num] = {}
if env.mission.version > 7 then
units_tbl[unit_num].unitName = env.getValueDictByKey(unit_data.name)
else
units_tbl[unit_num].unitName = unit_data.name
end
units_tbl[unit_num].type = unit_data.type
units_tbl[unit_num].skill = unit_data.skill --will be nil for statics
units_tbl[unit_num].unitId = unit_data.unitId
units_tbl[unit_num].category = category
units_tbl[unit_num].coalition = coa_name
units_tbl[unit_num].country = countryName
units_tbl[unit_num].countryId = cntry_data.id
units_tbl[unit_num].heading = unit_data.heading
units_tbl[unit_num].playerCanDrive = unit_data.playerCanDrive
units_tbl[unit_num].alt = unit_data.alt
units_tbl[unit_num].alt_type = unit_data.alt_type
units_tbl[unit_num].speed = unit_data.speed
units_tbl[unit_num].livery_id = unit_data.livery_id
if unit_data.point then --ME currently does not work like this, but it might one day
units_tbl[unit_num].point = unit_data.point
else
units_tbl[unit_num].point = {}
units_tbl[unit_num].point.x = unit_data.x
units_tbl[unit_num].point.y = unit_data.y
end
units_tbl[unit_num].x = unit_data.x
units_tbl[unit_num].y = unit_data.y
units_tbl[unit_num].callsign = unit_data.callsign
units_tbl[unit_num].onboard_num = unit_data.onboard_num
units_tbl[unit_num].hardpoint_racks = unit_data.hardpoint_racks
units_tbl[unit_num].psi = unit_data.psi
units_tbl[unit_num].groupName = groupName
units_tbl[unit_num].groupId = group_data.groupId
if unit_data.AddPropAircraft then
units_tbl[unit_num].AddPropAircraft = unit_data.AddPropAircraft
end
if category == 'static' then
units_tbl[unit_num].categoryStatic = unit_data.category
units_tbl[unit_num].shape_name = unit_data.shape_name
if unit_data.mass then
units_tbl[unit_num].mass = unit_data.mass
end
if unit_data.canCargo then
units_tbl[unit_num].canCargo = unit_data.canCargo
end
end
end --for unit_num, unit_data in pairs(group_data.units) do
end --if group_data and group_data.units then
end --for group_num, group_data in pairs(obj_type_data.group) do
end --if ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then
end --if obj_type_name == "helicopter" or obj_type_name == "ship" or obj_type_name == "plane" or obj_type_name == "vehicle" or obj_type_name == "static" then
end --for obj_type_name, obj_type_data in pairs(cntry_data) do
end --if type(cntry_data) == 'table' then
end --for cntry_id, cntry_data in pairs(coa_data.country) do
end --if coa_data.country then --there is a country table
end --if coa_name == 'red' or coa_name == 'blue' and type(coa_data) == 'table' then
end --for coa_name, coa_data in pairs(mission.coalition) do
mist.DBs.unitsByName = {}
mist.DBs.unitsById = {}
mist.DBs.unitsByCat = {}
mist.DBs.unitsByCat.helicopter = {} -- adding default categories
mist.DBs.unitsByCat.plane = {}
mist.DBs.unitsByCat.ship = {}
mist.DBs.unitsByCat.static = {}
mist.DBs.unitsByCat.vehicle = {}
mist.DBs.unitsByNum = {}
mist.DBs.groupsByName = {}
mist.DBs.groupsById = {}
mist.DBs.humansByName = {}
mist.DBs.humansById = {}
mist.DBs.dynGroupsAdded = {} -- will be filled by mist.dbUpdate from dynamically spawned groups
mist.DBs.activeHumans = {}
mist.DBs.aliveUnits = {} -- will be filled in by the "updateAliveUnits" coroutine in mist.main.
mist.DBs.removedAliveUnits = {} -- will be filled in by the "updateAliveUnits" coroutine in mist.main.
mist.DBs.const = {}
-- not accessible by SSE, must use static list :-/
mist.DBs.const.callsigns = {
['NATO'] = {
['rules'] = {
['groupLimit'] = 9,
},
['AWACS'] = {
['Overlord'] = 1,
['Magic'] = 2,
['Wizard'] = 3,
['Focus'] = 4,
['Darkstar'] = 5,
},
['TANKER'] = {
['Texaco'] = 1,
['Arco'] = 2,
['Shell'] = 3,
},
['JTAC'] = {
['Axeman'] = 1,
['Darknight'] = 2,
['Warrior'] = 3,
['Pointer'] = 4,
['Eyeball'] = 5,
['Moonbeam'] = 6,
['Whiplash'] = 7,
['Finger'] = 8,
['Pinpoint'] = 9,
['Ferret'] = 10,
['Shaba'] = 11,
['Playboy'] = 12,
['Hammer'] = 13,
['Jaguar'] = 14,
['Deathstar'] = 15,
['Anvil'] = 16,
['Firefly'] = 17,
['Mantis'] = 18,
['Badger'] = 19,
},
['aircraft'] = {
['Enfield'] = 1,
['Springfield'] = 2,
['Uzi'] = 3,
['Colt'] = 4,
['Dodge'] = 5,
['Ford'] = 6,
['Chevy'] = 7,
['Pontiac'] = 8,
},
['unique'] = {
['A10'] = {
['Hawg'] = 9,
['Boar'] = 10,
['Pig'] = 11,
['Tusk'] = 12,
['rules'] = {
['canUseAircraft'] = true,
['appliesTo'] = {
'A-10C',
'A-10A',
},
},
},
},
},
}
mist.DBs.const.shapeNames = {
["Landmine"] = "landmine",
["FARP CP Blindage"] = "kp_ug",
["Subsidiary structure C"] = "saray-c",
["Barracks 2"] = "kazarma2",
["Small house 2C"] = "dom2c",
["Military staff"] = "aviashtab",
["Tech hangar A"] = "ceh_ang_a",
["Oil derrick"] = "neftevyshka",
["Tech combine"] = "kombinat",
["Garage B"] = "garage_b",
["Airshow_Crowd"] = "Crowd1",
["Hangar A"] = "angar_a",
["Repair workshop"] = "tech",
["Subsidiary structure D"] = "saray-d",
["FARP Ammo Dump Coating"] = "SetkaKP",
["Small house 1C area"] = "dom2c-all",
["Tank 2"] = "airbase_tbilisi_tank_01",
["Boiler-house A"] = "kotelnaya_a",
["Workshop A"] = "tec_a",
["Small werehouse 1"] = "s1",
["Garage small B"] = "garagh-small-b",
["Small werehouse 4"] = "s4",
["Shop"] = "magazin",
["Subsidiary structure B"] = "saray-b",
["FARP Fuel Depot"] = "GSM Rus",
["Coach cargo"] = "wagon-gruz",
["Electric power box"] = "tr_budka",
["Tank 3"] = "airbase_tbilisi_tank_02",
["Red_Flag"] = "H-flag_R",
["Container red 3"] = "konteiner_red3",
["Garage A"] = "garage_a",
["Hangar B"] = "angar_b",
["Black_Tyre"] = "H-tyre_B",
["Cafe"] = "stolovaya",
["Restaurant 1"] = "restoran1",
["Subsidiary structure A"] = "saray-a",
["Container white"] = "konteiner_white",
["Warehouse"] = "sklad",
["Tank"] = "bak",
["Railway crossing B"] = "pereezd_small",
["Subsidiary structure F"] = "saray-f",
["Farm A"] = "ferma_a",
["Small werehouse 3"] = "s3",
["Water tower A"] = "wodokachka_a",
["Railway station"] = "r_vok_sd",
["Coach a tank blue"] = "wagon-cisterna_blue",
["Supermarket A"] = "uniwersam_a",
["Coach a platform"] = "wagon-platforma",
["Garage small A"] = "garagh-small-a",
["TV tower"] = "tele_bash",
["Comms tower M"] = "tele_bash_m",
["Small house 1A"] = "domik1a",
["Farm B"] = "ferma_b",
["GeneratorF"] = "GeneratorF",
["Cargo1"] = "ab-212_cargo",
["Container red 2"] = "konteiner_red2",
["Subsidiary structure E"] = "saray-e",
["Coach a passenger"] = "wagon-pass",
["Black_Tyre_WF"] = "H-tyre_B_WF",
["Electric locomotive"] = "elektrowoz",
["Shelter"] = "ukrytie",
["Coach a tank yellow"] = "wagon-cisterna_yellow",
["Railway crossing A"] = "pereezd_big",
[".Ammunition depot"] = "SkladC",
["Small werehouse 2"] = "s2",
["Windsock"] = "H-Windsock_RW",
["Shelter B"] = "ukrytie_b",
["Fuel tank"] = "toplivo-bak",
["Locomotive"] = "teplowoz",
[".Command Center"] = "ComCenter",
["Pump station"] = "nasos",
["Black_Tyre_RF"] = "H-tyre_B_RF",
["Coach cargo open"] = "wagon-gruz-otkr",
["Subsidiary structure 3"] = "hozdomik3",
["FARP Tent"] = "PalatkaB",
["White_Tyre"] = "H-tyre_W",
["Subsidiary structure G"] = "saray-g",
["Container red 1"] = "konteiner_red1",
["Small house 1B area"] = "domik1b-all",
["Subsidiary structure 1"] = "hozdomik1",
["Container brown"] = "konteiner_brown",
["Small house 1B"] = "domik1b",
["Subsidiary structure 2"] = "hozdomik2",
["Chemical tank A"] = "him_bak_a",
["WC"] = "WC",
["Small house 1A area"] = "domik1a-all",
["White_Flag"] = "H-Flag_W",
["Airshow_Cone"] = "Comp_cone",
}
-- create mist.DBs.oldAliveUnits
-- do
-- local intermediate_alive_units = {} -- between 0 and 0.5 secs old
-- local function make_old_alive_units() -- called every 0.5 secs, makes the old_alive_units DB which is just a copy of alive_units that is 0.5 to 1 sec old
-- if intermediate_alive_units then
-- mist.DBs.oldAliveUnits = mist.utils.deepCopy(intermediate_alive_units)
-- end
-- intermediate_alive_units = mist.utils.deepCopy(mist.DBs.aliveUnits)
-- timer.scheduleFunction(make_old_alive_units, nil, timer.getTime() + 0.5)
-- end
-- make_old_alive_units()
-- end
--Build DBs
for coa_name, coa_data in pairs(mist.DBs.units) do
for cntry_name, cntry_data in pairs(coa_data) do
for category_name, category_data in pairs(cntry_data) do
if type(category_data) == 'table' then
for group_ind, group_data in pairs(category_data) do
if type(group_data) == 'table' and group_data.units and type(group_data.units) == 'table' and #group_data.units > 0 then -- OCD paradigm programming
mist.DBs.groupsByName[group_data.groupName] = mist.utils.deepCopy(group_data)
mist.DBs.groupsById[group_data.groupId] = mist.utils.deepCopy(group_data)
for unit_ind, unit_data in pairs(group_data.units) do
mist.DBs.unitsByName[unit_data.unitName] = mist.utils.deepCopy(unit_data)
mist.DBs.unitsById[unit_data.unitId] = mist.utils.deepCopy(unit_data)
mist.DBs.unitsByCat[unit_data.category] = mist.DBs.unitsByCat[unit_data.category] or {} -- future-proofing against new categories...
table.insert(mist.DBs.unitsByCat[unit_data.category], mist.utils.deepCopy(unit_data))
dbLog:info('inserting $1', unit_data.unitName)
table.insert(mist.DBs.unitsByNum, mist.utils.deepCopy(unit_data))
if unit_data.skill and (unit_data.skill == "Client" or unit_data.skill == "Player") then
mist.DBs.humansByName[unit_data.unitName] = mist.utils.deepCopy(unit_data)
mist.DBs.humansById[unit_data.unitId] = mist.utils.deepCopy(unit_data)
--if Unit.getByName(unit_data.unitName) then
-- mist.DBs.activeHumans[unit_data.unitName] = mist.utils.deepCopy(unit_data)
-- mist.DBs.activeHumans[unit_data.unitName].playerName = Unit.getByName(unit_data.unitName):getPlayerName()
--end
end
end
end
end
end
end
end
end
--DynDBs
mist.DBs.MEunits = mist.utils.deepCopy(mist.DBs.units)
mist.DBs.MEunitsByName = mist.utils.deepCopy(mist.DBs.unitsByName)
mist.DBs.MEunitsById = mist.utils.deepCopy(mist.DBs.unitsById)
mist.DBs.MEunitsByCat = mist.utils.deepCopy(mist.DBs.unitsByCat)
mist.DBs.MEunitsByNum = mist.utils.deepCopy(mist.DBs.unitsByNum)
mist.DBs.MEgroupsByName = mist.utils.deepCopy(mist.DBs.groupsByName)
mist.DBs.MEgroupsById = mist.utils.deepCopy(mist.DBs.groupsById)
mist.DBs.deadObjects = {}
do
local mt = {}
function mt.__newindex(t, key, val)
local original_key = key --only for duplicate runtime IDs.
local key_ind = 1
while mist.DBs.deadObjects[key] do
dbLog:warn('duplicate runtime id of previously dead object key: $1', key)
key = tostring(original_key) .. ' #' .. tostring(key_ind)
key_ind = key_ind + 1
end
if mist.DBs.aliveUnits and mist.DBs.aliveUnits[val.object.id_] then
--dbLog:info('object found in alive_units')
val.objectData = mist.utils.deepCopy(mist.DBs.aliveUnits[val.object.id_])
local pos = Object.getPosition(val.object)
if pos then
val.objectPos = pos.p
end
val.objectType = mist.DBs.aliveUnits[val.object.id_].category
elseif mist.DBs.removedAliveUnits and mist.DBs.removedAliveUnits[val.object.id_] then -- it didn't exist in alive_units, check old_alive_units
--dbLog:info('object found in old_alive_units')
val.objectData = mist.utils.deepCopy(mist.DBs.removedAliveUnits[val.object.id_])
local pos = Object.getPosition(val.object)
if pos then
val.objectPos = pos.p
end
val.objectType = mist.DBs.removedAliveUnits[val.object.id_].category
else --attempt to determine if static object...
--dbLog:info('object not found in alive units or old alive units')
local pos = Object.getPosition(val.object)
if pos then
local static_found = false
for ind, static in pairs(mist.DBs.unitsByCat.static) do
if ((pos.p.x - static.point.x)^2 + (pos.p.z - static.point.y)^2)^0.5 < 0.1 then --really, it should be zero...
dbLog:info('correlated dead static object to position')
val.objectData = static
val.objectPos = pos.p
val.objectType = 'static'
static_found = true
break
end
end
if not static_found then
val.objectPos = pos.p
val.objectType = 'building'
end
else
val.objectType = 'unknown'
end
end
rawset(t, key, val)
end
setmetatable(mist.DBs.deadObjects, mt)
end
do -- mist unitID funcs
for id, idData in pairs(mist.DBs.unitsById) do
if idData.unitId > mist.nextUnitId then
mist.nextUnitId = mist.utils.deepCopy(idData.unitId)
end
if idData.groupId > mist.nextGroupId then
mist.nextGroupId = mist.utils.deepCopy(idData.groupId)
end
end
end
end
local function updateAliveUnits() -- 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
--dbLog:info("unit named $1 alive!", lunits[i].unitName) -- spammy
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
coroutine.yield()
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, objType)
dbLog:info('dbUpdate')
local newTable = {}
newTable.startTime = 0
if type(event) == 'string' then -- if name of an object.
local newObject
if Group.getByName(event) then
newObject = Group.getByName(event)
elseif StaticObject.getByName(event) then
newObject = StaticObject.getByName(event)
-- log:info('its static')
else
log:warn('$1 is not a Unit or Static Object. This should not be possible', event)
return false
end
newTable.name = newObject:getName()
newTable.groupId = tonumber(newObject:getID())
newTable.groupName = newObject:getName()
local unitOneRef
if objType == '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 objType == 'group' and Group.getByName(newTable.groupName):isExist() then
if catId == Group.getByName(newTable.groupName):getCategory() then
newTable.category = string.lower(catData)
end
elseif objType == '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 objType == '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
dbLog:info('endDbUpdate')
return newTable
end
--[[DB update code... FRACK. I need to refactor some of it.
The problem is that the DBs need to account better for shared object names. Needs to write over some data and outright remove other.
If groupName is used then entire group needs to be rewritten
what to do with old groups units DB entries?. Names cant be assumed to be the same.
-- new spawn event check.
-- event handler filters everything into groups: tempSpawnedGroups
-- this function then checks DBs to see if data has changed
]]
local function checkSpawnedEventsNew()
if tempSpawnGroupsCounter > 0 then
--[[local updatesPerRun = math.ceil(#tempSpawnedGroupsCounter/20)
if updatesPerRun < 5 then
updatesPerRun = 5
end]]
dbLog:info('iterate')
for name, gType in pairs(tempSpawnedGroups) do
dbLog:info(name)
local updated = false
if mist.DBs.groupsByName[name] then
-- first check group level properties, groupId, countryId, coalition
dbLog:info('Found in DBs, check if updated')
local dbTable = mist.DBs.groupsByName[name]
dbLog:info(dbTable)
if gType ~= 'static' then
dbLog:info('Not static')
local _g = Group.getByName(name)
local _u = _g:getUnit(1)
if dbTable.groupId ~= tonumber(_g:getID()) or _u:getCountry() ~= dbTable.countryId or _u:getCoalition() ~= dbTable.coaltionId then
dbLog:info('Group Data mismatch')
updated = true
else
dbLog:info('No Mismatch')
end
end
end
dbLog:info('Updated: $1', updated)
if updated == false and gType ~= 'static' then -- time to check units
dbLog:info('No Group Mismatch, Check Units')
for index, uObject in pairs(Group.getByName(name):getUnits()) do
dbLog:info(index)
if mist.DBs.unitsByName[uObject:getName()] then
dbLog:info('UnitByName table exists')
local uTable = mist.DBs.unitsByName[uObject:getName()]
if tonumber(uObject:getID()) ~= uTable.unitId or uObject:getTypeName() ~= uTable.type then
dbLog:info('Unit Data mismatch')
updated = true
break
end
end
end
end
if updated == true or not mist.DBs.groupsByName[name] then
dbLog:info('Get Table')
writeGroups[#writeGroups+1] = {data = dbUpdate(name, gType), isUpdated = updated}
end
-- Work done, so remove
tempSpawnedGroups[name] = nil
tempSpawnGroupsCounter = tempSpawnGroupsCounter - 1
end
end
end
local function updateDBTables()
local i = #writeGroups
local savesPerRun = math.ceil(i/10)
if savesPerRun < 5 then
savesPerRun = 5
end
if i > 0 then
dbLog:info('updateDBTables')
local ldeepCopy = mist.utils.deepCopy
for x = 1, i do
dbLog:info(writeGroups[x])
local newTable = writeGroups[x].data
local updated = writeGroups[x].isUpdated
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
dbLog:info('Update unitsBy')
for newId, newUnitData in pairs(newTable.units) do
dbLog:info(newId)
newUnitData.category = mistCategory
if newUnitData.unitId then
dbLog:info('byId')
mist.DBs.unitsById[tonumber(newUnitData.unitId)] = ldeepCopy(newUnitData)
end
dbLog:info(updated)
if mist.DBs.unitsByName[newUnitData.unitName] and updated == true then--if unit existed before and something was updated, write over the entry for a given unit name just in case.
dbLog:info('Updating Unit Tables')
for i = 1, #mist.DBs.unitsByCat[mistCategory] do
if mist.DBs.unitsByCat[mistCategory][i].unitName == newUnitData.unitName then
dbLog:info('Entry Found, Rewriting for unitsByCat')
mist.DBs.unitsByCat[mistCategory][i] = ldeepCopy(newUnitData)
break
end
end
for i = 1, #mist.DBs.unitsByNum do
if mist.DBs.unitsByNum[i].unitName == newUnitData.unitName then
dbLog:info('Entry Found, Rewriting for unitsByNum')
mist.DBs.unitsByNum[i] = ldeepCopy(newUnitData)
break
end
end
else
dbLog:info('Unitname not in use, add as normal')
mist.DBs.unitsByCat[mistCategory][#mist.DBs.unitsByCat[mistCategory] + 1] = ldeepCopy(newUnitData)
mist.DBs.unitsByNum[#mist.DBs.unitsByNum + 1] = ldeepCopy(newUnitData)
end
mist.DBs.unitsByName[newUnitData.unitName] = ldeepCopy(newUnitData)
end
-- this is a really annoying DB to populate. Gotta create new tables in case its missing
dbLog:info('write mist.DBs.units')
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
if updated == true then
dbLog:info('Updating DBsUnits')
for i = 1, #mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] do
if mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][i].groupName == newTable.groupName then
dbLog:info('Entry Found, Rewriting')
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][i] = ldeepCopy(newTable)
break
end
end
else
mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory][#mist.DBs.units[newTable.coalition][(newTable.country)][mistCategory] + 1] = ldeepCopy(newTable)
end
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
dbLog:info('endUpdateTables')
end
end
local function groupSpawned(event)
-- dont need to add units spawned in at the start of the mission if mist is loaded in init line
if event.id == world.event.S_EVENT_BIRTH and timer.getTime0() < timer.getAbsTime() then
dbLog:info('unitSpawnEvent')
--table.insert(tempSpawnedUnits,(event.initiator))
-------
-- New functionality below.
-------
if Object.getCategory(event.initiator) == 1 and not Unit.getPlayerName(event.initiator) then -- simple player check, will need to later check to see if unit was spawned with a player in a flight
dbLog:info('Object is a Unit')
dbLog:info(Unit.getGroup(event.initiator):getName())
if not tempSpawnedGroups[Unit.getGroup(event.initiator):getName()] then
dbLog:info('added')
tempSpawnedGroups[Unit.getGroup(event.initiator):getName()] = 'group'
tempSpawnGroupsCounter = tempSpawnGroupsCounter + 1
end
elseif Object.getCategory(event.initiator) == 3 or Object.getCategory(event.initiator) == 6 then
dbLog:info('Object is Static')
tempSpawnedGroups[StaticObject.getName(event.initiator)] = 'static'
tempSpawnGroupsCounter = tempSpawnGroupsCounter + 1
end
end
end
local function doScheduledFunctions()
local i = 1
while i <= #scheduledTasks do
if not scheduledTasks[i].rep then -- not a repeated process
if scheduledTasks[i].t <= timer.getTime() then