---- **Ops** - Chief of Staff. -- -- **Main Features:** -- -- * Stuff -- -- === -- -- ### Author: **funkyfranky** -- @module Ops.Chief -- @image OPS_Chief.png --- CHIEF class. -- @type CHIEF -- @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 targetqueue Target queue. -- @field Core.Set#SET_ZONE borderzoneset Set of zones defining the border of our territory. -- @field Core.Set#SET_ZONE yellowzoneset Set of zones defining the extended border. Defcon is set to YELLOW if enemy activity is detected. -- @field Core.Set#SET_ZONE engagezoneset Set of zones where enemies are actively engaged. -- @field #string Defcon Defence condition. -- @field Ops.Commander#COMMANDER commander Commander of assigned legions. -- @extends Ops.Intelligence#INTEL --- Be surprised! -- -- === -- -- # The CHIEF Concept -- -- The Chief of staff gathers intel and assigns missions (AUFTRAG) the airforce (WINGCOMMANDER), army (GENERAL) or navy (ADMIRAL). -- -- **Note** that currently only assignments to airborne forces (WINGCOMMANDER) are implemented. -- -- -- @field #CHIEF CHIEF = { ClassName = "CHIEF", verbose = 0, lid = nil, targetqueue = {}, borderzoneset = nil, yellowzoneset = nil, engagezoneset = nil, } --- Defence condition. -- @type CHIEF.DEFCON -- @field #string GREEN No enemy activities detected. -- @field #string YELLOW Enemy near our border. -- @field #string RED Enemy within our border. CHIEF.DEFCON = { GREEN="Green", YELLOW="Yellow", RED="Red", } --- Strategy. -- @type CHIEF.Strategy -- @field #string DEFENSIVE Only target in our own terretory are engaged. -- @field #string OFFENSIVE Targets in own terretory and yellow zones are engaged. -- @field #string AGGRESSIVE Targets in own terretroy, yellow zones and engage zones are engaged. -- @field #string TOTALWAR Anything is engaged anywhere. CHIEF.Strategy = { DEFENSIVE="Defensive", OFFENSIVE="Offensive", AGGRESSIVE="Aggressive", TOTALWAR="Total War" } --- CHIEF class version. -- @field #string version CHIEF.version="0.0.1" ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- TODO list ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- TODO: Create a good mission, which can be passed on to the COMMANDER. -- TODO: Capture OPSZONEs. -- TODO: Get list of own assets and capabilities. -- TODO: Get list/overview of enemy assets etc. -- TODO: Put all contacts into target list. Then make missions from them. -- TODO: Set of interesting zones. -- TODO: Define A2A and A2G parameters. -- DONE: Add/remove spawned flightgroups to detection set. -- DONE: Borderzones. -- NOGO: Maybe it's possible to preselect the assets for the mission. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Constructor ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Create a new CHIEF object and start the FSM. -- @param #CHIEF self -- @param Core.Set#SET_GROUP AgentSet Set of agents (groups) providing intel. Default is an empty set. -- @param #number Coalition Coalition side, e.g. `coaliton.side.BLUE`. Can also be passed as a string "red", "blue" or "neutral". -- @return #CHIEF self function CHIEF:New(AgentSet, Coalition) -- Inherit everything from INTEL class. local self=BASE:Inherit(self, INTEL:New(AgentSet, Coalition)) --#CHIEF -- Set some string id for output to DCS.log file. --self.lid=string.format("CHIEF | ") self:SetBorderZones() self:SetYellowZones() self:SetThreatLevelRange() -- Create a new COMMANDER. self.commander=COMMANDER:New() self.Defcon=CHIEF.DEFCON.GREEN -- Add FSM transitions. -- From State --> Event --> To State self:AddTransition("*", "AssignMissionAirforce", "*") -- Assign mission to a COMMANDER but request only AIR assets. self:AddTransition("*", "AssignMissionNavy", "*") -- Assign mission to a COMMANDER but request only NAVAL assets. self:AddTransition("*", "AssignMissionArmy", "*") -- Assign mission to a COMMANDER but request only GROUND assets. self:AddTransition("*", "MissionCancel", "*") -- Cancel mission. self:AddTransition("*", "Defcon", "*") -- Change defence condition. self:AddTransition("*", "Stategy", "*") -- Change strategy condition. self:AddTransition("*", "DeclareWar", "*") -- Declare War. ------------------------ --- Pseudo Functions --- ------------------------ --- Triggers the FSM event "Start". -- @function [parent=#CHIEF] Start -- @param #CHIEF self --- Triggers the FSM event "Start" after a delay. -- @function [parent=#CHIEF] __Start -- @param #CHIEF self -- @param #number delay Delay in seconds. --- Triggers the FSM event "Stop". -- @param #CHIEF self --- Triggers the FSM event "Stop" after a delay. -- @function [parent=#CHIEF] __Stop -- @param #CHIEF self -- @param #number delay Delay in seconds. --- Triggers the FSM event "Status". -- @function [parent=#CHIEF] Status -- @param #CHIEF self --- Triggers the FSM event "Status" after a delay. -- @function [parent=#CHIEF] __Status -- @param #CHIEF self -- @param #number delay Delay in seconds. --- Triggers the FSM event "MissionCancel". -- @function [parent=#CHIEF] MissionCancel -- @param #CHIEF self -- @param Ops.Auftrag#AUFTRAG Mission The mission. --- Triggers the FSM event "MissionCancel" after a delay. -- @function [parent=#CHIEF] MissionCancel -- @param #CHIEF self -- @param #number delay Delay in seconds. -- @param Ops.Auftrag#AUFTRAG Mission The mission. --- On after "MissionCancel" event. -- @function [parent=#CHIEF] OnAfterMissionCancel -- @param #CHIEF 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 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Set this to be an air-to-any dispatcher, i.e. engaging air, ground and naval targets. This is the default anyway. -- @param #CHIEF self -- @return #CHIEF self function CHIEF:SetAirToAny() self:SetFilterCategory({}) return self end --- Set this to be an air-to-air dispatcher. -- @param #CHIEF self -- @return #CHIEF self function CHIEF:SetAirToAir() self:SetFilterCategory({Unit.Category.AIRPLANE, Unit.Category.HELICOPTER}) return self end --- Set this to be an air-to-ground dispatcher, i.e. engage only ground units -- @param #CHIEF self -- @return #CHIEF self function CHIEF:SetAirToGround() self:SetFilterCategory({Unit.Category.GROUND_UNIT}) return self end --- Set this to be an air-to-sea dispatcher, i.e. engage only naval units. -- @param #CHIEF self -- @return #CHIEF self function CHIEF:SetAirToSea() self:SetFilterCategory({Unit.Category.SHIP}) return self end --- Set this to be an air-to-surface dispatcher, i.e. engaging ground and naval groups. -- @param #CHIEF self -- @return #CHIEF self function CHIEF:SetAirToSurface() self:SetFilterCategory({Unit.Category.GROUND_UNIT, Unit.Category.SHIP}) return self end --- Set a threat level range that will be engaged. Threat level is a number between 0 and 10, where 10 is a very dangerous threat. -- Targets with threat level 0 are usually harmless. -- @param #CHIEF self -- @param #number ThreatLevelMin Min threat level. Default 1. -- @param #number ThreatLevelMax Max threat level. Default 10. -- @return #CHIEF self function CHIEF:SetThreatLevelRange(ThreatLevelMin, ThreatLevelMax) self.threatLevelMin=ThreatLevelMin or 1 self.threatLevelMax=ThreatLevelMax or 10 return self end --- Set defence condition. -- @param #CHIEF self -- @param #string Defcon Defence condition. See @{#CHIEF.DEFCON}, e.g. `CHIEF.DEFCON.RED`. -- @return #CHIEF self function CHIEF:SetDefcon(Defcon) self.Defcon=Defcon --self:Defcon(Defcon) return self end --- Get the commander. -- @param #CHIEF self -- @return Ops.Commander#COMMANDER The commander. function CHIEF:GetCommander() return self.commander end --- Add mission to mission queue of the COMMANDER. -- @param #CHIEF self -- @param Ops.Auftrag#AUFTRAG Mission Mission to be added. -- @return #CHIEF self function CHIEF:AddMission(Mission) Mission.chief=self self.commander:AddMission(Mission) return self end --- Remove mission from queue. -- @param #CHIEF self -- @param Ops.Auftrag#AUFTRAG Mission Mission to be removed. -- @return #CHIEF self function CHIEF:RemoveMission(Mission) Mission.chief=nil self.commander:RemoveMission(Mission) return self end --- Add target. -- @param #CHIEF self -- @param Ops.Target#TARGET Target Target object to be added. -- @return #CHIEF self function CHIEF:AddTarget(Target) Target:SetPriority() Target:SetImportance() table.insert(self.targetqueue, Target) return self end --- Set border zone set. -- @param #CHIEF self -- @param Core.Set#SET_ZONE BorderZoneSet Set of zones, defining our borders. -- @return #CHIEF self function CHIEF:SetBorderZones(BorderZoneSet) -- Border zones. self.borderzoneset=BorderZoneSet or SET_ZONE:New() return self end --- Add a zone defining your territory. -- @param #CHIEF self -- @param Core.Zone#ZONE BorderZone The zone defining the border of your territory. -- @return #CHIEF self function CHIEF:AddBorderZone(BorderZone) -- Add a border zone. self.borderzoneset:AddZone(BorderZone) return self end --- Set yellow zone set. Detected enemy troops in this zone will trigger defence condition YELLOW. -- @param #CHIEF self -- @param Core.Set#SET_ZONE YellowZoneSet Set of zones, defining our borders. -- @return #CHIEF self function CHIEF:SetYellowZones(YellowZoneSet) -- Border zones. self.yellowzoneset=YellowZoneSet or SET_ZONE:New() return self end --- Add a zone defining an area outside your territory that is monitored for enemy activity. -- @param #CHIEF self -- @param Core.Zone#ZONE YellowZone The zone defining the border of your territory. -- @return #CHIEF self function CHIEF:AddYellowZone(YellowZone) -- Add a border zone. self.yellowzoneset:AddZone(YellowZone) return self end ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Start & Status ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- On after Start event. -- @param #CHIEF self -- @param Wrapper.Group#GROUP Group Flight group. -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. function CHIEF:onafterStart(From, Event, To) -- Short info. local text=string.format("Starting Chief of Staff") self:I(self.lid..text) -- Start parent INTEL. self:GetParent(self).onafterStart(self, From, Event, To) -- Start commander. if self.commander then if self.commander:GetState()=="NotReadyYet" then self.commander:Start() end end end --- On after "Status" event. -- @param #CHIEF self -- @param Wrapper.Group#GROUP Group Flight group. -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. function CHIEF:onafterStatus(From, Event, To) -- Start parent INTEL. self:GetParent(self).onafterStatus(self, From, Event, To) -- FSM state. local fsmstate=self:GetState() --- -- CONTACTS: Mission Cleanup --- -- Clean up missions where the contact was lost. for _,_contact in pairs(self.ContactsLost) do local contact=_contact --Ops.Intelligence#INTEL.Contact if contact.mission and contact.mission:IsNotOver() then -- Debug info. local text=string.format("Lost contact to target %s! %s mission %s will be cancelled.", contact.groupname, contact.mission.type:upper(), contact.mission.name) MESSAGE:New(text, 120, "CHIEF"):ToAll() self:I(self.lid..text) -- Cancel this mission. contact.mission:Cancel() -- TODO: contact.target end end --- -- CONTACTS: Create new TARGETS --- -- Create TARGETs for all new contacts. local Nred=0 ; local Nyellow=0 ; local Nengage=0 for _,_contact in pairs(self.Contacts) do local contact=_contact --Ops.Intelligence#INTEL.Contact local group=contact.group --Wrapper.Group#GROUP local inred=self:CheckGroupInBorder(group) if inred then Nred=Nred+1 end local inyellow=self:CheckGroupInYellow(group) if inyellow then Nyellow=Nyellow+1 end -- Is this a threat? local threat=contact.threatlevel>=self.threatLevelMin and contact.threatlevel<=self.threatLevelMax local redalert=true if self.borderzoneset:Count()>0 then redalert=inred end if redalert and threat and not contact.target then local Target=TARGET:New(contact.group) self:AddTarget(Target) end end --- -- Defcon --- -- TODO: Need to introduce time check to avoid fast oscillation between different defcon states in case groups move in and out of the zones. if Nred>0 then self:SetDefcon(CHIEF.DEFCON.RED) elseif Nyellow>0 then self:SetDefcon(CHIEF.DEFCON.YELLOW) else self:SetDefcon(CHIEF.DEFCON.GREEN) end --- -- Check Target Queue --- -- Check target queue and assign missions to new targets. self:CheckTargetQueue() --- -- Info General --- if self.verbose>=1 then local Nassets=self.commander:CountAssets() local Ncontacts=#self.contacts local Nmissions=#self.commander.missionqueue local Ntargets=#self.targetqueue -- Info message local text=string.format("Defcon=%s Assets=%d, Contacts: Total=%d Yellow=%d Red=%d, Targets=%d, Missions=%d", self.Defcon, Nassets, Ncontacts, Nyellow, Nred, Ntargets, Nmissions) self:I(self.lid..text) end --- -- Info Contacts --- -- Info about contacts. if self.verbose>=2 and #self.Contacts>0 then local text="Contacts:" for i,_contact in pairs(self.Contacts) do local contact=_contact --Ops.Intelligence#INTEL.Contact local mtext="N/A" if contact.mission then mtext=string.format("Mission %s (%s) %s", contact.mission.name, contact.mission.type, contact.mission.status:upper()) end text=text..string.format("\n[%d] %s Type=%s (%s): Threat=%d Mission=%s", i, contact.groupname, contact.categoryname, contact.typename, contact.threatlevel, mtext) end self:I(self.lid..text) end --- -- Info Targets --- if self.verbose>=3 and #self.targetqueue>0 then local text="Targets:" for i,_target in pairs(self.targetqueue) do local target=_target --Ops.Target#TARGET text=text..string.format("\n[%d] %s: Category=%s, prio=%d, importance=%d, alive=%s [%.1f/%.1f]", i, target:GetName(), target.category, target.prio, target.importance or -1, tostring(target:IsAlive()), target:GetLife(), target:GetLife0()) end self:I(self.lid..text) end --- -- Info Missions --- -- Mission queue. if self.verbose>=4 and #self.commander.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 --- -- Info Assets --- if self.verbose>=5 then local text="Assets:" for _,missiontype in pairs(AUFTRAG.Type) do local N=self.commander:CountAssets(nil, missiontype) if N>0 then text=text..string.format("\n- %s %d", missiontype, N) end end self:I(self.lid..text) local text="Assets:" for _,attribute in pairs(WAREHOUSE.Attribute) do local N=self.commander:CountAssets(nil, nil, attribute) if N>0 or self.verbose>=10 then text=text..string.format("\n- %s %d", attribute, N) end end self:I(self.lid..text) end end ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- FSM Events ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- On after "AssignMissionAssignAirforce" event. -- @param #CHIEF self -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. -- @param Ops.Auftrag#AUFTRAG Mission The mission. function CHIEF:onafterAssignMissionAirforce(From, Event, To, Mission) if self.commander then self:I(self.lid..string.format("Assigning mission %s (%s) to COMMANDER", Mission.name, Mission.type)) --TODO: Request only air assets. self.commander:AddMission(Mission) else self:E(self.lid..string.format("Mission cannot be assigned as no COMMANDER is defined!")) end end --- On after "AssignMissionAssignArmy" event. -- @param #CHIEF self -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. -- @param Ops.Auftrag#AUFTRAG Mission The mission. function CHIEF:onafterAssignMissionArmy(From, Event, To, Mission) if self.commander then self:I(self.lid..string.format("Assigning mission %s (%s) to COMMANDER", Mission.name, Mission.type)) --TODO: Request only ground assets. self.commander:AddMission(Mission) else self:E(self.lid..string.format("Mission cannot be assigned as no COMMANDER is defined!")) end end --- On after "MissionCancel" event. -- @param #CHIEF self -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. -- @param Ops.Auftrag#AUFTRAG Mission The mission. function CHIEF: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)) if Mission:IsPlanned() then -- Mission is still in planning stage. Should not have any LEGIONS assigned ==> Just remove it form the COMMANDER queue. self:RemoveMission(Mission) else -- COMMANDER will cancel mission. if Mission.commander then Mission.commander:MissionCancel(Mission) end end end --- On before "Defcon" event. -- @param #CHIEF self -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. -- @param #string Defcon New defence condition. function CHIEF:onbeforeDefcon(From, Event, To, Defcon) local gotit=false for _,defcon in pairs(CHIEF.DEFCON) do if defcon==Defcon then gotit=true end end if not gotit then self:E(self.lid..string.format("ERROR: Unknown DEFCON specified! Dont know defcon=%s", tostring(Defcon))) return false end -- Defcon did not change. if Defcon==self.Defcon then self:I(self.lid..string.format("Defcon %s unchanged. Not processing transition!", tostring(Defcon))) return false end return true end --- On after "Defcon" event. -- @param #CHIEF self -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. -- @param #string Defcon New defence condition. function CHIEF:onafterDefcon(From, Event, To, Defcon) self:I(self.lid..string.format("Changing Defcon from %s --> %s", self.Defcon, Defcon)) -- Set new defcon. self.Defcon=Defcon end --- On after "DeclareWar" event. -- @param #CHIEF self -- @param #string From From state. -- @param #string Event Event. -- @param #string To To state. -- @param #CHIEF Chief The Chief we declared war on. function CHIEF:onafterDeclareWar(From, Event, To, Chief) if Chief then self:AddWarOnChief(Chief) end end ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Target Functions ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Check target queue and assign ONE valid target by adding it to the mission queue of the COMMANDER. -- @param #CHIEF self function CHIEF:CheckTargetQueue() -- TODO: Sort mission queue. wrt what? Threat level? for _,_target in pairs(self.targetqueue) do local target=_target --Ops.Target#TARGET if target:IsAlive() and not target.mission then -- TODO: stategry self.strategy=CHIEF.Strategy.TOTALWAR local valid=false if self.strategy==CHIEF.Strategy.DEFENSIVE then if self:CheckTargetInZones(target, self.borderzoneset) then valid=true end elseif self.strategy==CHIEF.Strategy.OFFENSIVE then if self:CheckTargetInZones(target, self.borderzoneset) or self:CheckTargetInZones(target, self.yellowzoneset) then valid=true end elseif self.strategy==CHIEF.Strategy.AGGRESSIVE then if self:CheckTargetInZones(target, self.borderzoneset) or self:CheckTargetInZones(target, self.yellowzoneset) or self:CheckTargetInZones(target, self.engagezoneset) then valid=true end elseif self.strategy==CHIEF.Strategy.TOTALWAR then valid=true end -- Valid target? if valid then --TODO: Create a good mission, which can be passed on to the COMMANDER. -- Create mission. local mission=AUFTRAG:NewTargetAir(target) if mission then -- Set target mission entry. target.mission=mission -- Mission parameters. mission.prio=target.prio mission.importance=target.importance -- Add mission to COMMANDER queue. self:AddMission(mission) -- Only ONE target is assigned per check. return end end end end end ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Resources ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Check if group is inside our border. -- @param #CHIEF self -- @param Wrapper.Group#GROUP group The group. -- @return #boolean If true, group is in any zone. function CHIEF:CheckGroupInBorder(group) local inside=self:CheckGroupInZones(group, self.borderzoneset) return inside end --- Check if group is near our border (yellow zone). -- @param #CHIEF self -- @param Wrapper.Group#GROUP group The group. -- @return #boolean If true, group is in any zone. function CHIEF:CheckGroupInYellow(group) -- Check inside yellow but not inside our border. local inside=self:CheckGroupInZones(group, self.yellowzoneset) and not self:CheckGroupInZones(group, self.borderzoneset) return inside end --- Check if group is inside a zone. -- @param #CHIEF self -- @param Wrapper.Group#GROUP group The group. -- @param Core.Set#SET_ZONE zoneset Set of zones. -- @return #boolean If true, group is in any zone. function CHIEF:CheckGroupInZones(group, zoneset) for _,_zone in pairs(zoneset.Set or {}) do local zone=_zone --Core.Zone#ZONE if group:IsPartlyOrCompletelyInZone(zone) then return true end end return false end --- Check if group is inside a zone. -- @param #CHIEF self -- @param Ops.Target#TARGET target The target. -- @param Core.Set#SET_ZONE zoneset Set of zones. -- @return #boolean If true, group is in any zone. function CHIEF:CheckTargetInZones(target, zoneset) for _,_zone in pairs(zoneset.Set or {}) do local zone=_zone --Core.Zone#ZONE if zone:IsCoordinateInZone(target:GetCoordinate()) then return true end end return false end ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Resources ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Check resources. -- @param #CHIEF self -- @return #table function CHIEF:CheckResources() -- TODO: look at lower classes to do this! it's all there... local capabilities={} for _,MissionType in pairs(AUFTRAG.Type) do capabilities[MissionType]=0 for _,_airwing in pairs(self.airwings) do local airwing=_airwing --Ops.AirWing#AIRWING -- Get Number of assets that can do this type of missions. local _,assets=airwing:CanMission(MissionType) -- Add up airwing resources. capabilities[MissionType]=capabilities[MissionType]+#assets end end return capabilities end ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------