-- Fixing respawning of CargoGroups after dead event.

This commit is contained in:
FlightControl_Master 2018-03-29 12:48:24 +02:00
parent 93640b1d8e
commit 35f18d0d1f

View File

@ -226,18 +226,18 @@ do -- CARGO
Containable = false, Containable = false,
} }
--- @type CARGO.CargoObjects --- @type CARGO.CargoObjects
-- @map < #string, Wrapper.Positionable#POSITIONABLE > The alive POSITIONABLE objects representing the the cargo. -- @map < #string, Wrapper.Positionable#POSITIONABLE > The alive POSITIONABLE objects representing the the cargo.
--- CARGO Constructor. This class is an abstract class and should not be instantiated. --- CARGO Constructor. This class is an abstract class and should not be instantiated.
-- @param #CARGO self -- @param #CARGO self
-- @param #string Type -- @param #string Type
-- @param #string Name -- @param #string Name
-- @param #number Weight -- @param #number Weight
-- @param #number NearRadius (optional) -- @param #number NearRadius (optional)
-- @return #CARGO -- @return #CARGO
function CARGO:New( Type, Name, Weight ) --R2.1 function CARGO:New( Type, Name, Weight ) --R2.1
local self = BASE:Inherit( self, FSM:New() ) -- #CARGO local self = BASE:Inherit( self, FSM:New() ) -- #CARGO
self:F( { Type, Name, Weight } ) self:F( { Type, Name, Weight } )
@ -275,152 +275,152 @@ function CARGO:New( Type, Name, Weight ) --R2.1
return self return self
end end
--- Destroy the cargo. --- Destroy the cargo.
-- @param #CARGO self -- @param #CARGO self
function CARGO:Destroy() function CARGO:Destroy()
if self.CargoObject then if self.CargoObject then
self.CargoObject:Destroy() self.CargoObject:Destroy()
end end
self:Destroyed() self:Destroyed()
end end
--- Get the name of the Cargo. --- Get the name of the Cargo.
-- @param #CARGO self -- @param #CARGO self
-- @return #string The name of the Cargo. -- @return #string The name of the Cargo.
function CARGO:GetName() --R2.1 function CARGO:GetName() --R2.1
return self.Name return self.Name
end end
--- Get the object name of the Cargo. --- Get the object name of the Cargo.
-- @param #CARGO self -- @param #CARGO self
-- @return #string The object name of the Cargo. -- @return #string The object name of the Cargo.
function CARGO:GetObjectName() --R2.1 function CARGO:GetObjectName() --R2.1
if self:IsLoaded() then if self:IsLoaded() then
return self.CargoCarrier:GetName() return self.CargoCarrier:GetName()
else else
return self.CargoObject:GetName() return self.CargoObject:GetName()
end end
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.
function CARGO:GetType() function CARGO:GetType()
return self.Type return self.Type
end end
--- Get the current coordinates of the Cargo. --- Get the current coordinates of the Cargo.
-- @param #CARGO self -- @param #CARGO self
-- @return Core.Point#COORDINATE The coordinates of the Cargo. -- @return Core.Point#COORDINATE The coordinates of the Cargo.
function CARGO:GetCoordinate() function CARGO:GetCoordinate()
return self.CargoObject:GetCoordinate() return self.CargoObject:GetCoordinate()
end end
--- Check if cargo is destroyed. --- Check if cargo is destroyed.
-- @param #CARGO self -- @param #CARGO self
-- @return #boolean true if destroyed -- @return #boolean true if destroyed
function CARGO:IsDestroyed() function CARGO:IsDestroyed()
return self:Is( "Destroyed" ) return self:Is( "Destroyed" )
end end
--- Check if cargo is loaded. --- Check if cargo is loaded.
-- @param #CARGO self -- @param #CARGO self
-- @return #boolean true if loaded -- @return #boolean true if loaded
function CARGO:IsLoaded() function CARGO:IsLoaded()
return self:Is( "Loaded" ) return self:Is( "Loaded" )
end end
--- Check if cargo is unloaded. --- Check if cargo is unloaded.
-- @param #CARGO self -- @param #CARGO self
-- @return #boolean true if unloaded -- @return #boolean true if unloaded
function CARGO:IsUnLoaded() function CARGO:IsUnLoaded()
return self:Is( "UnLoaded" ) return self:Is( "UnLoaded" )
end end
--- Check if cargo is boarding. --- Check if cargo is boarding.
-- @param #CARGO self -- @param #CARGO self
-- @return #boolean true if boarding -- @return #boolean true if boarding
function CARGO:IsBoarding() function CARGO:IsBoarding()
return self:Is( "Boarding" ) return self:Is( "Boarding" )
end end
--- Check if cargo is alive. --- Check if cargo is alive.
-- @param #CARGO self -- @param #CARGO self
-- @return #boolean true if unloaded -- @return #boolean true if unloaded
function CARGO:IsAlive() function CARGO:IsAlive()
if self:IsLoaded() then if self:IsLoaded() then
return self.CargoCarrier:IsAlive() return self.CargoCarrier:IsAlive()
else else
return self.CargoObject:IsAlive() return self.CargoObject:IsAlive()
end end
end end
--- Set the cargo as deployed --- Set the cargo as deployed
-- @param #CARGO self -- @param #CARGO self
function CARGO:SetDeployed( Deployed ) function CARGO:SetDeployed( Deployed )
self.Deployed = Deployed self.Deployed = Deployed
end end
--- Is the cargo deployed --- Is the cargo deployed
-- @param #CARGO self -- @param #CARGO self
-- @return #boolean -- @return #boolean
function CARGO:IsDeployed() function CARGO:IsDeployed()
return self.Deployed return self.Deployed
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
-- @return #CARGO -- @return #CARGO
function CARGO:Spawn( PointVec2 ) function CARGO:Spawn( PointVec2 )
self:F() self:F()
end end
--- Signal a flare at the position of the CARGO. --- Signal a flare at the position of the CARGO.
-- @param #CARGO self -- @param #CARGO self
-- @param Utilities.Utils#FLARECOLOR FlareColor -- @param Utilities.Utils#FLARECOLOR FlareColor
function CARGO:Flare( FlareColor ) function CARGO:Flare( FlareColor )
if self:IsUnLoaded() then if self:IsUnLoaded() then
trigger.action.signalFlare( self.CargoObject:GetVec3(), FlareColor , 0 ) trigger.action.signalFlare( self.CargoObject:GetVec3(), FlareColor , 0 )
end end
end end
--- Signal a white flare at the position of the CARGO. --- Signal a white flare at the position of the CARGO.
-- @param #CARGO self -- @param #CARGO self
function CARGO:FlareWhite() function CARGO:FlareWhite()
self:Flare( trigger.flareColor.White ) self:Flare( trigger.flareColor.White )
end end
--- Signal a yellow flare at the position of the CARGO. --- Signal a yellow flare at the position of the CARGO.
-- @param #CARGO self -- @param #CARGO self
function CARGO:FlareYellow() function CARGO:FlareYellow()
self:Flare( trigger.flareColor.Yellow ) self:Flare( trigger.flareColor.Yellow )
end end
--- Signal a green flare at the position of the CARGO. --- Signal a green flare at the position of the CARGO.
-- @param #CARGO self -- @param #CARGO self
function CARGO:FlareGreen() function CARGO:FlareGreen()
self:Flare( trigger.flareColor.Green ) self:Flare( trigger.flareColor.Green )
end end
--- Signal a red flare at the position of the CARGO. --- Signal a red flare at the position of the CARGO.
-- @param #CARGO self -- @param #CARGO self
function CARGO:FlareRed() function CARGO:FlareRed()
self:Flare( trigger.flareColor.Red ) self:Flare( trigger.flareColor.Red )
end end
--- Smoke the CARGO. --- Smoke the CARGO.
-- @param #CARGO self -- @param #CARGO self
-- @param Utilities.Utils#SMOKECOLOR SmokeColor The color of the smoke. -- @param Utilities.Utils#SMOKECOLOR SmokeColor The color of the smoke.
-- @param #number Radius The radius of randomization around the center of the Cargo. -- @param #number Radius The radius of randomization around the center of the Cargo.
function CARGO:Smoke( SmokeColor, Radius ) function CARGO:Smoke( SmokeColor, Radius )
if self:IsUnLoaded() then if self:IsUnLoaded() then
if Radius then if Radius then
trigger.action.smoke( self.CargoObject:GetRandomVec3( Radius ), SmokeColor ) trigger.action.smoke( self.CargoObject:GetRandomVec3( Radius ), SmokeColor )
@ -428,48 +428,48 @@ function CARGO:Smoke( SmokeColor, Radius )
trigger.action.smoke( self.CargoObject:GetVec3(), SmokeColor ) trigger.action.smoke( self.CargoObject:GetVec3(), SmokeColor )
end end
end end
end end
--- Smoke the CARGO Green. --- Smoke the CARGO Green.
-- @param #CARGO self -- @param #CARGO self
function CARGO:SmokeGreen() function CARGO:SmokeGreen()
self:Smoke( trigger.smokeColor.Green, Range ) self:Smoke( trigger.smokeColor.Green, Range )
end end
--- Smoke the CARGO Red. --- Smoke the CARGO Red.
-- @param #CARGO self -- @param #CARGO self
function CARGO:SmokeRed() function CARGO:SmokeRed()
self:Smoke( trigger.smokeColor.Red, Range ) self:Smoke( trigger.smokeColor.Red, Range )
end end
--- Smoke the CARGO White. --- Smoke the CARGO White.
-- @param #CARGO self -- @param #CARGO self
function CARGO:SmokeWhite() function CARGO:SmokeWhite()
self:Smoke( trigger.smokeColor.White, Range ) self:Smoke( trigger.smokeColor.White, Range )
end end
--- Smoke the CARGO Orange. --- Smoke the CARGO Orange.
-- @param #CARGO self -- @param #CARGO self
function CARGO:SmokeOrange() function CARGO:SmokeOrange()
self:Smoke( trigger.smokeColor.Orange, Range ) self:Smoke( trigger.smokeColor.Orange, Range )
end end
--- Smoke the CARGO Blue. --- Smoke the CARGO Blue.
-- @param #CARGO self -- @param #CARGO self
function CARGO:SmokeBlue() function CARGO:SmokeBlue()
self:Smoke( trigger.smokeColor.Blue, Range ) self:Smoke( trigger.smokeColor.Blue, Range )
end end
--- Check if Cargo is the given @{Zone}. --- Check if Cargo is the given @{Zone}.
-- @param #CARGO self -- @param #CARGO self
-- @param Core.Zone#ZONE_BASE Zone -- @param Core.Zone#ZONE_BASE Zone
-- @return #boolean **true** if cargo is in the Zone, **false** if cargo is not in the Zone. -- @return #boolean **true** if cargo is in the Zone, **false** if cargo is not in the Zone.
function CARGO:IsInZone( Zone ) function CARGO:IsInZone( Zone )
self:F( { Zone } ) self:F( { Zone } )
if self:IsLoaded() then if self:IsLoaded() then
@ -485,15 +485,15 @@ function CARGO:IsInZone( Zone )
return nil return nil
end end
--- Check if CargoCarrier is near the Cargo to be Loaded. --- Check if CargoCarrier is near the Cargo to be Loaded.
-- @param #CARGO self -- @param #CARGO self
-- @param Core.Point#POINT_VEC2 PointVec2 -- @param Core.Point#POINT_VEC2 PointVec2
-- @param #number NearRadius The radius when the cargo will board the Carrier (to avoid collision). -- @param #number NearRadius The radius when the cargo will board the Carrier (to avoid collision).
-- @return #boolean -- @return #boolean
function CARGO:IsNear( PointVec2, NearRadius ) function CARGO:IsNear( PointVec2, NearRadius )
self:F( { PointVec2 = PointVec2, NearRadius = NearRadius } ) self:F( { PointVec2 = PointVec2, NearRadius = NearRadius } )
if self.CargoObject:IsAlive() then if self.CargoObject:IsAlive() then
@ -510,33 +510,32 @@ function CARGO:IsNear( PointVec2, NearRadius )
end end
return false return false
end end
--- Get the current PointVec2 of the cargo. --- Get the current PointVec2 of the cargo.
-- @param #CARGO self -- @param #CARGO self
-- @return Core.Point#POINT_VEC2 -- @return Core.Point#POINT_VEC2
function CARGO:GetPointVec2() function CARGO:GetPointVec2()
return self.CargoObject:GetPointVec2() return self.CargoObject:GetPointVec2()
end end
--- Get the current Coordinate of the cargo. --- Get the current Coordinate of the cargo.
-- @param #CARGO self -- @param #CARGO self
-- @return Core.Point#COORDINATE -- @return Core.Point#COORDINATE
function CARGO:GetCoordinate() function CARGO:GetCoordinate()
return self.CargoObject:GetCoordinate() return self.CargoObject:GetCoordinate()
end end
--- Set the weight of the cargo. --- Set the weight of the cargo.
-- @param #CARGO self -- @param #CARGO self
-- @param #number Weight The weight in kg. -- @param #number Weight The weight in kg.
-- @return #CARGO -- @return #CARGO
function CARGO:SetWeight( Weight ) function CARGO:SetWeight( Weight )
self.Weight = Weight self.Weight = Weight
return self return self
end end
end
end -- CARGO
do -- CARGO_REPRESENTABLE do -- CARGO_REPRESENTABLE
@ -600,7 +599,7 @@ do -- CARGO_REPRESENTABLE
end -- CARGO_REPRESENTABLE end -- CARGO_REPRESENTABLE
do -- CARGO_REPORTABLE do -- CARGO_REPORTABLE
--- @type CARGO_REPORTABLE --- @type CARGO_REPORTABLE
-- @extends #CARGO -- @extends #CARGO
@ -989,7 +988,6 @@ do -- CARGO_UNIT
end -- CARGO_UNIT end -- CARGO_UNIT
do -- CARGO_CRATE do -- CARGO_CRATE
--- Models the behaviour of cargo crates, which can be slingloaded and boarded on helicopters using the DCS menus. --- Models the behaviour of cargo crates, which can be slingloaded and boarded on helicopters using the DCS menus.
@ -1127,16 +1125,17 @@ function CARGO_GROUP:New( CargoGroup, Type, Name, ReportRadius )
self:SetDeployed( false ) self:SetDeployed( false )
local WeightGroup = 0 local WeightGroup = 0
local GroupName = CargoGroup:GetName()
self.GroupName = CargoGroup:GetName()
self.CargoTemplate = UTILS.DeepCopy( _DATABASE:GetGroupTemplate( self.GroupName ) )
CargoGroup:Destroy() CargoGroup:Destroy()
-- We iterate through the group template and for each unit in the template, we create a new group with one unit. -- We iterate through the group template and for each unit in the template, we create a new group with one unit.
for UnitID, UnitTemplate in pairs( _DATABASE:GetGroupTemplate(GroupName).units ) do for UnitID, UnitTemplate in pairs( self.CargoTemplate.units ) do
local GroupTemplate = UTILS.DeepCopy( _DATABASE:GetGroupTemplate(GroupName) ) local GroupTemplate = UTILS.DeepCopy( self.CargoTemplate )
local GroupName = env.getValueDictByKey( GroupTemplate.name ) local GroupName = env.getValueDictByKey( GroupTemplate.name )
self:E( GroupName )
-- We create a new group object with one unit... -- We create a new group object with one unit...
-- First we prepare the template... -- First we prepare the template...
@ -1557,7 +1556,7 @@ function CARGO_GROUP:OnEventCargoDead( EventData )
-- @param #CARGO_GROUP self -- @param #CARGO_GROUP self
function CARGO_GROUP:Respawn() function CARGO_GROUP:Respawn()
self:F({"Respawning"}) self:F( { "Respawning" } )
for CargoID, CargoData in pairs( self.CargoSet:GetSet() ) do for CargoID, CargoData in pairs( self.CargoSet:GetSet() ) do
local Cargo = CargoData -- #CARGO local Cargo = CargoData -- #CARGO
@ -1565,15 +1564,38 @@ function CARGO_GROUP:OnEventCargoDead( EventData )
Cargo:SetStartState( "UnLoaded" ) Cargo:SetStartState( "UnLoaded" )
end end
local CargoObject = self.CargoObject -- Wrapper.Group#GROUP
CargoObject:Destroy() -- We iterate through the group template and for each unit in the template, we create a new group with one unit.
local Template = CargoObject:GetTemplate() for UnitID, UnitTemplate in pairs( self.CargoTemplate.units ) do
CargoObject:Respawn( Template )
local GroupTemplate = UTILS.DeepCopy( self.CargoTemplate )
local GroupName = env.getValueDictByKey( GroupTemplate.name )
-- We create a new group object with one unit...
-- First we prepare the template...
GroupTemplate.name = GroupName .. "#CARGO#" .. UnitID
GroupTemplate.groupId = nil
GroupTemplate.units = {}
GroupTemplate.units[1] = UnitTemplate
local UnitName = UnitTemplate.name .. "#CARGO"
GroupTemplate.units[1].name = UnitTemplate.name .. "#CARGO"
-- Then we register the new group in the database
local CargoGroup = GROUP:NewTemplate( GroupTemplate, GroupTemplate.CoalitionID, GroupTemplate.CategoryID, GroupTemplate.CountryID)
-- Now we spawn the new group based on the template created.
_DATABASE:Spawn( GroupTemplate )
-- And we register the spawned unit as part of the CargoSet.
local Unit = UNIT:FindByName( UnitName )
--local WeightUnit = Unit:GetDesc().massEmpty
--WeightGroup = WeightGroup + WeightUnit
local CargoUnit = CARGO_UNIT:New( Unit, Type, UnitName, 10 )
self.CargoSet:Add( UnitName, CargoUnit )
end
self:SetDeployed( false ) self:SetDeployed( false )
local WeightGroup = 0
self:SetStartState( "UnLoaded" ) self:SetStartState( "UnLoaded" )
end end