From 7f3ebe4f4812bded86e2541b4ffa54ce66d8c550 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 7 Apr 2020 01:22:06 +0200 Subject: [PATCH] Ops --- Moose Development/Moose/Ops/AirWing.lua | 16 +- Moose Development/Moose/Ops/Auftrag.lua | 5 +- Moose Development/Moose/Ops/FlightGroup.lua | 6 +- Moose Development/Moose/Ops/Squadron.lua | 12 +- Moose Development/Moose/Ops/WingCommander.lua | 293 +++++++++++++++++- 5 files changed, 310 insertions(+), 22 deletions(-) diff --git a/Moose Development/Moose/Ops/AirWing.lua b/Moose Development/Moose/Ops/AirWing.lua index cf771cd38..0503660f1 100644 --- a/Moose Development/Moose/Ops/AirWing.lua +++ b/Moose Development/Moose/Ops/AirWing.lua @@ -82,6 +82,7 @@ AIRWING = { -- @field #table pylons Pylon data extracted for the unit template. -- @field #number navail Number of available payloads of this type. -- @field #boolean unlimited If true, this payload is unlimited and does not get consumed. +-- @field #number priority Priority of the payload. --- Patrol data. -- @type AIRWING.PatrolData @@ -211,14 +212,15 @@ function AIRWING:AddSquadron(Squadron) return self end ---- Add a **new** payload to air wing resources. +--- Add a **new** payload to the airwing resources. -- @param #AIRWING self -- @param Wrapper.Unit#UNIT Unit The unit, the payload is extracted from. Can also be given as *#string* name of the unit. -- @param #table MissionTypes Mission types this payload can be used for. -- @param #number Npayloads Number of payloads to add to the airwing resources. Default 99 (which should be enough for most scenarios). -- @param #boolean Unlimited If true, this payload is unlimited. +-- @param #number Priority Priority of the loadout. Loadouts with higher priority (lower value) are used first. Default is 50. -- @return #AIRWING.Payload The payload table or nil if the unit does not exist. -function AIRWING:NewPayload(Unit, MissionTypes, Npayloads, Unlimited) +function AIRWING:NewPayload(Unit, MissionTypes, Npayloads, Unlimited, Priority) if type(Unit)=="string" then Unit=UNIT:FindByName(Unit) @@ -255,6 +257,7 @@ function AIRWING:NewPayload(Unit, MissionTypes, Npayloads, Unlimited) if Unlimited then payload.navail=1 end + payload.priority=Priority or 50 -- Add payload table.insert(self.payloads, payload) @@ -270,11 +273,20 @@ function AIRWING:NewPayload(Unit, MissionTypes, Npayloads, Unlimited) end --- Fetch a payload from the airwing resources for a given unit and mission type. +-- The payload with the highest priority is preferred. -- @param #AIRWING self -- @param #string UnitType The type of the unit. -- @param #string MissionType The mission type. -- @return #AIRWING.Payload Payload table or *nil*. function AIRWING:FetchPayloadFromStock(UnitType, MissionType) + + local function sortpayloads(a,b) + local pA=a --#AIRWING.Payload + local pB=b --#AIRWING.Payload + return pA.priority1 then local fuel=unit:GetFuel() if fuel #assets then - self:I(self.lid.."Not enough assets available") + self:I(self.lid.."INFO: Not enough assets available! Got %d but need at least %d", #assets, Mission.nassets) cando=false end diff --git a/Moose Development/Moose/Ops/WingCommander.lua b/Moose Development/Moose/Ops/WingCommander.lua index c5bd09397..99ee65e43 100644 --- a/Moose Development/Moose/Ops/WingCommander.lua +++ b/Moose Development/Moose/Ops/WingCommander.lua @@ -21,6 +21,7 @@ -- @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 defining the extended border. Defcon is set to YELLOW if enemy activity is detected. +-- @field #string Defcon Defence condition. -- @extends Ops.Intelligence#INTEL --- Be surprised! @@ -51,11 +52,11 @@ WINGCOMMANDER = { -- @extends Ops.Intelligence#INTEL.DetectedItem --- Defence condition. --- @type WINGCOMMANDER.DefCon +-- @type WINGCOMMANDER.DEFCON -- @field #string GREEN No enemy activities detected. -- @field #string YELLOW Enemy near our border. -- @field #string RED Enemy within our border. -WINGCOMMANDER.DefCon = { +WINGCOMMANDER.DEFCON = { GREEN="Green", YELLOW="Yellow", RED="Red", @@ -69,11 +70,11 @@ WINGCOMMANDER.version="0.0.3" -- TODO list ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +-- TODO: Define A2A and A2G parameters. +-- TODO: Improve airwing selection. Mostly done! -- DONE: Add/remove spawned flightgroups to detection set. --- TODO: Define A2A and A2G parameters. Engagedistance, etc. --- TODO: Borderzones. --- TODO: Improve airwing selection. Look at CAP flights near by etc. --- TODO: Maybe it's possible to preselect the assets for the mission. +-- DONE: Borderzones. +-- NOGO: Maybe it's possible to preselect the assets for the mission. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Constructor @@ -95,11 +96,17 @@ function WINGCOMMANDER:New(AgentSet, Coalition) --self.lid=string.format("WINGCOMMANDER | ") self:SetBorderZones() + self:SetYellowZones() + + self:SetThreatLevelRange() + + self.Defcon=WINGCOMMANDER.DEFCON.GREEN -- Add FSM transitions. -- From State --> Event --> To State self:AddTransition("*", "MissionAssign", "*") -- Mission was assigned to an AIRWING. self:AddTransition("*", "CancelMission", "*") -- Cancel mission. + self:AddTransition("*", "Defcon", "*") -- Cancel mission. ------------------------ --- Pseudo Functions --- @@ -149,6 +156,83 @@ end -- User functions ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +--- Set this to be an air-to-any dispatcher, i.e. engaging air, ground and navel targets. This is the default anyway. +-- @param #WINGCOMMANDER self +-- @return #WINGCOMMANDER self +function WINGCOMMANDER:SetAirToAny() + + self:SetFilterCategory({}) + + return self +end + +--- Set this to be an air-to-air dispatcher. +-- @param #WINGCOMMANDER self +-- @return #WINGCOMMANDER self +function WINGCOMMANDER: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 #WINGCOMMANDER self +-- @return #WINGCOMMANDER self +function WINGCOMMANDER: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 #WINGCOMMANDER self +-- @return #WINGCOMMANDER self +function WINGCOMMANDER: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 #WINGCOMMANDER self +-- @return #WINGCOMMANDER self +function WINGCOMMANDER: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 #WINGCOMMANDER self +-- @param #number ThreatLevelMin Min threat level. Default 1. +-- @param #number ThreatLevelMax Max threat level. Default 10. +-- @return #WINGCOMMANDER self +function WINGCOMMANDER:SetThreatLevelRange(ThreatLevelMin, ThreatLevelMax) + + self.threatLevelMin=ThreatLevelMin or 1 + self.threatLevelMax=ThreatLevelMax or 10 + + return self +end + +--- Set defence condition. +-- @param #WINGCOMMANDER self +-- @param #string Defcon Defence condition. See @{#WINGCOMMANDER.DEFCON}, e.g. `WINGCOMMANDER.DEFCON.RED`. +-- @return #WINGCOMMANDER self +function WINGCOMMANDER:SetDefcon(Defcon) + + self.Defcon=Defcon + --self:Defcon(Defcon) + + return self +end + + --- Add an airwing to the wingcommander. -- @param #WINGCOMMANDER self -- @param Ops.AirWing#AIRWING Airwing The airwing to add. @@ -196,22 +280,21 @@ function WINGCOMMANDER:RemoveMission(Mission) return self end ---- Set accept zones. Only contacts detected in this/these zone(s) are considered. +--- Set border zone set. -- @param #WINGCOMMANDER self -- @param Core.Set#SET_ZONE BorderZoneSet Set of zones, defining our borders. -- @return #WINGCOMMANDER self function WINGCOMMANDER:SetBorderZones(BorderZoneSet) + + -- Border zones. self.borderzoneset=BorderZoneSet or SET_ZONE:New() - for _,zone in pairs(self.borderzoneset.Set) do - self:AddAcceptZone(zone) - end return self end ---- Set accept zones. Only contacts detected in this zone are considered. +--- Add a zone defining your territory. -- @param #WINGCOMMANDER self --- @param Core.Zone#ZONEBorderZone Add a zone to the accept zone set. +-- @param Core.Zone#ZONE BorderZone The zone defining the border of your territory. -- @return #WINGCOMMANDER self function WINGCOMMANDER:AddBorderZone(BorderZone) @@ -219,7 +302,31 @@ function WINGCOMMANDER:AddBorderZone(BorderZone) self.borderzoneset:AddZone(BorderZone) -- Set accept zone. - self:AddAcceptZone(BorderZone) + --self:AddAcceptZone(BorderZone) + + return self +end + +--- Set yellow zone set. Detected enemy troops in this zone will trigger defence condition YELLOW. +-- @param #WINGCOMMANDER self +-- @param Core.Set#SET_ZONE YellowZoneSet Set of zones, defining our borders. +-- @return #WINGCOMMANDER self +function WINGCOMMANDER: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 #WINGCOMMANDER self +-- @param Core.Zone#ZONE YellowZone The zone defining the border of your territory. +-- @return #WINGCOMMANDER self +function WINGCOMMANDER:AddYellowZone(YellowZone) + + -- Add a border zone. + self.yellowzoneset:AddZone(YellowZone) return self end @@ -287,6 +394,7 @@ function WINGCOMMANDER:onafterStatus(From, Event, To) -- Create missions for all new contacts. + --[[ for _,_contact in pairs(self.ContactsUnknown) do local contact=_contact --#WINGCOMMANDER.Contact local group=contact.group --Wrapper.Group#GROUP @@ -308,11 +416,83 @@ function WINGCOMMANDER:onafterStatus(From, Event, To) self:AddMission(mission) end end + ]] + + -- Create missions for all new contacts. + local Nred=0 + local Nyellow=0 + local Nengage=0 + for _,_contact in pairs(self.Contacts) do + local contact=_contact --#WINGCOMMANDER.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 + + local threat=contact.threatlevel>=self.threatLevelMin and contact.threatlevel<=self.threatLevelMax + + if not contact.mission then + + -- Create a mission based on group category. + local mission=AUFTRAG:NewAUTO(group) + + + -- Add mission to queue. + if mission then + + --TODO: Better amount of necessary assets. Count units in asset and in contact. Might need nassetMin/Max. + mission.nassets=1 + + -- Set mission contact. + contact.mission=mission + + -- Add mission to queue. + self:AddMission(mission) + end + + end + + end + + -- Set 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(WINGCOMMANDER.DEFCON.RED) + elseif Nyellow>0 then + self:SetDefcon(WINGCOMMANDER.DEFCON.YELLOW) + else + self:SetDefcon(WINGCOMMANDER.DEFCON.GREEN) + end -- Check mission queue and assign one PLANNED mission. self:CheckMissionQueue() + local text=string.format("Defcon=%s Missions=%d Contacts: Total=%d Yellow=%d Red=%d", self.Defcon, #self.missionqueue, #self.Contacts, Nyellow, Nred) + self:I(self.lid..text) + + -- Infor about contacts. + if #self.Contacts>0 then + local text="Contacts:" + for i,_contact in pairs(self.Contacts) do + local contact=_contact --#WINGCOMMANDER.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 + + -- Mission queue. if #self.missionqueue>0 then local text="Mission queue:" for i,_mission in pairs(self.missionqueue) do @@ -371,6 +551,50 @@ function WINGCOMMANDER:onafterCancelMission(From, Event, To, Mission) end +--- On before "Defcon" event. +-- @param #WINGCOMMANDER self +-- @param #string From From state. +-- @param #string Event Event. +-- @param #string To To state. +-- @param #string Defcon New defence condition. +-- @param Ops.Auftrag#AUFTRAG Mission The mission. +function WINGCOMMANDER:onbeforeDefcon(From, Event, To, Defcon) + + local gotit=false + for _,defcon in pairs(WINGCOMMANDER.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. No processing transition.", tostring(Defcon))) + return false + end + + return true +end + +--- On after "Defcon" event. +-- @param #WINGCOMMANDER self +-- @param #string From From state. +-- @param #string Event Event. +-- @param #string To To state. +-- @param #string Defcon New defence condition. +-- @param Ops.Auftrag#AUFTRAG Mission The mission. +function WINGCOMMANDER: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 + ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Resources ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -379,6 +603,8 @@ end -- @param #WINGCOMMANDER self function WINGCOMMANDER:CheckMissionQueue() + -- TODO: Sort mission queue. wrt what? Threat level? + for _,_mission in pairs(self.missionqueue) do local mission=_mission --Ops.Auftrag#AUFTRAG @@ -475,6 +701,47 @@ function WINGCOMMANDER:GetAirwingForMission(Mission) return nil end +--- Check if group is inside our border. +-- @param #WINGCOMMANDER self +-- @param Wrapper.Group#GROUP group The group. +-- @return #boolean If true, group is in any zone. +function WINGCOMMANDER:CheckGroupInBorder(group) + + local inside=self:CheckGroupInZones(group, self.borderzoneset) + + return inside +end + +--- Check if group is near our border (yellow zone). +-- @param #WINGCOMMANDER self +-- @param Wrapper.Group#GROUP group The group. +-- @return #boolean If true, group is in any zone. +function WINGCOMMANDER: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 #WINGCOMMANDER 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 WINGCOMMANDER: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 resources. -- @param #WINGCOMMANDER self -- @return #table