mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-08-15 10:47:21 +00:00
503 lines
17 KiB
Lua
503 lines
17 KiB
Lua
--- **Ops** - Commander of an Airwing, Brigade or Flotilla.
|
|
--
|
|
-- **Main Features:**
|
|
--
|
|
-- * Manages AIRWINGS, BRIGADEs and FLOTILLAs
|
|
-- * Handles missions (AUFTRAG) and finds the best airwing for the job
|
|
--
|
|
-- ===
|
|
--
|
|
-- ### Author: **funkyfranky**
|
|
-- @module Ops.WingCommander
|
|
-- @image OPS_WingCommander.png
|
|
|
|
|
|
--- COMMANDER class.
|
|
-- @type COMMANDER
|
|
-- @field #string ClassName Name of the class.
|
|
-- @field #number verbose Verbosity level.
|
|
-- @field #string lid Class id string for output to DCS log file.
|
|
-- @field #table legions Table of legions which are commanded.
|
|
-- @field #table missionqueue Mission queue.
|
|
-- @field Ops.ChiefOfStaff#CHIEF chief Chief of staff.
|
|
-- @extends Core.Fsm#FSM
|
|
|
|
--- Be surprised!
|
|
--
|
|
-- ===
|
|
--
|
|
-- # The COMMANDER Concept
|
|
--
|
|
-- A wing commander is the head of legions. He will find the best AIRWING to perform an assigned AUFTRAG (mission).
|
|
--
|
|
--
|
|
-- @field #COMMANDER
|
|
COMMANDER = {
|
|
ClassName = "COMMANDER",
|
|
Debug = nil,
|
|
lid = nil,
|
|
legions = {},
|
|
missionqueue = {},
|
|
}
|
|
|
|
--- COMMANDER class version.
|
|
-- @field #string version
|
|
COMMANDER.version="0.1.0"
|
|
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
-- TODO list
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
-- TODO: Improve airwing selection. Mostly done!
|
|
-- NOGO: Maybe it's possible to preselect the assets for the mission.
|
|
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
-- Constructor
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
--- Create a new COMMANDER object and start the FSM.
|
|
-- @param #COMMANDER self
|
|
-- @return #COMMANDER self
|
|
function COMMANDER:New()
|
|
|
|
-- Inherit everything from INTEL class.
|
|
local self=BASE:Inherit(self, FSM:New()) --#COMMANDER
|
|
|
|
-- Log ID.
|
|
self.lid="COMMANDER | "
|
|
|
|
-- Start state.
|
|
self:SetStartState("NotReadyYet")
|
|
|
|
-- Add FSM transitions.
|
|
-- From State --> Event --> To State
|
|
self:AddTransition("NotReadyYet", "Start", "OnDuty") -- Start COMMANDER.
|
|
self:AddTransition("*", "Status", "*") -- Status report.
|
|
self:AddTransition("*", "Stop", "Stopped") -- Stop COMMANDER.
|
|
|
|
self:AddTransition("*", "MissionAssign", "*") -- Mission was assigned to a LEGION.
|
|
self:AddTransition("*", "MissionCancel", "*") -- Cancel mission.
|
|
|
|
------------------------
|
|
--- Pseudo Functions ---
|
|
------------------------
|
|
|
|
--- Triggers the FSM event "Start". Starts the COMMANDER.
|
|
-- @function [parent=#COMMANDER] Start
|
|
-- @param #COMMANDER self
|
|
|
|
--- Triggers the FSM event "Start" after a delay. Starts the COMMANDER.
|
|
-- @function [parent=#COMMANDER] __Start
|
|
-- @param #COMMANDER self
|
|
-- @param #number delay Delay in seconds.
|
|
|
|
--- Triggers the FSM event "Stop". Stops the COMMANDER.
|
|
-- @param #COMMANDER self
|
|
|
|
--- Triggers the FSM event "Stop" after a delay. Stops the COMMANDER.
|
|
-- @function [parent=#COMMANDER] __Stop
|
|
-- @param #COMMANDER self
|
|
-- @param #number delay Delay in seconds.
|
|
|
|
--- Triggers the FSM event "Status".
|
|
-- @function [parent=#COMMANDER] Status
|
|
-- @param #COMMANDER self
|
|
|
|
--- Triggers the FSM event "Status" after a delay.
|
|
-- @function [parent=#COMMANDER] __Status
|
|
-- @param #COMMANDER self
|
|
-- @param #number delay Delay in seconds.
|
|
|
|
|
|
--- Triggers the FSM event "MissionAssign".
|
|
-- @function [parent=#COMMANDER] MissionAssign
|
|
-- @param #COMMANDER self
|
|
-- @param Ops.Legion#LEGION Legion The Legion.
|
|
-- @param Ops.Auftrag#AUFTRAG Mission The mission.
|
|
|
|
--- On after "MissionAssign" event.
|
|
-- @function [parent=#COMMANDER] OnAfterMissionAssign
|
|
-- @param #COMMANDER self
|
|
-- @param #string From From state.
|
|
-- @param #string Event Event.
|
|
-- @param #string To To state.
|
|
-- @param Ops.Legion#LEGION Legion The Legion.
|
|
-- @param Ops.Auftrag#AUFTRAG Mission The mission.
|
|
|
|
|
|
--- Triggers the FSM event "MissionCancel".
|
|
-- @function [parent=#COMMANDER] MissionCancel
|
|
-- @param #COMMANDER self
|
|
-- @param Ops.Auftrag#AUFTRAG Mission The mission.
|
|
|
|
--- On after "MissionCancel" event.
|
|
-- @function [parent=#COMMANDER] OnAfterMissionCancel
|
|
-- @param #COMMANDER self
|
|
-- @param #string From From state.
|
|
-- @param #string Event Event.
|
|
-- @param #string To To state.
|
|
-- @param Ops.Auftrag#AUFTRAG Mission The mission.
|
|
|
|
return self
|
|
end
|
|
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
-- User functions
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
--- Add an airwing to the wingcommander.
|
|
-- @param #COMMANDER self
|
|
-- @param Ops.AirWing#AIRWING Airwing The airwing to add.
|
|
-- @return #COMMANDER self
|
|
function COMMANDER:AddAirwing(Airwing)
|
|
|
|
-- This airwing is managed by this wing commander.
|
|
Airwing.commander=self
|
|
|
|
table.insert(self.legions, Airwing)
|
|
|
|
return self
|
|
end
|
|
|
|
--- Add mission to mission queue.
|
|
-- @param #COMMANDER self
|
|
-- @param Ops.Auftrag#AUFTRAG Mission Mission to be added.
|
|
-- @return #COMMANDER self
|
|
function COMMANDER:AddMission(Mission)
|
|
|
|
Mission.commander=self
|
|
|
|
Mission.statusCommander=AUFTRAG.Status.PLANNED
|
|
|
|
table.insert(self.missionqueue, Mission)
|
|
|
|
return self
|
|
end
|
|
|
|
--- Remove mission from queue.
|
|
-- @param #COMMANDER self
|
|
-- @param Ops.Auftrag#AUFTRAG Mission Mission to be removed.
|
|
-- @return #COMMANDER self
|
|
function COMMANDER:RemoveMission(Mission)
|
|
|
|
for i,_mission in pairs(self.missionqueue) do
|
|
local mission=_mission --Ops.Auftrag#AUFTRAG
|
|
|
|
if mission.auftragsnummer==Mission.auftragsnummer then
|
|
self:I(self.lid..string.format("Removing mission %s (%s) status=%s from queue", Mission.name, Mission.type, Mission.status))
|
|
mission.commander=nil
|
|
table.remove(self.missionqueue, i)
|
|
break
|
|
end
|
|
|
|
end
|
|
|
|
return self
|
|
end
|
|
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
-- Start & Status
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
--- On after Start event. Starts the FLIGHTGROUP FSM and event handlers.
|
|
-- @param #COMMANDER self
|
|
-- @param Wrapper.Group#GROUP Group Flight group.
|
|
-- @param #string From From state.
|
|
-- @param #string Event Event.
|
|
-- @param #string To To state.
|
|
function COMMANDER:onafterStart(From, Event, To)
|
|
|
|
-- Short info.
|
|
local text=string.format("Starting Commander")
|
|
self:I(self.lid..text)
|
|
|
|
-- Start attached legions.
|
|
for _,_legion in pairs(self.legions) do
|
|
local legion=_legion --Ops.Legion#LEGION
|
|
if legion:GetState()=="NotReadyYet" then
|
|
legion:Start()
|
|
end
|
|
end
|
|
|
|
self:__Status(-1)
|
|
end
|
|
|
|
--- On after "Status" event.
|
|
-- @param #COMMANDER self
|
|
-- @param Wrapper.Group#GROUP Group Flight group.
|
|
-- @param #string From From state.
|
|
-- @param #string Event Event.
|
|
-- @param #string To To state.
|
|
function COMMANDER:onafterStatus(From, Event, To)
|
|
|
|
-- FSM state.
|
|
local fsmstate=self:GetState()
|
|
|
|
-- Check mission queue and assign one PLANNED mission.
|
|
self:CheckMissionQueue()
|
|
|
|
-- Status.
|
|
local text=string.format("Status %s: Airwings=%d, Missions=%d", fsmstate, #self.legions, #self.missionqueue)
|
|
self:I(self.lid..text)
|
|
|
|
-- Airwing Info
|
|
if #self.legions>0 then
|
|
local text="Airwings:"
|
|
for _,_airwing in pairs(self.legions) do
|
|
local airwing=_airwing --Ops.AirWing#AIRWING
|
|
local Nassets=airwing:CountAssets()
|
|
local Nastock=airwing:CountAssets(true)
|
|
text=text..string.format("\n* %s [%s]: Assets=%s stock=%s", airwing.alias, airwing:GetState(), Nassets, Nastock)
|
|
for _,aname in pairs(AUFTRAG.Type) do
|
|
local na=airwing:CountAssets(true, {aname})
|
|
local np=airwing:CountPayloadsInStock({aname})
|
|
local nm=airwing:CountAssetsOnMission({aname})
|
|
if na>0 or np>0 then
|
|
text=text..string.format("\n - %s: assets=%d, payloads=%d, on mission=%d", aname, na, np, nm)
|
|
end
|
|
end
|
|
end
|
|
self:I(self.lid..text)
|
|
end
|
|
|
|
-- Mission queue.
|
|
if #self.missionqueue>0 then
|
|
|
|
local text="Mission queue:"
|
|
for i,_mission in pairs(self.missionqueue) do
|
|
local mission=_mission --Ops.Auftrag#AUFTRAG
|
|
|
|
local target=mission:GetTargetName() or "unknown"
|
|
|
|
text=text..string.format("\n[%d] %s (%s): status=%s, target=%s", i, mission.name, mission.type, mission.status, target)
|
|
end
|
|
self:I(self.lid..text)
|
|
|
|
end
|
|
|
|
self:__Status(-30)
|
|
end
|
|
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
-- FSM Events
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
--- On after "MissionAssign" event. Mission is added to a LEGION mission queue.
|
|
-- @param #COMMANDER self
|
|
-- @param #string From From state.
|
|
-- @param #string Event Event.
|
|
-- @param #string To To state.
|
|
-- @param Ops.Legion#LEGION Legion The LEGION.
|
|
-- @param Ops.Auftrag#AUFTRAG Mission The mission.
|
|
function COMMANDER:onafterMissionAssign(From, Event, To, Legion, Mission)
|
|
|
|
-- Debug info.
|
|
self:I(self.lid..string.format("Assigning mission %s (%s) to legion %s", Mission.name, Mission.type, Legion.alias))
|
|
|
|
-- Set mission commander status to QUEUED as it is now queued at a legion.
|
|
Mission.statusCommander=AUFTRAG.Status.QUEUED
|
|
|
|
-- Add mission to legion.
|
|
Legion:AddMission(Mission)
|
|
|
|
end
|
|
|
|
--- On after "MissionCancel" event.
|
|
-- @param #COMMANDER self
|
|
-- @param #string From From state.
|
|
-- @param #string Event Event.
|
|
-- @param #string To To state.
|
|
-- @param Ops.Auftrag#AUFTRAG Mission The mission.
|
|
function COMMANDER:onafterMissionCancel(From, Event, To, Mission)
|
|
|
|
-- Debug info.
|
|
self:I(self.lid..string.format("Cancelling mission %s (%s) in status %s", Mission.name, Mission.type, Mission.status))
|
|
|
|
-- Set commander status.
|
|
Mission.statusCommander=AUFTRAG.Status.CANCELLED
|
|
|
|
if Mission:IsPlanned() then
|
|
|
|
-- Mission is still in planning stage. Should not have a legion assigned ==> Just remove it form the queue.
|
|
self:RemoveMission(Mission)
|
|
|
|
else
|
|
|
|
-- Legion will cancel mission.
|
|
if #Mission.legions>0 then
|
|
for _,_legion in pairs(Mission.legions) do
|
|
local legion=_legion --Ops.Legion#LEGION
|
|
|
|
-- TODO: Should check that this legions actually belongs to this commander.
|
|
|
|
-- Legion will cancel the mission.
|
|
legion:MissionCancel(Mission)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
-- Resources
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
--- Check mission queue and assign ONE planned mission.
|
|
-- @param #COMMANDER self
|
|
function COMMANDER:CheckMissionQueue()
|
|
|
|
-- TODO: Sort mission queue. wrt what? Threat level?
|
|
|
|
for _,_mission in pairs(self.missionqueue) do
|
|
local mission=_mission --Ops.Auftrag#AUFTRAG
|
|
|
|
-- We look for PLANNED missions.
|
|
if mission.status==AUFTRAG.Status.PLANNED then
|
|
|
|
---
|
|
-- PLANNNED Mission
|
|
---
|
|
|
|
local airwings=self:GetLegionsForMission(mission)
|
|
|
|
if airwings then
|
|
|
|
for _,airwing in pairs(airwings) do
|
|
|
|
-- Add mission to airwing.
|
|
self:MissionAssign(airwing, mission)
|
|
|
|
end
|
|
|
|
return
|
|
end
|
|
|
|
else
|
|
|
|
---
|
|
-- Missions NOT in PLANNED state
|
|
---
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
--- Check all legions if they are able to do a specific mission type at a certain location with a given number of assets.
|
|
-- @param #COMMANDER self
|
|
-- @param Ops.Auftrag#AUFTRAG Mission The mission.
|
|
-- @return #table Table of LEGIONs that can do the mission and have at least one asset available right now.
|
|
function COMMANDER:GetLegionsForMission(Mission)
|
|
|
|
-- Table of legions that can do the mission.
|
|
local legions={}
|
|
|
|
-- Loop over all legions.
|
|
for _,_airwing in pairs(self.legions) do
|
|
local airwing=_airwing --Ops.AirWing#AIRWING
|
|
|
|
-- Check if airwing can do this mission.
|
|
local can,assets=airwing:CanMission(Mission)
|
|
|
|
-- Has it assets that can?
|
|
if #assets>0 then
|
|
|
|
-- Get coordinate of the target.
|
|
local coord=Mission:GetTargetCoordinate()
|
|
|
|
if coord then
|
|
|
|
-- Distance from airwing to target.
|
|
local distance=UTILS.MetersToNM(coord:Get2DDistance(airwing:GetCoordinate()))
|
|
|
|
-- Round: 55 NM ==> 5.5 ==> 6, 63 NM ==> 6.3 ==> 6
|
|
local dist=UTILS.Round(distance/10, 0)
|
|
|
|
-- Debug info.
|
|
self:I(self.lid..string.format("Got legion %s with Nassets=%d and dist=%.1f NM, rounded=%.1f", airwing.alias, #assets, distance, dist))
|
|
|
|
-- Add airwing to table of legions that can.
|
|
table.insert(legions, {airwing=airwing, distance=distance, dist=dist, targetcoord=coord, nassets=#assets})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Can anyone?
|
|
if #legions>0 then
|
|
|
|
--- Something like:
|
|
-- * Closest airwing that can should be first prio.
|
|
-- * However, there should be a certain "quantization". if wing is 50 or 60 NM way should not really matter. In that case, the airwing with more resources should get the job.
|
|
local function score(a)
|
|
local d=math.round(a.dist/10)
|
|
end
|
|
|
|
env.info(self.lid.."FF #legions="..#legions)
|
|
|
|
-- Sort table wrt distance and number of assets.
|
|
-- Distances within 10 NM are equal and the airwing with more assets is preferred.
|
|
local function sortdist(a,b)
|
|
local ad=a.dist
|
|
local bd=b.dist
|
|
return ad<bd or (ad==bd and a.nassets>b.nassets)
|
|
end
|
|
table.sort(legions, sortdist)
|
|
|
|
|
|
-- Loops over all legions and stop if enough assets are summed up.
|
|
local selection={} ; local N=0
|
|
for _,leg in ipairs(legions) do
|
|
local legion=leg.airwing --Ops.Legion#LEGION
|
|
|
|
Mission.Nassets=Mission.Nassets or {}
|
|
Mission.Nassets[legion.alias]=leg.nassets
|
|
|
|
table.insert(selection, legion)
|
|
|
|
N=N+leg.nassets
|
|
|
|
if N>=Mission.nassets then
|
|
self:I(self.lid..string.format("Found enough assets!"))
|
|
break
|
|
end
|
|
end
|
|
|
|
if N>=Mission.nassets then
|
|
self:I(self.lid..string.format("Found %d legions that can do mission %s (%s) requiring %d assets", #selection, Mission:GetName(), Mission:GetType(), Mission.nassets))
|
|
return selection
|
|
else
|
|
self:T(self.lid..string.format("Not enough LEGIONs found that could do the job :/"))
|
|
return nil
|
|
end
|
|
|
|
else
|
|
self:T(self.lid..string.format("No LEGION found that could do the job :/"))
|
|
end
|
|
|
|
return nil
|
|
end
|
|
|
|
--- Check mission queue and assign ONE planned mission.
|
|
-- @param #COMMANDER self
|
|
-- @param #boolean InStock If true, only assets that are in the warehouse stock/inventory are counted.
|
|
-- @param #table MissionTypes (Optional) Count only assest that can perform certain mission type(s). Default is all types.
|
|
-- @param #table Attributes (Optional) Count only assest that have a certain attribute(s), e.g. `WAREHOUSE.Attribute.AIR_BOMBER`.
|
|
-- @return #number Amount of asset groups in stock.
|
|
function COMMANDER:CountAssets(InStock, MissionTypes, Attributes)
|
|
local N=0
|
|
for _,_airwing in pairs(self.legions) do
|
|
local airwing=_airwing --Ops.AirWing#AIRWING
|
|
N=N+airwing:CountAssets(InStock, MissionTypes, Attributes)
|
|
end
|
|
|
|
return N
|
|
end
|
|
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |