First prototype of AI_CARGO_TROOPS

This commit is contained in:
FlightControl_Master 2018-03-27 12:13:08 +02:00
commit 395923eb07
4 changed files with 617 additions and 272 deletions

View File

@ -0,0 +1,247 @@
--- **AI** -- (R2.3) - Models the intelligent transportation of infantry (cargo).
--
-- ===
--
-- ### Author: **FlightControl**
--
-- ===
--
-- @module AI_Cargo_Troops
--- @type AI_CARGO_TROOPS
-- @extends Core.Fsm#FSM_CONTROLLABLE
--- # AI\_CARGO\_TROOPS class, extends @{Core.Base@BASE}
--
-- ===
--
-- @field #AI_CARGO_TROOPS
AI_CARGO_TROOPS = {
ClassName = "AI_CARGO_TROOPS",
Coordinate = nil -- Core.Point#COORDINATE,
}
--- Creates a new AI_CARGO_TROOPS object
-- @param #AI_CARGO_TROOPS self
-- @return #AI_CARGO_TROOPS
function AI_CARGO_TROOPS:New( CargoCarrier, CargoGroup, CombatRadius )
local self = BASE:Inherit( self, FSM_CONTROLLABLE:New( ) ) -- #AI_CARGO_TROOPS
self.CargoCarrier = CargoCarrier -- Wrapper.Unit#UNIT
self.CargoGroup = CargoGroup -- Core.Cargo#CARGO_GROUP
self.CombatRadius = CombatRadius
self.Zone = ZONE_UNIT:New( self.CargoCarrier:GetName() .. "-Zone", self.CargoCarrier, CombatRadius )
self.Coalition = self.CargoCarrier:GetCoalition()
self:SetControllable( CargoCarrier )
self:SetStartState( "UnLoaded" )
self:AddTransition( "*", "Load", "Boarding" )
self:AddTransition( "Boarding", "Board", "Boarding" )
self:AddTransition( "Boarding", "Loaded", "Loaded" )
self:AddTransition( "Loaded", "Unload", "Unboarding" )
self:AddTransition( "Unboarding", "Unboard", "Unboarding" )
self:AddTransition( "Unboarding", "Unloaded", "Unloaded" )
self:AddTransition( "*", "Monitor", "*" )
self:AddTransition( "*", "Follow", "Following" )
self:AddTransition( "*", "Guard", "Guarding" )
self:__Monitor( 1 )
self:__Load( 1 )
return self
end
--- Follow Infantry to the Carrier.
-- @param #AI_CARGO_TROOPS self
-- @return #AI_CARGO_TROOPS
function AI_CARGO_TROOPS:FollowToCarrier( Me )
self = Me
self:F( { self = self:GetClassNameAndID(), CargoGroup = self.CargoGroup:GetName() } )
-- We check if the Cargo is near to the CargoCarrier.
if self.CargoGroup:IsNear( self.CargoCarrier, 5 ) then
-- The Cargo does not need to follow the Carrier.
self:Guard()
else
-- The Cargo needs to continue to follow the Carrier.
if self:Is( "Following" ) then
-- For each Cargo object within the CARGO_GROUPED, route each object to the CargoLoadPointVec2
self.CargoGroup.CargoSet:ForEach(
--- @param Core.Cargo#CARGO Cargo
function( Cargo )
local CargoUnit = Cargo.CargoObject -- Wrapper.Unit#UNIT
self:F( { UnitName = CargoUnit:GetName() } )
if CargoUnit:IsAlive() then
local InfantryGroup = CargoUnit:GetGroup()
self:F( { GroupName = InfantryGroup:GetName() } )
local Waypoints = {}
-- Calculate the new Route.
local FromCoord = InfantryGroup:GetCoordinate()
local FromGround = FromCoord:WaypointGround( 10, "Diamond" )
table.insert( Waypoints, FromGround )
local ToCoord = self.CargoCarrier:GetCoordinate()
local ToGround = ToCoord:WaypointGround( 10, "Diamond" )
table.insert( Waypoints, ToGround )
local TaskRoute = InfantryGroup:TaskFunction( "AI_CARGO_TROOPS.FollowToCarrier", self )
self:F({Waypoints = Waypoints})
local Waypoint = Waypoints[#Waypoints]
InfantryGroup:SetTaskWaypoint( Waypoint, TaskRoute ) -- Set for the given Route at Waypoint 2 the TaskRouteToZone.
InfantryGroup:Route( Waypoints ) -- Move after a random seconds to the Route. See the Route method for details.
end
end
)
end
end
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterMonitor( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
if CargoCarrier and CargoCarrier:IsAlive() then
if self.CarrierCoordinate then
local Coordinate = CargoCarrier:GetCoordinate()
self.Zone:Scan( { Object.Category.UNIT } )
if self.Zone:IsAllInZoneOfCoalition( self.Coalition ) then
if self:Is( "Unloaded" ) or self:Is( "Guarding" ) then
-- There are no enemies within combat range. Load the CargoCarrier.
self:__Load( 1 )
end
else
if self:Is( "Loaded" ) then
-- There are enemies within combat range. Unload the CargoCarrier.
self:__Unload( 1 )
end
end
if self:Is( "Guarding" ) then
if not self.CargoGroup:IsNear( CargoCarrier, 5 ) then
self:Follow()
end
end
end
self.CarrierCoordinate = CargoCarrier:GetCoordinate()
end
self:__Monitor( -5 )
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterLoad( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
if CargoCarrier and CargoCarrier:IsAlive() then
CargoCarrier:RouteStop()
self:Board()
self.CargoGroup:Board( CargoCarrier, 100 )
end
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterBoard( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
if CargoCarrier and CargoCarrier:IsAlive() then
self:F({ IsLoaded = self.CargoGroup:IsLoaded() } )
if not self.CargoGroup:IsLoaded() then
self:__Board( 1 )
else
self:__Loaded( 1 )
end
end
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterLoaded( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
if CargoCarrier and CargoCarrier:IsAlive() then
CargoCarrier:RouteResume()
end
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterUnload( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
if CargoCarrier and CargoCarrier:IsAlive() then
CargoCarrier:RouteStop()
self.CargoGroup:UnBoard( )
self:__Unboard( 1 )
end
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterUnboard( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
if CargoCarrier and CargoCarrier:IsAlive() then
if not self.CargoGroup:IsUnLoaded() then
self:__Unboard( 1 )
else
self:Unloaded()
end
end
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterUnloaded( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
if CargoCarrier and CargoCarrier:IsAlive() then
self:Guard()
self.CargoCarrier = CargoCarrier
CargoCarrier:RouteResume()
end
end
--- @param #AI_CARGO_TROOPS self
-- @param Wrapper.Unit#UNIT CargoCarrier
function AI_CARGO_TROOPS:onafterFollow( CargoCarrier, From, Event, To )
self:F( { CargoCarrier, From, Event, To } )
self:F( "Follow" )
if CargoCarrier and CargoCarrier:IsAlive() then
self:FollowToCarrier( self )
end
end

View File

@ -491,17 +491,22 @@ end
-- @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, NearRadius } ) self:F( { PointVec2 = PointVec2, NearRadius = NearRadius } )
--local Distance = PointVec2:DistanceFromPointVec2( self.CargoObject:GetPointVec2() ) if self.CargoObject:IsAlive() then
local Distance = PointVec2:Get2DDistance( self.CargoObject:GetPointVec2() ) --local Distance = PointVec2:DistanceFromPointVec2( self.CargoObject:GetPointVec2() )
self:T( Distance ) self:F( { CargoObjectName = self.CargoObject:GetName() } )
self:F( { CargoObjectVec2 = self.CargoObject:GetVec2() } )
self:F( { PointVec2 = PointVec2:GetVec2() } )
local Distance = PointVec2:Get2DDistance( self.CargoObject:GetPointVec2() )
self:T( Distance )
if Distance <= NearRadius then if Distance <= NearRadius then
return true return true
else end
return false
end end
return false
end end
--- Get the current PointVec2 of the cargo. --- Get the current PointVec2 of the cargo.
@ -746,7 +751,7 @@ do -- CARGO_UNIT
function CARGO_UNIT:onenterUnBoarding( From, Event, To, ToPointVec2, NearRadius ) function CARGO_UNIT:onenterUnBoarding( From, Event, To, ToPointVec2, NearRadius )
self:F( { From, Event, To, ToPointVec2, NearRadius } ) self:F( { From, Event, To, ToPointVec2, NearRadius } )
NearRadius = NearRadius or 25 NearRadius = NearRadius or 100
local Angle = 180 local Angle = 180
local Speed = 60 local Speed = 60
@ -804,7 +809,7 @@ do -- CARGO_UNIT
function CARGO_UNIT:onleaveUnBoarding( From, Event, To, ToPointVec2, NearRadius ) function CARGO_UNIT:onleaveUnBoarding( From, Event, To, ToPointVec2, NearRadius )
self:F( { From, Event, To, ToPointVec2, NearRadius } ) self:F( { From, Event, To, ToPointVec2, NearRadius } )
NearRadius = NearRadius or 25 NearRadius = NearRadius or 100
local Angle = 180 local Angle = 180
local Speed = 10 local Speed = 10
@ -831,7 +836,7 @@ do -- CARGO_UNIT
function CARGO_UNIT:onafterUnBoarding( From, Event, To, ToPointVec2, NearRadius ) function CARGO_UNIT:onafterUnBoarding( From, Event, To, ToPointVec2, NearRadius )
self:F( { From, Event, To, ToPointVec2, NearRadius } ) self:F( { From, Event, To, ToPointVec2, NearRadius } )
NearRadius = NearRadius or 25 NearRadius = NearRadius or 100
self.CargoInAir = self.CargoObject:InAir() self.CargoInAir = self.CargoObject:InAir()
@ -1149,298 +1154,299 @@ do -- CARGO_GROUP
ClassName = "CARGO_GROUP", ClassName = "CARGO_GROUP",
} }
--- CARGO_GROUP constructor. --- CARGO_GROUP constructor.
-- @param #CARGO_GROUP self -- @param #CARGO_GROUP self
-- @param Wrapper.Group#GROUP CargoGroup -- @param Wrapper.Group#GROUP CargoGroup
-- @param #string Type -- @param #string Type
-- @param #string Name -- @param #string Name
-- @param #number ReportRadius (optional) -- @param #number ReportRadius (optional)
-- @param #number NearRadius (optional) -- @param #number NearRadius (optional)
-- @return #CARGO_GROUP -- @return #CARGO_GROUP
function CARGO_GROUP:New( CargoGroup, Type, Name, ReportRadius ) function CARGO_GROUP:New( CargoGroup, Type, Name, ReportRadius )
local self = BASE:Inherit( self, CARGO_REPORTABLE:New( CargoGroup, Type, Name, 0, ReportRadius ) ) -- #CARGO_GROUP local self = BASE:Inherit( self, CARGO_REPORTABLE:New( CargoGroup, Type, Name, 0, ReportRadius ) ) -- #CARGO_GROUP
self:F( { Type, Name, ReportRadius } ) self:F( { Type, Name, ReportRadius } )
self.CargoObject = CargoGroup self.CargoObject = CargoGroup
self:SetDeployed( false ) self:SetDeployed( false )
self.CargoGroup = CargoGroup self.CargoGroup = CargoGroup
local WeightGroup = 0 local WeightGroup = 0
for UnitID, UnitData in pairs( CargoGroup:GetUnits() ) do for UnitID, UnitData in pairs( CargoGroup:GetUnits() ) do
local Unit = UnitData -- Wrapper.Unit#UNIT local Unit = UnitData -- Wrapper.Unit#UNIT
local WeightUnit = Unit:GetDesc().massEmpty local WeightUnit = Unit:GetDesc().massEmpty
WeightGroup = WeightGroup + WeightUnit WeightGroup = WeightGroup + WeightUnit
local CargoUnit = CARGO_UNIT:New( Unit, Type, Unit:GetName(), WeightUnit ) local CargoUnit = CARGO_UNIT:New( Unit, Type, Unit:GetName(), WeightUnit )
self.CargoSet:Add( CargoUnit:GetName(), CargoUnit ) self.CargoSet:Add( CargoUnit:GetName(), CargoUnit )
end
self:SetWeight( WeightGroup )
self:T( { "Weight Cargo", WeightGroup } )
-- Cargo objects are added to the _DATABASE and SET_CARGO objects.
_EVENTDISPATCHER:CreateEventNewCargo( self )
self:HandleEvent( EVENTS.Dead, self.OnEventCargoDead )
self:HandleEvent( EVENTS.Crash, self.OnEventCargoDead )
self:HandleEvent( EVENTS.PlayerLeaveUnit, self.OnEventCargoDead )
self:SetEventPriority( 4 )
return self
end end
self:SetWeight( WeightGroup ) --- @param #CARGO_GROUP self
-- @param Core.Event#EVENTDATA EventData
function CARGO_GROUP:OnEventCargoDead( EventData )
self:T( { "Weight Cargo", WeightGroup } ) local Destroyed = false
-- Cargo objects are added to the _DATABASE and SET_CARGO objects. if self:IsDestroyed() or self:IsUnLoaded() then
_EVENTDISPATCHER:CreateEventNewCargo( self )
self:HandleEvent( EVENTS.Dead, self.OnEventCargoDead )
self:HandleEvent( EVENTS.Crash, self.OnEventCargoDead )
self:HandleEvent( EVENTS.PlayerLeaveUnit, self.OnEventCargoDead )
self:SetEventPriority( 4 )
return self
end
--- @param #CARGO_GROUP self
-- @param Core.Event#EVENTDATA EventData
function CARGO_GROUP:OnEventCargoDead( EventData )
local Destroyed = false
if self:IsDestroyed() or self:IsUnLoaded() then
Destroyed = true
for CargoID, CargoData in pairs( self.CargoSet:GetSet() ) do
local Cargo = CargoData -- #CARGO
if Cargo:IsAlive() then
Destroyed = false
else
Cargo:Destroyed()
end
end
else
local CarrierName = self.CargoCarrier:GetName()
if CarrierName == EventData.IniDCSUnitName then
MESSAGE:New( "Cargo is lost from carrier " .. CarrierName, 15 ):ToAll()
Destroyed = true Destroyed = true
self.CargoCarrier:ClearCargo() for CargoID, CargoData in pairs( self.CargoSet:GetSet() ) do
local Cargo = CargoData -- #CARGO
if Cargo:IsAlive() then
Destroyed = false
else
Cargo:Destroyed()
end
end
else
local CarrierName = self.CargoCarrier:GetName()
if CarrierName == EventData.IniDCSUnitName then
MESSAGE:New( "Cargo is lost from carrier " .. CarrierName, 15 ):ToAll()
Destroyed = true
self.CargoCarrier:ClearCargo()
end
end end
if Destroyed then
self:Destroyed()
self:E( { "Cargo group destroyed" } )
end
end end
if Destroyed then --- Enter Boarding State.
self:Destroyed() -- @param #CARGO_GROUP self
self:E( { "Cargo group destroyed" } ) -- @param Wrapper.Unit#UNIT CargoCarrier
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onenterBoarding( From, Event, To, CargoCarrier, NearRadius, ... )
self:F( { CargoCarrier.UnitName, From, Event, To } )
local NearRadius = NearRadius or 25
if From == "UnLoaded" then
-- For each Cargo object within the CARGO_GROUPED, route each object to the CargoLoadPointVec2
self.CargoSet:ForEach(
function( Cargo, ... )
Cargo:__Board( 1, CargoCarrier, NearRadius, ... )
end, ...
)
self:__Boarding( 1, CargoCarrier, NearRadius, ... )
end
end end
end --- Enter Loaded State.
-- @param #CARGO_GROUP self
-- @param Wrapper.Unit#UNIT CargoCarrier
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onenterLoaded( From, Event, To, CargoCarrier, ... )
self:F( { From, Event, To, CargoCarrier, ...} )
--- Enter Boarding State. if From == "UnLoaded" then
-- @param #CARGO_GROUP self -- For each Cargo object within the CARGO_GROUP, load each cargo to the CargoCarrier.
-- @param Wrapper.Unit#UNIT CargoCarrier for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
-- @param #string Event Cargo:Load( CargoCarrier )
-- @param #string From end
-- @param #string To end
function CARGO_GROUP:onenterBoarding( From, Event, To, CargoCarrier, NearRadius, ... )
self:F( { CargoCarrier.UnitName, From, Event, To } )
local NearRadius = NearRadius or 25 --self.CargoObject:Destroy()
self.CargoCarrier = CargoCarrier
if From == "UnLoaded" then
-- For each Cargo object within the CARGO_GROUPED, route each object to the CargoLoadPointVec2
self.CargoSet:ForEach(
function( Cargo, ... )
Cargo:__Board( 1, CargoCarrier, NearRadius, ... )
end, ...
)
self:__Boarding( 1, CargoCarrier, NearRadius, ... )
end end
end --- Leave Boarding State.
-- @param #CARGO_GROUP self
-- @param Wrapper.Unit#UNIT CargoCarrier
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onafterBoarding( From, Event, To, CargoCarrier, NearRadius, ... )
self:F( { CargoCarrier.UnitName, From, Event, To } )
--- Enter Loaded State. local NearRadius = NearRadius or 100
-- @param #CARGO_GROUP self
-- @param Wrapper.Unit#UNIT CargoCarrier
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onenterLoaded( From, Event, To, CargoCarrier, ... )
self:F( { From, Event, To, CargoCarrier, ...} )
if From == "UnLoaded" then local Boarded = true
-- For each Cargo object within the CARGO_GROUP, load each cargo to the CargoCarrier. local Cancelled = false
local Dead = true
self.CargoSet:Flush()
-- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2
for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
Cargo:Load( CargoCarrier ) self:T( { Cargo:GetName(), Cargo.current } )
end
end
--self.CargoObject:Destroy()
self.CargoCarrier = CargoCarrier
end
--- Leave Boarding State.
-- @param #CARGO_GROUP self
-- @param Wrapper.Unit#UNIT CargoCarrier
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onafterBoarding( From, Event, To, CargoCarrier, NearRadius, ... )
self:F( { CargoCarrier.UnitName, From, Event, To } )
local NearRadius = NearRadius or 25
local Boarded = true
local Cancelled = false
local Dead = true
self.CargoSet:Flush()
-- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2
for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
self:T( { Cargo:GetName(), Cargo.current } )
if not Cargo:is( "Loaded" ) if not Cargo:is( "Loaded" )
and (not Cargo:is( "Destroyed" )) then -- If one or more units of a group defined as CARGO_GROUP died, the CARGO_GROUP:Board() command does not trigger the CARGO_GRUOP:OnEnterLoaded() function. and (not Cargo:is( "Destroyed" )) then -- If one or more units of a group defined as CARGO_GROUP died, the CARGO_GROUP:Board() command does not trigger the CARGO_GRUOP:OnEnterLoaded() function.
Boarded = false Boarded = false
end
if Cargo:is( "UnLoaded" ) then
Cancelled = true
end
if not Cargo:is( "Destroyed" ) then
Dead = false
end
end end
if Cargo:is( "UnLoaded" ) then if not Dead then
Cancelled = true
end
if not Cargo:is( "Destroyed" ) then if not Cancelled then
Dead = false if not Boarded then
end self:__Boarding( 1, CargoCarrier, NearRadius, ... )
else
end self:F("Group Cargo is loaded")
self:__Load( 1, CargoCarrier, ... )
if not Dead then end
if not Cancelled then
if not Boarded then
self:__Boarding( 1, CargoCarrier, NearRadius, ... )
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
else
self:__Destroyed( 1, CargoCarrier, NearRadius, ... )
end
end
--- Get the amount of cargo units in the group.
-- @param #CARGO_GROUP self
-- @return #CARGO_GROUP
function CARGO_GROUP:GetCount()
return self.CargoSet:Count()
end
--- Enter UnBoarding State.
-- @param #CARGO_GROUP self
-- @param Core.Point#POINT_VEC2 ToPointVec2
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onenterUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... )
self:F( {From, Event, To, ToPointVec2, NearRadius } )
NearRadius = NearRadius or 25
local Timer = 1
if From == "Loaded" then
if self.CargoObject then
self.CargoObject:Destroy()
end end
-- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2
self.CargoSet:ForEach(
function( Cargo, NearRadius )
Cargo:__UnBoard( Timer, ToPointVec2, NearRadius )
Timer = Timer + 10
end, { NearRadius }
)
self:__UnBoarding( 1, ToPointVec2, NearRadius, ... )
end end
end --- Get the amount of cargo units in the group.
-- @param #CARGO_GROUP self
-- @return #CARGO_GROUP
function CARGO_GROUP:GetCount()
return self.CargoSet:Count()
end
--- Leave UnBoarding State.
-- @param #CARGO_GROUP self
-- @param Core.Point#POINT_VEC2 ToPointVec2
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onleaveUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... )
self:F( { From, Event, To, ToPointVec2, NearRadius } )
--local NearRadius = NearRadius or 25 --- Enter UnBoarding State.
-- @param #CARGO_GROUP self
-- @param Core.Point#POINT_VEC2 ToPointVec2
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onenterUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... )
self:F( {From, Event, To, ToPointVec2, NearRadius } )
local Angle = 180 NearRadius = NearRadius or 100
local Speed = 10
local Distance = 5
if From == "UnBoarding" then local Timer = 1
local UnBoarded = true
-- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2 if From == "Loaded" then
for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
self:T( Cargo.current ) if self.CargoObject then
if not Cargo:is( "UnLoaded" ) then self.CargoObject:Destroy()
UnBoarded = false
end end
end
if UnBoarded then -- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2
return true self.CargoSet:ForEach(
else function( Cargo, NearRadius )
Cargo:__UnBoard( Timer, ToPointVec2, NearRadius )
Timer = Timer + 10
end, { NearRadius }
)
self:__UnBoarding( 1, ToPointVec2, NearRadius, ... ) self:__UnBoarding( 1, ToPointVec2, NearRadius, ... )
end end
return false
end end
end --- Leave UnBoarding State.
-- @param #CARGO_GROUP self
-- @param Core.Point#POINT_VEC2 ToPointVec2
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onleaveUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... )
self:F( { From, Event, To, ToPointVec2, NearRadius } )
--- UnBoard Event. --local NearRadius = NearRadius or 25
-- @param #CARGO_GROUP self
-- @param Core.Point#POINT_VEC2 ToPointVec2
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onafterUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... )
self:F( { From, Event, To, ToPointVec2, NearRadius } )
--local NearRadius = NearRadius or 25 local Angle = 180
local Speed = 10
local Distance = 5
self:__UnLoad( 1, ToPointVec2, ... ) if From == "UnBoarding" then
end local UnBoarded = true
-- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2
for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
--- Enter UnLoaded State. self:T( Cargo.current )
-- @param #CARGO_GROUP self if not Cargo:is( "UnLoaded" ) then
-- @param Core.Point#POINT_VEC2 UnBoarded = false
-- @param #string Event end
-- @param #string From
-- @param #string To
function CARGO_GROUP:onenterUnLoaded( From, Event, To, ToPointVec2, ... )
self:F( { From, Event, To, ToPointVec2 } )
if From == "Loaded" then
-- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2
self.CargoSet:ForEach(
function( Cargo )
--Cargo:UnLoad( ToPointVec2 )
local RandomVec2=ToPointVec2:GetRandomPointVec2InRadius(10)
Cargo:UnLoad( RandomVec2 )
end end
)
if UnBoarded then
return true
else
self:__UnBoarding( 1, ToPointVec2, NearRadius, ... )
end
return false
end
end end
end --- UnBoard Event.
-- @param #CARGO_GROUP self
-- @param Core.Point#POINT_VEC2 ToPointVec2
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onafterUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... )
self:F( { From, Event, To, ToPointVec2, NearRadius } )
--local NearRadius = NearRadius or 25
self:__UnLoad( 1, ToPointVec2, ... )
end
--- Enter UnLoaded State.
-- @param #CARGO_GROUP self
-- @param Core.Point#POINT_VEC2
-- @param #string Event
-- @param #string From
-- @param #string To
function CARGO_GROUP:onenterUnLoaded( From, Event, To, ToPointVec2, ... )
self:F( { From, Event, To, ToPointVec2 } )
if From == "Loaded" then
-- For each Cargo object within the CARGO_GROUP, route each object to the CargoLoadPointVec2
self.CargoSet:ForEach(
function( Cargo )
--Cargo:UnLoad( ToPointVec2 )
local RandomVec2=ToPointVec2:GetRandomPointVec2InRadius(10)
Cargo:UnLoad( RandomVec2 )
end
)
end
end
--- Respawn the cargo when destroyed --- Respawn the cargo when destroyed
@ -1459,6 +1465,43 @@ end
end end
--- Route Cargo to Coordinate and randomize locations.
-- @param #CARGO_GROUP self
-- @param Core.Point#COORDINATE Coordinate
function CARGO_GROUP:RouteTo( Coordinate )
self:F( {Coordinate = Coordinate } )
-- For each Cargo object within the CARGO_GROUPED, route each object to the CargoLoadPointVec2
self.CargoSet:ForEach(
function( Cargo )
Cargo.CargoObject:RouteGroundTo( Coordinate, 10, "vee", 0 )
end
)
end
--- Check if Cargo is near to the Carrier.
-- The Cargo is near to the Carrier if the first unit of the Cargo Group is within NearRadius.
-- @param #CARGO_GROUP self
-- @param Wrapper.Group#GROUP CargoCarrier
-- @param #number NearRadius
-- @return #boolean The Cargo is near to the Carrier.
-- @return #nil The Cargo is not near to the Carrier.
function CARGO_GROUP:IsNear( CargoCarrier, NearRadius )
self:F( {NearRadius = NearRadius } )
local FirstCargo = self.CargoSet:GetFirst() -- #CARGO
if FirstCargo then
if FirstCargo:IsNear( CargoCarrier:GetCoordinate(), NearRadius ) then
self:F( "Near" )
return true
end
end
return nil
end
end -- CARGO_GROUP end -- CARGO_GROUP
do -- CARGO_PACKAGE do -- CARGO_PACKAGE

