From 0f9f6153133887761596e9931b74fd6485b8c6e4 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Thu, 8 Jun 2017 11:53:53 +0200 Subject: [PATCH] Progress --- Moose Development/Moose/AI/AI_A2A.lua | 43 +++++++++++++++++-- .../Moose/AI/AI_A2A_Dispatcher.lua | 24 +++++++---- .../Moose/AI/AI_A2A_Intercept.lua | 8 +++- Moose Development/Moose/AI/AI_A2A_Patrol.lua | 2 + 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/Moose Development/Moose/AI/AI_A2A.lua b/Moose Development/Moose/AI/AI_A2A.lua index 9cdb27e70..c860f0a48 100644 --- a/Moose Development/Moose/AI/AI_A2A.lua +++ b/Moose Development/Moose/AI/AI_A2A.lua @@ -174,7 +174,7 @@ function AI_A2A:New( AIGroup ) -- @param #AI_A2A self -- @param #number Delay The delay in seconds. - self:AddTransition( "*", "RTB", "Returning" ) -- FSM_CONTROLLABLE Transition for type #AI_A2A. + self:AddTransition( "*", "RTB", "*" ) -- FSM_CONTROLLABLE Transition for type #AI_A2A. --- OnBefore Transition Handler for Event RTB. -- @function [parent=#AI_A2A] OnBeforeRTB @@ -219,6 +219,11 @@ function AI_A2A:New( AIGroup ) -- @param #string Event The Event string. -- @param #string To The To State string. + + self:AddTransition( "*", "Return", "Returning" ) + self:AddTransition( "*", "LostControl", "LostControl" ) + self:AddTransition( "*", "Fuel", "Fuel" ) + self:AddTransition( "*", "Damaged", "Damaged" ) self:AddTransition( "*", "Eject", "*" ) self:AddTransition( "*", "Crash", "Crashed" ) self:AddTransition( "*", "PilotDead", "*" ) @@ -234,6 +239,21 @@ function AI_A2A:GetDispatcher() return self.Dispatcher end +function AI_A2A:SetTargetDistance( Coordinate ) + + local CurrentCoord = self.Controllable:GetCoordinate() + self.TargetDistance = CurrentCoord:Get2DDistance( Coordinate ) + + self.ClosestTargetDistance = ( not self.ClosestTargetDistance or self.ClosestTargetDistance > self.TargetDistance ) and self.TargetDistance or self.ClosestTargetDistance +end + + +function AI_A2A:ClearTargetDistance() + + self.TargetDistance = nil + self.ClosestTargetDistance = nil +end + --- Sets (modifies) the minimum and maximum speed of the patrol. -- @param #AI_A2A self @@ -356,7 +376,7 @@ function AI_A2A:onafterStatus() local Fuel = self.Controllable:GetUnit(1):GetFuel() self:F({Fuel=Fuel}) if Fuel < self.PatrolFuelTresholdPercentage then - self:E( self.Controllable:GetName() .. " is out of fuel:" .. Fuel .. ", RTB!" ) + self:E( self.Controllable:GetName() .. " is out of fuel: " .. Fuel .. " ... RTB!" ) local OldAIControllable = self.Controllable local AIControllableTemplate = self.Controllable:GetTemplate() @@ -364,6 +384,7 @@ function AI_A2A:onafterStatus() local TimedOrbitTask = OldAIControllable:TaskControlled( OrbitTask, OldAIControllable:TaskCondition(nil,nil,nil,nil,self.PatrolOutOfFuelOrbitTime,nil ) ) OldAIControllable:SetTask( TimedOrbitTask, 10 ) + self:Fuel() RTB = true else end @@ -373,9 +394,23 @@ function AI_A2A:onafterStatus() local InitialLife = self.Controllable:GetLife0() self:F( { Damage = Damage, InitialLife = InitialLife, DamageTreshold = self.PatrolDamageTreshold } ) if ( Damage / InitialLife ) < self.PatrolDamageTreshold then - self:E( self.Controllable:GetName() .. " is damaged:" .. Damage .. ", RTB!" ) + self:E( self.Controllable:GetName() .. " is damaged: " .. Damage .. " ... RTB!" ) + self:Damaged() RTB = true end + + -- Check if planes went RTB + local TargetDistance = self.TargetDistance + local ClosestTargetDistance = self.ClosestTargetDistance + if TargetDistance then + if ClosestTargetDistance <= 40000 then + if TargetDistance > 40000 then + self:E( "Lost control of group " .. self.Controllable:GetName() .. " ... RTB!" ) + self:LostControl() + RTB = true + end + end + end if RTB == true then self:RTB() @@ -403,6 +438,8 @@ function AI_A2A:onafterRTB( AIGroup, From, Event, To ) self.CheckStatus = false + self:ClearTargetDistance() + local EngageRoute = {} --- Calculate the target route point. diff --git a/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua b/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua index b233ce2f0..cf6f0bf31 100644 --- a/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua +++ b/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua @@ -404,17 +404,27 @@ do -- AI_A2A_DISPATCHER --- -- @param #AI_A2A_DISPATCHER self function AI_A2A_DISPATCHER:ClearDefenderTaskTarget( AIGroup ) - if AIGroup:IsAlive() and self.DefenderTasks[AIGroup] then - local Target = self.DefenderTasks[AIGroup].Target - local Message = "Clearing (" .. self.DefenderTasks[AIGroup].Type .. ") " + + local DefenderTask = self:GetDefenderTask( AIGroup ) + + if AIGroup:IsAlive() and DefenderTask then + local Target = DefenderTask.Target + local Message = "Clearing (" .. DefenderTask.Type .. ") " Message = Message .. AIGroup:GetName() if Target then Message = Message .. ( Target and ( " from " .. Target.Index .. " [" .. Target.Set:Count() .. "]" ) ) or "" end self:F( { Target = Message } ) end - if AIGroup and self.DefenderTasks[AIGroup] and self.DefenderTasks[AIGroup].Target then - self.DefenderTasks[AIGroup].Target = nil + if AIGroup and DefenderTask and DefenderTask.Target then + DefenderTask.Target = nil + end + if AIGroup and DefenderTask then + if DefenderTask.Fsm:Is( "Fuel" ) + or DefenderTask.Fsm:Is( "LostControl") + or DefenderTask.Fsm:Is( "Damaged" ) then + self:ClearDefenderTask( AIGroup ) + end end return self end @@ -829,8 +839,6 @@ do -- AI_A2A_DISPATCHER -- @param #AI_A2A_DISPATCHER self function AI_A2A_DISPATCHER:onafterENGAGE( From, Event, To, Target, AIGroups ) - self:F( { AIGroups = AIGroups } ) - if AIGroups then for AIGroupID, AIGroup in pairs( AIGroups ) do @@ -1010,7 +1018,7 @@ do -- AI_A2A_DISPATCHER self:ClearDefenderTaskTarget( AIGroup ) end end - end + end end end diff --git a/Moose Development/Moose/AI/AI_A2A_Intercept.lua b/Moose Development/Moose/AI/AI_A2A_Intercept.lua index ee2fb9364..b1a6dbf76 100644 --- a/Moose Development/Moose/AI/AI_A2A_Intercept.lua +++ b/Moose Development/Moose/AI/AI_A2A_Intercept.lua @@ -335,7 +335,8 @@ end -- @param #string To The To State string. function AI_A2A_INTERCEPT:onafterAbort( AIGroup, From, Event, To ) AIGroup:ClearTasks() - self:__Route( 1 ) + self:Return() + self:__RTB( 1 ) end @@ -361,7 +362,10 @@ function AI_A2A_INTERCEPT:onafterEngage( AIGroup, From, Event, To, AttackSetUnit --- Calculate the target route point. local CurrentCoord = AIGroup:GetCoordinate() + local ToTargetCoord = self.AttackSetUnit:GetFirst():GetCoordinate() + self:SetTargetDistance( ToTargetCoord ) -- For RTB status check + local ToTargetSpeed = math.random( self.MinSpeed, self.MaxSpeed ) local ToInterceptAngle = CurrentCoord:GetAngleDegrees( CurrentCoord:GetDirectionVec3( ToTargetCoord ) ) @@ -398,6 +402,7 @@ function AI_A2A_INTERCEPT:onafterEngage( AIGroup, From, Event, To, AttackSetUnit if #AttackTasks == 0 then self:E("No targets found -> Going RTB") + self:Return() self:__RTB( 1 ) else AttackTasks[#AttackTasks+1] = AIGroup:TaskFunction( 1, #AttackTasks, "AI_A2A_INTERCEPT.InterceptRoute" ) @@ -413,6 +418,7 @@ function AI_A2A_INTERCEPT:onafterEngage( AIGroup, From, Event, To, AttackSetUnit end else self:E("No targets found -> Going RTB") + self:Return() self:__RTB( 1 ) end end diff --git a/Moose Development/Moose/AI/AI_A2A_Patrol.lua b/Moose Development/Moose/AI/AI_A2A_Patrol.lua index c721e093d..c427c3011 100644 --- a/Moose Development/Moose/AI/AI_A2A_Patrol.lua +++ b/Moose Development/Moose/AI/AI_A2A_Patrol.lua @@ -297,6 +297,8 @@ end function AI_A2A_PATROL:onafterPatrol( Controllable, From, Event, To ) self:F2() + self:ClearTargetDistance() + self:__Route( 1 ) self.Controllable:OnReSpawn(