-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn.lua
More file actions
1500 lines (1403 loc) · 51.7 KB
/
spawn.lua
File metadata and controls
1500 lines (1403 loc) · 51.7 KB
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
local addGroup = coalition.addGroup
local addStaticObject = coalition.addStaticObject
local scheduleFunction = timer.scheduleFunction
local getModelTime = timer.getTime
local logwrite = log.write
local format = string.format
local deepCopy
local inherit
local database = {
["groupsByName"] = {},
["unitsByName"] = {},
["staticsByName"] = {},
["zonesByName"] = {}
}
Spawn = {}
-- # ***Class***: Spawn
--
-- ***Author:*** Wizard
--
-- ***Created:*** June 7th, 2022
--
-- ***Version:*** 0.0.1
--
-- ***Description:***
-- A dynamic spawn class for groups, units, and statics in DCS World
--
-- ***Features:***
-- * Object Orientated
-- * Integrated Logging
-- * Spawn objects from late activated templates
-- * Spawn objects from custom templates
-- * Spawn objects from a variable amount of arguments
-- * Spawn with original group and unit names
-- * Spawn with a new nickname for the group and its units
-- * Spawn with a set schedule on repeat
-- * Spawn units with different payloads
-- * Spawn units with different liverys
-- * Spawn from a template
-- * Spawn from a zone
-- * Spawn from a zone on the nearest road
-- * Spawn from a random zone
-- * Spawn from a Vec3 position
-- * Spawn from a airbase runway
-- * Spawn from a airbase parking spot in a hot configuration
-- * Spawn from a airbase parking spot in a cold configuration
-- * Various `Set` methods to assign data for the templates to spawn with
-- * Various `Get` methods to acquire data from templates like payloads and liverys
-- * Get open airbase parking spots as well as get the first open spot with the option to filter terminal types
-- * Mark parking spots at an airbase to determine viable parking spot locations
-- * Add group, unit, and static Templates into the global `Database`
--
-- # ***Methods & Fields***
--
-- ## ***Fields***
--
-- @fields
-- @field Spawn.Version #string [semantic version]
Spawn.Version = "0.0.1"
-- @field Spawn.Source #string [script source for dcs.log prefix]
Spawn.Source = "Spawn.lua"
-- @field Spawn.DebugLevel #number [the max log level ]
Spawn.DebugLevel = 5
-- @field Spawn.DebugLevels #table [log level enumerators ]
Spawn.DebugLevels = {
["Alert"] = 1,
["Error"] = 2,
["Warning"] = 3,
["Info"] = 4,
["Debug"] = 5
}
-- @field Spawn.Category #table [enumerators for spawn group categories]
Spawn.Category = {
["Airplane"] = Group.Category.AIRPLANE,
["Helicopter"] = Group.Category.HELICOPTER,
["Vehicle"] = Group.Category.GROUND,
["Ship"] = Group.Category.SHIP,
}
-- @field Spawn.Takeoff #table [enumerators for taking off from an airbase]
Spawn.Takeoff = {
["FromRunway"] = {name = "Takeoff from runway", type = "TakeOff", action = "From Runway"},
["FromParkingHot"] = {name = "Takeoff from parking hot", type = "TakeOffParkingHot", action = "From Parking Area Hot"},
["FromParkingCold"] = {name = "Takeoff from parking", type = "TakeOffParking", action = "From Parking Area"}
}
-- @field Spawn.Skill #table [enumerators for skills]
Spawn.Skill = {
["Average"] = "Average",
["Good"] = "Good",
["High"] = "High",
["Excellent"] = "Excellent",
["Random"] = "Random"
}
-- @field Spawn.Waypoint #table [enumerators for waypoints]
Spawn.Waypoint = {
["TurningPoint"] = {name = "Turning point", type = "Turning Point", action = "Turning Point" },
["FlyOverPoint"] = {name = "Fly over point", type = "Turning Point", action = "Fly Over Point"},
["FinPoint"] = {name = "Fin point N/A", type = "Fin Point", action = "Fin Point"},
["TakeoffFromRunway"] = {name = "Takeoff from runway", type = "TakeOff", action = "From Runway"},
["TakeoffFromParking"] = {name = "Takeoff from parking", type = "TakeOffParking", action = "From Parking Area"},
["TakeoffFromParkingHot"] = {name = "Takeoff from parking hot", type = "TakeOffParkingHot", action = "From Parking Area Hot"},
["LandingReFuAr"] = {name = "LandingReFuAr", type = "LandingReFuAr", action = "LandingReFuAr"},
["Landing"] = {name = "Landing", type = "Land", action = "Landing"},
["OffRoad"] = {name = "Offroad", type = "Turning Point", action = "Off Road"},
["OnRoad"] = {name = "On road", type = "Turning Point", action = "On Road"},
["Rank"] = {name = "Rank", type = "Turning Point", action = "Rank"},
["Cone"] = {name = "Cone", type = "Turning Point", action = "Cone"},
["Vee"] = {name = "Vee", type = "Turning Point", action = "Vee"},
["Diamond"] = {name = "Diamond", type = "Turning Point", action = "Diamond"},
["EchelonL"] = {name = "Echelon Left", type = "Turning Point", action = "EchelonL"},
["EchelonR"] = {name = "Echelon Right", type = "Turning Point", action = "EchelonR"},
["CustomForm"] = {name = "Custom", type = "Turning Point", action = "Custom"},
}
--
-- ## ***Methods***
--
--- Create a new instance of a spawn object by name
-- @param #Spawn self <*>
-- @param #string templateName <*> [the group, unit, or static name of the ME Template]
-- @param #string nickname [optional nickname the Spawn object will use instead of its template name]
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
function Spawn:New(templateName, nickname)
local self = inherit(self, Spawn)
self.baseTemplate, self.staticTemplate = self:GetTemplate(templateName)
if not self.baseTemplate then
self:Error("Spawn:New() | couldn't find template %s in database", templateName)
return self
end
self.templateName = templateName
self.nickname = nickname
self.spawnCount = 0
self.countryId = self.baseTemplate.countryId
self.categoryId = self.baseTemplate.categoryId
self.spawnedGroups = {}
self.spawnedStatics = {}
self.aliveGroups = {}
self.aliveUnits = {}
self.aliveStatics = {}
return self
end
--- Create a new instance of a Spawn object from a template
-- @param #Spawn self <*>
-- @param #table template <*> [the template table with required data for spawning]
-- @param #string nickname [optional nickname the Spawn object will use instead of its template name]
-- @param #boolean staticTemplate [optional boolean if the template is a static object]
-- @return #Spawn self
-- @usage
-- local ViperGroupTemplate = Spawn:GetGroupTemplate("Viper Group")
-- local ViperSpawn = Spawn:NewFromTemplate(ViperGroupTemplate)
--
-- local StaticDepotSpawn = Spawn:NewFromTemplate(Spawn:GetStaticTemplate("Static Depot"), nil, true)
function Spawn:NewFromTemplate(template, nickname, staticTemplate)
local self = inherit(self, Spawn)
self.baseTemplate = deepCopy(template)
self.staticTemplate = staticTemplate
self.templateName = self.baseTemplate.name
self.nickname = nickname
self.spawnCount = 0
self.countryId = self.baseTemplate.countryId
self.categoryId = self.baseTemplate.categoryId
self.spawnedGroups = {}
self.spawnedStatics = {}
self.aliveGroups = {}
self.aliveUnits = {}
self.aliveStatics = {}
return self
end
--- Create a new instance of a Spawn object from a table of properties
-- **Required Unit Properties:**
-- * type
-- * countryId
-- * categoryId
--
-- **Required Static Properties:**
-- * category
-- * shapeName
-- * staticTemplate
--
-- **Optional Properties:**
-- * skill
-- * canDrive
-- * alt
-- * altType
-- * heading
-- * action
-- * name
-- * waypoint
--
-- @param #Spawn self <*>
-- @param #table properties <*> [table of properties to give to the Spawn object]
-- @return #Spawn self
-- @usage
-- -- Tank Unit
-- local TankProperties = {}
-- TankProperties.type = "T-90" -- type required
-- TankProperties.countryId = country.id.RUSSIA -- countryId required
-- TankProperties.categoryId = Spawn.Category.Vehicle -- categoryId required
--
-- -- Static Tank
-- local StaticTankProperties = {}
-- StaticTankProperties.type = "T-90" -- type required
-- StaticTankProperties.countryId = country.id.RUSSIA -- countryId required
-- StaticTankProperties.category = "" -- category required
-- StaticTankProperties.shapeName = "" -- shapeName required
function Spawn:NewFromTable(properties)
local spawnTemplate
if properties.staticTemplate then
spawnTemplate = self:GetStaticSpawnTemplate()
spawnTemplate.countryId = properties.countryId
spawnTemplate.units[1].category = properties.category
spawnTemplate.units[1].shape_name = properties.shapeName
spawnTemplate.units[1].type = properties.type
spawnTemplate.units[1].heading = properties.heading or 0
else
spawnTemplate = self:GetSpawnTemplate()
spawnTemplate.countryId = properties.countryId
spawnTemplate.categoryId = properties.categoryId
spawnTemplate.name = properties.name or properties.type
if properties.units then
spawnTemplate.units = properties.units
else
spawnTemplate.units[1].type = properties.type
spawnTemplate.units[1].skill = properties.skill or "Random"
spawnTemplate.units[1].heading = properties.heading * math.pi / 180 or 0
spawnTemplate.units[1].playerCanDrive = properties.canDrive or false
end
spawnTemplate.route.points[1].alt = properties.alt or 0
spawnTemplate.route.points[1].alt_type = properties.altType or "BARO"
if properties.waypoint then
spawnTemplate.route.points[1].type = properties.waypoint.type or "Turning Point"
spawnTemplate.route.points[1].action = properties.waypoint.action or "Turning Point"
end
end
local self = Spawn:NewFromTemplate(spawnTemplate, properties.nickname, properties.staticTemplate)
return self
end
--- Set the Spawn object to keep group or unit names
-- @param #Spawn self <*>
-- @param #boolean keepGroupName [true or false to keep the group name of the template]
-- @param #boolean keepUnitNames [true or false to keep all the unit names for the group template]
-- @return #Spawn self
-- @usage
-- local TankSpawn = Spawn:New("Tank Group")
-- TankSpawn:SetKeepNames(true, true)
function Spawn:SetKeepNames(keepGroupName, keepUnitNames)
self.keepGroupName = keepGroupName
self.keepUnitNames = keepUnitNames
return self
end
--- Set the Spawn object to only allow a certain amount of groups and units to be alive
-- @param #Spawn self <*>
-- @param #number maxAliveGroups <*> [the max amount of groups that can be alive at a given time]
-- @param #number maxAliveUnits <*> [the max amount of unit that can be alive at a given time]
-- @return #Spawn self
-- @usage
-- local TankSpawn = Spawn:New("Tank Group")
-- TankSpawn:SetMaxAlive(2, 2)
function Spawn:SetMaxAlive(maxAliveGroups, maxAliveUnits)
self.maxAliveGroups = maxAliveGroups
self.maxAliveUnits = maxAliveUnits
return self
end
--- Set the Spawn object to use a certain heading
-- converts a degree heading into radians
-- @param #Spawn self <*>
-- @param #number heading <*> [the heading to give to the group or a unit at spawn time]
-- @param #number unitId [optional unitId to provide for a specific unit within the group]
-- @return #Spawn self
-- @usage
-- local TankSpawn = Spawn:New("Tank Group")
-- TankSpawn:SetHeading(90, 2)
function Spawn:SetHeading(heading, unitId)
self.heading = heading * math.pi / 180
self.headingId = unitId
return self
end
--- Set the spawn object to spawn from a vec3
-- @param #Spawn self <*>
-- @param #table vec3 <*> [the vec3 point to spawn at]
-- @param #number alt <*> [required for airplanes and helicopters only]
-- @return #Spawn self
-- @usage
-- local TankSpawn = Spawn:New("Tank Group")
-- TankSpawn:SetSpawnVec3(Spawn:GetZoneVec3("spawn zone"))
function Spawn:SetSpawnVec3(vec3, alt)
self.spawnVec3 = vec3
self.spawnVec3Alt = alt
return self
end
--- Set the Spawn object to spawn from a zone
-- @param #Spawn self <*>
-- @param #string zoneName <*> [the name of the trigger zone to spawn at]
-- @param #number alt <*> [required for airplanes and helicopters only]
-- @return #Spawn self
-- @usage
-- local TankSpawn = Spawn:New("Tank Group")
-- TankSpawn:SetSpawnZone("spawn zone")
function Spawn:SetSpawnZone(zoneName, alt)
self.spawnZoneName = zoneName
self.spawnZoneAlt = alt
return self
end
--- Set the Spawn object to spawn from a airbase
-- @param #Spawn self <*>
-- @param #string airbaseName <*> [the airbase name as seen in DCS to spawn at]
-- @param #enum takeoff <*> [use: `Spawn.Takeoff`]
-- @param #array spots [optional parking spots to provide, otherwise they will be decided by DCS.]
-- You can use `Spawn:GetFreeParkingSpots(airbaseName)` obtain useable spots.
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetSpawnAirbase("Aleppo", Spawn.Takeoff.FromParkingHot)
function Spawn:SetSpawnAirbase(airbaseName, takeoff, spots)
self.spawnAirbaseName = airbaseName
self.spawnAirbaseTakeoff = takeoff
self.spawnAirbaseSpots = spots
return self
end
--- Set the Spawn object to spawn from a random template
-- @param #Spawn self <*>
-- @param #array templateList <*> [a list of template names where one will be randomly selected each time it spawns]
-- @return #Spawn self
-- @usage
-- local spawnList = {
-- "Tank Group 1",
-- "Tank Group 2",
-- "Tank Group 3",
-- "Tank Group 4",
-- }
-- local TankSpawn = Spawn:New("Tank Group")
-- TankSpawn:SetRandomFromTemplate(spawnList)
function Spawn:SetRandomFromTemplate(templateList)
self.randomTemplate = true
self.templateList = templateList
return self
end
--- Set the Spawn objects nickname
-- @param #Spawn self <*>
-- @param #string nickname <*> [will be used over the template name, unit names will be followed by -Id]
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetNickname("Hornet CAP")
function Spawn:SetNickname(nickname)
self.nickname = nickname
return self
end
--- Set the Spawn object to use a new paylaod
-- @param #Spawn self <*>
-- @param #table payload <*> [payload to provide to the group or a specifc unit. airplanes and helicopters only.]
-- the return of `Spawn:GetPayload(unitName)` can be used.
-- @param #number unitId [optional unit Id to set the payload for a specifc unit within the group]
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetPayload("Hornet CAP")
function Spawn:SetPayload(payload, unitId)
self.payload = payload
self.payloadId = unitId
return self
end
--- Set the Spawn object to use a new livery
-- @param #Spawn self <*>
-- @param #string livery <*> [livery name to provide to the group or specific unit.]
-- the return of `Spawn:GetLivery(unitName)` can be used.
-- @param #number unitId [optional unit Id to set the livery for a specifc unit within the group]
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetLivery("Hornet CAP")
function Spawn:SetLivery(liveryName, unitId)
self.livery = liveryName
self.liveryId = unitId
return self
end
--- Set the Spawn object to use a new skill level
-- @param #Spawn self <*>
-- @param #string skill <*> [skill level than can be used, can also use `Spawn.Skill`]
-- @param #number unitId [optional unit Id to set the skill for a specific unit]
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetSkill(Spawn.Skill.Random)
function Spawn:SetSkill(skill, unitId)
self.skill = skill
self.skillId = unitId
return self
end
--- Set the Spawn object to use a certain debug level
-- @param #Spawn self <*>
-- @param #number level <*> [max debug level where only set level and below will be used. can use `Spawn.DebugLevel`]
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetDebugLevel(Spawn.DebugLevel.Info)
function Spawn:SetDebugLevel(level)
if type(level) == "string" then
self.DebugLevel = Spawn.DebugLevels[level]
elseif type(level) == "number" then
self.DebugLevel = level
end
return self
end
--- Set the Spawn object to use a specific method for spawning on a repeating schedule
-- @param #Spawn self <*>
-- @param #function method <*> [the spawn method that will be scheduled]
-- @param #array params [the parameters to give to the method]
-- @param #number delay <*> the delay in seconds
-- @return #Spawn self
-- -- spawns the group from a zone every 300 seconds
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetScheduler(Spawn.SpawnFromZone, {HornetSpawn, "spawn zone", 5000}, 300)
-- HornetSpawn:SpawnFromZone("spawn zone", 5000)
function Spawn:SetScheduler(method, params, delay)
self.scheduledSpawn = true
self.scheduledMethod = method
self.scheduledParams = params
self.scheduledDelay = delay
return self
end
--- Set a spawn hook to capture the event of spawning
-- @param #string spawnHook <*> [the name of the method to callback to upon spawning]
-- @return #Spawn self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetSpawnHook("OnEventSpawn")
-- function HornetSpawn:OnEventSpawn(spawnData)
-- self:Info("%s has spawned", spawnData.GroupName)
-- end
-- HornetSpawn:SpawnToWorld()
function Spawn:SetSpawnHook(spawnHook)
self.spawnHook = spawnHook
return self
end
--- Get the currently spawned DCS Class Group
-- @param #Spawn self <*>
-- @return #DCSGroup self
-- @usage
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SpawnToWorld()
-- local SpawnedGroup = HornetSpawn:GetSpawnedGroup()
function Spawn:GetSpawnedGroup()
return self.spawnedGroup
end
--- Get the currently spawned DCS Class StaticObject
-- @param #Spawn self <*>
-- @return #DCSStaticObject self
-- @usage
-- local CrateSpawn = Spawn:New("Static Crate")
-- CrateSpawn:SpawnToWorld()
-- local SpawnedStatic = CrateSpawn:GetSpawnedStatic()
function Spawn:GetSpawnedStatic()
return self.spawnedStatic
end
--- Get a payload table from a unit by name
-- @param #Spawn self <*>
-- @param #string unitName <*> [the name of a unit in the database to get a payload table from]
-- @return #table payload
-- @usage
-- local CapPayload = Spawn:GetPayload("CAP Payload")
-- local HornetSpawn = Spawn:New("Hornet Group")
-- HornetSpawn:SetPayload(CapPayload)
-- HornetSpawn:SpawnToWorld()
function Spawn:GetPayload(unitName)
if Database.unitsByName[unitName] then
local payload = deepCopy(Database.unitsByName[unitName].payload)
return payload
end
end
--- Get a livery name from a unit by name
-- @param #Spawn self <*>
-- @param #string unitName
-- @return #string liveryName
function Spawn:GetLiveryName(unitName)
if Database.unitsByName[unitName] then
local liveryName = Database.unitsByName[unitName].livery_id
return liveryName
end
end
--- Get a group template by name
-- @param #Spawn self <*>
-- @param #string groupName
-- @return #table groupTemplate
function Spawn:GetGroupTemplate(groupName)
if Database.groupsByName[groupName] then
return deepCopy(Database.groupsByName[groupName])
end
end
--- Get a unit template by name
-- @param #Spawn self <*>
-- @param #string unitName
-- @return #table unitTemplate
function Spawn:GetUnitTemplate(unitName)
if Database.unitsByName[unitName] then
return deepCopy(Database.unitsByName[unitName])
end
end
--- Get a static template by name
-- @param #Spawn self <*>
-- @param #string staticName
-- @return #table staticTemplate
function Spawn:GetStaticTemplate(staticName)
if Database.staticsByName[staticName] then
return deepCopy(Database.staticsByName[staticName])
end
end
--- Get a template by name
-- this function also returns a second boolean variable if the template is static
-- @param #Spawn self <*>
-- @param #string templateName
-- @return #table template
function Spawn:GetTemplate(templateName)
if Database.groupsByName[templateName] then
self:Info("Spawn:GetTemplate() | returning group template: "..templateName)
return deepCopy(Database.groupsByName[templateName])
elseif Database.unitsByName[templateName] then
self:Info("Spawn:GetTemplate() | returning unit template: "..templateName)
return deepCopy(Database.unitsByName[templateName])
elseif Database.staticsByName[templateName] then
self:Info("Spawn:GetTemplate() | returning static template: "..templateName)
return deepCopy(Database.staticsByName[templateName]), true
end
end
--- Get the Spawn objects base template
-- @param #Spawn self <*>
-- @return #table baseTemplate, #boolean staticTemplate
function Spawn:GetBaseTemplate()
local baseTemplate, staticTemplate = self.baseTemplate, self.staticTemplate
if self.randomTemplate then
local randomKey = math.random(1, #self.templateList)
local templateName = self.templateList[randomKey]
baseTemplate, staticTemplate = self:GetTemplate(templateName)
end
return deepCopy(baseTemplate), staticTemplate
end
--- Get a empty spawn table for groups and units
-- @param #Spawn self <*>
-- @return #table spawnTemplate
function Spawn:GetSpawnTemplate()
local spawnTemplate = {
["visible"] = true,
["lateActivation"] = false,
["tasks"] = {},
["uncontrollable"] = false,
["task"] = "",
["taskSelected"] = true,
["route"] = {
["points"] = {
[1] = {
["alt"] = 0,
["type"] = "Turning Point",
["ETA"] = 0,
["alt_type"] = "",
["formation_template"] = "",
["y"] = 0,
["x"] = 0,
["ETA_locked"] = true,
["speed"] = 0,
["action"] = "Turning Point",
["task"] = {
["id"] = "ComboTask",
["params"] = {
["tasks"] = {},
},
},
["speed_locked"] = true,
},
},
},
["hidden"] = false,
["units"] = {
[1] = {
["type"] = "",
["skill"] = "",
["y"] = 0,
["x"] = 0,
["name"] = "",
["heading"] = 0,
["playerCanDrive"] = true,
}
},
["y"] = 0,
["x"] = 0,
["name"] = "",
["start_time"] = 0,
}
return spawnTemplate
end
--- Get a empty spawn table for statics
-- @param #Spawn self <*>
-- @return #table staticSpawnTemplate
function Spawn:GetStaticSpawnTemplate()
local staticSpawnTemplate = {
["heading"] = 0,
["route"] = {
["points"] = {
[1] = {
["alt"] = 0,
["type"] = "",
["name"] = "",
["y"] = 0,
["speed"] = 0,
["x"] = 0,
["formation_template"] = "",
["action"] = "",
},
},
},
["units"] = {
[1] = {
["category"] = "",
["shape_name"] = "",
["type"] = "",
["rate"] = 0,
["y"] = 0,
["x"] = 0,
["name"] = "",
["heading"] = 0,
},
},
["y"] = 0,
["x"] = 0,
["name"] = "",
["dead"] = false,
}
return staticSpawnTemplate
end
--- Get a zone template by name
-- @param #Spawn self <*>
-- @param #string zoneName
-- @return #table zoneTemplate
function Spawn:GetZoneTemplate(zoneName)
if Database.zonesByName[zoneName] then
return deepCopy(Database.zonesByName[zoneName])
end
end
--- Get a quad zones points by name
-- @param #Spawn self <*>
-- @param #string zoneName
-- @return #table points
function Spawn:GetQuadZonePoints(zoneName)
local zoneTemplate = self:GetZoneTemplate(zoneName)
if zoneTemplate then
if zoneTemplate.type == 2 then
local points = deepCopy(zoneTemplate.vertices)
return points
end
end
end
--- Get a zones radius by name
-- @param #Spawn self <*>
-- @param #string zoneName
-- @return #number self radius
function Spawn:GetZoneRadius(zoneName)
local zoneTemplate = self:GetZoneTemplate(zoneName)
if zoneTemplate then
if zoneTemplate.type == 0 then
local radius = deepCopy(zoneTemplate.radius)
return radius
end
end
end
--- Get a zones vec3 points by name
-- @param #Spawn self <*>
-- @param #string zoneName
-- @return #table vec3
function Spawn:GetZoneVec3(zoneName)
local zone = self:GetZoneTemplate(zoneName)
if zone then
local vec3 = deepCopy(zone.vec3)
return vec3
end
end
--- Get all the open parking spots at an airbase by name
-- @param #Spawn self <*>
-- @param #string airbaseName
-- @param #number terminalType
-- @return #table openParkingSpots
function Spawn:GetOpenParkingSpots(airbaseName, terminalType)
local airbase = Airbase.getByName(airbaseName)
if airbase then
local openParkingSpots = {}
for _, spot in pairs(airbase:getParking()) do
if not spot.TO_AC then
if terminalType then
if spot.Term_Type == terminalType then
openParkingSpots[#openParkingSpots+1] = {
termIndex = spot.Term_Index,
termVec3 = spot.vTerminalPos
}
end
else
openParkingSpots[#openParkingSpots+1] = {
termIndex = spot.Term_Index,
termVec3 = spot.vTerminalPos
}
end
end
end
return openParkingSpots
end
end
--- Get the first open parking spot an airbase by name
-- @param #Spawn self <*>
-- @param #string airbaseName
-- @param #number terminalType
-- @return #table openSpot
function Spawn:GetFirstOpenParkingSpot(airbaseName, terminalType)
local airbase = Airbase.getByName(airbaseName)
if airbase then
for _, spot in pairs(airbase:getParking()) do
if not spot.TO_AC then
if terminalType then
if spot.Term_Type == terminalType then
local openSpot = {
termIndex = spot.Term_Index,
termVec3 = spot.vTerminalPos
}
return openSpot
end
else
local openSpot = {
termIndex = spot.Term_Index,
termVec3 = spot.vTerminalPos
}
return openSpot
end
end
end
end
end
--- Get the the terminal data from an airbase by name
-- @param #Spawn self <*>
-- @param #string airbaseName
-- @param #number spots
-- @return #table terminalData
function Spawn:GetTerminalData(airbaseName, spots)
local airbase = Airbase.getByName(airbaseName)
if airbase then
local terminalData = {}
for _, spot in pairs(airbase:getParking()) do
if not spot.TO_AC then
for _, termIndex in pairs(spots) do
if spot.Term_Index == termIndex then
terminalData[#terminalData+1] = {
termIndex = spot.Term_Index,
termVec3 = spot.vTerminalPos
}
end
end
end
end
return terminalData
end
return self
end
--- Get a DCS Group object by its spawn index
-- @param #Spawn self <*>
-- @param #number spawnIndex
-- @return #DCSGroup self
function Spawn:GetGroupFromIndex(spawnIndex)
self:Alert(spawnIndex)
local groupName = self.spawnedGroups[spawnIndex]
return Group.getByName(groupName)
end
--- Get a DCS StaticObject by its spawn index
-- @param #Spawn self <*>
-- @param #number spawnIndex
-- @return #DCSStaticObject self
function Spawn:GetStaticFromIndex(spawnIndex)
local staticName = self.spawnedStatics[spawnIndex]
return StaticObject.getByName(staticName)
end
--- Return true or false if the current Spawn obejct is alive
-- @param #Spawn self <*>
-- @return #boolean alive
function Spawn:IsAlive()
if self.staticTemplate then
return self:IsStaticAlive()
end
return self:IsGroupAlive()
end
--- Return true or false if the current Spawn group object is alive
-- @param #Spawn self <*>
-- @return #boolean isAlive
function Spawn:IsGroupAlive()
local spawnedGroup = Group.getByName(self.spawnedGroupName)
if spawnedGroup then
local spawnedUnit = spawnedGroup:getUnit(1)
if spawnedUnit:isExist() then
if spawnedUnit:isActive() then
if spawnedUnit:getLife() > 0 then
return true
end
end
end
end
return false
end
--- Return true or false if the current Spawn static object is alive
-- @param #Spawn self <*>
-- @return #boolean isAlive
function Spawn:IsStaticAlive()
local spawnedStatic = StaticObject.getByName(self.spawnedStaticName)
if spawnedStatic then
if spawnedStatic:isExist() then
if spawnedStatic:getLife() > 0 then
return true
end
end
end
return false
end
--- Mark the parking spots at an airbase by name
-- @param #Spawn self <*>
-- @param #string airbaseName
-- @return none
function Spawn:MarkParkingSpots(airbaseName)
local airbase = Airbase.getByName(airbaseName)
if airbase then
for _, spot in pairs(airbase:getParking()) do
trigger.action.markToAll(-1, "Terminal Type: "..spot.Term_Type.."\nTerminal Index: "..spot.Term_Index, spot.vTerminalPos)
end
end
end
--- Add a group template to the database
-- @param #Spawn self <*>
-- @param #table template
-- @return none
function Spawn:AddGroupTemplate(template)
Database.groupsByName[template.name] = deepCopy(template)
for _, unitTemplate in pairs(template.units) do
self:AddUnitTemplate(unitTemplate)
end
end
--- Add a unit template to the database
-- @param #Spawn self <*>
-- @param #table template
-- @return none
function Spawn:AddUnitTemplate(template)
Database.unitsByName[template.name] = deepCopy(template)
end
--- Add a static template to the database
-- @param #Spawn self <*>
-- @param #table template
-- @return none
function Spawn:AddStaticTemplate(template)
Database.staticsByName[template.units[1].name] = deepCopy(template)
end
--- Spawn the object to the world
-- @param #Spawn self <*>
-- @return #Spawn self
function Spawn:SpawnToWorld()
self._spawnTemplate, self.staticTemplate = self:GetBaseTemplate()
self:_InitializeTemplate()
return self
end
--- Spawn an object with a spawn method to be scheduled
-- @param #Spawn self <*>
-- @param #function method
-- @param #array params
-- @param #number delay
-- @return none
function Spawn:SpawnScheduled(method, params, delay)
method = self.scheduledMethod or method
params = self.scheduledParams or params
delay = self.scheduledDelay or delay
scheduleFunction(function() method(unpack(params)) end, nil, getModelTime() + delay)
return self
end
function Spawn:SpawnLateActivated()
self._spawnTemplate, self.staticTemplate = self:GetBaseTemplate()
self._spawnTemplate.lateActivation = true
self:_InitializeTemplate()
return self
end
--- Respawn the object
-- @param #Spawn self <*>
-- @return #Spawn self
function Spawn:Respawn()
self:_AddToWorld()
return self
end
--- Spawn an object from a template
-- @param #Spawn self <*>
-- @param #table template
-- @param #number countryId
-- @param #number categoryId
-- @param #boolean static
-- @return #Spawn self
function Spawn:SpawnFromTemplate(template, countryId, categoryId, static)
if static then
local spawnedStatic = addStaticObject(countryId, template)
template.countryId = countryId
self:AddStaticTemplate(template)
else
local spawnedGroup = addGroup(countryId, categoryId, template)
template.countryId = countryId
template.categoryId = categoryId
self:AddGroupTemplate(template)
end
local self = Spawn:New(template.name)
return self
end
--- Spawn an object from a zone by name
-- @param #Spawn self <*>
-- @param #string zoneName
-- @param #number alt
-- @return #Spawn self
function Spawn:SpawnFromZone(zoneName, alt)
zoneName = zoneName or self.spawnZoneName
alt = alt or self.spawnZoneAlt
local spawnZoneVec3 = self:GetZoneVec3(zoneName)
self:SpawnFromVec3(spawnZoneVec3, alt)
return self
end
--- Spawn an object from a zone on the nearest road
-- @param #Spawn self <*>
-- @param #string zoneName
-- @return #Spawn self
function Spawn:SpawnFromZoneOnNearestRoad(zoneName)
local spawnZoneVec3 = self:GetZoneVec3(zoneName)
self:SpawnFromVec3OnNearestRoad(spawnZoneVec3)
return self
end
--- Spawn an object from a random zone from a list
-- @param #Spawn self <*>
-- @param #array zoneList
-- @param #number alt
-- @return #Spawn self
function Spawn:SpawnFromRandomZone(zoneList, alt)
local randomNum = math.random(1, #zoneList)
local randomZone = zoneList[randomNum]
self:SpawnFromZone(randomZone, alt)
return self