Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Staging searches once per unit, not once per candidate hex (WebGL golden OOM, Refs #2621) (#2950)

Eyal Golane41b13156970
diff --git a/changelog.d/route-search-once.md b/changelog.d/route-search-once.md
new file mode 100644
index 000000000..8b7a494ab
--- /dev/null
+++ b/changelog.d/route-search-once.md
@@ -0,0 +1,5 @@
+### Fixed
+
+- Behind the scenes: the automated player that plays the test games searched the whole map once for every hex it
+ considered when moving a brigade into place for an operation card. It now searches once per brigade and picks the same
+ moves. A full test game had used about 300 MB more memory since #2621. The game you play does not use this player.
diff --git a/sim/Statecraft.Sim/HistoricalAI.cs b/sim/Statecraft.Sim/HistoricalAI.cs
index d80e53c6f..0c3836a24 100644
--- a/sim/Statecraft.Sim/HistoricalAI.cs
+++ b/sim/Statecraft.Sim/HistoricalAI.cs
@@ -123,10 +123,11 @@ namespace Statecraft.Sim
if (destination != null && need.Option.RequiresUnitRegions.Contains(destination.Id, StringComparer.Ordinal)) continue;
}
}
+ var routes = Movement.Routes(c, probe, id, spent); // one search for all the candidate hexes
var targetHex = need.Option.RequiresUnitRegions
.SelectMany(region => c.PlayerRegions.Regions[region].MemberHexes)
.Distinct().Where(h => !closed.Contains((h.Q, h.R)))
- .Select(h => (Hex: h, Route: Movement.Route(c, probe, id, h.Q, h.R, spent)))
+ .Select(h => (Hex: h, Route: routes(h.Q, h.R)))
.Where(x => x.Route.Count != 0 && Movement.Full(c, probe, unit, x.Hex.Q, x.Hex.R) == null)
.OrderBy(x => x.Route[x.Route.Count - 1].Turn).ThenBy(x => x.Route.Count)
.ThenBy(x => x.Hex.Q).ThenBy(x => x.Hex.R).FirstOrDefault();
diff --git a/sim/Statecraft.Sim/Movement.cs b/sim/Statecraft.Sim/Movement.cs
index 96a17943c..08c6d0eff 100644
--- a/sim/Statecraft.Sim/Movement.cs
+++ b/sim/Statecraft.Sim/Movement.cs
@@ -100,17 +100,38 @@ namespace Statecraft.Sim
/// move, its own hex, or a hex no path reaches. With <paramref name="card"/> the route is an operation card's (R-ENEMY-005)
/// and goes round every enemy-held locality but (q, r) (<see cref="Search"/>).</summary>
public static IReadOnlyList<(int Q, int R, int Turn)> Route(Content c, GameState s, int unitId, int q, int r, IReadOnlyDictionary<int, int> spent, bool card = false)
+ {
+ if (!s.Units.TryGetValue(unitId, out var u) || IsEnemy(u) || (u.Q, u.R) == (q, r) || Points(c, u) == 0)
+ return new List<(int Q, int R, int Turn)>();
+ var (dist, prev) = Search(c, s, u, int.MaxValue, card: card ? (q, r) : ((int, int)?)null);
+ return Split(c, s, u, q, r, spent, dist, prev);
+ }
+
+ /// <summary><see cref="Route"/> without a card, to many hexes of one unit: the search does not depend on the target, so
+ /// it runs once (at the first call) and each call only reads that unit's path to (q, r). The same routes as Route.</summary>
+ internal static Func<int, int, IReadOnlyList<(int Q, int R, int Turn)>> Routes(Content c, GameState s, int unitId, IReadOnlyDictionary<int, int> spent)
+ {
+ (Dictionary<(int Q, int R), int> Dist, Dictionary<(int Q, int R), (int Q, int R)> Prev)? search = null;
+ return (q, r) =>
+ {
+ if (!s.Units.TryGetValue(unitId, out var u) || IsEnemy(u) || (u.Q, u.R) == (q, r) || Points(c, u) == 0)
+ return new List<(int Q, int R, int Turn)>();
+ search ??= Search(c, s, u, int.MaxValue);
+ return Split(c, s, u, q, r, spent, search.Value.Dist, search.Value.Prev);
+ };
+ }
+
+ /// <summary>The path to (q, r) in one search, split into turns (R-MOVE-001, R-MOVE-003); empty when no path reaches it.</summary>
+ static IReadOnlyList<(int Q, int R, int Turn)> Split(Content c, GameState s, Unit u, int q, int r, IReadOnlyDictionary<int, int> spent,
+ Dictionary<(int Q, int R), int> dist, Dictionary<(int Q, int R), (int Q, int R)> prev)
{
var result = new List<(int Q, int R, int Turn)>();
- if (!s.Units.TryGetValue(unitId, out var u) || IsEnemy(u) || (u.Q, u.R) == (q, r)) return result;
var full = Points(c, u);
- if (full == 0) return result;
- var (dist, prev) = Search(c, s, u, int.MaxValue, card: card ? (q, r) : ((int, int)?)null);
if (!dist.ContainsKey((q, r))) return result;
var hexes = new List<(int Q, int R)>();
for (var h = (q, r); h != (u.Q, u.R); h = prev[h]) hexes.Add(h);
hexes.Reverse();
- spent.TryGetValue(unitId, out var used);
+ spent.TryGetValue(u.Id, out var used);
int left = full - used, turn = 1, before = 0, first = 0;
for (int i = 0; i < hexes.Count; i++)
{