From d8ba37af8d0ed60b90a654ced38c38c9a489fe93 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Tue, 9 May 2017 09:29:08 +0200 Subject: [PATCH] When cargo is destroyed, it will stop working... --- Moose Development/Moose/Core/Cargo.lua | 59 ++++- .../Moose/Tasking/Task_CARGO.lua | 209 ++++++++++-------- 2 files changed, 170 insertions(+), 98 deletions(-) diff --git a/Moose Development/Moose/Core/Cargo.lua b/Moose Development/Moose/Core/Cargo.lua index 281d81224..88635422f 100644 --- a/Moose Development/Moose/Core/Cargo.lua +++ b/Moose Development/Moose/Core/Cargo.lua @@ -246,6 +246,8 @@ function CARGO:New( Type, Name, Weight ) --R2.1 self:AddTransition( "UnBoarding", "UnBoarding", "UnBoarding" ) self:AddTransition( "UnBoarding", "UnLoad", "UnLoaded" ) self:AddTransition( "Loaded", "UnLoad", "UnLoaded" ) + self:AddTransition( "*", "Destroyed", "Destroyed" ) + self:AddTransition( "*", "Respawn", "UnLoaded" ) self.Type = Type @@ -263,6 +265,7 @@ function CARGO:New( Type, Name, Weight ) --R2.1 CARGOS[self.Name] = self self:SetEventPriority( 5 ) + return self end @@ -274,6 +277,17 @@ function CARGO:GetName() --R2.1 return self.Name end +--- Get the object name of the Cargo. +-- @param #CARGO self +-- @return #string The object name of the Cargo. +function CARGO:GetObjectName() --R2.1 + if self:IsLoaded() then + return self.CargoCarrier:GetName() + else + return self.CargoObject:GetName() + end +end + --- Get the type of the Cargo. -- @param #CARGO self -- @return #string The type of the Cargo. @@ -303,6 +317,20 @@ function CARGO:IsUnLoaded() return self:Is( "UnLoaded" ) end +--- Check if cargo is alive. +-- @param #CARGO self +-- @return #boolean true if unloaded +function CARGO:IsAlive() + + if self:IsLoaded() then + return self.CargoCarrier:IsAlive() + else + return self.CargoObject:IsAlive() + end +end + + + --- Template method to spawn a new representation of the CARGO in the simulator. -- @param #CARGO self @@ -529,6 +557,16 @@ function CARGO_UNIT:New( CargoUnit, Type, Name, Weight, NearRadius ) self:T( self.ClassName ) + self:HandleEvent( EVENTS.Dead, + --- @param #CARGO Cargo + -- @param Core.Event#EVENTDATA EventData + function( Cargo, EventData ) + if Cargo:GetObjectName() == EventData.IniUnit:GetName() then + self:E( { "Cargo destroyed", Cargo } ) + Cargo:Destroyed() + end + end + ) return self end @@ -949,6 +987,7 @@ function CARGO_GROUP:onafterBoarding( From, Event, To, CargoCarrier, NearRadius, local Boarded = true local Cancelled = false + local Dead = true self.CargoSet:Flush() @@ -962,18 +1001,26 @@ function CARGO_GROUP:onafterBoarding( From, Event, To, CargoCarrier, NearRadius, if Cargo:is( "UnLoaded" ) then Cancelled = true end - + + if not Cargo:is( "Destroyed" ) then + Dead = false + end end - if not Cancelled then - if not Boarded then - self:__Boarding( 1, CargoCarrier, NearRadius, ... ) + if not Dead then + + if not Cancelled then + if not Boarded then + self:__Boarding( 1, CargoCarrier, NearRadius, ... ) + else + self:__Load( 1, CargoCarrier, ... ) + end else - self:__Load( 1, CargoCarrier, ... ) + self:__CancelBoarding( 1, CargoCarrier, NearRadius, ... ) end else - self:__CancelBoarding( 1, CargoCarrier, NearRadius, ... ) + self:__Destroyed( 1, CargoCarrier, NearRadius, ... ) end end diff --git a/Moose Development/Moose/Tasking/Task_CARGO.lua b/Moose Development/Moose/Tasking/Task_CARGO.lua index 2ee08dc55..776fe499b 100644 --- a/Moose Development/Moose/Tasking/Task_CARGO.lua +++ b/Moose Development/Moose/Tasking/Task_CARGO.lua @@ -224,50 +224,54 @@ do -- TASK_CARGO --- @param Core.Cargo#CARGO Cargo function( Cargo ) - if Cargo:IsUnLoaded() then - if Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then - MENU_GROUP_COMMAND:New( - TaskUnit:GetGroup(), - "Board cargo " .. Cargo.Name, - TaskUnit.Menu, - self.MenuBoardCargo, - self, - Cargo - ):SetTime(MenuTime) - else - MENU_GROUP_COMMAND:New( - TaskUnit:GetGroup(), - "Route to Pickup cargo " .. Cargo.Name, - TaskUnit.Menu, - self.MenuRouteToPickup, - self, - Cargo - ):SetTime(MenuTime) - end - end - - if Cargo:IsLoaded() then - - MENU_GROUP_COMMAND:New( - TaskUnit:GetGroup(), - "Unboard cargo " .. Cargo.Name, - TaskUnit.Menu, - self.MenuUnBoardCargo, - self, - Cargo - ):SetTime(MenuTime) - - -- Deployzones are optional zones that can be selected to request routing information. - for DeployZoneName, DeployZone in pairs( Task.DeployZones ) do - if not Cargo:IsInZone( DeployZone ) then + + if Cargo:IsAlive() then + + if Cargo:IsUnLoaded() then + if Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then MENU_GROUP_COMMAND:New( TaskUnit:GetGroup(), - "Route to Deploy cargo at " .. DeployZoneName, + "Board cargo " .. Cargo.Name, TaskUnit.Menu, - self.MenuRouteToDeploy, + self.MenuBoardCargo, self, - DeployZone + Cargo ):SetTime(MenuTime) + else + MENU_GROUP_COMMAND:New( + TaskUnit:GetGroup(), + "Route to Pickup cargo " .. Cargo.Name, + TaskUnit.Menu, + self.MenuRouteToPickup, + self, + Cargo + ):SetTime(MenuTime) + end + end + + if Cargo:IsLoaded() then + + MENU_GROUP_COMMAND:New( + TaskUnit:GetGroup(), + "Unboard cargo " .. Cargo.Name, + TaskUnit.Menu, + self.MenuUnBoardCargo, + self, + Cargo + ):SetTime(MenuTime) + + -- Deployzones are optional zones that can be selected to request routing information. + for DeployZoneName, DeployZone in pairs( Task.DeployZones ) do + if not Cargo:IsInZone( DeployZone ) then + MENU_GROUP_COMMAND:New( + TaskUnit:GetGroup(), + "Route to Deploy cargo at " .. DeployZoneName, + TaskUnit.Menu, + self.MenuRouteToDeploy, + self, + DeployZone + ):SetTime(MenuTime) + end end end end @@ -313,13 +317,19 @@ do -- TASK_CARGO -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit -- @param Tasking.Task_Cargo#TASK_CARGO Task + -- @param From + -- @param Event + -- @param To + -- @param Core.Cargo#CARGO Cargo function Fsm:onafterRouteToPickup( TaskUnit, Task, From, Event, To, Cargo ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) + if Cargo:IsAlive() then + self.Cargo = Cargo -- Core.Cargo#CARGO + Task:SetCargoPickup( self.Cargo, TaskUnit ) + self:__RouteToPickupPoint( -0.1 ) + end - self.Cargo = Cargo -- Core.Cargo#CARGO - Task:SetCargoPickup( self.Cargo, TaskUnit ) - self:__RouteToPickupPoint( -0.1 ) end @@ -330,11 +340,13 @@ do -- TASK_CARGO function Fsm:onafterArriveAtPickup( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - if TaskUnit:IsAir() then - self.Cargo.CargoObject:GetUnit(1):SmokeRed() - self:__Land( -0.1, "Pickup" ) - else - self:__SelectAction( -0.1 ) + if self.Cargo:IsAlive() then + if TaskUnit:IsAir() then + self.Cargo.CargoObject:GetUnit(1):SmokeRed() + self:__Land( -0.1, "Pickup" ) + else + self:__SelectAction( -0.1 ) + end end end @@ -375,19 +387,21 @@ do -- TASK_CARGO function Fsm:onafterLand( TaskUnit, Task, From, Event, To, Action ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then - if TaskUnit:InAir() then - Task:GetMission():GetCommandCenter():MessageToGroup( "Land", TaskUnit:GetGroup() ) - self:__Land( -10, Action ) + if self.Cargo:IsAlive() then + if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then + if TaskUnit:InAir() then + Task:GetMission():GetCommandCenter():MessageToGroup( "Land", TaskUnit:GetGroup() ) + self:__Land( -10, Action ) + else + Task:GetMission():GetCommandCenter():MessageToGroup( "Landed ...", TaskUnit:GetGroup() ) + self:__Landed( -0.1, Action ) + end else - Task:GetMission():GetCommandCenter():MessageToGroup( "Landed ...", TaskUnit:GetGroup() ) - self:__Landed( -0.1, Action ) - end - else - if Action == "Pickup" then - self:__RouteToPickupZone( -0.1 ) - else - self:__RouteToDeployZone( -0.1 ) + if Action == "Pickup" then + self:__RouteToPickupZone( -0.1 ) + else + self:__RouteToDeployZone( -0.1 ) + end end end end @@ -399,17 +413,19 @@ do -- TASK_CARGO function Fsm:onafterLanded( TaskUnit, Task, From, Event, To, Action ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then - if TaskUnit:InAir() then - self:__Land( -0.1, Action ) + if self.Cargo:IsAlive() then + if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then + if TaskUnit:InAir() then + self:__Land( -0.1, Action ) + else + self:__SelectAction( -0.1 ) + end else - self:__SelectAction( -0.1 ) - end - else - if Action == "Pickup" then - self:__RouteToPickupZone( -0.1 ) - else - self:__RouteToDeployZone( -0.1 ) + if Action == "Pickup" then + self:__RouteToPickupZone( -0.1 ) + else + self:__RouteToDeployZone( -0.1 ) + end end end end @@ -421,8 +437,10 @@ do -- TASK_CARGO function Fsm:onafterPrepareBoarding( TaskUnit, Task, From, Event, To, Cargo ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - self.Cargo = Cargo -- Core.Cargo#CARGO_GROUP - self:__Board( -0.1 ) + if Cargo and Cargo:IsAlive() then + self.Cargo = Cargo -- Core.Cargo#CARGO_GROUP + self:__Board( -0.1 ) + end end --- @@ -437,15 +455,17 @@ do -- TASK_CARGO TaskProcess:__Boarded( 0.1 ) end - if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then - if TaskUnit:InAir() then - --- ABORT the boarding. Split group if any and go back to select action. + if self.Cargo:IsAlive() then + if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then + if TaskUnit:InAir() then + --- ABORT the boarding. Split group if any and go back to select action. + else + self.Cargo:MessageToGroup( "Boarding ...", TaskUnit:GetGroup() ) + self.Cargo:Board( TaskUnit, 20, self ) + end else - self.Cargo:MessageToGroup( "Boarding ...", TaskUnit:GetGroup() ) - self.Cargo:Board( TaskUnit, 20, self ) + --self:__ArriveAtCargo( -0.1 ) end - else - --self:__ArriveAtCargo( -0.1 ) end end @@ -462,8 +482,10 @@ do -- TASK_CARGO -- TODO:I need to find a more decent solution for this. Task:E( { CargoPickedUp = Task.CargoPickedUp } ) - if Task.CargoPickedUp then - Task:CargoPickedUp( TaskUnit, self.Cargo ) + if self.Cargo:IsAlive() then + if Task.CargoPickedUp then + Task:CargoPickedUp( TaskUnit, self.Cargo ) + end end end @@ -485,13 +507,15 @@ do -- TASK_CARGO self.DeployZone = nil -- Check if the Cargo is at a deployzone... If it is, provide it as a parameter! - for DeployZoneName, DeployZone in pairs( Task.DeployZones ) do - if Cargo:IsInZone( DeployZone ) then - self.DeployZone = DeployZone -- Core.Zone#ZONE_BASE - break + if Cargo:IsAlive() then + for DeployZoneName, DeployZone in pairs( Task.DeployZones ) do + if Cargo:IsInZone( DeployZone ) then + self.DeployZone = DeployZone -- Core.Zone#ZONE_BASE + break + end end + self:__UnBoard( -0.1, Cargo, self.DeployZone ) end - self:__UnBoard( -0.1, Cargo, self.DeployZone ) end --- @@ -507,15 +531,14 @@ do -- TASK_CARGO self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID(), From, Event, To, Cargo, DeployZone } ) function self.Cargo:OnEnterUnLoaded( From, Event, To, DeployZone, TaskProcess ) - self:E({From, Event, To, DeployZone, TaskProcess }) - TaskProcess:__UnBoarded( -0.1 ) - end - self.Cargo:MessageToGroup( "UnBoarding ...", TaskUnit:GetGroup() ) - self.Cargo:UnBoard( DeployZone:GetPointVec2(), 400, self ) + if self.Cargo:IsAlive() then + self.Cargo:MessageToGroup( "UnBoarding ...", TaskUnit:GetGroup() ) + self.Cargo:UnBoard( DeployZone:GetPointVec2(), 400, self ) + end end @@ -530,8 +553,10 @@ do -- TASK_CARGO -- TODO:I need to find a more decent solution for this. Task:E( { CargoDeployed = Task.CargoDeployed } ) - if Task.CargoDeployed then - Task:CargoDeployed( TaskUnit, self.Cargo, self.DeployZone ) + if self.Cargo:IsAlive() then + if Task.CargoDeployed then + Task:CargoDeployed( TaskUnit, self.Cargo, self.DeployZone ) + end end self:__SelectAction( 1 )