-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskynet-iads.lua
More file actions
618 lines (535 loc) · 19.2 KB
/
skynet-iads.lua
File metadata and controls
618 lines (535 loc) · 19.2 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
do
SkynetIADS = {}
SkynetIADS.__index = SkynetIADS
SkynetIADS.database = samTypesDB
function SkynetIADS:create(name)
local iads = {}
setmetatable(iads, SkynetIADS)
iads.radioMenu = nil
iads.earlyWarningRadars = {}
iads.samSites = {}
iads.commandCenters = {}
iads.ewRadarScanMistTaskID = nil
iads.coalition = nil
iads.contacts = {}
iads.maxTargetAge = 32
iads.name = name
iads.harmDetection = SkynetIADSHARMDetection:create(iads)
iads.logger = SkynetIADSLogger:create(iads)
if iads.name == nil then
iads.name = ""
end
iads.contactUpdateInterval = 5
return iads
end
function SkynetIADS:setUpdateInterval(interval)
self.contactUpdateInterval = interval
end
function SkynetIADS:setCoalition(item)
if item then
local coalitionID = item:getCoalition()
if self.coalitionID == nil then
self.coalitionID = coalitionID
end
if self.coalitionID ~= coalitionID then
self:printOutputToLog("element: "..item:getName().." has a different coalition than the IADS", true)
end
end
end
function SkynetIADS:addJammer(jammer)
table.insert(self.jammers, jammer)
end
function SkynetIADS:getCoalition()
return self.coalitionID
end
function SkynetIADS:getDestroyedEarlyWarningRadars()
local destroyedSites = {}
for i = 1, #self.earlyWarningRadars do
local ewSite = self.earlyWarningRadars[i]
if ewSite:isDestroyed() then
table.insert(destroyedSites, ewSite)
end
end
return destroyedSites
end
function SkynetIADS:getUsableAbstractRadarElemtentsOfTable(abstractRadarTable)
local usable = {}
for i = 1, #abstractRadarTable do
local abstractRadarElement = abstractRadarTable[i]
if abstractRadarElement:hasActiveConnectionNode() and abstractRadarElement:hasWorkingPowerSource() and abstractRadarElement:isDestroyed() == false then
table.insert(usable, abstractRadarElement)
end
end
return usable
end
function SkynetIADS:getUsableEarlyWarningRadars()
return self:getUsableAbstractRadarElemtentsOfTable(self.earlyWarningRadars)
end
function SkynetIADS:createTableDelegator(units)
local sites = SkynetIADSTableDelegator:create()
for i = 1, #units do
local site = units[i]
table.insert(sites, site)
end
return sites
end
function SkynetIADS:addEarlyWarningRadarsByPrefix(prefix)
self:deactivateEarlyWarningRadars()
self.earlyWarningRadars = {}
for unitName, unit in pairs(mist.DBs.unitsByName) do
local pos = self:findSubString(unitName, prefix)
--somehow the MIST unit db contains StaticObject, we check to see we only add Units
local unit = Unit.getByName(unitName)
if pos and pos == 1 and unit then
self:addEarlyWarningRadar(unitName)
end
end
return self:createTableDelegator(self.earlyWarningRadars)
end
function SkynetIADS:addEarlyWarningRadar(earlyWarningRadarUnitName)
local earlyWarningRadarUnit = Unit.getByName(earlyWarningRadarUnitName)
if earlyWarningRadarUnit == nil then
self:printOutputToLog("you have added an EW Radar that does not exist, check name of Unit in Setup and Mission editor: "..earlyWarningRadarUnitName, true)
return
end
self:setCoalition(earlyWarningRadarUnit)
local ewRadar = nil
local category = earlyWarningRadarUnit:getDesc().category
if category == Unit.Category.AIRPLANE or category == Unit.Category.SHIP then
ewRadar = SkynetIADSAWACSRadar:create(earlyWarningRadarUnit, self)
else
ewRadar = SkynetIADSEWRadar:create(earlyWarningRadarUnit, self)
end
ewRadar:setupElements()
ewRadar:setCachedTargetsMaxAge(self:getCachedTargetsMaxAge())
-- for performance improvement, if iads is not scanning no update coverage update needs to be done, will be executed once when iads activates
if self.ewRadarScanMistTaskID ~= nil then
self:buildRadarCoverageForEarlyWarningRadar(ewRadar)
end
ewRadar:setActAsEW(true)
ewRadar:setToCorrectAutonomousState()
ewRadar:goLive()
table.insert(self.earlyWarningRadars, ewRadar)
if self:getDebugSettings().addedEWRadar then
self:printOutputToLog("ADDED: "..ewRadar:getDescription())
end
return ewRadar
end
function SkynetIADS:getCachedTargetsMaxAge()
return self.contactUpdateInterval
end
function SkynetIADS:getEarlyWarningRadars()
return self:createTableDelegator(self.earlyWarningRadars)
end
function SkynetIADS:getEarlyWarningRadarByUnitName(unitName)
for i = 1, #self.earlyWarningRadars do
local ewRadar = self.earlyWarningRadars[i]
if ewRadar:getDCSName() == unitName then
return ewRadar
end
end
end
function SkynetIADS:findSubString(haystack, needle)
return string.find(haystack, needle, 1, true)
end
function SkynetIADS:addSAMSitesByPrefix(prefix)
self:deativateSAMSites()
self.samSites = {}
for groupName, groupData in pairs(mist.DBs.groupsByName) do
local pos = self:findSubString(groupName, prefix)
if pos and pos == 1 then
--mist returns groups, units and, StaticObjects
local dcsObject = Group.getByName(groupName)
if dcsObject then
self:addSAMSite(groupName)
end
end
end
return self:createTableDelegator(self.samSites)
end
function SkynetIADS:getSAMSitesByPrefix(prefix)
local returnSams = {}
for i = 1, #self.samSites do
local samSite = self.samSites[i]
local groupName = samSite:getDCSName()
local pos = self:findSubString(groupName, prefix)
if pos and pos == 1 then
table.insert(returnSams, samSite)
end
end
return self:createTableDelegator(returnSams)
end
function SkynetIADS:addSAMSite(samSiteName)
local samSiteDCS = Group.getByName(samSiteName)
if samSiteDCS == nil then
self:printOutputToLog("you have added an SAM Site that does not exist, check name of Group in Setup and Mission editor: "..tostring(samSiteName), true)
return
end
self:setCoalition(samSiteDCS)
local samSite = SkynetIADSSamSite:create(samSiteDCS, self)
samSite:setupElements()
samSite:setCanEngageAirWeapons(true)
samSite:goLive()
samSite:setCachedTargetsMaxAge(self:getCachedTargetsMaxAge())
if samSite:getNatoName() == "UNKNOWN" then
self:printOutputToLog("you have added an SAM site that Skynet IADS can not handle: "..samSite:getDCSName(), true)
samSite:cleanUp()
else
samSite:goDark()
table.insert(self.samSites, samSite)
if self:getDebugSettings().addedSAMSite then
self:printOutputToLog("ADDED: "..samSite:getDescription())
end
-- for performance improvement, if iads is not scanning no update coverage update needs to be done, will be executed once when iads activates
if self.ewRadarScanMistTaskID ~= nil then
self:buildRadarCoverageForSAMSite(samSite)
end
return samSite
end
end
function SkynetIADS:getUsableSAMSites()
return self:getUsableAbstractRadarElemtentsOfTable(self.samSites)
end
function SkynetIADS:getDestroyedSAMSites()
local destroyedSites = {}
for i = 1, #self.samSites do
local samSite = self.samSites[i]
if samSite:isDestroyed() then
table.insert(destroyedSites, samSite)
end
end
return destroyedSites
end
function SkynetIADS:getSAMSites()
return self:createTableDelegator(self.samSites)
end
function SkynetIADS:getActiveSAMSites()
local activeSAMSites = {}
for i = 1, #self.samSites do
if self.samSites[i]:isActive() then
table.insert(activeSAMSites, self.samSites[i])
end
end
return activeSAMSites
end
function SkynetIADS:getSAMSiteByGroupName(groupName)
for i = 1, #self.samSites do
local samSite = self.samSites[i]
if samSite:getDCSName() == groupName then
return samSite
end
end
end
function SkynetIADS:getSAMSitesByNatoName(natoName)
local selectedSAMSites = SkynetIADSTableDelegator:create()
for i = 1, #self.samSites do
local samSite = self.samSites[i]
if samSite:getNatoName() == natoName then
table.insert(selectedSAMSites, samSite)
end
end
return selectedSAMSites
end
function SkynetIADS:addCommandCenter(commandCenter)
self:setCoalition(commandCenter)
local comCenter = SkynetIADSCommandCenter:create(commandCenter, self)
table.insert(self.commandCenters, comCenter)
-- when IADS is active the radars will be added to the new command center. If it not active this will happen when radar coverage is built
if self.ewRadarScanMistTaskID ~= nil then
self:addRadarsToCommandCenters()
end
return comCenter
end
function SkynetIADS:isCommandCenterUsable()
if #self:getCommandCenters() == 0 then
return true
end
local usableComCenters = self:getUsableAbstractRadarElemtentsOfTable(self:getCommandCenters())
return (#usableComCenters > 0)
end
function SkynetIADS:getCommandCenters()
return self.commandCenters
end
function SkynetIADS.evaluateContacts(self)
local ewRadars = self:getUsableEarlyWarningRadars()
local samSites = self:getUsableSAMSites()
--will add SAM Sites acting as EW Rardars to the ewRadars array:
for i = 1, #samSites do
local samSite = samSites[i]
--We inform SAM sites that a target update is about to happen. If they have no targets in range after the cycle they go dark
samSite:targetCycleUpdateStart()
if samSite:getActAsEW() then
table.insert(ewRadars, samSite)
end
--if the sam site is not in ew mode and active we grab the detected targets right here
if samSite:isActive() and samSite:getActAsEW() == false then
local contacts = samSite:getDetectedTargets()
for j = 1, #contacts do
local contact = contacts[j]
self:mergeContact(contact)
end
end
end
local samSitesToTrigger = {}
for i = 1, #ewRadars do
local ewRadar = ewRadars[i]
--call go live in case ewRadar had to shut down (HARM attack)
ewRadar:goLive()
-- if an awacs has traveled more than a predeterminded distance we update the autonomous state of the SAMs
if getmetatable(ewRadar) == SkynetIADSAWACSRadar and ewRadar:isUpdateOfAutonomousStateOfSAMSitesRequired() then
self:buildRadarCoverageForEarlyWarningRadar(ewRadar)
end
local ewContacts = ewRadar:getDetectedTargets()
if #ewContacts > 0 then
local samSitesUnderCoverage = ewRadar:getUsableChildRadars()
for j = 1, #samSitesUnderCoverage do
local samSiteUnterCoverage = samSitesUnderCoverage[j]
-- only if a SAM site is not active we add it to the hash of SAM sites to be iterated later on
if samSiteUnterCoverage:isActive() == false then
--we add them to a hash to make sure each SAM site is in the collection only once, reducing the number of loops we conduct later on
samSitesToTrigger[samSiteUnterCoverage:getDCSName()] = samSiteUnterCoverage
end
end
for j = 1, #ewContacts do
local contact = ewContacts[j]
self:mergeContact(contact)
end
end
end
self:cleanAgedTargets()
for samName, samToTrigger in pairs(samSitesToTrigger) do
for j = 1, #self.contacts do
local contact = self.contacts[j]
-- the DCS Radar only returns enemy aircraft, if that should change a coalition check will be required
-- currently every type of object in the air is handed of to the SAM site, including missiles
local description = contact:getDesc()
local category = description.category
if category and category ~= Unit.Category.GROUND_UNIT and category ~= Unit.Category.SHIP and category ~= Unit.Category.STRUCTURE then
samToTrigger:informOfContact(contact)
end
end
end
for i = 1, #samSites do
local samSite = samSites[i]
samSite:targetCycleUpdateEnd()
end
self.harmDetection:setContacts(self:getContacts())
self.harmDetection:evaluateContacts()
self.logger:printSystemStatus()
end
function SkynetIADS:cleanAgedTargets()
local contactsToKeep = {}
for i = 1, #self.contacts do
local contact = self.contacts[i]
if contact:getAge() < self.maxTargetAge then
table.insert(contactsToKeep, contact)
end
end
self.contacts = contactsToKeep
end
--TODO unit test this method:
function SkynetIADS:getAbstracRadarElements()
local abstractRadarElements = {}
local ewRadars = self:getEarlyWarningRadars()
local samSites = self:getSAMSites()
for i = 1, #ewRadars do
local ewRadar = ewRadars[i]
table.insert(abstractRadarElements, ewRadar)
end
for i = 1, #samSites do
local samSite = samSites[i]
table.insert(abstractRadarElements, samSite)
end
return abstractRadarElements
end
function SkynetIADS:addRadarsToCommandCenters()
--we clear any existing radars that may have been added earlier
local comCenters = self:getCommandCenters()
for i = 1, #comCenters do
local comCenter = comCenters[i]
comCenter:clearChildRadars()
end
-- then we add child radars to the command centers
local abstractRadarElements = self:getAbstracRadarElements()
for i = 1, #abstractRadarElements do
local abstractRadar = abstractRadarElements[i]
self:addSingleRadarToCommandCenters(abstractRadar)
end
end
function SkynetIADS:addSingleRadarToCommandCenters(abstractRadarElement)
local comCenters = self:getCommandCenters()
for i = 1, #comCenters do
local comCenter = comCenters[i]
comCenter:addChildRadar(abstractRadarElement)
end
end
-- this method rebuilds the radar coverage of the IADS, a complete rebuild is only required the first time the IADS is activated
-- during runtime it is sufficient to call buildRadarCoverageForSAMSite or buildRadarCoverageForEarlyWarningRadar method that just updates the IADS for one unit, this saves script execution time
function SkynetIADS:buildRadarCoverage()
--to build the basic radar coverage we use all SAM sites. Checks if SAM site has power or a connection node is done when using the SAM site later on
local samSites = self:getSAMSites()
--first we clear all child and parent radars that may have been added previously
for i = 1, #samSites do
local samSite = samSites[i]
samSite:clearChildRadars()
samSite:clearParentRadars()
end
local ewRadars = self:getEarlyWarningRadars()
for i = 1, #ewRadars do
local ewRadar = ewRadars[i]
ewRadar:clearChildRadars()
end
--then we rebuild the radar coverage
local abstractRadarElements = self:getAbstracRadarElements()
for i = 1, #abstractRadarElements do
local abstract = abstractRadarElements[i]
self:buildRadarCoverageForAbstractRadarElement(abstract)
end
self:addRadarsToCommandCenters()
--we call this once on all sam sites, to make sure autonomous sites go live when IADS activates
for i = 1, #samSites do
local samSite = samSites[i]
samSite:informChildrenOfStateChange()
end
end
function SkynetIADS:buildRadarCoverageForAbstractRadarElement(abstractRadarElement)
local abstractRadarElements = self:getAbstracRadarElements()
for i = 1, #abstractRadarElements do
local aElementToCompare = abstractRadarElements[i]
if aElementToCompare ~= abstractRadarElement then
if abstractRadarElement:isInRadarDetectionRangeOf(aElementToCompare) then
self:buildRadarAssociation(aElementToCompare, abstractRadarElement)
end
if aElementToCompare:isInRadarDetectionRangeOf(abstractRadarElement) then
self:buildRadarAssociation(abstractRadarElement, aElementToCompare)
end
end
end
end
function SkynetIADS:buildRadarAssociation(parent, child)
--chilren should only be SAM sites not EW radars
if ( getmetatable(child) == SkynetIADSSamSite ) then
parent:addChildRadar(child)
end
--Only SAM Sites should have parent Radars, not EW Radars
if ( getmetatable(child) == SkynetIADSSamSite ) then
child:addParentRadar(parent)
end
end
function SkynetIADS:buildRadarCoverageForSAMSite(samSite)
self:buildRadarCoverageForAbstractRadarElement(samSite)
self:addSingleRadarToCommandCenters(samSite)
end
function SkynetIADS:buildRadarCoverageForEarlyWarningRadar(ewRadar)
self:buildRadarCoverageForAbstractRadarElement(ewRadar)
self:addSingleRadarToCommandCenters(ewRadar)
end
function SkynetIADS:mergeContact(contact)
local existingContact = false
for i = 1, #self.contacts do
local iadsContact = self.contacts[i]
if iadsContact:getName() == contact:getName() then
iadsContact:refresh()
--these contacts are used in the logger we set a kown harm state of a contact coming from a SAM site. So the logger will show them als HARMs
contact:setHARMState(iadsContact:getHARMState())
local radars = contact:getAbstractRadarElementsDetected()
for j = 1, #radars do
local radar = radars[j]
iadsContact:addAbstractRadarElementDetected(radar)
end
existingContact = true
end
end
if existingContact == false then
table.insert(self.contacts, contact)
end
end
function SkynetIADS:getContacts()
return self.contacts
end
function SkynetIADS:getDebugSettings()
return self.logger.debugOutput
end
function SkynetIADS:printOutput(output, typeWarning)
self.logger:printOutput(output, typeWarning)
end
function SkynetIADS:printOutputToLog(output)
self.logger:printOutputToLog(output)
end
-- will start going through the Early Warning Radars and SAM sites to check what targets they have detected
function SkynetIADS.activate(self)
mist.removeFunction(self.ewRadarScanMistTaskID)
self.ewRadarScanMistTaskID = mist.scheduleFunction(SkynetIADS.evaluateContacts, {self}, 1, self.contactUpdateInterval)
self:buildRadarCoverage()
end
function SkynetIADS:setupSAMSitesAndThenActivate(setupTime)
self:activate()
self.logger:printOutputToLog("DEPRECATED: setupSAMSitesAndThenActivate, no longer needed since using enableEmission instead of AI on / off allows for the Ground units to setup with their radars turned off")
end
function SkynetIADS:deactivate()
mist.removeFunction(self.ewRadarScanMistTaskID)
mist.removeFunction(self.samSetupMistTaskID)
self:deativateSAMSites()
self:deactivateEarlyWarningRadars()
self:deactivateCommandCenters()
end
function SkynetIADS:deactivateCommandCenters()
for i = 1, #self.commandCenters do
local comCenter = self.commandCenters[i]
comCenter:cleanUp()
end
end
function SkynetIADS:deativateSAMSites()
for i = 1, #self.samSites do
local samSite = self.samSites[i]
samSite:cleanUp()
end
end
function SkynetIADS:deactivateEarlyWarningRadars()
for i = 1, #self.earlyWarningRadars do
local ewRadar = self.earlyWarningRadars[i]
ewRadar:cleanUp()
end
end
function SkynetIADS:addRadioMenu()
self.radioMenu = missionCommands.addSubMenu('SKYNET IADS '..self:getCoalitionString())
local displayIADSStatus = missionCommands.addCommand('show IADS Status', self.radioMenu, SkynetIADS.updateDisplay, {self = self, value = true, option = 'IADSStatus'})
local displayIADSStatus = missionCommands.addCommand('hide IADS Status', self.radioMenu, SkynetIADS.updateDisplay, {self = self, value = false, option = 'IADSStatus'})
local displayIADSStatus = missionCommands.addCommand('show contacts', self.radioMenu, SkynetIADS.updateDisplay, {self = self, value = true, option = 'contacts'})
local displayIADSStatus = missionCommands.addCommand('hide contacts', self.radioMenu, SkynetIADS.updateDisplay, {self = self, value = false, option = 'contacts'})
end
function SkynetIADS:removeRadioMenu()
missionCommands.removeItem(self.radioMenu)
end
function SkynetIADS.updateDisplay(params)
local option = params.option
local self = params.self
local value = params.value
if option == 'IADSStatus' then
self:getDebugSettings()[option] = value
elseif option == 'contacts' then
self:getDebugSettings()[option] = value
end
end
function SkynetIADS:getCoalitionString()
local coalitionStr = "RED"
if self.coalitionID == coalition.side.BLUE then
coalitionStr = "BLUE"
elseif self.coalitionID == coalition.side.NEUTRAL then
coalitionStr = "NEUTRAL"
end
if self.name then
coalitionStr = "COALITION: "..coalitionStr.." | NAME: "..self.name
end
return coalitionStr
end
function SkynetIADS:getMooseConnector()
if self.mooseConnector == nil then
self.mooseConnector = SkynetMooseA2ADispatcherConnector:create(self)
end
return self.mooseConnector
end
function SkynetIADS:addMooseSetGroup(mooseSetGroup)
self:getMooseConnector():addMooseSetGroup(mooseSetGroup)
end
end