OPSZONE and CHIEF

This commit is contained in:
Frank
2021-09-14 23:51:03 +02:00
parent f038564b1b
commit 35b50e1a9d
6 changed files with 688 additions and 137 deletions

View File

@@ -18,9 +18,18 @@
-- @field #number verbose Verbosity of output.
-- @field Core.Zone#ZONE zone The zone.
-- @field #string zoneName Name of the zone.
-- @field #number zoneRadius Radius of the zone in meters.
-- @field #number ownerCurrent Coalition of the current owner of the zone.
-- @field #number ownerPrevious Coalition of the previous owner of the zone.
-- @field Core.Timer#TIMER timerStatus Timer for calling the status update.
-- @field #number Nred Number of red units in the zone.
-- @field #number Nblu Number of blue units in the zone.
-- @field #number Nnut Number of neutral units in the zone.
-- @field #table ObjectCategories Object categories for the scan.
-- @field #table UnitCategories Unit categories for the scan.
-- @field #number Tattacked Abs. mission time stamp when an attack was started.
-- @field #number dTCapture Time interval in seconds until a zone is captured.
-- @field #boolean neutralCanCapture Neutral units can capture. Default `false`.
-- @extends Core.Fsm#FSM
--- Be surprised!
@@ -31,24 +40,32 @@
--
-- An OPSZONE is a strategically important area.
--
-- **Restrictions**
--
-- * Since we are using a DCS routine that scans a zone for units or other objects present in the zone and this DCS routine is limited to cicular zones, only those can be used.
--
-- @field #OPSZONE
OPSZONE = {
ClassName = "OPSZONE",
verbose = 3,
verbose = 0,
Nred = 0,
Nblu = 0,
Nnut = 0,
}
--- OPSZONE class version.
-- @field #string version
OPSZONE.version="0.0.1"
OPSZONE.version="0.1.0"
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- ToDo list
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- TODO: Can neutrals capture?
-- TODO: Can statics capture or hold a zone?
-- TODO: Pause/unpause evaluations.
-- TODO: Capture time, i.e. time how long a single coalition has to be inside the zone to capture it.
-- TODO: Can neutrals capture? No, since they are _neutral_!
-- TODO: Can statics capture or hold a zone? No, unless explicitly requested by mission designer.
-- TODO: Differentiate between ground attack and boming by air or arty.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -78,34 +95,139 @@ function OPSZONE:New(Zone, CoalitionOwner)
self:E("ERROR: OPSZONE must be a SPHERICAL zone due to DCS restrictions!")
return nil
end
self.zone=Zone
self.zoneName=Zone:GetName()
self.zoneRadius=Zone:GetRadius()
self.ownerCurrent=CoalitionOwner or coalition.side.NEUTRAL
self.ownerPrevious=CoalitionOwner or coalition.side.NEUTRAL
-- Set some string id for output to DCS.log file.
self.lid=string.format("OPSZONE %s | ", Zone:GetName())
-- FMS start state is PLANNED.
-- Set some values.
self.zone=Zone
self.zoneName=Zone:GetName()
self.zoneRadius=Zone:GetRadius()
-- Current and previous owners.
self.ownerCurrent=CoalitionOwner or coalition.side.NEUTRAL
self.ownerPrevious=CoalitionOwner or coalition.side.NEUTRAL
-- Set object categories.
self:SetObjectCategories()
self:SetUnitCategories()
-- Status timer.
self.timerStatus=TIMER:New(OPSZONE.Status, self)
-- FMS start state is EMPTY.
self:SetStartState("Empty")
-- Add FSM transitions.
-- From State --> Event --> To State
self:AddTransition("*", "Start", "*") -- Start FSM.
self:AddTransition("*", "Stop", "*") -- Start FSM.
self:AddTransition("*", "Stop", "*") -- Stop FSM.
self:AddTransition("*", "Captured", "Guarded") -- Start FSM.
self:AddTransition("*", "Empty", "Empty") -- Start FSM.
self:AddTransition("*", "Captured", "Guarded") -- Zone was captured.
self:AddTransition("*", "Empty", "Empty") -- No red or blue units inside the zone.
self:AddTransition("*", "Attacked", "Attacked") -- A guarded zone is under attack.
self:AddTransition("*", "Defeated", "Guarded") -- The owning coalition defeated an attack.
------------------------
--- Pseudo Functions ---
------------------------
self.timerStatus=TIMER:New(OPSZONE.Status, self)
--- Triggers the FSM event "Start".
-- @function [parent=#OPSZONE] Start
-- @param #OPSZONE self
--- Triggers the FSM event "Start" after a delay.
-- @function [parent=#OPSZONE] __Start
-- @param #OPSZONE self
-- @param #number delay Delay in seconds.
--- Triggers the FSM event "Stop".
-- @param #OPSZONE self
--- Triggers the FSM event "Stop" after a delay.
-- @function [parent=#OPSZONE] __Stop
-- @param #OPSZONE self
-- @param #number delay Delay in seconds.
--- Triggers the FSM event "Captured".
-- @function [parent=#OPSZONE] Captured
-- @param #OPSZONE self
-- @param #number Coalition Coalition side that captured the zone.
--- Triggers the FSM event "Captured" after a delay.
-- @function [parent=#OPSZONE] __Captured
-- @param #OPSZONE self
-- @param #number delay Delay in seconds.
-- @param #number Coalition Coalition side that captured the zone.
--- On after "Captured" event.
-- @function [parent=#OPSZONE] OnAfterCaptured
-- @param #OPSZONE self
-- @param #string From From state.
-- @param #string Event Event.
-- @param #string To To state.
-- @param #number Coalition Coalition side that captured the zone.
--- Triggers the FSM event "Empty".
-- @function [parent=#OPSZONE] Empty
-- @param #OPSZONE self
--- Triggers the FSM event "Empty" after a delay.
-- @function [parent=#OPSZONE] __Empty
-- @param #OPSZONE self
-- @param #number delay Delay in seconds.
--- On after "Empty" event.
-- @function [parent=#OPSZONE] OnAfterEmpty
-- @param #OPSZONE self
-- @param #string From From state.
-- @param #string Event Event.
-- @param #string To To state.
--- Triggers the FSM event "Attacked".
-- @function [parent=#OPSZONE] Attacked
-- @param #OPSZONE self
-- @param #number AttackerCoalition Coalition side that is attacking the zone.
--- Triggers the FSM event "Attacked" after a delay.
-- @function [parent=#OPSZONE] __Attacked
-- @param #OPSZONE self
-- @param #number delay Delay in seconds.
-- @param #number AttackerCoalition Coalition side that is attacking the zone.
--- On after "Attacked" event.
-- @function [parent=#OPSZONE] OnAfterAttacked
-- @param #OPSZONE self
-- @param #string From From state.
-- @param #string Event Event.
-- @param #string To To state.
-- @param #number AttackerCoalition Coalition side that is attacking the zone.
--- Triggers the FSM event "Defeated".
-- @function [parent=#OPSZONE] Defeated
-- @param #OPSZONE self
-- @param #number DefeatedCoalition Coalition side that was defeated.
--- Triggers the FSM event "Defeated" after a delay.
-- @function [parent=#OPSZONE] __Defeated
-- @param #OPSZONE self
-- @param #number delay Delay in seconds.
-- @param #number DefeatedCoalition Coalition side that was defeated.
--- On after "Defeated" event.
-- @function [parent=#OPSZONE] OnAfterDefeated
-- @param #OPSZONE self
-- @param #string From From state.
-- @param #string Event Event.
-- @param #string To To state.
-- @param #number DefeatedCoalition Coalition side that was defeated.
return self
end
@@ -114,6 +236,58 @@ end
-- User Functions
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Set verbosity level.
-- @param #OPSZONE self
-- @param #number VerbosityLevel Level of output (higher=more). Default 0.
-- @return #OPSZONE self
function OPSZONE:SetVerbosity(VerbosityLevel)
self.verbose=VerbosityLevel or 0
return self
end
--- Set categories of objects that can capture or hold the zone.
-- @param #OPSZONE self
-- @param #table Categories Object categories. Default is `{Object.Category.UNIT, Object.Category.STATIC}`, i.e. UNITs and STATICs.
-- @return #OPSZONE self
function OPSZONE:SetObjectCategories(Categories)
-- Ensure table if something was passed.
if Categories and type(Categories)~="table" then
Categories={Categories}
end
-- Set categories.
self.ObjectCategories=Categories or {Object.Category.UNIT, Object.Category.STATIC}
return self
end
--- Set categories of units that can capture or hold the zone. See [DCS Class Unit](https://wiki.hoggitworld.com/view/DCS_Class_Unit).
-- @param #OPSZONE self
-- @param #table Categories Table of unit categories. Default `{Unit.Category.GROUND_UNIT}`.
-- @return #OPSZONE
function OPSZONE:SetUnitCategories(Categories)
-- Ensure table.
if Categories and type(Categories)~="table" then
Categories={Categories}
end
-- Set categories.
self.UnitCategories=Categories or {Unit.Category.GROUND_UNIT}
return self
end
--- Set whether *neutral* units can capture the zone.
-- @param #OPSZONE self
-- @param #boolean CanCapture If `true`, neutral units can.
-- @return #OPSZONE self
function OPSZONE:SetNeutralCanCapture(CanCapture)
self.neutralCanCapture=CanCapture
return self
end
--- Get current owner of the zone.
-- @param #OPSZONE self
-- @return #number Owner coalition.
@@ -128,6 +302,19 @@ function OPSZONE:GetPreviousOwner()
return self.ownerPrevious
end
--- Get duration of the current attack.
-- @param #OPSZONE self
-- @return #number Duration in seconds since when the last attack began. Is `nil` if the zone is not under attack currently.
function OPSZONE:GetAttackDuration()
if self:IsAttacked() and self.Tattacked then
local dT=timer.getAbsTime()-self.Tattacked
return dT
end
return nil
end
--- Check if the red coalition is currently owning the zone.
-- @param #OPSZONE self
@@ -156,7 +343,7 @@ end
--- Check if zone is guarded.
-- @param #OPSZONE self
-- @return #boolean If `true`, zone is guarded.
function OPSZONE:IsEmpty()
function OPSZONE:IsGuarded()
local is=self:is("Guarded")
return is
end
@@ -186,7 +373,7 @@ end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- FSM Functions
-- Start/Stop and Status Functions
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Start OPSZONE FSM.
@@ -199,6 +386,9 @@ function OPSZONE:onafterStart(From, Event, To)
-- Info.
self:I(self.lid..string.format("Starting OPSZONE v%s", OPSZONE.version))
-- Reinit the timer.
self.timerStatus=self.timerStatus or TIMER:New(OPSZONE.Status, self)
-- Status update.
self.timerStatus:Start(1, 60)
@@ -210,10 +400,15 @@ function OPSZONE:Status()
-- Current FSM state.
local fsmstate=self:GetState()
-- Get contested.
local contested=tostring(self:IsContested())
-- Info message.
local text=string.format("State %s: Owner %d (previous %d), contested=%s, Nunits: red=%d, blue=%d, neutral=%d", fsmstate, self.ownerCurrent, self.ownerPrevious, tostring(self:IsContested()), 0, 0, 0)
self:I(self.lid..text)
if self.verbose>=1 then
local text=string.format("State %s: Owner %d (previous %d), contested=%s, Nunits: red=%d, blue=%d, neutral=%d", fsmstate, self.ownerCurrent, self.ownerPrevious, contested, self.Nred, self.Nblu, self.Nnut)
self:I(self.lid..text)
end
-- Scanning zone.
self:Scan()
@@ -233,8 +428,9 @@ end
function OPSZONE:onafterCaptured(From, Event, To, NewOwnerCoalition)
-- Debug info.
self:I(self.lid..string.format("Zone captured by %d coalition", NewOwnerCoalition))
self:T(self.lid..string.format("Zone captured by coalition=%d", NewOwnerCoalition))
-- Set owners.
self.ownerPrevious=self.ownerCurrent
self.ownerCurrent=NewOwnerCoalition
@@ -248,7 +444,7 @@ end
function OPSZONE:onafterEmpty(From, Event, To)
-- Debug info.
self:I(self.lid..string.format("Zone is empty now"))
self:T(self.lid..string.format("Zone is empty now"))
end
@@ -261,10 +457,7 @@ end
function OPSZONE:onafterAttacked(From, Event, To, AttackerCoalition)
-- Debug info.
self:I(self.lid..string.format("Zone is being attacked by coalition %s!", tostring(AttackerCoalition)))
-- Time stam when the attack started.
self.Tattacked=timer.getAbsTime()
self:T(self.lid..string.format("Zone is being attacked by coalition=%s!", tostring(AttackerCoalition)))
end
@@ -277,7 +470,7 @@ end
function OPSZONE:onafterEmpty(From, Event, To)
-- Debug info.
self:I(self.lid..string.format("Zone is empty now"))
self:T(self.lid..string.format("Zone is empty now"))
end
@@ -286,10 +479,11 @@ end
-- @param #string From From state.
-- @param #string Event Event.
-- @param #string To To state.
function OPSZONE:onafterDefeated(From, Event, To)
-- @param #number DefeatedCoalition Coalition side that was defeated.
function OPSZONE:onafterDefeated(From, Event, To, DefeatedCoalition)
-- Debug info.
self:I(self.lid..string.format("Attack on zone has been defeated"))
self:T(self.lid..string.format("Defeated attack on zone by coalition=%d", DefeatedCoalition))
-- Not attacked any more.
self.Tattacked=nil
@@ -304,8 +498,9 @@ end
function OPSZONE:onenterGuarded(From, Event, To)
-- Debug info.
self:I(self.lid..string.format("Zone is guarded"))
self:T(self.lid..string.format("Zone is guarded"))
-- Not attacked any more.
self.Tattacked=nil
end
@@ -318,9 +513,10 @@ end
function OPSZONE:onenterAttacked(From, Event, To)
-- Debug info.
self:I(self.lid..string.format("Zone is Attacked"))
self:T(self.lid..string.format("Zone is Attacked"))
self.Tattacked=nil
-- Time stamp when the attack started.
self.Tattacked=timer.getAbsTime()
end
@@ -334,14 +530,15 @@ end
function OPSZONE:Scan()
-- Debug info.
local text=string.format("Scanning zone %s R=%.1f m", self.zone:GetName(), self.zone:GetRadius())
self:I(self.lid..text)
if self.verbose>=3 then
local text=string.format("Scanning zone %s R=%.1f m", self.zoneName, self.zoneRadius)
self:I(self.lid..text)
end
-- Search.
local SphereSearch={id=world.VolumeType.SPHERE, params={point=self.zone:GetVec3(), radius=self.zone:GetRadius(),}}
local SphereSearch={id=world.VolumeType.SPHERE, params={point=self.zone:GetVec3(), radius=self.zoneRadius}}
local ObjectCategories={Object.Category.UNIT, Object.Category.STATIC}
-- Init number of red, blue and neutral units.
local Nred=0
local Nblu=0
local Nnut=0
@@ -362,12 +559,67 @@ function OPSZONE:Scan()
-- UNIT
---
-- This is a DCS unit object.
local DCSUnit=ZoneObject --DCS#Unit
--TODO: only ground units!
--- Function to check if unit category is included.
local function Included()
if not self.UnitCategories then
-- Any unit is included.
return true
else
-- Check if found object is in specified categories.
local CategoryDCSUnit = ZoneObject:getDesc().category
for _,UnitCategory in pairs(self.UnitCategories) do
if UnitCategory==CategoryDCSUnit then
return true
end
end
end
return false
end
local Coalition=DCSUnit:getCoalition()
if Included() then
-- Get Coalition.
local Coalition=DCSUnit:getCoalition()
-- Increase counter.
if Coalition==coalition.side.RED then
Nred=Nred+1
elseif Coalition==coalition.side.BLUE then
Nblu=Nblu+1
elseif Coalition==coalition.side.NEUTRAL then
Nnut=Nnut+1
end
-- Debug info.
if self.verbose>=4 then
self:I(self.lid..string.format("Found unit %s (coalition=%d)", DCSUnit:getName(), Coalition))
end
end
elseif ObjectCategory==Object.Category.STATIC and ZoneObject:isExist() then
---
-- STATIC
---
-- This is a DCS static object.
local DCSStatic=ZoneObject --DCS#Static
-- Get coalition.
local Coalition=DCSStatic:getCoalition()
-- CAREFUL! Downed pilots break routine here without any error thrown.
--local unit=STATIC:Find(DCSStatic)
-- Increase counter.
if Coalition==coalition.side.RED then
Nred=Nred+1
elseif Coalition==coalition.side.BLUE then
@@ -376,23 +628,10 @@ function OPSZONE:Scan()
Nnut=Nnut+1
end
local unit=UNIT:Find(DCSUnit)
env.info(string.format("FF found unit %s", unit:GetName()))
elseif ObjectCategory==Object.Category.STATIC and ZoneObject:isExist() then
---
-- STATIC
---
local DCSStatic=ZoneObject --DCS#Static
-- CAREFUL! Downed pilots break routine here without any error thrown.
local unit=STATIC:Find(DCSStatic)
--env.info(string.format("FF found static %s", unit:GetName()))
-- Debug info
if self.verbose>=4 then
self:I(self.lid..string.format("Found static %s (coalition=%d)", DCSStatic:getName(), Coalition))
end
elseif ObjectCategory==Object.Category.SCENERY then
@@ -403,9 +642,8 @@ function OPSZONE:Scan()
local SceneryType = ZoneObject:getTypeName()
local SceneryName = ZoneObject:getName()
local Scenery=SCENERY:Register(SceneryName, ZoneObject)
env.info(string.format("FF found scenery type=%s, name=%s", SceneryType, SceneryName))
-- Debug info.
self:T2(self.lid..string.format("Found scenery type=%s, name=%s", SceneryType, SceneryName))
end
end
@@ -414,11 +652,18 @@ function OPSZONE:Scan()
end
-- Search objects.
world.searchObjects(ObjectCategories, SphereSearch, EvaluateZone)
world.searchObjects(self.ObjectCategories, SphereSearch, EvaluateZone)
-- Debug info.
local text=string.format("Scan result Nred=%d, Nblue=%d, Nneutrl=%d", Nred, Nblu, Nnut)
self:I(self.lid..text)
if self.verbose>=3 then
local text=string.format("Scan result Nred=%d, Nblue=%d, Nneutral=%d", Nred, Nblu, Nnut)
self:I(self.lid..text)
end
-- Set values.
self.Nred=Nred
self.Nblu=Nblu
self.Nnut=Nnut
if self:IsRed() then
@@ -438,7 +683,9 @@ function OPSZONE:Scan()
self:Captured(coalition.side.NEUTRAL)
else
-- Red zone is now empty (but will remain red).
self:Empty()
if not self:IsEmpty() then
self:Empty()
end
end
else
@@ -486,7 +733,9 @@ function OPSZONE:Scan()
self:Captured(coalition.side.NEUTRAL)
else
-- Blue zone is empty now.
self:Empty()
if not self:IsEmpty() then
self:Empty()
end
end
else
@@ -530,7 +779,7 @@ function OPSZONE:Scan()
-- No neutral units in neutral zone any more.
if Nred>0 and Nblu>0 then
env.info("FF neutrals left neutral zone and red and blue are present! What to do?")
env.info(self.lid.."FF neutrals left neutral zone and red and blue are present! What to do?")
-- TODO Contested!
self:Attacked()
self.isContested=true
@@ -550,7 +799,7 @@ function OPSZONE:Scan()
--end
else
env.info("FF error")
self:E(self.lid.."ERROR!")
end
return self