When cargo is destroyed, it will stop working...

This commit is contained in:
FlightControl
2017-05-09 09:29:08 +02:00
parent ce1f85e09a
commit d8ba37af8d
2 changed files with 170 additions and 98 deletions
+53 -6
View File
@@ -246,6 +246,8 @@ function CARGO:New( Type, Name, Weight ) --R2.1
self:AddTransition( "UnBoarding", "UnBoarding", "UnBoarding" ) self:AddTransition( "UnBoarding", "UnBoarding", "UnBoarding" )
self:AddTransition( "UnBoarding", "UnLoad", "UnLoaded" ) self:AddTransition( "UnBoarding", "UnLoad", "UnLoaded" )
self:AddTransition( "Loaded", "UnLoad", "UnLoaded" ) self:AddTransition( "Loaded", "UnLoad", "UnLoaded" )
self:AddTransition( "*", "Destroyed", "Destroyed" )
self:AddTransition( "*", "Respawn", "UnLoaded" )
self.Type = Type self.Type = Type
@@ -263,6 +265,7 @@ function CARGO:New( Type, Name, Weight ) --R2.1
CARGOS[self.Name] = self CARGOS[self.Name] = self
self:SetEventPriority( 5 ) self:SetEventPriority( 5 )
return self return self
end end
@@ -274,6 +277,17 @@ function CARGO:GetName() --R2.1
return self.Name return self.Name
end 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. --- Get the type of the Cargo.
-- @param #CARGO self -- @param #CARGO self
-- @return #string The type of the Cargo. -- @return #string The type of the Cargo.
@@ -303,6 +317,20 @@ function CARGO:IsUnLoaded()
return self:Is( "UnLoaded" ) return self:Is( "UnLoaded" )
end 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. --- Template method to spawn a new representation of the CARGO in the simulator.
-- @param #CARGO self -- @param #CARGO self
@@ -529,6 +557,16 @@ function CARGO_UNIT:New( CargoUnit, Type, Name, Weight, NearRadius )
self:T( self.ClassName ) 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 return self
end end
@@ -949,6 +987,7 @@ function CARGO_GROUP:onafterBoarding( From, Event, To, CargoCarrier, NearRadius,
local Boarded = true local Boarded = true
local Cancelled = false local Cancelled = false
local Dead = true
self.CargoSet:Flush() self.CargoSet:Flush()
@@ -962,18 +1001,26 @@ function CARGO_GROUP:onafterBoarding( From, Event, To, CargoCarrier, NearRadius,
if Cargo:is( "UnLoaded" ) then if Cargo:is( "UnLoaded" ) then
Cancelled = true Cancelled = true
end end
if not Cargo:is( "Destroyed" ) then
Dead = false
end
end end
if not Cancelled then if not Dead then
if not Boarded then
self:__Boarding( 1, CargoCarrier, NearRadius, ... ) if not Cancelled then
if not Boarded then
self:__Boarding( 1, CargoCarrier, NearRadius, ... )
else
self:__Load( 1, CargoCarrier, ... )
end
else else
self:__Load( 1, CargoCarrier, ... ) self:__CancelBoarding( 1, CargoCarrier, NearRadius, ... )
end end
else else
self:__CancelBoarding( 1, CargoCarrier, NearRadius, ... ) self:__Destroyed( 1, CargoCarrier, NearRadius, ... )
end end
end end
+117 -92
View File
@@ -224,50 +224,54 @@ do -- TASK_CARGO
--- @param Core.Cargo#CARGO Cargo --- @param Core.Cargo#CARGO Cargo
function( Cargo ) function( Cargo )
if Cargo:IsUnLoaded() then
if Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then if Cargo:IsAlive() then
MENU_GROUP_COMMAND:New(
TaskUnit:GetGroup(), if Cargo:IsUnLoaded() then
"Board cargo " .. Cargo.Name, if Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then
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
MENU_GROUP_COMMAND:New( MENU_GROUP_COMMAND:New(
TaskUnit:GetGroup(), TaskUnit:GetGroup(),
"Route to Deploy cargo at " .. DeployZoneName, "Board cargo " .. Cargo.Name,
TaskUnit.Menu, TaskUnit.Menu,
self.MenuRouteToDeploy, self.MenuBoardCargo,
self, self,
DeployZone Cargo
):SetTime(MenuTime) ):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 end
end end
@@ -313,13 +317,19 @@ do -- TASK_CARGO
-- @param #FSM_PROCESS self -- @param #FSM_PROCESS self
-- @param Wrapper.Unit#UNIT TaskUnit -- @param Wrapper.Unit#UNIT TaskUnit
-- @param Tasking.Task_Cargo#TASK_CARGO Task -- @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 ) function Fsm:onafterRouteToPickup( TaskUnit, Task, From, Event, To, Cargo )
self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) 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 end
@@ -330,11 +340,13 @@ do -- TASK_CARGO
function Fsm:onafterArriveAtPickup( TaskUnit, Task ) function Fsm:onafterArriveAtPickup( TaskUnit, Task )
self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
if TaskUnit:IsAir() then if self.Cargo:IsAlive() then
self.Cargo.CargoObject:GetUnit(1):SmokeRed() if TaskUnit:IsAir() then
self:__Land( -0.1, "Pickup" ) self.Cargo.CargoObject:GetUnit(1):SmokeRed()
else self:__Land( -0.1, "Pickup" )
self:__SelectAction( -0.1 ) else
self:__SelectAction( -0.1 )
end
end end
end end
@@ -375,19 +387,21 @@ do -- TASK_CARGO
function Fsm:onafterLand( TaskUnit, Task, From, Event, To, Action ) function Fsm:onafterLand( TaskUnit, Task, From, Event, To, Action )
self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then if self.Cargo:IsAlive() then
if TaskUnit:InAir() then if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then
Task:GetMission():GetCommandCenter():MessageToGroup( "Land", TaskUnit:GetGroup() ) if TaskUnit:InAir() then
self:__Land( -10, Action ) 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 else
Task:GetMission():GetCommandCenter():MessageToGroup( "Landed ...", TaskUnit:GetGroup() ) if Action == "Pickup" then
self:__Landed( -0.1, Action ) self:__RouteToPickupZone( -0.1 )
end else
else self:__RouteToDeployZone( -0.1 )
if Action == "Pickup" then end
self:__RouteToPickupZone( -0.1 )
else
self:__RouteToDeployZone( -0.1 )
end end
end end
end end
@@ -399,17 +413,19 @@ do -- TASK_CARGO
function Fsm:onafterLanded( TaskUnit, Task, From, Event, To, Action ) function Fsm:onafterLanded( TaskUnit, Task, From, Event, To, Action )
self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then if self.Cargo:IsAlive() then
if TaskUnit:InAir() then if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then
self:__Land( -0.1, Action ) if TaskUnit:InAir() then
self:__Land( -0.1, Action )
else
self:__SelectAction( -0.1 )
end
else else
self:__SelectAction( -0.1 ) if Action == "Pickup" then
end self:__RouteToPickupZone( -0.1 )
else else
if Action == "Pickup" then self:__RouteToDeployZone( -0.1 )
self:__RouteToPickupZone( -0.1 ) end
else
self:__RouteToDeployZone( -0.1 )
end end
end end
end end
@@ -421,8 +437,10 @@ do -- TASK_CARGO
function Fsm:onafterPrepareBoarding( TaskUnit, Task, From, Event, To, Cargo ) function Fsm:onafterPrepareBoarding( TaskUnit, Task, From, Event, To, Cargo )
self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
self.Cargo = Cargo -- Core.Cargo#CARGO_GROUP if Cargo and Cargo:IsAlive() then
self:__Board( -0.1 ) self.Cargo = Cargo -- Core.Cargo#CARGO_GROUP
self:__Board( -0.1 )
end
end end
--- ---
@@ -437,15 +455,17 @@ do -- TASK_CARGO
TaskProcess:__Boarded( 0.1 ) TaskProcess:__Boarded( 0.1 )
end end
if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then if self.Cargo:IsAlive() then
if TaskUnit:InAir() then if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then
--- ABORT the boarding. Split group if any and go back to select action. 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 else
self.Cargo:MessageToGroup( "Boarding ...", TaskUnit:GetGroup() ) --self:__ArriveAtCargo( -0.1 )
self.Cargo:Board( TaskUnit, 20, self )
end end
else
--self:__ArriveAtCargo( -0.1 )
end end
end end
@@ -462,8 +482,10 @@ do -- TASK_CARGO
-- TODO:I need to find a more decent solution for this. -- TODO:I need to find a more decent solution for this.
Task:E( { CargoPickedUp = Task.CargoPickedUp } ) Task:E( { CargoPickedUp = Task.CargoPickedUp } )
if Task.CargoPickedUp then if self.Cargo:IsAlive() then
Task:CargoPickedUp( TaskUnit, self.Cargo ) if Task.CargoPickedUp then
Task:CargoPickedUp( TaskUnit, self.Cargo )
end
end end
end end
@@ -485,13 +507,15 @@ do -- TASK_CARGO
self.DeployZone = nil self.DeployZone = nil
-- Check if the Cargo is at a deployzone... If it is, provide it as a parameter! -- 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:IsAlive() then
if Cargo:IsInZone( DeployZone ) then for DeployZoneName, DeployZone in pairs( Task.DeployZones ) do
self.DeployZone = DeployZone -- Core.Zone#ZONE_BASE if Cargo:IsInZone( DeployZone ) then
break self.DeployZone = DeployZone -- Core.Zone#ZONE_BASE
break
end
end end
self:__UnBoard( -0.1, Cargo, self.DeployZone )
end end
self:__UnBoard( -0.1, Cargo, self.DeployZone )
end end
--- ---
@@ -507,15 +531,14 @@ do -- TASK_CARGO
self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID(), From, Event, To, Cargo, DeployZone } ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID(), From, Event, To, Cargo, DeployZone } )
function self.Cargo:OnEnterUnLoaded( From, Event, To, DeployZone, TaskProcess ) function self.Cargo:OnEnterUnLoaded( From, Event, To, DeployZone, TaskProcess )
self:E({From, Event, To, DeployZone, TaskProcess }) self:E({From, Event, To, DeployZone, TaskProcess })
TaskProcess:__UnBoarded( -0.1 ) TaskProcess:__UnBoarded( -0.1 )
end end
self.Cargo:MessageToGroup( "UnBoarding ...", TaskUnit:GetGroup() ) if self.Cargo:IsAlive() then
self.Cargo:UnBoard( DeployZone:GetPointVec2(), 400, self ) self.Cargo:MessageToGroup( "UnBoarding ...", TaskUnit:GetGroup() )
self.Cargo:UnBoard( DeployZone:GetPointVec2(), 400, self )
end
end end
@@ -530,8 +553,10 @@ do -- TASK_CARGO
-- TODO:I need to find a more decent solution for this. -- TODO:I need to find a more decent solution for this.
Task:E( { CargoDeployed = Task.CargoDeployed } ) Task:E( { CargoDeployed = Task.CargoDeployed } )
if Task.CargoDeployed then if self.Cargo:IsAlive() then
Task:CargoDeployed( TaskUnit, self.Cargo, self.DeployZone ) if Task.CargoDeployed then
Task:CargoDeployed( TaskUnit, self.Cargo, self.DeployZone )
end
end end
self:__SelectAction( 1 ) self:__SelectAction( 1 )