mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-10-29 16:58:06 +00:00
- Documentation
- Added the methods Added and Removed to the SET - Cleanup of code.
This commit is contained in:
@@ -9,66 +9,115 @@
|
||||
-- @module AI_Cargo_Dispatcher
|
||||
|
||||
--- @type AI_CARGO_DISPATCHER
|
||||
-- @extends Core.Fsm#FSM_CONTROLLABLE
|
||||
-- @extends Core.Fsm#FSM
|
||||
|
||||
|
||||
--- # AI\_CARGO\_DISPATCHER class, extends @{Core.Base#BASE}
|
||||
--- # AI\_CARGO\_DISPATCHER class, extends @{Core.Fsm#FSM}
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- AI\_CARGO\_DISPATCHER brings a dynamic cargo handling capability for AI groups.
|
||||
--
|
||||
-- Armoured Personnel APCs (APC), Trucks, Jeeps and other carrier equipment can be mobilized to intelligently transport infantry and other cargo within the simulation.
|
||||
-- The AI\_CARGO\_DISPATCHER module uses the @{Cargo} capabilities within the MOOSE framework.
|
||||
-- Carrier equipment can be mobilized to intelligently transport infantry and other cargo within the simulation.
|
||||
-- The AI\_CARGO\_DISPATCHER module uses the @{Cargo} capabilities within the MOOSE framework, to enable Carrier GROUP objects
|
||||
-- to transport @{Cargo} towards several deploy zones.
|
||||
-- CARGO derived objects must be declared within the mission to make the AI\_CARGO\_DISPATCHER object recognize the cargo.
|
||||
-- Please consult the @{Cargo} module for more information.
|
||||
--
|
||||
-- ## 1. AI\_CARGO\_DISPATCHER constructor
|
||||
--
|
||||
-- * @{#AI_CARGO_DISPATCHER.New}(): Creates a new AI\_CARGO\_DISPATCHER object.
|
||||
--
|
||||
-- ## 2. AI\_CARGO\_DISPATCHER is a FSM
|
||||
--
|
||||
-- 
|
||||
--
|
||||
-- ### 2.1. AI\_CARGO\_DISPATCHER States
|
||||
--
|
||||
-- * **Monitoring**: The process is dispatching.
|
||||
-- * **Idle**: The process is idle.
|
||||
--
|
||||
-- ### 2.2. AI\_CARGO\_DISPATCHER Events
|
||||
--
|
||||
-- * **Monitor**: Monitor and take action.
|
||||
-- * **Start**: Start the transport process.
|
||||
-- * **Stop**: Stop the transport process.
|
||||
-- * **Pickup**: Pickup cargo.
|
||||
-- * **Load**: Load the cargo.
|
||||
-- * **Loaded**: Flag that the cargo is loaded.
|
||||
-- * **Deploy**: Deploy cargo to a location.
|
||||
-- * **Unload**: Unload the cargo.
|
||||
-- * **Unloaded**: Flag that the cargo is unloaded.
|
||||
-- * **Home**: A Carrier is going home.
|
||||
--
|
||||
-- ## 3. Set the pickup parameters.
|
||||
--
|
||||
-- Several parameters can be set to pickup cargo:
|
||||
--
|
||||
-- * @{#AI_CARGO_DISPATCHER.SetPickupRadius}(): Sets or randomizes the pickup location for the carrier around the cargo coordinate in a radius defined an outer and optional inner radius.
|
||||
-- * @{#AI_CARGO_DISPATCHER.SetPickupSpeed}(): Set the speed or randomizes the speed in km/h to pickup the cargo.
|
||||
--
|
||||
-- ## 4. Set the deploy parameters.
|
||||
--
|
||||
-- Several parameters can be set to deploy cargo:
|
||||
--
|
||||
-- * @{#AI_CARGO_DISPATCHER.SetDeployRadius}(): Sets or randomizes the deploy location for the carrier around the cargo coordinate in a radius defined an outer and an optional inner radius.
|
||||
-- * @{#AI_CARGO_DISPATCHER.SetDeploySpeed}(): Set the speed or randomizes the speed in km/h to deploy the cargo.
|
||||
--
|
||||
-- ## 5. Set the home zone when there isn't any more cargo to pickup.
|
||||
--
|
||||
-- A home zone can be specified to where the Carriers will move when there isn't any cargo left for pickup.
|
||||
-- Use @{#AI_CARGO_DISPATCHER.SetHomeZone}() to specify the home zone.
|
||||
--
|
||||
-- If no home zone is specified, the carriers will wait near the deploy zone for a new pickup command.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- @field #AI_CARGO_DISPATCHER
|
||||
AI_CARGO_DISPATCHER = {
|
||||
ClassName = "AI_CARGO_DISPATCHER",
|
||||
SetAPC = nil,
|
||||
SetCarrier = nil,
|
||||
SetDeployZones = nil,
|
||||
AI_CARGO_APC = {}
|
||||
AI_Cargo = {},
|
||||
PickupCargo = {}
|
||||
}
|
||||
|
||||
--- @type AI_CARGO_DISPATCHER.AI_CARGO_APC
|
||||
-- @map <Wrapper.Group#GROUP, AI.AI_Cargo_APC#AI_CARGO_APC>
|
||||
|
||||
--- @field #AI_CARGO_DISPATCHER.AI_CARGO_APC
|
||||
--- @field #AI_CARGO_DISPATCHER.AI_Cargo
|
||||
AI_CARGO_DISPATCHER.AI_Cargo = {}
|
||||
|
||||
--- @field #AI_CARGO_DISPATCHER.PickupCargo
|
||||
AI_CARGO_DISPATCHER.PickupCargo = {}
|
||||
|
||||
|
||||
|
||||
--- Creates a new AI_CARGO_DISPATCHER object.
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param Core.Set#SET_GROUP SetAPC
|
||||
-- @param Core.Set#SET_GROUP SetCarrier
|
||||
-- @param Core.Set#SET_CARGO SetCargo
|
||||
-- @param Core.Set#SET_ZONE SetDeployZone
|
||||
-- @return #AI_CARGO_DISPATCHER
|
||||
-- @usage
|
||||
--
|
||||
-- -- Create a new cargo dispatcher
|
||||
-- SetAPC = SET_GROUP:New():FilterPrefixes( "APC" ):FilterStart()
|
||||
-- SetCarrier = SET_GROUP:New():FilterPrefixes( "APC" ):FilterStart()
|
||||
-- SetCargo = SET_CARGO:New():FilterTypes( "Infantry" ):FilterStart()
|
||||
-- SetDeployZone = SET_ZONE:New():FilterPrefixes( "Deploy" ):FilterStart()
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZone )
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetCarrier, SetCargo, SetDeployZone )
|
||||
--
|
||||
function AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZones )
|
||||
function AI_CARGO_DISPATCHER:New( SetCarrier, SetCargo, SetDeployZones )
|
||||
|
||||
local self = BASE:Inherit( self, FSM:New() ) -- #AI_CARGO_DISPATCHER
|
||||
|
||||
self.SetAPC = SetAPC -- Core.Set#SET_GROUP
|
||||
self.SetCarrier = SetCarrier -- Core.Set#SET_GROUP
|
||||
self.SetCargo = SetCargo -- Core.Set#SET_CARGO
|
||||
self.SetDeployZones = SetDeployZones -- Core.Set#SET_ZONE
|
||||
|
||||
self:SetStartState( "Dispatch" )
|
||||
self:SetStartState( "Idle" )
|
||||
|
||||
self:AddTransition( "Monitoring", "Monitor", "Monitoring" )
|
||||
|
||||
self:AddTransition( "Idle", "Start", "Monitoring" )
|
||||
self:AddTransition( "Monitoring", "Stop", "Idle" )
|
||||
|
||||
self:AddTransition( "*", "Monitor", "*" )
|
||||
|
||||
self:AddTransition( "*", "Pickup", "*" )
|
||||
self:AddTransition( "*", "Loading", "*" )
|
||||
@@ -84,8 +133,16 @@ function AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZones )
|
||||
self.DeployRadiusInner = 200
|
||||
self.DeployRadiusOuter = 500
|
||||
|
||||
self.PickupCargo = {}
|
||||
self.CarrierHome = {}
|
||||
|
||||
-- Put a Dead event handler on SetCarrier, to ensure that when a carrier is destroyed, that all internal parameters are reset.
|
||||
function SetCarrier.OnAfterRemoved( SetCarrier, From, Event, To, CarrierName, Carrier )
|
||||
self:F( { Carrier = Carrier:GetName() } )
|
||||
self.PickupCargo[Carrier] = nil
|
||||
self.CarrierHome[Carrier] = nil
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
@@ -99,7 +156,7 @@ end
|
||||
-- @usage
|
||||
--
|
||||
-- -- Create a new cargo dispatcher
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZone )
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetCarrier, SetCargo, SetDeployZone )
|
||||
--
|
||||
-- -- Set the home coordinate
|
||||
-- local HomeZone = ZONE:New( "Home" )
|
||||
@@ -132,7 +189,7 @@ end
|
||||
-- @usage
|
||||
--
|
||||
-- -- Create a new cargo dispatcher
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZone )
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetCarrier, SetCargo, SetDeployZone )
|
||||
--
|
||||
-- -- Set the carrier to land within a band around the cargo coordinate between 500 and 300 meters!
|
||||
-- AICargoDispatcher:SetPickupRadius( 500, 300 )
|
||||
@@ -157,7 +214,7 @@ end
|
||||
-- @usage
|
||||
--
|
||||
-- -- Create a new cargo dispatcher
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZone )
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetCarrier, SetCargo, SetDeployZone )
|
||||
--
|
||||
-- -- Set the minimum pickup speed to be 100 km/h and the maximum speed to be 200 km/h.
|
||||
-- AICargoDispatcher:SetPickupSpeed( 200, 100 )
|
||||
@@ -190,7 +247,7 @@ end
|
||||
-- @usage
|
||||
--
|
||||
-- -- Create a new cargo dispatcher
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZone )
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetCarrier, SetCargo, SetDeployZone )
|
||||
--
|
||||
-- -- Set the carrier to land within a band around the cargo coordinate between 500 and 300 meters!
|
||||
-- AICargoDispatcher:SetDeployRadius( 500, 300 )
|
||||
@@ -215,7 +272,7 @@ end
|
||||
-- @usage
|
||||
--
|
||||
-- -- Create a new cargo dispatcher
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetAPC, SetCargo, SetDeployZone )
|
||||
-- AICargoDispatcher = AI_CARGO_DISPATCHER:New( SetCarrier, SetCargo, SetDeployZone )
|
||||
--
|
||||
-- -- Set the minimum deploy speed to be 100 km/h and the maximum speed to be 200 km/h.
|
||||
-- AICargoDispatcher:SetDeploySpeed( 200, 100 )
|
||||
@@ -235,64 +292,66 @@ end
|
||||
|
||||
--- The Start trigger event, which actually takes action at the specified time interval.
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param Wrapper.Group#GROUP APC
|
||||
-- @return #AI_CARGO_DISPATCHER
|
||||
function AI_CARGO_DISPATCHER:onafterMonitor()
|
||||
|
||||
for APCGroupName, Carrier in pairs( self.SetAPC:GetSet() ) do
|
||||
for CarrierGroupName, Carrier in pairs( self.SetCarrier:GetSet() ) do
|
||||
local Carrier = Carrier -- Wrapper.Group#GROUP
|
||||
local AI_Cargo = self.AI_Cargo[Carrier]
|
||||
if not AI_Cargo then
|
||||
|
||||
-- ok, so this APC does not have yet an AI_CARGO_APC object...
|
||||
-- ok, so this Carrier does not have yet an AI_CARGO handling object...
|
||||
-- let's create one and also declare the Loaded and UnLoaded handlers.
|
||||
self.AI_Cargo[Carrier] = self:AICargo( Carrier, self.SetCargo, self.CombatRadius )
|
||||
AI_Cargo = self.AI_Cargo[Carrier]
|
||||
|
||||
function AI_Cargo.OnAfterPickup( AI_Cargo, APC, From, Event, To, Cargo )
|
||||
self:Pickup( APC, Cargo )
|
||||
function AI_Cargo.OnAfterPickup( AI_Cargo, Carrier, From, Event, To, Cargo )
|
||||
self:Pickup( Carrier, Cargo )
|
||||
end
|
||||
|
||||
function AI_Cargo.OnAfterLoad( AI_Cargo, APC )
|
||||
self:Loading( APC )
|
||||
function AI_Cargo.OnAfterLoad( AI_Cargo, Carrier )
|
||||
self:Loading( Carrier )
|
||||
end
|
||||
|
||||
function AI_Cargo.OnAfterLoaded( AI_Cargo, APC, From, Event, To, Cargo )
|
||||
self:Loaded( APC, Cargo )
|
||||
function AI_Cargo.OnAfterLoaded( AI_Cargo, Carrier, From, Event, To, Cargo )
|
||||
self:Loaded( Carrier, Cargo )
|
||||
end
|
||||
|
||||
function AI_Cargo.OnAfterDeploy( AI_Cargo, APC )
|
||||
self:Deploy( APC )
|
||||
function AI_Cargo.OnAfterDeploy( AI_Cargo, Carrier )
|
||||
self:Deploy( Carrier )
|
||||
end
|
||||
|
||||
function AI_Cargo.OnAfterUnload( AI_Cargo, APC )
|
||||
self:Unloading( APC )
|
||||
function AI_Cargo.OnAfterUnload( AI_Cargo, Carrier )
|
||||
self:Unloading( Carrier )
|
||||
end
|
||||
|
||||
function AI_Cargo.OnAfterUnloaded( AI_Cargo, APC )
|
||||
self:Unloaded( APC )
|
||||
function AI_Cargo.OnAfterUnloaded( AI_Cargo, Carrier )
|
||||
self:Unloaded( Carrier )
|
||||
end
|
||||
end
|
||||
|
||||
-- The Pickup sequence ...
|
||||
-- Check if this APC need to go and Pickup something...
|
||||
-- Check if this Carrier need to go and Pickup something...
|
||||
self:I( { IsTransporting = AI_Cargo:IsTransporting() } )
|
||||
if AI_Cargo:IsTransporting() == false then
|
||||
-- ok, so there is a free APC
|
||||
-- ok, so there is a free Carrier
|
||||
-- now find the first cargo that is Unloaded
|
||||
|
||||
local PickupCargo = nil
|
||||
|
||||
for CargoName, Cargo in pairs( self.SetCargo:GetSet() ) do
|
||||
local Cargo = Cargo -- Cargo.Cargo#CARGO
|
||||
self:F( { Cargo = Cargo:GetName(), UnLoaded = Cargo:IsUnLoaded(), Deployed = Cargo:IsDeployed(), PickupCargo = self.PickupCargo[Cargo] ~= nil } )
|
||||
self:F( { Cargo = Cargo:GetName(), UnLoaded = Cargo:IsUnLoaded(), Deployed = Cargo:IsDeployed(), PickupCargo = self.PickupCargo[Carrier] ~= nil } )
|
||||
if Cargo:IsUnLoaded() and not Cargo:IsDeployed() then
|
||||
local CargoCoordinate = Cargo:GetCoordinate()
|
||||
local CoordinateFree = true
|
||||
for APC, Coordinate in pairs( self.PickupCargo ) do
|
||||
if CargoCoordinate:Get2DDistance( Coordinate ) <= 25 then
|
||||
CoordinateFree = false
|
||||
break
|
||||
for CarrierPickup, Coordinate in pairs( self.PickupCargo ) do
|
||||
if CarrierPickup:IsAlive() == true then
|
||||
if CargoCoordinate:Get2DDistance( Coordinate ) <= 25 then
|
||||
CoordinateFree = false
|
||||
break
|
||||
end
|
||||
else
|
||||
self.PickupCargo[CarrierPickup] = nil
|
||||
end
|
||||
end
|
||||
if CoordinateFree == true then
|
||||
@@ -319,36 +378,90 @@ function AI_CARGO_DISPATCHER:onafterMonitor()
|
||||
end
|
||||
|
||||
self:__Monitor( self.MonitorTimeInterval )
|
||||
end
|
||||
|
||||
return self
|
||||
--- Start Handler OnBefore for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] OnBeforeStart
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param #string From
|
||||
-- @param #string Event
|
||||
-- @param #string To
|
||||
-- @return #boolean
|
||||
|
||||
--- Start Handler OnAfter for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] OnAfterStart
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param #string From
|
||||
-- @param #string Event
|
||||
-- @param #string To
|
||||
|
||||
--- Start Trigger for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] Start
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
|
||||
--- Start Asynchronous Trigger for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] __Start
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param #number Delay
|
||||
|
||||
function AI_CARGO_DISPATCHER:onafterStart( From, Event, To )
|
||||
self:Monitor()
|
||||
end
|
||||
|
||||
--- Stop Handler OnBefore for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] OnBeforeStop
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param #string From
|
||||
-- @param #string Event
|
||||
-- @param #string To
|
||||
-- @return #boolean
|
||||
|
||||
--- Stop Handler OnAfter for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] OnAfterStop
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param #string From
|
||||
-- @param #string Event
|
||||
-- @param #string To
|
||||
|
||||
--- Stop Trigger for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] Stop
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
|
||||
--- Stop Asynchronous Trigger for AI_CARGO_DISPATCHER
|
||||
-- @function [parent=#AI_CARGO_DISPATCHER] __Stop
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param #number Delay
|
||||
|
||||
|
||||
|
||||
--- Make a Carrier run for a cargo deploy action after the cargo Pickup trigger has been initiated, by default.
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param From
|
||||
-- @param Event
|
||||
-- @param To
|
||||
-- @param Wrapper.Group#GROUP Carrier
|
||||
-- @param Cargo.Cargo#CARGO Cargo
|
||||
-- @return #AI_CARGO_DISPATCHER
|
||||
function AI_CARGO_DISPATCHER:OnAfterPickup( From, Event, To, Carrier, Cargo )
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- Make a APC run for a cargo deploy action after the cargo Pickup trigger has been initiated, by default.
|
||||
--- Make a Carrier run for a cargo deploy action after the cargo has been loaded, by default.
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param Wrapper.Group#GROUP APC
|
||||
-- @param From
|
||||
-- @param Event
|
||||
-- @param To
|
||||
-- @param Wrapper.Group#GROUP Carrier
|
||||
-- @param Cargo.Cargo#CARGO Cargo
|
||||
-- @return #AI_CARGO_DISPATCHER
|
||||
function AI_CARGO_DISPATCHER:onafterPickup( From, Event, To, APC, Cargo )
|
||||
return self
|
||||
end
|
||||
function AI_CARGO_DISPATCHER:OnAfterLoaded( From, Event, To, Carrier, Cargo )
|
||||
|
||||
--- Make a APC run for a cargo deploy action after the cargo has been loaded, by default.
|
||||
-- @param #AI_CARGO_DISPATCHER self
|
||||
-- @param Wrapper.Group#GROUP APC
|
||||
-- @return #AI_CARGO_DISPATCHER
|
||||
function AI_CARGO_DISPATCHER:OnAfterLoaded( From, Event, To, APC, Cargo )
|
||||
|
||||
self:I( { "Loaded Dispatcher", APC } )
|
||||
local DeployZone = self.SetDeployZones:GetRandomZone()
|
||||
self:I( { RandomZone = DeployZone } )
|
||||
|
||||
local DeployCoordinate = DeployZone:GetCoordinate():GetRandomCoordinateInRadius( self.DeployOuterRadius, self.DeployInnerRadius )
|
||||
self.AI_Cargo[APC]:Deploy( DeployCoordinate, math.random( self.DeployMinSpeed, self.DeployMaxSpeed ) )
|
||||
self.AI_Cargo[Carrier]:Deploy( DeployCoordinate, math.random( self.DeployMinSpeed, self.DeployMaxSpeed ) )
|
||||
|
||||
self.PickupCargo[APC] = nil
|
||||
|
||||
return self
|
||||
self.PickupCargo[Carrier] = nil
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user