Skip to content

Commit

Permalink
Fix loading all population (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekolis committed May 22, 2022
1 parent c6130ec commit cd78ff3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
11 changes: 9 additions & 2 deletions FrEee/Game/Objects/Civilization/CargoDelta.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrEee.Game.Enumerations;
using FrEee.Game.Enumerations;
using FrEee.Game.Interfaces;
using FrEee.Modding;
using FrEee.Utility;
Expand All @@ -24,7 +24,14 @@ public CargoDelta()
UnitTypeTonnage = new SafeDictionary<VehicleTypes, int?>();
}

/// <summary>
/// Should we transfer as much population as possible, regardless of race?
/// </summary>
public bool AllPopulation { get; set; }

/// <summary>
/// Amount of population to transfer where the race of the population is irrelevant.
/// </summary>
public long AnyPopulation { get; set; }

/// <summary>
Expand Down Expand Up @@ -116,4 +123,4 @@ public override string ToString()
return string.Join(", ", items.ToArray());
}
}
}
}
34 changes: 32 additions & 2 deletions FrEee/Utility/Extensions/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit cd78ff3

Please sign in to comment.