/
veafSpawn.lua
1856 lines (1573 loc) · 80.7 KB
/
veafSpawn.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
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- VEAF spawn command and functions for DCS World
-- By zip (2018)
--
-- Features:
-- ---------
-- * Listen to marker change events and execute spawn commands, with optional parameters
-- * Possibilities :
-- * - spawn a specific ennemy unit or group
-- * - create a cargo drop to be picked by a helo
-- * Works with all current and future maps (Caucasus, NTTR, Normandy, PG, ...)
--
-- Prerequisite:
-- ------------
-- * This script requires DCS 2.5.1 or higher and MIST 4.3.74 or higher.
-- * It also requires the base veaf.lua script library (version 1.0 or higher)
-- * It also requires the veafMarkers.lua script library (version 1.0 or higher)
-- * It also requires the dcsUnits.lua script library (version 1.0 or higher)
-- * It also requires the veafUnits.lua script library (version 1.0 or higher)
--
-- Load the script:
-- ----------------
-- 1.) Download the script and save it anywhere on your hard drive.
-- 2.) Open your mission in the mission editor.
-- 3.) Add a new trigger:
-- * TYPE "4 MISSION START"
-- * ACTION "DO SCRIPT FILE"
-- * OPEN --> Browse to the location of MIST and click OK.
-- * ACTION "DO SCRIPT FILE"
-- * OPEN --> Browse to the location of veaf.lua and click OK.
-- * ACTION "DO SCRIPT FILE"
-- * OPEN --> Browse to the location of veafMarkers.lua and click OK.
-- * ACTION "DO SCRIPT FILE"
-- * OPEN --> Browse to the location of veafUnits.lua and click OK.
-- * ACTION "DO SCRIPT FILE"
-- * OPEN --> Browse to the location of this script and click OK.
-- * ACTION "DO SCRIPT"
-- * set the script command to "veafSpawn.initialize()" and click OK.
-- 4.) Save the mission and start it.
-- 5.) Have fun :)
--
-- Basic Usage:
-- ------------
-- 1.) Place a mark on the F10 map.
-- 2.) As text enter a command
-- 3.) Click somewhere else on the map to submit the new text.
-- 4.) The command will be processed. A message will appear to confirm this
-- 5.) The original mark will disappear.
--
-- Commands and options: see online help function veafSpawn.help()
--
-- *** NOTE ***
-- * All keywords are CaSE inSenSITvE.
-- * Commas are the separators between options ==> They are IMPORTANT!
--
-------------------------------------------------------------------------------------------------------------------------------------------------------------
--- veafSpawn Table.
veafSpawn = {}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Global settings. Stores the script constants
-------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Identifier. All output in DCS.log will start with this.
veafSpawn.Id = "SPAWN - "
--- Version.
veafSpawn.Version = "1.19.0"
-- trace level, specific to this module
veafSpawn.Debug = false
veafSpawn.Trace = false
--- Key phrase to look for in the mark text which triggers the spawn command.
veafSpawn.SpawnKeyphrase = "_spawn"
--- Key phrase to look for in the mark text which triggers the destroy command.
veafSpawn.DestroyKeyphrase = "_destroy"
--- Key phrase to look for in the mark text which triggers the teleport command.
veafSpawn.TeleportKeyphrase = "_teleport"
--- Name of the spawned units group
veafSpawn.RedSpawnedUnitsGroupName = "VEAF Spawned Units"
--- Illumination flare default initial altitude (in meters AGL)
veafSpawn.IlluminationFlareAglAltitude = 1000
veafSpawn.RadioMenuName = "SPAWN"
--- static object type spawned when using the "logistic" keyword
veafSpawn.LogisticUnitType = "FARP Ammo Dump Coating"
veafSpawn.LogisticUnitCategory = "Fortifications"
veafSpawn.ShellingInterval = 5 -- seconds between shells, randomized by 30%
veafSpawn.IlluminationShellingInterval = 30 -- seconds between illumination shells, randomized by 30%
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Do not change anything below unless you know what you are doing!
-------------------------------------------------------------------------------------------------------------------------------------------------------------
veafSpawn.rootPath = nil
-- counts the units generated
veafSpawn.spawnedUnitsCounter = 0
-- store all the convoys spawned
veafSpawn.spawnedConvoys = {}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Utility methods
-------------------------------------------------------------------------------------------------------------------------------------------------------------
function veafSpawn.logError(message)
veaf.logError(veafSpawn.Id .. message)
end
function veafSpawn.logInfo(message)
veaf.logInfo(veafSpawn.Id .. message)
end
function veafSpawn.logDebug(message)
if message and veafSpawn.Debug then
veaf.logDebug(veafSpawn.Id .. message)
end
end
function veafSpawn.logTrace(message)
if message and veafSpawn.Trace then
veaf.logTrace(veafSpawn.Id .. message)
end
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Event handler functions.
-------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Function executed when a mark has changed. This happens when text is entered or changed.
function veafSpawn.onEventMarkChange(eventPos, event)
veafSpawn.logTrace(string.format("event = %s", veaf.p(event)))
-- choose by default the coalition opposing the player who triggered the event
local invertedCoalition = 1
if event.coalition == 1 then
invertedCoalition = 2
end
veafSpawn.logTrace(string.format("event.idx = %s", veaf.p(event.idx)))
if veafSpawn.executeCommand(eventPos, event.text, invertedCoalition, event.idx) then
-- Delete old mark.
veafSpawn.logTrace(string.format("Removing mark # %d.", event.idx))
trigger.action.removeMark(event.idx)
end
end
function veafSpawn.executeCommand(eventPos, eventText, coalition, markId, bypassSecurity, spawnedGroups)
veafSpawn.logDebug(string.format("veafSpawn.executeCommand(eventText=[%s])", eventText))
veafSpawn.logTrace(string.format("markId = %s", veaf.p(markId)))
veafSpawn.logTrace(string.format("bypassSecurity = %s", veaf.p(bypassSecurity)))
-- Check if marker has a text and the veafSpawn.SpawnKeyphrase keyphrase.
if eventText ~= nil and (eventText:lower():find(veafSpawn.SpawnKeyphrase) or eventText:lower():find(veafSpawn.DestroyKeyphrase) or eventText:lower():find(veafSpawn.TeleportKeyphrase)) then
-- Analyse the mark point text and extract the keywords.
local options = veafSpawn.markTextAnalysis(eventText)
if options then
for i=1,options.multiplier do
local spawnedGroup = nil
if not options.side then
if options.country then
-- deduct the side from the country
options.side = veaf.getCoalitionForCountry(options.country, true)
else
options.side = coalition
end
end
if not options.country then
-- deduct the country from the side
options.country = veaf.getCountryForCoalition(options.side)
end
veafSpawn.logTrace(string.format("options.side=%s",tostring(options.side)))
veafSpawn.logTrace(string.format("options.country=%s",tostring(options.country)))
local routeDone = false
-- Check options commands
if options.unit then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
local code = options.laserCode
local channel = options.freq
local band = options.mod
if options.role == "tacan" then
channel = options.tacanChannel or 99
code = options.tacanCode or "T"..tostring(channel)
band = options.tacanBand or "X"
end
spawnedGroup = veafSpawn.spawnUnit(eventPos, options.radius, options.name, options.country, options.altitude, options.heading, options.unitName, options.role, code, channel, band, bypassSecurity)
elseif options.farp then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnFarp(eventPos, options.radius, options.name, options.country, options.farpType, options.side, options.heading, options.spacing, bypassSecurity)
elseif options.group then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnGroup(eventPos, options.radius, options.name, options.country, options.altitude, options.heading, options.spacing, bypassSecurity)
elseif options.infantryGroup then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnInfantryGroup(eventPos, options.radius, options.country, options.side, options.heading, options.spacing, options.defense, options.armor, options.size, bypassSecurity)
elseif options.armoredPlatoon then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnArmoredPlatoon(eventPos, options.radius, options.country, options.side, options.heading, options.spacing, options.defense, options.armor, options.size, bypassSecurity)
elseif options.airDefenseBattery then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnAirDefenseBattery(eventPos, options.radius, options.country, options.side, options.heading, options.spacing, options.defense, bypassSecurity)
elseif options.transportCompany then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnTransportCompany(eventPos, options.radius, options.country, options.side, options.heading, options.spacing, options.defense, options.size, bypassSecurity)
elseif options.fullCombatGroup then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnFullCombatGroup(eventPos, options.radius, options.country, options.side, options.heading, options.spacing, options.defense, options.armor, options.size, bypassSecurity)
elseif options.convoy then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnConvoy(eventPos, options.radius, options.country, options.side, options.speed, options.patrol, options.offroad, options.destination, options.defense, options.size, options.armor, bypassSecurity)
routeDone = true
elseif options.cargo then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnCargo(eventPos, options.radius, options.cargoType, options.cargoSmoke, options.unitName, bypassSecurity)
elseif options.logistic then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L9(options.password, markId)) then return end
spawnedGroup = veafSpawn.spawnLogistic(eventPos, options.radius, bypassSecurity)
elseif options.destroy then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L1(options.password, markId)) then return end
veafSpawn.destroy(eventPos, options.radius, options.unitName)
elseif options.teleport then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L1(options.password, markId)) then return end
veafSpawn.teleport(eventPos, options.radius, options.name, bypassSecurity)
elseif options.bomb then
-- check security
if not (bypassSecurity or veafSecurity.checkSecurity_L1(options.password, markId)) then return end
veafSpawn.spawnBomb(eventPos, options.radius, options.shells, options.bombPower, options.password)
elseif options.smoke then
veafSpawn.spawnSmoke(eventPos, options.smokeColor, options.radius, options.shells)
elseif options.flare then
veafSpawn.spawnIlluminationFlare(eventPos, options.radius, options.shells, options.alt)
elseif options.signal then
veafSpawn.spawnSignalFlare(eventPos, options.radius, options.shells, options.smokeColor)
end
if spawnedGroup then
local groupObject = Group.getByName(spawnedGroup)
-- make the group combat ready !
veaf.readyForCombat(groupObject)
if not routeDone and options.destination then
-- make the group go to destination
local actualPosition = groupObject:getUnit(1):getPosition().p
local route = veaf.generateVehiclesRoute(actualPosition, options.destination, not options.offroad, options.speed, options.patrol)
mist.goRoute(groupObject, route)
end
-- add the group to the IADS, if there is one
if veafSkynet and options.skynet then -- only add static stuff like sam groups and sam batteries, not mobile groups and convoys
veafSkynet.addGroupToNetwork(groupObject)
end
if spawnedGroups then
table.insert(spawnedGroups, spawnedGroup)
end
end
end
return true
end
end
return false
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Analyse the mark text and extract keywords.
-------------------------------------------------------------------------------------------------------------------------------------------------------------
function veafSpawn.convertLaserToFreq(laser)
veafSpawn.logTrace(string.format("convertLaserToFreq(laser=%s)", tostring(laser)))
local laser = tonumber(laser)
if laser and laser >= 1111 and laser <= 1688 then
local laserB = math.floor((laser - 1000)/100)
local laserCD = laser - 1000 - laserB*100
local frequency = tostring(30+laserB+laserCD*0.05)
veafSpawn.logTrace(string.format("laserB=%s", tostring(laserB)))
veafSpawn.logTrace(string.format("laserCD=%s", tostring(laserCD)))
veafSpawn.logTrace(string.format("frequency=%s", tostring(frequency)))
return frequency
else
return nil
end
end
--- Extract keywords from mark text.
function veafSpawn.markTextAnalysis(text)
veafSpawn.logTrace(string.format("veafSpawn.markTextAnalysis(text=%s)", text))
-- Option parameters extracted from the mark text.
local switch = {}
switch.unit = false
switch.group = false
switch.farp = false
switch.farpType = "invisible"
switch.cargo = false
switch.logistic = false
switch.smoke = false
switch.flare = false
switch.signal = false
switch.bomb = false
switch.destroy = false
switch.teleport = false
switch.convoy = false
switch.role = nil
switch.laserCode = 1688
switch.infantryGroup = false
switch.armoredPlatoon = false
switch.airDefenseBattery = false
switch.transportCompany = false
switch.fullCombatGroup = false
switch.speed = nil
switch.shells = 1
switch.multiplier = 1
switch.skynet = false -- if true, add to skynet
-- spawned group/unit type/alias
switch.name = ""
-- spawned unit name
switch.unitName = nil
-- spawned group units spacing
switch.spacing = 5
switch.country = nil
switch.side = nil
switch.altitude = 0
switch.heading = 0
-- if true, group is part of a road convoy
switch.isConvoy = false
-- if true, group is patroling between its spawn point and its destination named point
switch.patrol = false
-- if true, group is set to not follow roads
switch.offroad = false
-- if set and convoy is true, send the group to the named point
switch.destination = nil
-- the size of the generated dynamic groups (platoons, convoys, etc.)
switch.size = math.random(7) + 8
-- defenses force ; ranges from 1 to 5, 5 being the toughest.
switch.defense = math.random(5)
-- armor force ; ranges from 1 to 5, 5 being the strongest and most modern.
switch.armor = math.random(5)
-- bomb power
switch.bombPower = 100
-- smoke color
switch.smokeColor = trigger.smokeColor.Red
-- optional cargo smoke
switch.cargoSmoke = false
-- spawn or destruction radius
switch.radius = 150
-- cargo type
switch.cargoType = "ammo_cargo"
-- flare agl altitude (meters)
switch.alt = veafSpawn.IlluminationFlareAglAltitude
switch.password = nil
-- JTAC radio comms
switch.freq = veafSpawn.convertLaserToFreq(switch.laserCode)
switch.mod = "fm"
-- TACAN name and channel
switch.tacanChannel = 99
switch.tacanBand = "X"
-- Check for correct keywords.
if text:lower():find(veafSpawn.SpawnKeyphrase .. " unit") then
switch.unit = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " group") then
switch.group = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " farp") then
switch.farp = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " convoy") then
switch.convoy = true
switch.size = 10 -- default the size parameter to 10
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " infantrygroup") then
switch.infantryGroup = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " armorgroup") then
switch.armoredPlatoon = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " samgroup") then
switch.airDefenseBattery = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " transportgroup") then
switch.transportCompany = true
switch.size = math.random(2, 5)
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " combatgroup") then
switch.fullCombatGroup = true
switch.size = 1 -- default the size parameter to 1
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " smoke") then
switch.smoke = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " flare") then
switch.flare = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " signal") then
switch.signal = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " cargo") then
switch.cargo = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " logistic") then
switch.logistic = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " bomb") then
switch.bomb = true
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " jtac") then
switch.role = 'jtac'
switch.unit = true
-- default country for friendly JTAC: USA
switch.country = "USA"
-- default name for JTAC
switch.name = "APC M1025 HMMWV"
-- default JTAC name (will overwrite previous unit with same name)
switch.unitName = "JTAC1"
elseif text:lower():find(veafSpawn.SpawnKeyphrase .. " tacan") then
switch.role = 'tacan'
switch.unit = true
-- default country for friendly tacan: USA
switch.country = "USA"
-- default name for tacan
switch.name = "TACAN_beacon"
-- default name (will overwrite previous unit with same name)
switch.unitName = "TACAN TCN"
elseif text:lower():find(veafSpawn.DestroyKeyphrase) then
switch.destroy = true
elseif text:lower():find(veafSpawn.TeleportKeyphrase) then
switch.teleport = true
else
return nil
end
-- keywords are split by ","
local keywords = veaf.split(text, ",")
for _, keyphrase in pairs(keywords) do
-- Split keyphrase by space. First one is the key and second, ... the parameter(s) until the next comma.
local str = veaf.breakString(veaf.trim(keyphrase), " ")
local key = str[1]
local val = str[2] or ""
if key:lower() == "unitname" then
-- Set name.
veafSpawn.logTrace(string.format("Keyword unitname = %s", tostring(val)))
switch.unitName = val
end
if (switch.group or switch.teleport or switch.unit) and key:lower() == "name" then
-- Set name.
veafSpawn.logTrace(string.format("Keyword name = %s", tostring(val)))
switch.name = val
end
if (key:lower() == "destination" or key:lower() == "dest") then
-- Set destination.
veafSpawn.logTrace(string.format("Keyword destination = %s", tostring(val)))
switch.destination = val
end
if key:lower() == "isconvoy" then
veafSpawn.logTrace("Keyword isconvoy found")
switch.convoy = true
end
if key:lower() == "patrol" then
veafSpawn.logTrace("Keyword patrol found")
switch.patrol = true
end
if key:lower() == "offroad" then
veafSpawn.logTrace("Keyword offroad found")
switch.offroad = true
end
if key:lower() == "skynet" then
-- Set name.
veafSpawn.logTrace(string.format("Keyword skynet = %s", tostring(val)))
switch.skynet = (val:lower() == "true")
end
if key:lower() == "radius" then
-- Set name.
veafSpawn.logTrace(string.format("Keyword radius = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.radius = nVal
end
if key:lower() == "spacing" then
-- Set spacing.
veafSpawn.logTrace(string.format("Keyword spacing = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.spacing = nVal
end
if key:lower() == "multiplier" then
-- Set multiplier.
veafSpawn.logTrace(string.format("Keyword multiplier = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.multiplier = nVal
end
if key:lower() == "alt" then
-- Set altitude.
veafSpawn.logTrace(string.format("Keyword alt = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.altitude = nVal
end
if key:lower() == "speed" then
-- Set altitude.
veafSpawn.logTrace(string.format("Keyword speed = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.speed = nVal
end
if key:lower() == "shells" then
-- Set altitude.
veafSpawn.logTrace(string.format("Keyword shells = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.shells = nVal
end
if key:lower() == "hdg" then
-- Set heading.
veafSpawn.logTrace(string.format("Keyword hdg = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.heading = nVal
end
if key:lower() == "heading" then
-- Set heading.
veafSpawn.logTrace(string.format("Keyword heading = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.heading = nVal
end
if key:lower() == "country" then
-- Set country
veafSpawn.logTrace(string.format("Keyword country = %s", tostring(val)))
switch.country = val:upper()
end
if key:lower() == "side" then
-- Set side
veafSpawn.logTrace(string.format("Keyword side = %s", tostring(val)))
if val:upper() == "BLUE" then
switch.side = veafCasMission.SIDE_BLUE
else
switch.side = veafCasMission.SIDE_RED
end
end
if key:lower() == "password" then
-- Unlock the command
veafSpawn.logTrace(string.format("Keyword password", tostring(val)))
switch.password = val
end
if key:lower() == "power" then
-- Set bomb power.
veafSpawn.logTrace(string.format("Keyword power = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.bombPower = nVal
end
if key:lower() == "laser" then
-- Set laser code.
veafSpawn.logTrace(string.format("laser code = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.freq = veafSpawn.convertLaserToFreq(nVal)
switch.laserCode = nVal
end
if key:lower() == "freq" then
-- Set JTAC frequency.
veafSpawn.logTrace(string.format("freq = %s", tostring(val)))
switch.freq = val
end
if key:lower() == "mod" then
-- Set JTAC modulation.
veafSpawn.logTrace(string.format("mod = %s", tostring(val)))
switch.mod = val
end
if key:lower() == "band" then
-- Set TACAN band
veafSpawn.logTrace(string.format("band = %s", tostring(val)))
switch.tacanBand = val
end
if key:lower() == "code" then
-- Set TACAN code
veafSpawn.logTrace(string.format("code = %s", tostring(val)))
switch.tacanCode = val
end
if key:lower() == "channel" then
-- Set TACAN channel.
veafSpawn.logTrace(string.format("channel = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.tacanChannel = nVal
end
if key:lower() == "color" then
-- Set smoke color.
veafSpawn.logTrace(string.format("Keyword color = %s", tostring(val)))
if (val:lower() == "red") then
switch.smokeColor = trigger.smokeColor.Red
elseif (val:lower() == "green") then
switch.smokeColor = trigger.smokeColor.Green
elseif (val:lower() == "orange") then
switch.smokeColor = trigger.smokeColor.Orange
elseif (val:lower() == "blue") then
switch.smokeColor = trigger.smokeColor.Blue
elseif (val:lower() == "white") then
switch.smokeColor = trigger.smokeColor.White
end
end
if key:lower() == "alt" then
-- Set alt.
veafSpawn.logTrace(string.format("Keyword alt = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.alt = nVal
end
if switch.cargo and key:lower() == "name" then
-- Set cargo type.
veafSpawn.logTrace(string.format("Keyword name = %s", tostring(val)))
switch.cargoType = val
end
if switch.farp and key:lower() == "type" then
-- Set farp type.
veafSpawn.logTrace(string.format("Keyword type = %s", tostring(val)))
switch.farpType = val
end
if switch.cargo and key:lower() == "smoke" then
-- Mark with green smoke.
veafSpawn.logTrace("Keyword smoke is set")
switch.cargoSmoke = true
end
if key:lower() == "size" then
-- Set size.
veafSpawn.logTrace(string.format("Keyword size = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
switch.size = nVal
end
if key:lower() == "defense" then
-- Set defense.
veafSpawn.logTrace(string.format("Keyword defense = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
if nVal >= 0 then
switch.defense = nVal
end
end
if key:lower() == "armor" then
-- Set armor.
veafSpawn.logTrace(string.format("Keyword armor = %s", tostring(val)))
local nVal = veaf.getRandomizableNumeric(val)
if nVal >= 0 then
switch.armor = nVal
end
end
end
-- check mandatory parameter "name" for command "group"
if switch.group and not(switch.name) then return nil end
-- check mandatory parameter "name" for command "unit"
if switch.unit and not(switch.name) then return nil end
return switch
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Group spawn command
-------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Spawn a specific group at a specific spot
function veafSpawn.doSpawnGroup(spawnSpot, radius, groupDefinition, country, alt, hdg, spacing, groupName, silent, shuffle)
veafSpawn.logDebug(string.format("doSpawnGroup(country=%s, alt=%s, hdg=%s, spacing=%s, groupName=%s)", tostring(country), tostring(alt), tostring(hdg), tostring(spacing), tostring(groupName)))
local spawnSpot = veaf.placePointOnLand(mist.getRandPointInCircle(spawnSpot, radius))
veafSpawn.logTrace("spawnSpot=" .. veaf.vecToString(spawnSpot))
veafSpawn.spawnedUnitsCounter = veafSpawn.spawnedUnitsCounter + 1
if type(groupDefinition) == "string" then
local name = groupDefinition
-- find the desired group in the groups database
groupDefinition = veafUnits.findGroup(name)
if not(groupDefinition) then
veafSpawn.logInfo("cannot find group "..name)
if not(silent) then
trigger.action.outText("cannot find group "..name, 5)
end
return
end
end
veafSpawn.logTrace("doSpawnGroup: groupDefinition.description=" .. groupDefinition.description)
local units = {}
-- place group units on the map
local group, cells = veafUnits.placeGroup(groupDefinition, spawnSpot, spacing, hdg)
veafUnits.traceGroup(group, cells)
if not(groupName) then
groupName = group.groupName .. " #" .. veafSpawn.spawnedUnitsCounter
end
for i=1, #group.units do
local unit = group.units[i]
local unitType = unit.typeName
local unitName = groupName .. " / " .. unit.displayName .. " #" .. i
local spawnPoint = unit.spawnPoint
if alt > 0 then
spawnPoint.y = alt
end
-- check if position is correct for the unit type
if not veafUnits.checkPositionForUnit(spawnPoint, unit) then
veafSpawn.logInfo("cannot find a suitable position for spawning unit ".. unitType)
if not(silent) then
trigger.action.outText("cannot find a suitable position for spawning unit "..unitType, 5)
end
else
local toInsert = {
["x"] = spawnPoint.x,
["y"] = spawnPoint.z,
["alt"] = spawnPoint.y,
["type"] = unitType,
["name"] = unitName,
["speed"] = 0, -- speed in m/s
["skill"] = "Random",
["heading"] = spawnPoint.hdg
}
veafSpawn.logTrace(string.format("toInsert x=%.1f y=%.1f, alt=%.1f, type=%s, name=%s, speed=%d, heading=%d, skill=%s, country=%s", toInsert.x, toInsert.y, toInsert.alt, toInsert.type, toInsert.name, toInsert.speed, mist.utils.toDegree(toInsert.heading), toInsert.skill, country ))
table.insert(units, toInsert)
end
end
-- shuffle the group if needed (useful for randomizing convoys)
if shuffle then
units = veaf.shuffle(units)
end
-- actually spawn the group
if group.naval then
mist.dynAdd({country = country, category = "SHIP", name = groupName, hidden = false, units = units})
elseif group.air then
mist.dynAdd({country = country, category = "AIRPLANE", name = groupName, hidden = false, units = units})
else
mist.dynAdd({country = country, category = "GROUND_UNIT", name = groupName, hidden = false, units = units})
end
if not(silent) then
-- message the group spawning
trigger.action.outText("A " .. group.description .. "("..country..") has been spawned", 5)
end
return groupName
end
--- Spawn a FARP
function veafSpawn.spawnFarp(spawnSpot, radius, name, country, farptype, side, hdg, spacing, silent)
veafSpawn.logDebug(string.format("spawnFarp(name=%s, country=%s, farptype=%s, side=%s, hdg=%s, spacing=%s)",veaf.p(name), veaf.p(country), veaf.p(farptype), veaf.p(side), veaf.p(hdg), veaf.p(spacing)))
local radius = radius or 0
local name = name
local hdg = hdg or 0
local side = side or 1
local spacing = spacing or 50
local silent = silent or false
local country = country or "usa"
local farptype = farptype or ""
local spawnPosition = veaf.placePointOnLand(mist.getRandPointInCircle(spawnSpot, radius))
veafSpawn.logTrace(string.format("spawnPosition=%s", veaf.p(spawnPosition)))
if not name or name == "" then
local _lat, _lon = coord.LOtoLL(spawnSpot)
veafSpawn.logTrace(string.format("_lat=%s", veaf.p(_lat)))
veafSpawn.logTrace(string.format("_lon=%s", veaf.p(_lon)))
local _mgrs = coord.LLtoMGRS(_lat, _lon)
veafSpawn.logTrace(string.format("_mgrs=%s", veaf.p(_mgrs)))
--local _UTM = _mgrs.UTMZone .. _mgrs.MGRSDigraph .. math.floor(_mgrs.Easting / 1000) .. math.floor(_mgrs.Northing / 1000)
local _UTM = _mgrs.MGRSDigraph .. math.floor(_mgrs.Easting / 1000) .. math.floor(_mgrs.Northing / 1000)
name = "FARP ".. _UTM:upper()
end
local _type = "Invisible FARP"
local _shape = "invisiblefarp"
if farptype:lower() == "quad" then
_type = "FARP"
_shape = "FARPs"
elseif farptype:lower() == "single" then
_type = "FARP"
_shape = "FARP"
end
-- spawn the FARP
local _farpStatic = {
["category"] = "Heliports",
["shape_name"] = _shape,
["type"] = _type,
-- ["unitId"] = _unitId,
["y"] = spawnPosition.z,
["x"] = spawnPosition.x,
["groupName"] = name,
["name"] = name,
["canCargo"] = false,
["heading"] = hdg,
["country"] = country,
["coalition"] = side
}
mist.dynAddStatic(_farpStatic)
local _spawnedFARP = StaticObject.getByName(name)
veafSpawn.logTrace(string.format("_spawnedFARP=%s", veaf.p(_spawnedFARP)))
if _spawnedFARP then
veafSpawn.logDebug(string.format("Spawned the FARP static %s", veaf.p(name)))
-- populate the FARP
veafGrass.buildFarpUnits(_farpStatic)
end
end
--- Spawn a specific group at a specific spot
function veafSpawn.spawnGroup(spawnSpot, radius, name, country, alt, hdg, spacing, silent)
veafSpawn.logDebug(string.format("spawnGroup(name = %s, country=%s, alt=%d, hdg=%d, spacing=%d)",name, country, alt, hdg, spacing))
local spawnedGroupName = veafSpawn.doSpawnGroup(spawnSpot, radius, name, country, alt, hdg, spacing, nil, silent)
return spawnedGroupName
end
function veafSpawn._createDcsUnits(country, units, groupName)
veafSpawn.logDebug(string.format("veafSpawn._createDcsUnits([%s])",country or ""))
local dcsUnits = {}
for i=1, #units do
local unit = units[i]
local unitType = unit.typeName
local unitName = groupName .. " / " .. unit.displayName .. " #" .. i
local hdg = unit.heading or math.random(0, 359)
local spawnPosition = unit.spawnPoint
-- check if position is correct for the unit type
if veafUnits.checkPositionForUnit(spawnPosition, unit) then
local toInsert = {
["x"] = spawnPosition.x,
["y"] = spawnPosition.z,
["alt"] = spawnPosition.y,
["type"] = unitType,
["name"] = unitName,
["speed"] = 0,
["skill"] = "Excellent",
["heading"] = hdg
}
veafSpawn.logTrace(string.format("toInsert x=%.1f y=%.1f, alt=%.1f, type=%s, name=%s, speed=%d, heading=%d, skill=%s, country=%s", toInsert.x, toInsert.y, toInsert.alt, toInsert.type, toInsert.name, toInsert.speed, toInsert.heading, toInsert.skill, country ))
table.insert(dcsUnits, toInsert)
end
end
-- actually spawn groups
mist.dynAdd({country = country, category = "GROUND_UNIT", name = groupName, hidden = false, units = dcsUnits})
-- set AI options
local controller = Group.getByName(groupName):getController()
controller:setOption(AI.Option.Ground.id.ENGAGE_AIR_WEAPONS, true) -- engage air-to-ground weapons with SAMs
controller:setOption(AI.Option.Air.id.ROE, 2) -- set fire at will
controller:setOption(AI.Option.Ground.id.ROE, 2) -- set fire at will
controller:setOption(AI.Option.Naval.id.ROE, 2) -- set fire at will
controller:setOption(AI.Option.Ground.id.ALARM_STATE, 2) -- set alarm state to red
controller:setOption(AI.Option.Ground.id.DISPERSE_ON_ATTACK, 1) -- set disperse on attack according to the option
end
--- Spawns a dynamic infantry group
function veafSpawn.spawnInfantryGroup(spawnSpot, radius, country, side, heading, spacing, defense, armor, size, silent)
veafSpawn.logDebug(string.format("spawnInfantryGroup(country=%s, side=%s, heading=%s, spacing=%s, defense=%s, armor=%s, size=%s)", veaf.p(country), veaf.p(side), veaf.p(heading), veaf.p(spacing), veaf.p(defense), veaf.p(armor), veaf.p(size)))
local spawnSpot = veaf.placePointOnLand(mist.getRandPointInCircle(spawnSpot, radius))
veafSpawn.logTrace("spawnSpot=" .. veaf.vecToString(spawnSpot))
local groupName = "spawn-" .. math.random(99999) .. " - Infantry Section "
local group = veafCasMission.generateInfantryGroup(groupName, defense, armor, side, size)
local group = veafUnits.processGroup(group)
local groupPosition = veaf.placePointOnLand(spawnSpot)
veafSpawn.logTrace(string.format("groupPosition = %s",veaf.vecToString(groupPosition)))
local group, cells = veafUnits.placeGroup(group, groupPosition, spacing, heading)
-- shuffle the units in the group
units = veaf.shuffle(group.units)
veafSpawn._createDcsUnits(country, group.units, groupName)
if not silent then
trigger.action.outText("Spawned dynamic infantry group "..groupName, 5)
end
return groupName
end
--- Spawns a dynamic armored platoon
function veafSpawn.spawnArmoredPlatoon(spawnSpot, radius, country, side, heading, spacing, defense, armor, size, silent)
veafSpawn.logDebug(string.format("spawnArmoredPlatoon(country=%s, side=%s, heading=%s, spacing=%s, defense=%s, armor=%s, size=%s)", veaf.p(country), veaf.p(side), veaf.p(heading), veaf.p(spacing), veaf.p(defense), veaf.p(armor), veaf.p(size)))
local spawnSpot = veaf.placePointOnLand(mist.getRandPointInCircle(spawnSpot, radius))
veafSpawn.logTrace("spawnSpot=" .. veaf.vecToString(spawnSpot))
local groupName = "spawn-" .. math.random(99999) .. " - Armored Platoon "
local group = veafCasMission.generateArmorPlatoon(groupName, defense, armor, side, size)
local group = veafUnits.processGroup(group)
local groupPosition = veaf.placePointOnLand(spawnSpot)
veafSpawn.logTrace(string.format("groupPosition = %s",veaf.vecToString(groupPosition)))
local group, cells = veafUnits.placeGroup(group, groupPosition, spacing, heading)
-- shuffle the units in the group
units = veaf.shuffle(group.units)
veafSpawn._createDcsUnits(country, group.units, groupName)
if not silent then
trigger.action.outText("Spawned dynamic armored platoon "..groupName, 5)
end
return groupName
end
--- Spawns a dynamic air defense battery
function veafSpawn.spawnAirDefenseBattery(spawnSpot, radius, country, side, heading, spacing, defense, silent)
veafSpawn.logDebug(string.format("spawnAirDefenseBattery(country=%s, side=%s, heading=%s, spacing=%s, defense=%s)", veaf.p(country), veaf.p(side), veaf.p(heading), veaf.p(spacing), veaf.p(defense)))
local spawnSpot = veaf.placePointOnLand(mist.getRandPointInCircle(spawnSpot, radius))
veafSpawn.logTrace("spawnSpot=" .. veaf.vecToString(spawnSpot))
local groupName = "spawn-" .. math.random(99999) .. " - Air Defense Battery "
local group = veafCasMission.generateAirDefenseGroup(groupName, defense, side)
local group = veafUnits.processGroup(group)
local groupPosition = veaf.placePointOnLand(spawnSpot)
veafSpawn.logTrace(string.format("groupPosition = %s",veaf.vecToString(groupPosition)))
local group, cells = veafUnits.placeGroup(group, groupPosition, spacing, heading)
-- shuffle the units in the group
units = veaf.shuffle(group.units)
veafSpawn._createDcsUnits(country or veaf.getCountryForCoalition(side), group.units, groupName)
if not silent then
trigger.action.outText("Spawned dynamic air defense battery "..groupName, 5)
end
return groupName
end
--- Spawns a dynamic transport company
function veafSpawn.spawnTransportCompany(spawnSpot, radius, country, side, heading, spacing, defense, size, silent)