mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-08-15 10:47:21 +00:00
Suppression + RAT
This commit is contained in:
@@ -36,11 +36,14 @@
|
||||
-- @field #number Tsuppress_min Minimum time in seconds the group gets suppressed.
|
||||
-- @field #number Tsuppress_max Maximum time in seconds the group gets suppressed.
|
||||
-- @field #number life Relative life in precent of the group.
|
||||
-- @field #number Tsuppress Time in seconds the groups is suppressed. Randomly chosen between Tsuppress_min and Tsuppress_max.
|
||||
-- @field #number TsuppressionStart Time at which the suppression started.
|
||||
-- @field #number TsuppressionOver Time at which the suppression will be over.
|
||||
-- @field #number Thit Last time the unit was hit.
|
||||
-- @field #number Nhit Number of times the unit was hit since it last was in state "CombatReady".
|
||||
-- @field Core.Zone#ZONE Zone_Retreat Zone into which a group retreats.
|
||||
-- @field #number LifeMin Life of group in percent at which the group will be ordered to retreat.
|
||||
-- @field #number LifeThreshold Life of group in percent at which the group will be ordered to retreat.
|
||||
-- @field #number IniGroupStrength Number of units in a group at start.
|
||||
-- @field #number GroupStrengthThreshold Threshold of group strength before retreat is ordered.
|
||||
-- @extends Core.Fsm#FSM_CONTROLLABLE
|
||||
--
|
||||
|
||||
@@ -54,11 +57,14 @@ AI_Suppression={
|
||||
ClassName = "AI_Suppression",
|
||||
Tsuppress_min = 5,
|
||||
Tsuppress_max = 20,
|
||||
Tsuppress = nil,
|
||||
TsuppressStart = nil,
|
||||
TsuppressOver = nil,
|
||||
Thit = nil,
|
||||
Nhit = 0,
|
||||
Zone_Retreat = nil,
|
||||
LifeMin = 25,
|
||||
LifeThreshold = 25,
|
||||
IniGroupStrength = nil,
|
||||
GroupStrengthThreshold=80,
|
||||
}
|
||||
|
||||
--- Some ID to identify who we are in output of the DCS.log file.
|
||||
@@ -67,7 +73,7 @@ AI_Suppression.id="SFX | "
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--TODO: Figure out who was shooting and move away from him <== Not possible!
|
||||
--TODO: Figure out who was shooting and move away from him.
|
||||
--TODO: Move behind a scenery building if there is one nearby.
|
||||
--TODO: Retreat to a given zone or point.
|
||||
|
||||
@@ -81,38 +87,52 @@ function AI_Suppression:New(Group)
|
||||
|
||||
-- Check that group is present.
|
||||
if Group then
|
||||
env.info("Suppression fire for group "..Group:GetName())
|
||||
env.info(AI_Suppression.id.."Suppression fire for group "..Group:GetName())
|
||||
else
|
||||
env.info("Suppression fire: Group does not exist!")
|
||||
env.info(AI_Suppression.id.."Suppression fire: Requested group does not exist! (Has to be a MOOSE group.)")
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Check that we actually have a GROUND group.
|
||||
if Group:IsGround()==false then
|
||||
env.error("Suppression fire group "..Group:GetName().." has to be a GROUND group!")
|
||||
env.error(AI_Suppression.id.."Suppression fire group "..Group:GetName().." has to be a GROUND group!")
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Inherits from FSM_CONTROLLABLE
|
||||
local self=BASE:Inherit(self, FSM_CONTROLLABLE:New()) -- #AI_Suppression
|
||||
|
||||
|
||||
-- Set the controllable for the FSM.
|
||||
self:SetControllable(Group)
|
||||
|
||||
-- Initial group strength.
|
||||
self.IniGroupStrength=#Group:GetUnits()
|
||||
|
||||
|
||||
-- Get life of group in %.
|
||||
local life_min, life_max, life_ave, groupstrength=self:_GetLife()
|
||||
|
||||
-- Group is initially in state CombatReady.
|
||||
self:SetStartState("CombatReady")
|
||||
self:SetStartState("none")
|
||||
|
||||
-- Transitions:
|
||||
---------------
|
||||
|
||||
-- Transition from anything to "Suppressed" after event "Hit".
|
||||
self:AddTransition("*", "Hit", "Suppressed")
|
||||
-- Transition from anything to "Suppressed" after event "Hit".
|
||||
self:AddTransition("*", "Start", "CombatReady")
|
||||
|
||||
-- Transition from anything to "Suppressed" after event "Hit".
|
||||
self:AddTransition("*", "Hit", "*")
|
||||
|
||||
-- Transition from "Suppressed" back to "CombatReady after the unit had time to recover.
|
||||
self:AddTransition("Suppressed", "Recovered", "CombatReady")
|
||||
self:AddTransition("*", "Recovered", "*")
|
||||
|
||||
-- Transition from "Suppressed" back to "CombatReady after the unit had time to recover.
|
||||
self:AddTransition("*", "Suppress", "Suppressed")
|
||||
|
||||
-- Transition from "Suppressed" to "Hiding" after event "Hit".
|
||||
self:AddTransition("Suppressed", "TakeCover", "Hiding")
|
||||
self:AddTransition("*", "TakeCover", "Hiding")
|
||||
|
||||
-- Transition from anything to "Retreating" after e.g. being severely damaged.
|
||||
self:AddTransition("*", "Retreat", "Retreating")
|
||||
@@ -123,12 +143,7 @@ function AI_Suppression:New(Group)
|
||||
-- Check status of the group.
|
||||
self:AddTransition("*", "Status", "*")
|
||||
|
||||
|
||||
-- Handle DCS event hit.
|
||||
self:HandleEvent(EVENTS.Hit, self.OnEventHit)
|
||||
|
||||
-- Handle DCS event dead.
|
||||
self:HandleEvent(EVENTS.Dead, self.OnEventDead)
|
||||
--self:TakeCover()
|
||||
|
||||
-- return self
|
||||
return self
|
||||
@@ -158,6 +173,21 @@ end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- After "Start" event.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:onafterStart(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("onafterStart: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Handle DCS event hit.
|
||||
self:HandleEvent(EVENTS.Hit, self._OnHit)
|
||||
|
||||
-- Handle DCS event dead.
|
||||
self:HandleEvent(EVENTS.Dead, self._OnDead)
|
||||
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Before "Status" event.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnBeforeStatus(Controlable, From, Event, To)
|
||||
@@ -174,10 +204,16 @@ function AI_Suppression:OnAfterStatus(Controlable, From, Event, To)
|
||||
self:__Status(30)
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Before "Hit" event. (Of course, this is not really before the group got hit.)
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnBeforeHit(Controlable, From, Event, To)
|
||||
-- @param Wrapper.Controllable#CONTROLLABLE Controlable Controllable of the group.
|
||||
-- @param #string From From state.
|
||||
-- @param #string Event Event.
|
||||
-- @param #string To To state.
|
||||
-- @param Core.Point#COORDINATE Fallback Fallback coordinates (or nil if no attacker could be found).
|
||||
function AI_Suppression:OnBeforeHit(Controlable, From, Event, To, Fallback)
|
||||
env.info(AI_Suppression.id..string.format("OnBeforeHit: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Increase Hit counter.
|
||||
@@ -185,16 +221,51 @@ function AI_Suppression:OnBeforeHit(Controlable, From, Event, To)
|
||||
|
||||
-- Info on hit times.
|
||||
env.info(AI_Suppression.id..string.format("Group has just been hit %d times.", self.Nhit))
|
||||
|
||||
end
|
||||
|
||||
--- After "Hit" event.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnAfterHit(Controlable, From, Event, To)
|
||||
function AI_Suppression:OnAfterHit(Controlable, From, Event, To, Fallback)
|
||||
env.info(AI_Suppression.id..string.format("OnAfterHit: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Nothing to do yet. Just monitoring the event.
|
||||
|
||||
-- Suppress fire of group.
|
||||
self:_Suppress()
|
||||
|
||||
-- Get life of group in %.
|
||||
local life_min, life_max, life_ave, groupstrength=self:_GetLife()
|
||||
|
||||
if self:is("CombatReady") then
|
||||
env.info(AI_Suppression.id..string.format("Group %s is currently CombatReady.", Controlable:GetName()))
|
||||
self:Suppress()
|
||||
elseif self:Is("Suppressed") then
|
||||
env.info(AI_Suppression.id..string.format("Group %s is currently Suppressed.", Controlable:GetName()))
|
||||
elseif self:Is("Retreating") then
|
||||
env.info(AI_Suppression.id..string.format("Group %s is currently Retreating.", Controlable:GetName()))
|
||||
elseif self:is("Hiding") then
|
||||
env.info(AI_Suppression.id..string.format("Group %s is currently Hiding.", Controlable:GetName()))
|
||||
end
|
||||
|
||||
-- After three hits fall back a bit.
|
||||
local nfallback=3
|
||||
if self.Nhit==nfallback then
|
||||
env.info(AI_Suppression.id..string.format("Group %s is falling back after %d hits.", Controlable:GetName(), nfallback))
|
||||
Fallback:SmokeGreen()
|
||||
local FallbackMarkerID=Fallback:MarkToAll("Fall back position for group "..Controlable:GetName():GetName())
|
||||
self:_FallBack(Fallback)
|
||||
end
|
||||
|
||||
-- If life of one unit is below threshold, the group is ordered to retreat (if a zone has been specified).
|
||||
if not self:Is("Retreating") then
|
||||
if groupstrength<self.GroupStrengthThreshold or (self.IniGroupStrength==1 and life_min < self.LifeThreshold) then
|
||||
self.Controllable:ClearTasks()
|
||||
self:Retreat()
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Before "Recovered" event.
|
||||
-- @param #AI_Suppression self
|
||||
@@ -204,12 +275,7 @@ function AI_Suppression:OnBeforeRecovered(Controlable, From, Event, To)
|
||||
-- Current time.
|
||||
local Tnow=timer.getTime()
|
||||
|
||||
-- Here I need to figure our how to correctly go back to "CombatReady".
|
||||
-- Problem is that another "Hit" event might occur while the group is recovering.
|
||||
-- If that happens the time to recover should be reset.
|
||||
-- Only after a unit has not been hit for X seconds.
|
||||
-- We can return false if the recovery should not be executed!
|
||||
|
||||
-- Debug info
|
||||
env.info(AI_Suppression.id..string.format("OnBeforeRecovered: Time: %d - Time over: %d", Tnow, self.TsuppressionOver))
|
||||
|
||||
-- Recovery is only possible if enough time since the last hit has passed.
|
||||
@@ -232,6 +298,7 @@ function AI_Suppression:OnAfterRecovered(Controlable, From, Event, To)
|
||||
-- Nothing to do yet. Just monitoring the event.
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Before "Retreat" event.
|
||||
-- @param #AI_Suppression self
|
||||
@@ -240,10 +307,13 @@ function AI_Suppression:OnBeforeRetreat(Controlable, From, Event, To)
|
||||
|
||||
-- Retreat is only possible if a zone has been defined by the user.
|
||||
if self.Zone_Retreat==nil then
|
||||
env.info("SFX: Retreat NOT possible! No Zone specified.")
|
||||
env.info(AI_Suppression.id.."Retreat NOT possible! No Zone specified.")
|
||||
return false
|
||||
elseif self:Is("Retreating") then
|
||||
env.info(AI_Suppression.id.."Group is already retreating.")
|
||||
return false
|
||||
else
|
||||
env.info("SFX: Retreat possible, zone specified.")
|
||||
env.info(AI_Suppression.id.."Retreat possible, zone specified.")
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -253,12 +323,18 @@ end
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnAfterRetreat(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnAfterRetreat: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Set the ALARM STATE to GREEN. Then the unit will move even if it is under fire.
|
||||
Controlable:OptionAlarmStateGreen()
|
||||
|
||||
-- Route the group to a zone.
|
||||
MESSAGE:New(string.format("Group %s is retreating!", Controlable:GetName()), 30):ToAll()
|
||||
self:_RetreatToZone(self.Zone_Retreat, 50, "Vee")
|
||||
|
||||
-- Message.
|
||||
local text=string.format("Group %s is retreating to zone %s.", Controlable:GetName(), self.Zone_Retreat:GetName())
|
||||
MESSAGE:New(text, 30):ToAll()
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Before "TakeCover" event.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnBeforeTakeCover(Controlable, From, Event, To)
|
||||
@@ -266,16 +342,18 @@ function AI_Suppression:OnBeforeTakeCover(Controlable, From, Event, To)
|
||||
|
||||
-- We search objects in a zone with radius 100 m around the group.
|
||||
-- TODO: Maybe make the zone radius larger for vehicles.
|
||||
local Zone = ZONE_GROUP:New("Zone_Hiding", Controlable, 100)
|
||||
local Zone = ZONE_GROUP:New("Zone_Hiding", Controlable, 500)
|
||||
|
||||
-- Scan for Scenery objects to run/drive to.
|
||||
Zone:Scan( Object.Category.SCENERY )
|
||||
Zone:Scan(Object.Category.SCENERY)
|
||||
|
||||
local gothideout=false
|
||||
for SceneryTypeName, SceneryData in pairs( Zone:GetScannedScenery() ) do
|
||||
for SceneryName, SceneryObject in pairs( SceneryData ) do
|
||||
local SceneryObject = SceneryObject -- Wrapper.Scenery#SCENERY
|
||||
MESSAGE:NewType( "Scenery: " .. SceneryObject:GetTypeName() .. ", Coord LL DMS: " .. SceneryObject:GetCoordinate():ToStringLLDMS(), MESSAGE.Type.Information ):ToAll()
|
||||
local text=self.Controllable:GetName().. " scenery: " .. SceneryObject:GetTypeName() .. ", Coord LL DMS: " .. SceneryObject:GetCoordinate():ToStringLLDMS()
|
||||
MESSAGE:New(text, 10):ToAll()
|
||||
env.info(AI_Suppression.id..text)
|
||||
-- TODO: Add check if scenery name matches a specific type like tree or building. This might be tricky though!
|
||||
end
|
||||
end
|
||||
@@ -287,77 +365,69 @@ end
|
||||
|
||||
--- After "TakeCover" event.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnBeforeTakeCover(Controlable, From, Event, To)
|
||||
function AI_Suppression:OnAfterTakeCover(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnAfterTakeCover: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
local text=string.format("Group %s is taking cover!", Controlable:GetName())
|
||||
MESSAGE:New(text, 30):ToAll()
|
||||
|
||||
-- Set the ALARM STATE to GREEN. Then the unit will move even if it is under fire.
|
||||
Controlable:OptionAlarmStateGreen()
|
||||
|
||||
-- Route the group to a zone.
|
||||
MESSAGE:New(string.format("Group %s would be(!) hiding now!", Controlable:GetName()), 30):ToAll()
|
||||
|
||||
--TODO: Search place to hide. For each unit (disperse) or same for all?
|
||||
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Entering "CombatReady" state. The group will be able to fight back.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnEnterCombatReady(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnEnterCombatReady: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Group can fight again.
|
||||
self.Controllable:OptionROEOpenFire()
|
||||
|
||||
-- Nothing to do yet. Just monitoring the event
|
||||
end
|
||||
|
||||
--- Leaving "CombatReady" state.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnLeaveCombatReady(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnLeaveCombatReady: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Nothing to do yet. Just monitoring the event
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Entering "Suppressed" state. Group will not fight but hold their weapons.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnEnterSuppressed(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnEnterSuppression: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Current time.
|
||||
local Tnow=timer.getTime()
|
||||
|
||||
-- Group will hold their weapons.
|
||||
Controlable:OptionROEHoldFire()
|
||||
|
||||
-- Get randomized time the unit is suppressed.
|
||||
self.Tsuppress=math.random(self.Tsuppress_min, self.Tsuppress_max)
|
||||
|
||||
-- Time at which the suppression is over.
|
||||
self.TsuppressionOver=Tnow+self.Tsuppress
|
||||
|
||||
-- Recovery event will be called in Tsuppress seconds. (We add one second to be sure the time has really passed when recovery is checked.)
|
||||
self:__Recovered(self.Tsuppress+1)
|
||||
|
||||
-- Get life of group in %.
|
||||
local life_min, life_max, life_ave=self:_GetLife()
|
||||
|
||||
-- If life of one unit is below threshold, the group is ordered to retreat (if a zone has been specified).
|
||||
if life_min < self.LifeMin then
|
||||
self:Retreat()
|
||||
end
|
||||
|
||||
env.info(AI_Suppression.id..string.format("OnEnterSuppression: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
-- Nothing to do yet. Just monitoring the event
|
||||
end
|
||||
|
||||
--- Leaving "Suppressed" state.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:OnLeaveSuppressed(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnLeaveSuppression: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Group can fight again.
|
||||
self.Controllable:OptionROEOpenFire()
|
||||
|
||||
local text=string.format("Suppression of group %s ended at %f and should have ended at %f.", self.Controllable:GetName(), timer.getTime(), self.TsuppressionOver)
|
||||
env.info(AI_Suppression.id..text)
|
||||
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Entering "Retreating" state. Group will be send to a zone.
|
||||
-- @param #AI_Suppression self
|
||||
-- @param Wrapper.Controllable#CONTROLLABLE Controlable Controllable of the AI group.
|
||||
function AI_Suppression:OnEnterRetreating(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnEnterRetreating: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Set the ALARM STATE to GREEN. Then the unit will move even if it is under fire.
|
||||
Controlable:OptionAlarmStateGreen()
|
||||
|
||||
-- Route the group to a zone.
|
||||
MESSAGE:New(string.format("Group %s is retreating!", Controlable:GetName()), 30):ToAll()
|
||||
self:_RetreatToZone(self.Zone_Retreat, 50, "Vee")
|
||||
|
||||
env.info(AI_Suppression.id..string.format("OnEnterRetreating: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
end
|
||||
|
||||
--- Leaving "Retreating" state.
|
||||
@@ -370,21 +440,13 @@ function AI_Suppression:OnLeaveRetreating(Controlable, From, Event, To)
|
||||
Controlable:OptionAlarmStateAuto()
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Entering "Hiding" state. Group will try to take cover at neargy scenery objects.
|
||||
-- @param #AI_Suppression self
|
||||
-- @param Wrapper.Controllable#CONTROLLABLE Controlable Controllable of the AI group.
|
||||
function AI_Suppression:OnEnterHiding(Controlable, From, Event, To)
|
||||
env.info(AI_Suppression.id..string.format("OnEnterHiding: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
|
||||
-- Set the ALARM STATE to GREEN. Then the unit will move even if it is under fire.
|
||||
Controlable:OptionAlarmStateGreen()
|
||||
|
||||
-- Route the group to a zone.
|
||||
MESSAGE:New(string.format("Group %s would be(!) hiding now!", Controlable:GetName()), 30):ToAll()
|
||||
|
||||
--TODO: Search place to hide. For each unit (disperse) or same for all?
|
||||
|
||||
env.info(AI_Suppression.id..string.format("OnEnterHiding: %s event %s from %s to %s", Controlable:GetName(), Event, From, To))
|
||||
end
|
||||
|
||||
--- Leaving "Hiding" state.
|
||||
@@ -397,35 +459,128 @@ function AI_Suppression:OnLeaveHiding(Controlable, From, Event, To)
|
||||
Controlable:OptionAlarmStateAuto()
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Handle the DCS event hit.
|
||||
-- @param #AI_Suppression self
|
||||
-- @param Core.Event#EVENTDATA EventData
|
||||
function AI_Suppression:OnEventHit(EventData)
|
||||
self:E({"EventHit", EventData })
|
||||
env.info("Hitevent")
|
||||
|
||||
if EventData.IniDCSUnit then
|
||||
-- Call "Hit" event.
|
||||
self:Hit()
|
||||
function AI_Suppression:_OnHit(EventData)
|
||||
self:E( {AI_Suppression.id.."_OnHit", EventData })
|
||||
--env.info(AI_Suppression.id.."Initiator : "..EventData.IniDCSGroupName)
|
||||
--env.info(AI_Suppression.id.."Target : "..EventData.TgtDCSGroupName)
|
||||
--env.info(AI_Suppression.id.."Controllable: "..self.Controllable:GetName())
|
||||
|
||||
if EventData.TgtDCSGroup then
|
||||
|
||||
local TargetGroup=EventData.TgtGroup --Wrapper.Group#GROUP
|
||||
|
||||
end
|
||||
if EventData.TgtDCSGroupName==self.Controllable:GetName() then
|
||||
|
||||
-- Figure out who shot.
|
||||
local InitiatorName="unknown"
|
||||
local Fallback=nil
|
||||
if EventData.IniDCSUnit then
|
||||
|
||||
local InitiatorUnit=EventData.IniUnit --Wrapper.Unit#UNIT
|
||||
InitiatorName=EventData.IniDCSGroupName
|
||||
|
||||
local TC=TargetGroup:GetCoordinate()
|
||||
local IC=InitiatorUnit:GetCoordinate()
|
||||
|
||||
-- Create a fall back point.
|
||||
Fallback=self:_FallBackCoord(TC, IC , 200) -- Core.Point#COORDINATE
|
||||
end
|
||||
|
||||
-- Get life of group in %.
|
||||
local life_min, life_max, life_ave, groupstrength=self:_GetLife()
|
||||
|
||||
-- Debug message.
|
||||
local text=string.format("Group %s was hit by %s. Life min=%02d %%, max=%02d %%, average=%02d %%, group=%3.0f", EventData.TgtDCSGroupName, InitiatorName, life_min, life_max, life_ave, groupstrength)
|
||||
MESSAGE:New(text, 10):ToAll()
|
||||
env.info(AI_Suppression.id..text)
|
||||
|
||||
-- Trigger Hit event.
|
||||
self:Hit(Fallback)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Handle the DCS event dead.
|
||||
-- @param #AI_Suppression self
|
||||
-- @param Core.Event#EVENTDATA EventData
|
||||
function AI_Suppression:OnEventDead(EventData)
|
||||
self:E({"EventDead", EventData })
|
||||
env.info("Deadevent")
|
||||
function AI_Suppression:_OnDead(EventData)
|
||||
self:E({AI_Suppression.id.."_OnDead", EventData})
|
||||
|
||||
if EventData.IniDCSUnit then
|
||||
--self:Died()
|
||||
end
|
||||
if EventData.IniDCSGroupName==self.Controllable:GetName() then
|
||||
|
||||
-- Number of units left in the group.
|
||||
local nunits=#self.Controllable:GetUnits()-1
|
||||
|
||||
local text=string.format("A unit from group %s just died! %d units left.", self.Controllable:GetName(), nunits)
|
||||
MESSAGE:New(text, 10):ToAll()
|
||||
env.info(AI_Suppression.id..text)
|
||||
|
||||
-- Go to stop state.
|
||||
if nunits==0 then
|
||||
self:Stop()
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
--- Suppress fire of a unit.
|
||||
-- @param #AI_Suppression self
|
||||
function AI_Suppression:_Suppress()
|
||||
|
||||
-- Current time.
|
||||
local Tnow=timer.getTime()
|
||||
|
||||
-- Controllable
|
||||
local Controlable=self.Controllable
|
||||
|
||||
-- Group will hold their weapons.
|
||||
Controlable:OptionROEHoldFire()
|
||||
|
||||
-- Get randomized time the unit is suppressed.
|
||||
local Tsuppress=math.random(self.Tsuppress_min, self.Tsuppress_max)
|
||||
|
||||
-- Time the suppression started
|
||||
self.TsuppressionStart=Tnow
|
||||
|
||||
-- Time at which the suppression is over.
|
||||
local renew=true
|
||||
if self.TsuppressionOver~=nil then
|
||||
if Tsuppress+Tnow>self.TsuppressionOver then
|
||||
self.TsuppressionOver=Tnow+Tsuppress
|
||||
else
|
||||
renew=false
|
||||
end
|
||||
else
|
||||
self.TsuppressionOver=Tnow+Tsuppress
|
||||
end
|
||||
|
||||
-- Recovery event will be called in Tsuppress seconds. (We add one second to be sure the time has really passed when recovery is checked.)
|
||||
if renew then
|
||||
self:__Recovered(self.TsuppressionOver-Tnow)
|
||||
end
|
||||
|
||||
-- Debug message.
|
||||
local text=string.format("Group %s is suppressed for %d seconds.", Controlable:GetName(), Tsuppress)
|
||||
MESSAGE:New(text, 30):ToAll()
|
||||
env.info(AI_Suppression.id..text)
|
||||
text=string.format("Suppression starts at %f and ends at %f.", Tnow, self.TsuppressionOver)
|
||||
env.info(AI_Suppression.id..text)
|
||||
|
||||
end
|
||||
|
||||
|
||||
--- Get (relative) life in percent of a group. Function returns the value of the units with the smallest and largest life. Also the average value of all groups is returned.
|
||||
-- @param #AI_Suppression self
|
||||
-- @param Wrapper.Group#GROUP group Group of unit.
|
||||
@@ -440,10 +595,12 @@ function AI_Suppression:_GetLife()
|
||||
local life_ave=0
|
||||
local n=0
|
||||
local units=group:GetUnits()
|
||||
local groupstrength=#units/self.IniGroupStrength*100
|
||||
for _,unit in pairs(units) do
|
||||
if unit then
|
||||
local unit=unit -- Wrapper.Unit#UNIT
|
||||
if unit and unit:IsActive() then
|
||||
n=n+1
|
||||
local life=unit:GetLife()/unit:GetLife0()*100
|
||||
local life=unit:GetLife()/(unit:GetLife0()+1)*100
|
||||
if life < life_min then
|
||||
life_min=life
|
||||
end
|
||||
@@ -451,10 +608,13 @@ function AI_Suppression:_GetLife()
|
||||
life_max=life
|
||||
end
|
||||
life_ave=life_ave+life
|
||||
local text=string.format("n=%d: Life = %3.1f, Life0 = %3.1f, min=%3.1f, max=%3.1f, ave=%3.1f, group=%3.1f", n, unit:GetLife(), unit:GetLife0(), life_min, life_max, life_ave/n,groupstrength)
|
||||
env.info(AI_Suppression.id..text)
|
||||
end
|
||||
end
|
||||
life_ave=life_ave/n
|
||||
return life_min, life_max, life_ave
|
||||
|
||||
return life_min, life_max, life_ave, groupstrength
|
||||
else
|
||||
return 0, 0, 0
|
||||
end
|
||||
@@ -473,12 +633,102 @@ function AI_Suppression:_RetreatToZone(zone, speed, formation)
|
||||
speed = speed or 999
|
||||
formation = formation or "Vee"
|
||||
|
||||
--
|
||||
env.info(AI_Suppression.id.."Retreat zone : "..zone:GetName())
|
||||
|
||||
-- Get a random point in the retreat zone.
|
||||
local ZonePoint=zone:GetRandomPointVec2()
|
||||
local ZoneCoord=zone:GetRandomCoordinate() -- Core.Point#COORDINATE
|
||||
local ZoneVec2=ZoneCoord:GetVec2()
|
||||
|
||||
-- Debug smoke zone and point.
|
||||
ZoneCoord:SmokeBlue()
|
||||
zone:SmokeZone(SMOKECOLOR.Red, 12)
|
||||
|
||||
-- Set task to go to zone.
|
||||
self.Controllable:TaskRouteToVec2(ZonePoint, speed, formation)
|
||||
self.Controllable:TaskRouteToVec2(ZoneVec2, speed, formation)
|
||||
|
||||
end
|
||||
|
||||
--- Determine the coordinate to which a unit should fall back.
|
||||
--@param #AI_Suppression self
|
||||
--@param Core.Point#COORDINATE a Coordinate of the defending group.
|
||||
--@param Core.Point#COORDINATE b Coordinate of the attacking group.
|
||||
--@return Core.Point#COORDINATE Fallback coordinates.
|
||||
function AI_Suppression:_FallBackCoord(a, b, distance)
|
||||
local dx = b.x-a.x
|
||||
-- take the right value for y-coordinate (if we have "alt" then "y" if not "z")
|
||||
local ay
|
||||
if a.alt then
|
||||
ay=a.y
|
||||
else
|
||||
ay=a.z
|
||||
end
|
||||
local by
|
||||
if b.alt then
|
||||
by=b.y
|
||||
else
|
||||
by=b.z
|
||||
end
|
||||
local dy = by-ay
|
||||
local angle = math.deg(math.atan2(dy,dx))
|
||||
if angle < 0 then
|
||||
angle = 360 + angle
|
||||
end
|
||||
angle=angle-180
|
||||
local fbp=a:Translate(distance, angle)
|
||||
return fbp
|
||||
end
|
||||
|
||||
|
||||
--- Fall back (move away) from enemy who is shooting on the group.
|
||||
--@param #AI_Suppression self
|
||||
--@param Core.Point#COORDINATE coord_fbp Coordinate of the fall back point.
|
||||
function AI_Suppression:_FallBack(coord_fbp)
|
||||
|
||||
local group=self.Controllable -- Wrapper.Controllable#CONTROLLABLE
|
||||
|
||||
local Waypoints = group:GetTemplateRoutePoints()
|
||||
|
||||
local coord_grp = group:GetCoordinate()
|
||||
local wp1 = coord_grp:WaypointGround(99, "Vee")
|
||||
local wp2 = coord_fbp:WaypointGround(99, "Vee")
|
||||
|
||||
table.insert(Waypoints, 1, wp1)
|
||||
table.insert(Waypoints, 2, wp2)
|
||||
|
||||
-- Condition to wait.
|
||||
local ConditionWait=group:TaskCondition(nil, nil, nil, nil, 30, nil)
|
||||
|
||||
-- Task to hold.
|
||||
local TaskHold = group:TaskHold()
|
||||
|
||||
local TaskRoute1 = group:TaskFunction("AI_Suppression._Passing_Waypoint", self, 0)
|
||||
local TaskCombo2 = {}
|
||||
TaskCombo2[#TaskCombo2+1] = group:TaskFunction("AI_Suppression._Passing_Waypoint", self, 1)
|
||||
TaskCombo2[#TaskCombo2+1] = group:TaskControlled(TaskHold, ConditionWait)
|
||||
local TaskRoute2 = group:TaskCombo(TaskCombo2)
|
||||
|
||||
group:SetTaskWaypoint(Waypoints[1], TaskRoute1)
|
||||
group:SetTaskWaypoint(Waypoints[2], TaskRoute2)
|
||||
|
||||
group:Route(Waypoints)
|
||||
|
||||
end
|
||||
|
||||
|
||||
--- Group has reached a waypoint.
|
||||
--@param #AI_Suppression self
|
||||
--@param #number i Waypoint number that has been reached.
|
||||
function AI_Suppression._Passing_Waypoint(group, Fsm, i)
|
||||
env.info(AI_Suppression.id.."Passing waypoint")
|
||||
BASE:E(group)
|
||||
BASE:E(Fsm)
|
||||
BASE:E(i)
|
||||
|
||||
MESSAGE:New(string.format("Group %s passing waypoint %d", group:GetName(), i),30):ToAll()
|
||||
if i==1 then
|
||||
MESSAGE:New(string.format("Group %s has reached fallback point.", group:GetName(), i),30):ToAll()
|
||||
end
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Reference in New Issue
Block a user