Optimized boarding logic. Now it works correct.

Adjust speed at the end to ensure that vehicles park closer to the cargo when carriers are in formation.
This commit is contained in:
FlightControl
2018-08-30 06:07:26 +02:00
parent 8e6fc439ec
commit 42b04dedaa
2 changed files with 33 additions and 26 deletions

View File

@@ -418,8 +418,12 @@ function AI_CARGO_APC:onbeforeLoad( APC, From, Event, To )
if APC and APC:IsAlive() then if APC and APC:IsAlive() then
self.APC_Cargo = {} self.APC_Cargo = {}
self.APC_Cargo_Weight = {}
for _, APCUnit in pairs( APC:GetUnits() ) do for _, APCUnit in pairs( APC:GetUnits() ) do
local APCUnit = APCUnit -- Wrapper.Unit#UNIT local APCUnit = APCUnit -- Wrapper.Unit#UNIT
self.APC_Cargo_Weight[APCUnit] = APCUnit:GetCargoBayFreeWeight()
--for _, Cargo in pairs( self.CargoSet:GetSet() ) do --for _, Cargo in pairs( self.CargoSet:GetSet() ) do
for _, Cargo in UTILS.spairs( self.CargoSet:GetSet(), function( t, a, b ) return t[a]:GetWeight() > t[b]:GetWeight() end ) do for _, Cargo in UTILS.spairs( self.CargoSet:GetSet(), function( t, a, b ) return t[a]:GetWeight() > t[b]:GetWeight() end ) do
local Cargo = Cargo -- Cargo.Cargo#CARGO local Cargo = Cargo -- Cargo.Cargo#CARGO
@@ -428,7 +432,7 @@ function AI_CARGO_APC:onbeforeLoad( APC, From, Event, To )
if Cargo:IsInLoadRadius( APCUnit:GetCoordinate() ) then if Cargo:IsInLoadRadius( APCUnit:GetCoordinate() ) then
self:F( { "In radius", APCUnit:GetName() } ) self:F( { "In radius", APCUnit:GetName() } )
local CargoBayFreeWeight = APCUnit:GetCargoBayFreeWeight() local CargoBayFreeWeight = self.APC_Cargo_Weight[APCUnit]
local CargoWeight = Cargo:GetWeight() local CargoWeight = Cargo:GetWeight()
self:F({CargoBayFreeWeight=CargoBayFreeWeight}) self:F({CargoBayFreeWeight=CargoBayFreeWeight})
@@ -438,11 +442,12 @@ function AI_CARGO_APC:onbeforeLoad( APC, From, Event, To )
APC:RouteStop() APC:RouteStop()
--Cargo:Ungroup() --Cargo:Ungroup()
Cargo:Board( APCUnit, 25 ) Cargo:Board( APCUnit, 25 )
self:__Board( 1, Cargo ) self:__Board( 1, Cargo, APCUnit )
-- So now this APCUnit has Cargo that is being loaded. -- So now this APCUnit has Cargo that is being loaded.
-- This will be used further in the logic to follow and to check cargo status. -- This will be used further in the logic to follow and to check cargo status.
self.APC_Cargo[APCUnit] = Cargo self.APC_Cargo[APCUnit] = Cargo
self.APC_Cargo_Weight[APCUnit] = self.APC_Cargo_Weight[APCUnit] - CargoWeight
Boarding = true Boarding = true
break break
end end
@@ -462,35 +467,34 @@ end
-- @param #string From From state. -- @param #string From From state.
-- @param #string Event Event. -- @param #string Event Event.
-- @param #string To To state. -- @param #string To To state.
-- @param #string Cargo.Cargo#CARGO Cargo Cargo object. -- @param Cargo.Cargo#CARGO Cargo Cargo object.
function AI_CARGO_APC:onafterBoard( APC, From, Event, To, Cargo ) -- @param Wrapper.Unit#UNIT APCUnit
self:F( { APC, From, Event, To, Cargo } ) function AI_CARGO_APC:onafterBoard( APC, From, Event, To, Cargo, APCUnit )
self:F( { APC, From, Event, To, Cargo, APCUnit:GetName() } )
if APC and APC:IsAlive() then if APC and APC:IsAlive() then
self:F({ IsLoaded = Cargo:IsLoaded(), Cargo:GetName(), APC:GetName() } ) self:F({ IsLoaded = Cargo:IsLoaded(), Cargo:GetName(), APC:GetName() } )
if not Cargo:IsLoaded() then if not Cargo:IsLoaded() then
self:__Board( 10, Cargo ) self:__Board( 10, Cargo, APCUnit )
else else
for _, APCUnit in pairs( APC:GetUnits() ) do for _, Cargo in UTILS.spairs( self.CargoSet:GetSet(), function( t, a, b ) return t[a]:GetWeight() > t[b]:GetWeight() end ) do
local APCUnit = APCUnit -- Wrapper.Unit#UNIT local Cargo = Cargo -- Cargo.Cargo#CARGO
for _, Cargo in UTILS.spairs( self.CargoSet:GetSet(), function( t, a, b ) return t[a]:GetWeight() > t[b]:GetWeight() end ) do if Cargo:IsUnLoaded() then
local Cargo = Cargo -- Cargo.Cargo#CARGO if Cargo:IsInLoadRadius( APCUnit:GetCoordinate() ) then
if Cargo:IsUnLoaded() then local CargoBayFreeWeight = self.APC_Cargo_Weight[APCUnit]
if Cargo:IsInLoadRadius( APCUnit:GetCoordinate() ) then local CargoWeight = Cargo:GetWeight()
local CargoBayFreeWeight = APCUnit:GetCargoBayFreeWeight()
local CargoWeight = Cargo:GetWeight()
self:F({CargoBayFreeWeight=CargoBayFreeWeight}) self:F({CargoBayFreeWeight=CargoBayFreeWeight})
-- Only when there is space within the bay to load the next cargo item! -- Only when there is space within the bay to load the next cargo item!
if CargoBayFreeWeight > CargoWeight then --and CargoBayFreeVolume > CargoVolume then if CargoBayFreeWeight > CargoWeight then --and CargoBayFreeVolume > CargoVolume then
Cargo:Board( APCUnit, 25 ) Cargo:Board( APCUnit, 25 )
self:__Board( 10, Cargo ) self:__Board( 10, Cargo, APCUnit )
-- So now this APCUnit has Cargo that is being loaded. -- So now this APCUnit has Cargo that is being loaded.
-- This will be used further in the logic to follow and to check cargo status. -- This will be used further in the logic to follow and to check cargo status.
self.APC_Cargo[APCUnit] = Cargo self.APC_Cargo[APCUnit] = Cargo
return self.APC_Cargo_Weight[APCUnit] = self.APC_Cargo_Weight[APCUnit] - CargoWeight
end return
end end
end end
end end
@@ -552,6 +556,7 @@ function AI_CARGO_APC:onafterUnload( APC, From, Event, To, Deployed )
for _, Cargo in pairs( APCUnit:GetCargo() ) do for _, Cargo in pairs( APCUnit:GetCargo() ) do
if Cargo:IsLoaded() then if Cargo:IsLoaded() then
Cargo:UnBoard() Cargo:UnBoard()
Cargo:SetDeployed( true )
self:__Unboard( 10, Cargo, Deployed ) self:__Unboard( 10, Cargo, Deployed )
end end
end end
@@ -580,6 +585,7 @@ function AI_CARGO_APC:onafterUnboard( APC, From, Event, To, Cargo, Deployed )
for _, Cargo in pairs( APCUnit:GetCargo() ) do for _, Cargo in pairs( APCUnit:GetCargo() ) do
if Cargo:IsLoaded() then if Cargo:IsLoaded() then
Cargo:UnBoard() Cargo:UnBoard()
Cargo:SetDeployed( true )
self:__Unboard( 10, Cargo, Deployed ) self:__Unboard( 10, Cargo, Deployed )
return return
end end
@@ -604,7 +610,6 @@ function AI_CARGO_APC:onbeforeUnloaded( APC, From, Event, To, Cargo, Deployed )
self:F( { APC, From, Event, To, Cargo:GetName(), Deployed = Deployed } ) self:F( { APC, From, Event, To, Cargo:GetName(), Deployed = Deployed } )
local AllUnloaded = true local AllUnloaded = true
Cargo:SetDeployed( true )
--Cargo:Regroup() --Cargo:Regroup()

View File

@@ -2062,6 +2062,8 @@ do -- Route methods
local dist=ToCoordinate:Get2DDistance(PathOnRoad[#PathOnRoad-1]) local dist=ToCoordinate:Get2DDistance(PathOnRoad[#PathOnRoad-1])
if dist>10 then if dist>10 then
table.insert(route, ToCoordinate:WaypointGround(Speed, OffRoadFormation)) table.insert(route, ToCoordinate:WaypointGround(Speed, OffRoadFormation))
table.insert(route, ToCoordinate:GetRandomCoordinateInRadius(10,5):WaypointGround(5, OffRoadFormation))
table.insert(route, ToCoordinate:GetRandomCoordinateInRadius(10,5):WaypointGround(5, OffRoadFormation))
end end
end end