From 25777afdd188793696380ccfccefcc02cfa13243 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Wed, 29 Aug 2018 19:23:46 +0200 Subject: [PATCH] - Now the cargo is sorted from large to small for dispatching. - The carrier which has a unit that fits the group size, will be routed towards that cargo. - When boarding, the carrier(s) will then be boarded with any cargo group that is available that fits the remaining cargo bays. --- Moose Development/Moose/AI/AI_Cargo_APC.lua | 5 +++-- .../Moose/AI/AI_Cargo_Dispatcher.lua | 22 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Moose Development/Moose/AI/AI_Cargo_APC.lua b/Moose Development/Moose/AI/AI_Cargo_APC.lua index 466803656..19e674583 100644 --- a/Moose Development/Moose/AI/AI_Cargo_APC.lua +++ b/Moose Development/Moose/AI/AI_Cargo_APC.lua @@ -420,7 +420,8 @@ function AI_CARGO_APC:onbeforeLoad( APC, From, Event, To ) self.APC_Cargo = {} for _, APCUnit in pairs( APC:GetUnits() ) do local APCUnit = APCUnit -- Wrapper.Unit#UNIT - 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 local Cargo = Cargo -- Cargo.Cargo#CARGO self:F( { IsUnLoaded = Cargo:IsUnLoaded(), IsDeployed = Cargo:IsDeployed(), Cargo:GetName(), APC:GetName() } ) if Cargo:IsUnLoaded() then -- and not Cargo:IsDeployed() then @@ -472,7 +473,7 @@ function AI_CARGO_APC:onafterBoard( APC, From, Event, To, Cargo ) else for _, APCUnit in pairs( APC:GetUnits() ) do local APCUnit = APCUnit -- Wrapper.Unit#UNIT - 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 local Cargo = Cargo -- Cargo.Cargo#CARGO if Cargo:IsUnLoaded() then if Cargo:IsInLoadRadius( APCUnit:GetCoordinate() ) then diff --git a/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua b/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua index 602868607..71359203b 100644 --- a/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua +++ b/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua @@ -411,7 +411,7 @@ function AI_CARGO_DISPATCHER:onafterMonitor() local PickupCargo = nil - for CargoName, Cargo in pairs( self.SetCargo:GetSet() ) do + for CargoName, Cargo in UTILS.spairs( self.SetCargo:GetSet(), function( t, a, b ) return t[a]:GetWeight() < t[b]:GetWeight() end ) do local Cargo = Cargo -- Cargo.Cargo#CARGO self:F( { Cargo = Cargo:GetName(), UnLoaded = Cargo:IsUnLoaded(), Deployed = Cargo:IsDeployed(), PickupCargo = self.PickupCargo[Carrier] ~= nil } ) if Cargo:IsUnLoaded() == true and Cargo:IsDeployed() == false then @@ -428,9 +428,23 @@ function AI_CARGO_DISPATCHER:onafterMonitor() end end if CoordinateFree == true then - self.PickupCargo[Carrier] = CargoCoordinate - PickupCargo = Cargo - break + -- Check if this cargo can be picked-up by at least one carrier unit of AI_Cargo. + local LargestLoadCapacity = 0 + for _, Carrier in pairs( Carrier:GetUnits() ) do + local LoadCapacity = Carrier:GetCargoBayFreeWeight() + if LargestLoadCapacity < LoadCapacity then + LargestLoadCapacity = LoadCapacity + end + end + -- So if there is aa carrier that has the required load capacity to load the total weight of the cargo, dispatch the carrier. + -- Otherwise break and go to the next carrier. + -- This will skip cargo which is too large to be able to be loaded by carriers + -- and will secure an efficient dispatching scheme. + if LargestLoadCapacity >= Cargo:GetWeight() then + self.PickupCargo[Carrier] = CargoCoordinate + PickupCargo = Cargo + break + end end end end