AUTOLASE - added Cancel FSM to stop, docu changes, option to notify pilots in the set only

This commit is contained in:
Applevangelist 2021-10-02 14:45:37 +02:00
parent 4c3a97e2b2
commit cd79c57a27

View File

@ -1,5 +1,17 @@
--- **Functional** - Autolase targets in the field.
--
-- ===
--
-- **AUOTLASE** - Autolase targets in the field.
--
-- ===
--
-- ## Missions:
--
-- ### [Autolase](https://github.com/FlightControl-Master/MOOSE_MISSIONS/tree/develop/)
--
-- ===
--
-- **Main Features:**
--
-- * Detect and lase contacts automatically
@ -12,8 +24,8 @@
-- ### Author: **applevangelist**
-- @module Functional.Autolase
-- @image Designation.JPG
--
-- Date: 01 Oct 2021
-- Date: Oct 2021
---
--- Class AUTOLASE
@ -66,20 +78,16 @@
-- :InitLimit(1,0)
-- :SpawnScheduled(30,0.5)
--
-- ## 2.6 Example - Inform pilots about a new target:
-- ## 2.6 Example - Inform pilots about events:
--
-- function autolaser:OnAfterLasing(From,Event,To,LaserSpot)
-- local laserspot = LaserSpot -- #AUTOLASE.LaserSpot
-- local text = string.format("%s is lasing %s code %d\nat %s",laserspot.reccename,laserspot.unittype,laserspot.lasercode,laserspot.location)
-- local m = MESSAGE:New(text,15,"Autolase"):ToAll()
-- return self
-- end
-- autolaser:SetNotifyPilots(true) -- defaults to true, also shown if debug == true
-- -- Note - message are shown to pilots in the #SET_CLIENT only if using the pilotset option, else to the coalition.
--
-- @field #AUTOLASE
AUTOLASE = {
ClassName = "AUTOLASE",
lid = "",
verbose = 2,
verbose = 0,
alias = "",
debug = false,
}
@ -98,7 +106,7 @@ AUTOLASE = {
--- AUTOLASE class version.
-- @field #string version
AUTOLASE.version = "0.0.1"
AUTOLASE.version = "0.0.2"
-------------------------------------------------------------------
-- Begin Functional.Autolase.lua
@ -169,6 +177,7 @@ function AUTOLASE:New(RecceSet, Coalition, Alias, PilotSet)
self.reporttimelong = 30
self.smoketargets = false
self.smokecolor = SMOKECOLOR.Red
self.notifypilots = true
-- Set some string id for output to DCS.log file.
self.lid=string.format("AUTOLASE %s (%s) | ", self.alias, self.coalition and UTILS.GetCoalitionName(self.coalition) or "unknown")
@ -181,6 +190,7 @@ function AUTOLASE:New(RecceSet, Coalition, Alias, PilotSet)
self:AddTransition("*", "TargetDestroyed", "*") -- Target destroyed
self:AddTransition("*", "RecceKIA", "*") -- Recce KIA
self:AddTransition("*", "LaserTimeout", "*") -- Laser timed out
self:AddTransition("*", "Cancel", "*") -- Stop Autolase
-- Menu Entry
if not PilotSet then
@ -195,7 +205,7 @@ function AUTOLASE:New(RecceSet, Coalition, Alias, PilotSet)
self:SetClusterAnalysis(false, false)
self:__Start(2)
self:__Monitor(-5)
self:__Monitor(math.random(5,10))
return self
@ -204,12 +214,21 @@ function AUTOLASE:New(RecceSet, Coalition, Alias, PilotSet)
------------------------
--- Triggers the FSM event "Monitor".
-- @function [parent=#INTEL] Status
-- @param #INTEL self
-- @function [parent=#AUTOLASE] Status
-- @param #AUTOLASE self
--- Triggers the FSM event "Monitor" after a delay.
-- @function [parent=#INTEL] __Status
-- @param #INTEL self
-- @function [parent=#AUTOLASE] __Status
-- @param #AUTOLASE self
-- @param #number delay Delay in seconds.
--- Triggers the FSM event "Cancel".
-- @function [parent=#AUTOLASE] Cancel
-- @param #AUTOLASE self
--- Triggers the FSM event "Cancel" after a delay.
-- @function [parent=#AUTOLASE] __Cancel
-- @param #AUTOLASE self
-- @param #number delay Delay in seconds.
--- On After "RecceKIA" event.
@ -270,7 +289,7 @@ function AUTOLASE:SetPilotMenu()
local Unit = _unit -- Wrapper.Unit#UNIT
if Unit and Unit:IsAlive() then
local Group = Unit:GetGroup()
local lasemenu = MENU_GROUP_COMMAND:New(Group,"Autolase",nil,self.ShowStatus,self,Group)
local lasemenu = MENU_GROUP_COMMAND:New(Group,"Autolase Status",nil,self.ShowStatus,self,Group)
lasemenu:Refresh()
end
end
@ -310,6 +329,15 @@ function AUTOLASE:SetMaxLasingTargets(Number)
return self
end
--- Function set notify pilots on events
-- @param #AUTOLASE self
-- @param #boolean OnOff Switch messaging on (true) or off (false)
-- @return #AUTOLASE self
function AUTOLASE:SetNotifyPilots(OnOff)
self.notifypilots = OnOff and true
return self
end
--- (User) Function to set a specific code to a Recce.
-- @param #AUTOLASE self
-- @param #string RecceName Name of the Recce
@ -458,6 +486,29 @@ function AUTOLASE:ShowStatus(Group)
return self
end
--- (Internal) Function to show messages.
-- @param #AUTOLASE self
-- @param #string Message The message to be sent
-- @param #number Duration Duration in seconds
-- @return #AUTOLASE self
function AUTOLASE:NotifyPilots(Message,Duration)
if self.usepilotset then
local pilotset = self.pilotset:GetSetObjects() --#table
for _,_pilot in pairs(pilotset) do
local pilot = _pilot -- Wrapper.Unit#UNIT
if pilot and pilot:IsAlive() then
local Group = pilot:GetGroup()
local m = MESSAGE:New(Message,Duration,"Autolase"):ToGroup(Group)
end
end
elseif not self.debug then
local m = MESSAGE:New(Message,Duration,"Autolase"):ToCoalition(self.coalition)
else
local m = MESSAGE:New(Message,Duration,"Autolase"):ToAll()
end
return self
end
-------------------------------------------------------------------
-- FSM Functions
-------------------------------------------------------------------
@ -474,6 +525,8 @@ function AUTOLASE:onafterMonitor(From, Event, To)
-- Housekeeping
local countlases = self:CleanCurrentLasing()
self:SetPilotMenu()
local detecteditems = self.Contacts or {} -- #table of Ops.Intelligence#INTEL.Contact
local groupsbythreat = {}
--self:T("Detected Items:")
@ -596,7 +649,7 @@ function AUTOLASE:onafterMonitor(From, Event, To)
end
end
self:__Monitor(-20)
self:__Monitor(-30)
return self
end
@ -609,8 +662,10 @@ end
-- @return #AUTOLASE self
function AUTOLASE:onbeforeRecceKIA(From,Event,To,RecceName)
self:T({From, Event, To, RecceName})
local text = string.format("Recce %s KIA!",RecceName)
local m = MESSAGE:New(text,self.reporttimeshort,"Autolase"):ToAllIf(self.debug)
if self.notifypilots or self.debug then
local text = string.format("Recce %s KIA!",RecceName)
self:NotifyPilots(text,self.reporttimeshort)
end
return self
end
@ -624,8 +679,10 @@ end
-- @return #AUTOLASE self
function AUTOLASE:onbeforeTargetDestroyed(From,Event,To,UnitName,RecceName)
self:T({From, Event, To, UnitName, RecceName})
local text = string.format("Unit %s destroyed! Good job!",UnitName)
local m = MESSAGE:New(text,self.reporttimeshort,"Autolase"):ToAllIf(self.debug)
if self.notifypilots or self.debug then
local text = string.format("Unit %s destroyed! Good job!",UnitName)
self:NotifyPilots(text,self.reporttimeshort)
end
return self
end
@ -639,8 +696,10 @@ end
-- @return #AUTOLASE self
function AUTOLASE:onbeforeTargetLost(From,Event,To,UnitName,RecceName)
self:T({From, Event, To, UnitName,RecceName})
local text = string.format("%s lost sight of unit %s.",RecceName,UnitName)
local m = MESSAGE:New(text,self.reporttimeshort,"Autolase"):ToAllIf(self.debug)
if self.notifypilots or self.debug then
local text = string.format("%s lost sight of unit %s.",RecceName,UnitName)
self:NotifyPilots(text,self.reporttimeshort)
end
return self
end
@ -654,8 +713,10 @@ end
-- @return #AUTOLASE self
function AUTOLASE:onbeforeLaserTimeout(From,Event,To,UnitName,RecceName)
self:T({From, Event, To, UnitName,RecceName})
local text = string.format("%s laser timeout on unit %s.",RecceName,UnitName)
local m = MESSAGE:New(text,self.reporttimeshort,"Autolase"):ToAllIf(self.debug)
if self.notifypilots or self.debug then
local text = string.format("%s laser timeout on unit %s.",RecceName,UnitName)
self:NotifyPilots(text,self.reporttimeshort)
end
return self
end
@ -668,9 +729,23 @@ end
-- @return #AUTOLASE self
function AUTOLASE:onbeforeLasing(From,Event,To,LaserSpot)
self:T({From, Event, To, LaserSpot.unittype})
local laserspot = LaserSpot -- #AUTOLASE.LaserSpot
local text = string.format("%s is lasing %s code %d\nat %s",laserspot.reccename,laserspot.unittype,laserspot.lasercode,laserspot.location)
local m = MESSAGE:New(text,self.reporttimeshort,"Autolase"):ToAllIf(self.debug)
if self.notifypilots or self.debug then
local laserspot = LaserSpot -- #AUTOLASE.LaserSpot
local text = string.format("%s is lasing %s code %d\nat %s",laserspot.reccename,laserspot.unittype,laserspot.lasercode,laserspot.location)
self:NotifyPilots(text,self.reporttimeshort)
end
return self
end
--- (Internal) FSM Function onbeforeCancel
-- @param #AUTOLASE self
-- @param #string From The from state
-- @param #string Event The event
-- @param #string To The to state
-- @return #AUTOLASE self
function AUTOLASE:onbeforeCancel(From,Event,To)
self:UnHandleEvent(EVENTS.PlayerEnterAircraft)
self:__Stop(2)
return self
end