Repository navigation
Expand file tree
/
Copy pathAirTaskingOrder.lua
More file actions
579 lines (550 loc) · 19.8 KB
/
Copy pathAirTaskingOrder.lua
File metadata and controls
579 lines (550 loc) · 19.8 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
-- Air Command system by Arctic Fox --
-- air tasking and interception system --
local utils = require("Utils")
local defs = require("Defs")
local class = require("Class")
local Orbit = require("Positions.Orbit")
local Zone = require("Positions.Zone")
local PatrolZone = require("Positions.PatrolZone")
local SquadronAirbase = require("Airbase.SquadronAirbase")
local Package = require("Flights.Package")
local Flight = require("Flights.Flight")
local Intercept = require("Missions.Intercept")
local QRA = require("Missions.QRA")
local CAP = require("Missions.CAP")
local AMBUSHCAP = require("Missions.AMBUSHCAP")
local Escort = require("Missions.Escort")
local HAVCAP = require("Missions.HAVCAP")
local Tanker = require("Missions.Tanker")
local AEW = require("Missions.AEW")
---------------------------------------------------------------------------------------------------------------------------
--[[
System handling package and flight tasking and dispatch
Parameters:
side - current coalition
airbases - list of airbases
packages - list of currently active packages
orbits - list of available support orbits
patrolZones - list of available patrol zones
ADZExclusion - list of air defence exclusion zones
]]--
local AirTaskingOrder = class()
function AirTaskingOrder:addAirbase(airbaseData)
local airbase = SquadronAirbase:new(airbaseData.name, airbaseData.takeoffHeading)
-- create squadrons if data exists
if airbaseData.squadrons ~= nil then
for key, squadronData in pairs(airbaseData.squadrons) do
airbase:addSquadron(squadronData)
end
end
table.insert(self.airbases, airbase)
end
function AirTaskingOrder:addPatrolZone(zoneData)
local zone = PatrolZone:new(zoneData)
table.insert(self.patrolZones, zone)
end
function AirTaskingOrder:addADZExclusion(zoneData)
local zone = Zone:new(zoneData)
table.insert(self.ADZExclusion, zone)
end
function AirTaskingOrder:addOrbit(orbitData)
local orbit = Orbit:new(orbitData)
table.insert(self.orbits, orbit)
end
-- modify a combat mission's chance to activate by player count
function AirTaskingOrder:playerModifier(chance)
local playerCount = utils.getPlayerNumber(self.side)
if playerCount > self.parameters.modifierPlayerCeiling then
return chance
end
if playerCount <= self.parameters.modifierPlayerFloor then
return (chance * self.parameters.maxPlayerModifier)
end
-- I dunno
local modifier =
((playerCount - self.parameters.modifierPlayerFloor) *
((1 - self.parameters.maxPlayerModifier) / (self.parameters.modifierPlayerCeiling - self.parameters.modifierPlayerFloor))) +
self.parameters.maxPlayerModifier
-- return modified chance
local finalChance = chance * modifier
if finalChance > 100 then
return 100
end
return finalChance
end
-- get modified intercept range between a minimum and maximum based on track quality between 0 and 1
function AirTaskingOrder:getInterceptRange(minRange, maxRange, trackQuality)
-- if maximum range is lower than minimum for some reason, return it unmodified
if maxRange < minRange then
return maxRange
end
local range = minRange + ((maxRange - minRange) * trackQuality)
return range
end
function AirTaskingOrder:launchSortie(airbase, squadron, mission)
local flightSize = squadron.baseFlightSize -- how many airframes we want to launch
-- random chance for more aircraft
for i = flightSize + 1, squadron.maxFlightSize do
local rand = math.random(10)
if rand > 9 then
flightSize = flightSize + 1
end
end
-- launch the flight
-- force ground start for non-intercepts
local groundStart = true
if mission.type == defs.missionType.Intercept or mission.type == defs.missionType.QRA then
groundStart = false
end
local flight = Flight:launch(airbase, squadron, mission, flightSize, groundStart)
if flight ~= nil then
-- assign aircraft parameters
flight:setParameters(self.aircraftParameters[flight:getCategory()])
-- set airfield readiness time for next intercept
if mission.type == defs.missionType.Intercept or mission.type == defs.missionType.QRA then
airbase:setPreparation(self.parameters.preparationTime)
end
-- return flight
return flight
else
-- if our flight didn't launch for whatever reason, mark airfield skip so next time we'll try again from a different one
airbase:skip(defs.skipResetTime)
return nil
end
end
-- return a random squadron for intercept mission
function AirTaskingOrder:selectInterceptorSquadron(track)
local trackPosition = track:getPosition()
local availableAirbases = {}
for key, airbase in pairs(self.airbases) do
if airbase:isActive(self.side) and timer.getTime() > airbase.readiness then
local airbaseLocation = airbase:getLocation()
local airbaseData = {
["airbase"] = airbase,
["distance"] = utils.getDistance(trackPosition.x, trackPosition.y, airbaseLocation.x, airbaseLocation.y)
}
table.insert(availableAirbases, airbaseData)
end
end
table.sort(availableAirbases, utils.sortByDistance)
-- find an applicable squadron, randomizing from the closest airbases in order
for key, airbaseData in ipairs(availableAirbases) do
local airbase = airbaseData.airbase
local airbaseLocation = airbase:getLocation()
local interceptSquadrons = {}
-- add up all the squadrons in the airbase and select a random one
for key, squadron in pairs(airbase.squadrons) do
local missionCapable
if squadron.missions[defs.missionType.Intercept] == true or (squadron.missions[defs.missionType.QRA] == true and airbaseData.distance < self.parameters.QRARadius) then
missionCapable = true
end
if missionCapable and squadron.targetCategories[track.category] then
if utils.getDistance(trackPosition.x, trackPosition.y, airbaseLocation.x, airbaseLocation.y) < self:getInterceptRange(self.parameters.minScrambleRange, squadron.interceptRadius, track:getQuality()) then
-- select squadron if appropriate for track threat type
local threatAllowed
for threatType, value in pairs(track.threatTypes) do
if value == true and squadron.threatTypes[threatType] == true then
threatAllowed = true
end
end
if threatAllowed then
table.insert(interceptSquadrons, squadron)
end
end
end
end
if utils.getTableSize(interceptSquadrons) > 0 then
local squadron = interceptSquadrons[math.random(utils.getTableSize(interceptSquadrons))]
local squadronData = {
["airbase"] = airbase,
["squadron"] = squadron
}
return squadronData
end
end
return nil
end
-- return a random squadron for a given mission
function AirTaskingOrder:selectSquadron(missionType, allowedTypes)
-- TODO: Sort by closest
local availableSquadrons = {}
for key, airbase in pairs(self.airbases) do
if airbase:isActive(self.side) ~= false then
for key, squadron in pairs(airbase.squadrons) do
if squadron.missions[missionType] == true then
if allowedTypes == nil or allowedTypes[squadron.type] == true then
local squadronData = {
["airbase"] = airbase,
["squadron"] = squadron
}
table.insert(availableSquadrons, squadronData)
end
end
end
end
end
local squadronCount = utils.getTableSize(availableSquadrons)
if squadronCount > 0 then
-- return a table containing the airbase and squadron to be used
return availableSquadrons[math.random(utils.getTableSize(availableSquadrons))]
else
return nil
end
end
-- launch escort flight
function AirTaskingOrder:escortMission(escortTarget, package, isHAVCAP)
-- select regular escort or HAVCAP
local missionType = defs.missionType.Escort
if isHAVCAP then
missionType = defs.missionType.HAVCAP
end
-- select a random squadron to provide escort
local squadronData = self:selectSquadron(missionType)
if squadronData ~= nil then
local airbase = squadronData.airbase
local squadron = squadronData.squadron
local mission
if missionType == defs.missionType.Escort then
mission = Escort:new(escortTarget)
else
mission = HAVCAP:new(escortTarget)
end
local flight = self:launchSortie(airbase, squadron, mission)
if flight ~= nil then
package:addFlight(flight)
end
end
end
-- tanker or C2 mission
function AirTaskingOrder:supportMission(missionType)
-- find all unoccupied orbits available and pick one at random
local openOrbits = {}
for key, orbit in pairs(self.orbits) do
local assigned = false
for key, package in pairs(self.packages) do
if package.aborted ~= true and package.mission.orbit == orbit then
assigned = true
end
end
if assigned == false then
-- pick compatible squadron to dispatch if this orbit is selected
local squadronData = self:selectSquadron(missionType, orbit.airframes)
if squadronData ~= nil then
local orbitData = {
["orbit"] = orbit,
["squadronData"] = squadronData
}
table.insert(openOrbits, orbitData)
end
end
end
if utils.getTableSize(openOrbits) > 0 then
-- select orbit to dispatch mission to
local orbitData = openOrbits[math.random(utils.getTableSize(openOrbits))]
-- create mission
local mission
if missionType == defs.missionType.Tanker then
mission = Tanker:new(orbitData.orbit)
elseif missionType == defs.missionType.AEW then
mission = AEW:new(orbitData.orbit)
end
-- launch support flight
local flight = self:launchSortie(orbitData.squadronData.airbase, orbitData.squadronData.squadron, mission)
-- create package
if flight ~= nil then
local package = Package:new(mission)
package:addFlight(flight)
table.insert(self.packages, package)
end
end
end
function AirTaskingOrder:patrolMission(missionType)
local orbit
-- select CAP zone
local activeZones = {}
local zoneCount = 0
for key, zone in pairs(self.patrolZones) do
if zone:isActive(self.side) then
table.insert(activeZones, zone)
zoneCount = zoneCount + 1
end
end
if zoneCount > 0 then
local zone = activeZones[math.random(zoneCount)]
-- build patrol track points
-- regular CAP faces the reference point, AMBUSHCAP runs perpendicular
if missionType ~= defs.missionType.AMBUSHCAP then
orbit = zone:getOrbit(self.parameters.CAPTrackLength)
else
orbit = zone:getPerpendicularOrbit(self.parameters.CAPTrackLength)
end
-- pick random squadron to dispatch
local squadronData = self:selectSquadron(missionType, zone.airframes)
if squadronData ~= nil then
-- create mission
local mission
if missionType == defs.missionType.CAP then
mission = CAP:new(orbit)
elseif missionType == defs.missionType.AMBUSHCAP then
mission = AMBUSHCAP:new(orbit)
end
-- launch flight
local flight = self:launchSortie(squadronData.airbase, squadronData.squadron, mission)
-- create package
if flight ~= nil then
local package = Package:new(mission)
package:addFlight(flight)
table.insert(self.packages, package)
end
end
end
end
function AirTaskingOrder:strikeMission(target)
local squadronData = self:selectInterceptorSquadron(target)
if squadronData ~= nil then
-- decide regular intercept or QRA based on distance
local mission
local airbaseLocation = squadronData.airbase:getLocation()
local distance = utils.getDistance(target.x, target.y, airbaseLocation.x, airbaseLocation.y)
if distance > self.parameters.QRARadius then
mission = Intercept:new(target)
else
mission = QRA:new(target)
end
-- launch flight
local flight = self:launchSortie(squadronData.airbase, squadronData.squadron, mission)
-- create package
if flight ~= nil then
local package = Package:new(mission)
package:addFlight(flight)
table.insert(self.packages, package)
end
end
end
function AirTaskingOrder:interceptMission(target)
local squadronData = self:selectInterceptorSquadron(target)
if squadronData ~= nil then
-- decide regular intercept or QRA based on distance
local mission
local airbaseLocation = squadronData.airbase:getLocation()
local distance = utils.getDistance(target.x, target.y, airbaseLocation.x, airbaseLocation.y)
if distance > self.parameters.QRARadius then
mission = Intercept:new(target)
else
mission = QRA:new(target)
end
-- launch flight
local flight = self:launchSortie(squadronData.airbase, squadronData.squadron, mission)
-- create package
if flight ~= nil then
local package = Package:new(mission)
package:addFlight(flight)
table.insert(self.packages, package)
end
end
end
-- loop for assigning command and control packages
function AirTaskingOrder:C2ATO()
-- dispatch C2 missions
if math.random(100) <= self.parameters.AEWChance then
self:supportMission(defs.missionType.AEW)
end
timer.scheduleFunction(self.C2ATO, self, timer.getTime() + math.random(self.parameters.minPackageTime, self.parameters.maxPackageTime))
end
-- loop for assigning logistics packages
function AirTaskingOrder:logisticsATO()
-- dispatch tanker missions
if math.random(100) <= self.parameters.tankerChance then
self:supportMission(defs.missionType.Tanker)
end
timer.scheduleFunction(self.logisticsATO, self, timer.getTime() + math.random(self.parameters.minPackageTime, self.parameters.maxPackageTime))
end
-- loop for assigning air patrol packages
function AirTaskingOrder:patrolATO()
-- dispatch CAP missions
if math.random(100) < self:playerModifier(self.parameters.CAPChance) then
if math.random(100) < self.parameters.AMBUSHChance then
self:patrolMission(defs.missionType.AMBUSHCAP)
else
self:patrolMission(defs.missionType.CAP)
end
end
timer.scheduleFunction(self.patrolATO, self, timer.getTime() + math.random(self.parameters.minPackageTime, self.parameters.maxPackageTime))
end
-- main loop for dispatching interceptors
function AirTaskingOrder:interceptATO()
-- find unengaged targets and assign interceptors
for key, track in pairs(self.trackingSystem.tracks) do
-- check if track is in an ADZ exclusion zone or engaged by other interceptors
local excluded = false
local engaged = false
for key, zone in pairs(self.ADZExclusion) do
if zone:inZone(track.x, track.y) then
excluded = true
break
end
end
for key, package in pairs(self.packages) do
for key, flight in pairs(package.flights) do
if flight.mission.targetTrack == track then
engaged = true
break
end
end
end
-- find any available escort or CAP flight for intercept before scrambling
if excluded ~= true then
for key, package in pairs(self.packages) do
for key, flight in pairs(package.flights) do
local flightTarget = flight.mission.targetTrack
if flightTarget == nil then
local flightAirborne = flight:isAirborne()
if flightAirborne and flight:allowedTargetCategory(track.category) then
local targetRange
local targetInRange = false
if flight.mission.type == defs.missionType.Escort or flight.mission.type == defs.missionType.HAVCAP then
targetRange = package:getDistance(track.x, track.y)
if targetRange < self:getInterceptRange(flight.parameters.emergencyCommitRange, flight.parameters.escortCommitRange, track:getQuality()) then
targetInRange = true
end
elseif flight.mission.type == defs.missionType.CAP then
targetRange = flight:getClosestDistance(track.x, track.y)
if targetRange < self:getInterceptRange(flight.parameters.emergencyCommitRange, flight.parameters.commitRange, track:getQuality()) then
targetInRange = true
end
elseif flight.mission.type == defs.missionType.AMBUSHCAP then
targetRange = flight:getClosestDistance(track.x, track.y)
if targetRange < self:getInterceptRange(flight.parameters.emergencyCommitRange, flight.parameters.ambushCommitRange, track:getQuality()) then
targetInRange = true
end
end
if engaged ~= true and targetInRange then
flight.mission.targetTrack = track
flight:intercept()
engaged = true
-- if target is extremely close, intercept regardless of whether it's engaged already
elseif targetRange ~= nil and targetRange < flight.parameters.emergencyCommitRange then
flight.mission.targetTrack = track
flight:intercept()
engaged = true
end
end
end
end
end
-- if an existing flight isn't available, launch interceptors
if engaged ~= true then
self:interceptMission(track)
end
end
end
timer.scheduleFunction(self.interceptATO, self, timer.getTime() + 15)
end
-- retasks interceptors to a CAP near their location
function AirTaskingOrder:retaskInterceptors(flight)
local activeZones = {}
for key, zone in pairs(self.patrolZones) do
if zone:isActive(self.side) then
table.insert(activeZones, zone)
end
end
local lowestDistance
local patrolZone
for key, zone in pairs(activeZones) do
local distance = flight:getDistance(zone.x, zone.y)
if lowestDistance == nil or distance < lowestDistance then
lowestDistance = distance
patrolZone = zone
end
end
if patrolZone == nil then
flight:ReturnToBase()
return nil
end
local orbit = patrolZone:getOrbit(self.parameters.CAPTrackLength)
return CAP:new(orbit)
end
-- TODO: Sort out this and the flight control function under package
-- loop for handling package behaviour
function AirTaskingOrder:handlePackages()
for key, package in pairs(self.packages) do
-- clean up package if there are no more flights in it
if utils.getTableSize(package.flights) < 1 then
self.packages[key] = nil
else
-- abort package if main flight is gone or refresh HAVCAP is needed
local mainFlight
local HAVCAPFlight = false
for key, flight in pairs(package.flights) do
if flight.group:isExist() ~= true then
package.flights[key] = nil
-- check if the primary flight exists
else
if flight.mission.type == package.mission.type then
mainFlight = flight
end
-- check if the HAVCAP exists
if flight.mission.type == defs.missionType.HAVCAP then
HAVCAPFlight = true
-- check if interceptors need to be retasked
elseif flight.mission.type == defs.missionType.Intercept or flight.mission.type == defs.missionType.QRA then
if flight.mission.targetTrack == nil then
local mission = self:retaskInterceptors(flight)
if mission ~= nil then
flight.mission = mission
package.mission = mission
flight:assignMission()
end
end
end
end
end
-- if there's no primary flight, abort the package
if mainFlight == nil then
if package.aborted ~= true then
package:abort()
end
-- if package requires HAVCAP but none is on station, launch new HAVCAP
else
local HAVCAPRequired = true
if HAVCAPFlight == true then
HAVCAPRequired = false
elseif defs.HAVCAPRequired[package.mission.type] ~= true then
HAVCAPRequired = false
elseif package.mission.orbit ~= nil and package.mission.orbit.HAVCAP ~= true then
HAVCAPRequired = false
end
if HAVCAPRequired then
self:escortMission(mainFlight, package, true)
end
end
end
end
timer.scheduleFunction(self.handlePackages, self, timer.getTime() + 30)
end
function AirTaskingOrder:activate(immediate)
if immediate then
self:logisticsATO()
self:C2ATO()
else
timer.scheduleFunction(self.logisticsATO, self, timer.getTime() + math.random(self.parameters.minPackageTime, self.parameters.maxPackageTime))
timer.scheduleFunction(self.C2ATO, self, timer.getTime() + math.random(self.parameters.minPackageTime, self.parameters.maxPackageTime))
end
timer.scheduleFunction(self.patrolATO, self, timer.getTime() + math.random(self.parameters.minPackageTime, self.parameters.maxPackageTime))
timer.scheduleFunction(self.interceptATO, self, timer.getTime() + 15)
timer.scheduleFunction(self.handlePackages, self, timer.getTime() + 30)
end
-- TODO: When initiating squadrons, check if any squadron is AMBUSHCAP capable - if not, disable the AMBUSHCAP check
-- (otherwise CAP flights will be unintentionally reduced)
function AirTaskingOrder:init(side, parameters, aircraftParameters, trackingSystem)
self.side = side
self.parameters = parameters
self.aircraftParameters = aircraftParameters
self.trackingSystem = trackingSystem
self.airbases = {}
self.packages = {}
self.orbits = {}
self.patrolZones = {}
self.ADZExclusion = {}
end
return AirTaskingOrder