Fixed CAP not counting correctly

* CAP now counts correctly per squadron. The specified amount of CAP
will work now.
* CAP now schedules at different start times, and have different repeat
times. More random.
This commit is contained in:
FlightControl_Master 2017-08-28 11:33:37 +02:00
parent 5be01775f7
commit 8bb3d5a760

View File

@ -1348,11 +1348,12 @@ do -- AI_A2A_DISPATCHER
---
-- @param #AI_A2A_DISPATCHER self
function AI_A2A_DISPATCHER:SetDefenderTask( Defender, Type, Fsm, Target )
function AI_A2A_DISPATCHER:SetDefenderTask( SquadronName, Defender, Type, Fsm, Target )
self.DefenderTasks[Defender] = self.DefenderTasks[Defender] or {}
self.DefenderTasks[Defender].Type = Type
self.DefenderTasks[Defender].Fsm = Fsm
self.DefenderTasks[Defender].SquadronName = SquadronName
if Target then
self:SetDefenderTaskTarget( Defender, Target )
@ -1580,14 +1581,15 @@ do -- AI_A2A_DISPATCHER
local Scheduler = Cap.Scheduler -- Core.Scheduler#SCHEDULER
local ScheduleID = Cap.ScheduleID
local Variance = ( Cap.HighInterval - Cap.LowInterval ) / 2
local Median = Cap.LowInterval + Variance
local Randomization = Variance / Median
local Repeat = Cap.LowInterval + Variance
local Randomization = Variance / Repeat
local Start = math.random( 1, Cap.HighInterval )
if ScheduleID then
Scheduler:Stop( ScheduleID )
end
Cap.ScheduleID = Scheduler:Schedule( self, self.SchedulerCAP, { SquadronName }, Start, Repeat, Randomization )
else
error( "This squadron does not exist:" .. SquadronName )
end
@ -2471,12 +2473,14 @@ do -- AI_A2A_DISPATCHER
local DefenderSquadron = self.DefenderSquadrons[SquadronName]
if DefenderSquadron then
for AIGroup, DefenderTask in pairs( self:GetDefenderTasks() ) do
if DefenderTask.Type == "CAP" then
if AIGroup:IsAlive() then
-- Check if the CAP is patrolling or engaging. If not, this is not a valid CAP, even if it is alive!
-- The CAP could be damaged, lost control, or out of fuel!
if DefenderTask.Fsm:Is( "Patrolling" ) or DefenderTask.Fsm:Is( "Engaging" ) or DefenderTask.Fsm:Is( "Refuelling" )then
CapCount = CapCount + 1
if DefenderTask.SquadronName == SquadronName then
if DefenderTask.Type == "CAP" then
if AIGroup:IsAlive() then
-- Check if the CAP is patrolling or engaging. If not, this is not a valid CAP, even if it is alive!
-- The CAP could be damaged, lost control, or out of fuel!
if DefenderTask.Fsm:Is( "Patrolling" ) or DefenderTask.Fsm:Is( "Engaging" ) or DefenderTask.Fsm:Is( "Refuelling" )then
CapCount = CapCount + 1
end
end
end
end
@ -2494,12 +2498,17 @@ do -- AI_A2A_DISPATCHER
-- First, count the active AIGroups Units, targetting the DetectedSet
local AIUnitCount = 0
self:E( "Counting Defenders Engaged for Attacker:" )
local DetectedSet = Target.Set
DetectedSet:Flush()
local DefenderTasks = self:GetDefenderTasks()
for AIGroup, DefenderTask in pairs( DefenderTasks ) do
local AIGroup = AIGroup -- Wrapper.Group#GROUP
local DefenderTask = self:GetDefenderTaskTarget( AIGroup )
if DefenderTask and DefenderTask.Index == Target.Index then
AIUnitCount = AIUnitCount + AIGroup:GetSize()
self:E( "Defender Group Name: " .. AIGroup:GetName() .. ", Size: " .. AIGroup:GetSize() )
end
end
@ -2587,7 +2596,7 @@ do -- AI_A2A_DISPATCHER
Fsm:Start()
Fsm:__Patrol( 2 )
self:SetDefenderTask( DefenderCAP, "CAP", Fsm )
self:SetDefenderTask( SquadronName, DefenderCAP, "CAP", Fsm )
end
end
end
@ -2742,7 +2751,7 @@ do -- AI_A2A_DISPATCHER
Fsm:__Engage( 2, DetectedItem.Set ) -- Engage on the TargetSetUnit
self:SetDefenderTask( DefenderGCI, "GCI", Fsm, DetectedItem )
self:SetDefenderTask( ClosestDefenderSquadronName, DefenderGCI, "GCI", Fsm, DetectedItem )
function Fsm:onafterRTB( Defender, From, Event, To )
@ -2875,14 +2884,14 @@ do -- AI_A2A_DISPATCHER
self:ClearDefenderTask( AIGroup )
else
if DefenderTask.Target then
local Target = Detection:GetDetectedItem( DefenderTask.Target.Index )
if not Target then
local AttackerItem = Detection:GetDetectedItem( DefenderTask.Target.Index )
if not AttackerItem then
self:F( { "Removing obsolete Target:", DefenderTask.Target.Index } )
self:ClearDefenderTaskTarget( AIGroup )
else
if DefenderTask.Target.Set then
if DefenderTask.Target.Set:Count() == 0 then
local AttackerCount = DefenderTask.Target.Set:Count()
if AttackerCount == 0 then
self:F( { "All Targets destroyed in Target, removing:", DefenderTask.Target.Index } )
self:ClearDefenderTaskTarget( AIGroup )
end
@ -2968,7 +2977,7 @@ do -- AI_A2A_DISPATCHER
end
Report:Add( string.format( "\n - %d Tasks", TaskCount ) )
self:T( Report:Text( "\n" ) )
self:E( Report:Text( "\n" ) )
trigger.action.outText( Report:Text( "\n" ), 25 )
end