From cd78ff374f3d4a9312424a6e5b7439a3cddfdb08 Mon Sep 17 00:00:00 2001 From: Ed Kolis Date: Sun, 22 May 2022 14:49:46 -0400 Subject: [PATCH] Fix loading all population (#215) --- FrEee/Game/Objects/Civilization/CargoDelta.cs | 11 ++++-- FrEee/Utility/Extensions/CommonExtensions.cs | 34 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/FrEee/Game/Objects/Civilization/CargoDelta.cs b/FrEee/Game/Objects/Civilization/CargoDelta.cs index 06ff4cdb4..4005ba5a7 100644 --- a/FrEee/Game/Objects/Civilization/CargoDelta.cs +++ b/FrEee/Game/Objects/Civilization/CargoDelta.cs @@ -1,4 +1,4 @@ -using FrEee.Game.Enumerations; +using FrEee.Game.Enumerations; using FrEee.Game.Interfaces; using FrEee.Modding; using FrEee.Utility; @@ -24,7 +24,14 @@ public CargoDelta() UnitTypeTonnage = new SafeDictionary(); } + /// + /// Should we transfer as much population as possible, regardless of race? + /// public bool AllPopulation { get; set; } + + /// + /// Amount of population to transfer where the race of the population is irrelevant. + /// public long AnyPopulation { get; set; } /// @@ -116,4 +123,4 @@ public override string ToString() return string.Join(", ", items.ToArray()); } } -} \ No newline at end of file +} diff --git a/FrEee/Utility/Extensions/CommonExtensions.cs b/FrEee/Utility/Extensions/CommonExtensions.cs index 6316b7448..5b9350d4e 100644 --- a/FrEee/Utility/Extensions/CommonExtensions.cs +++ b/FrEee/Utility/Extensions/CommonExtensions.cs @@ -1604,26 +1604,56 @@ public static void TransferCargo(this ICargoContainer src, CargoDelta delta, ICa var anyPopLeft = delta.AnyPopulation; foreach (var kvp in src.AllPopulation.ToArray()) { + // how much population to transfer for this race? var amount = long.MaxValue; // limit by desired amount to transfer - if (!delta.AllPopulation) - amount = Math.Min(amount, anyPopLeft); + amount = Math.Min(amount, anyPopLeft); // limit by amount available amount = Math.Min(amount, kvp.Value); // limit by amount of free space amount = Math.Min(amount, dest.PopulationStorageFree + (long)((dest.CargoStorage - dest.Cargo.Size) / Mod.Current.Settings.PopulationSize)); + // transfer population from source to destination amount -= src.RemovePopulation(kvp.Key, amount); dest.AddPopulation(kvp.Key, amount); + // log warnings if (amount < anyPopLeft) emp.Log.Add(src.CreateLogMessage(src + " could transfer only " + amount.ToUnitString(true) + " of the desired " + kvp.Value.ToUnitString(true) + " general population to " + dest + " due to lack of population available or lack of storage space.", LogMessageType.Warning)); + // done transferring population if (amount == 0) continue; } + // transfer all-population + if (delta.AllPopulation) + { + foreach (var kvp in src.AllPopulation.ToArray()) + { + // how much population to transfer for this race? + var amount = long.MaxValue; + + // limit by amount available + amount = Math.Min(amount, kvp.Value); + // limit by amount of free space + amount = Math.Min(amount, dest.PopulationStorageFree + (long)((dest.CargoStorage - dest.Cargo.Size) / Mod.Current.Settings.PopulationSize)); + + // transfer population from source to destination + amount -= src.RemovePopulation(kvp.Key, amount); + dest.AddPopulation(kvp.Key, amount); + + // log warnings + if (amount < anyPopLeft) + emp.Log.Add(src.CreateLogMessage(src + " could transfer only " + amount.ToUnitString(true) + " of the desired " + kvp.Value.ToUnitString(true) + " general population to " + dest + " due to lack of population available or lack of storage space.", LogMessageType.Warning)); + + // no population to transfer + if (amount == 0) + continue; + } + } + // clear population that was emptied out foreach (var race in src.Cargo.Population.Where(kvp => kvp.Value <= 0).Select(kvp => kvp.Key).ToArray()) src.Cargo.Population.Remove(race);