View File

@ -192,6 +192,20 @@ do -- COORDINATE
return self return self
end end
--- COORDINATE constructor.
-- @param #COORDINATE self
-- @param #COORDINATE Coordinate.
-- @return #COORDINATE
function COORDINATE:NewFromCoordinate( Coordinate )
local self = BASE:Inherit( self, BASE:New() ) -- #COORDINATE
self.x = Coordinate.x
self.y = Coordinate.y
self.z = Coordinate.z
return self
end
--- Create a new COORDINATE object from Vec2 coordinates. --- Create a new COORDINATE object from Vec2 coordinates.
-- @param #COORDINATE self -- @param #COORDINATE self
-- @param Dcs.DCSTypes#Vec2 Vec2 The Vec2 point. -- @param Dcs.DCSTypes#Vec2 Vec2 The Vec2 point.
@ -241,6 +255,25 @@ do -- COORDINATE
return { x = self.x, y = self.z } return { x = self.x, y = self.z }
end end
--- Returns if the 2 coordinates are at the same 2D position.
-- @param #COORDINATE self
-- @param #COORDINATE Coordinate
-- @param #number Precision
-- @return #boolean true if at the same position.
function COORDINATE:IsAtCoordinate2D( Coordinate, Precision )
self:F( { Coordinate = Coordinate:GetVec2() } )
self:F( { self = self:GetVec2() } )
local x = Coordinate.x
local z = Coordinate.z
return x - Precision <= self.x and x + Precision >= self.x and z - Precision <= self.z and z + Precision >= self.z
end
--TODO: check this to replace --TODO: check this to replace
--- Calculate the distance from a reference @{DCSTypes#Vec2}. --- Calculate the distance from a reference @{DCSTypes#Vec2}.
-- @param #COORDINATE self -- @param #COORDINATE self

View File

@ -1908,6 +1908,28 @@ function CONTROLLABLE:Route( Route, DelaySeconds )
end end
--- Stops the movement of the vehicle on the route.
-- @param #CONTROLLABLE self
-- @return #CONTROLLABLE
function CONTROLLABLE:RouteStop()
self:F2()
local CommandStop = self:CommandStopRoute( true )
self:SetCommand( CommandStop )
end
--- Resumes the movement of the vehicle on the route.
-- @param #CONTROLLABLE self
-- @return #CONTROLLABLE
function CONTROLLABLE:RouteResume()
self:F2()
local CommandResume = self:CommandStopRoute( false )
self:SetCommand( CommandResume )
end
--- Make the GROUND Controllable to drive towards a specific point. --- Make the GROUND Controllable to drive towards a specific point.
-- @param #CONTROLLABLE self -- @param #CONTROLLABLE self
-- @param Core.Point#COORDINATE ToCoordinate A Coordinate to drive to. -- @param Core.Point#COORDINATE ToCoordinate A Coordinate to drive to.