A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
sim: a read-only helper for the advisors' city warning (S4 Task 15) (#1538)
Eyal Golan26a33e11d624
diff --git a/changelog.d/advisors-s4-task15.md b/changelog.d/advisors-s4-task15.mdnew file mode 100644index 000000000..3c827123e--- /dev/null+++ b/changelog.d/advisors-s4-task15.md@@ -0,0 +1,3 @@+- The simulation can name the player's cities the enemy is likely to attack or enter this turn, by running the+ enemy's own orders on a copy of the game; it changes nothing and draws no random number. The advisors' city warning+ will read it (Refs #1484).diff --git a/sim/Statecraft.Sim.Tests/ThreatsTests.cs b/sim/Statecraft.Sim.Tests/ThreatsTests.csnew file mode 100644index 000000000..35abefef1--- /dev/null+++ b/sim/Statecraft.Sim.Tests/ThreatsTests.cs@@ -0,0 +1,58 @@+using System;+using System.Collections.Generic;+using System.Linq;+using Statecraft.Sim;+using Xunit;++/// <summary>Spec 5's one sim helper for the advisors: the cities the enemy will likely attack or enter this turn, from+/// resolver step 1 run on a copy (R-ENEMY-001: the orders come from the state alone). Read-only.</summary>+public class ThreatsTests+{+ const string Types = Fixture.GddUnitTypes+ + " - { id: soldier, kind: unit, side: israel, class: infantry, movement_points: 3 }\n"+ + " - { id: enemy, kind: unit, side: egypt, class: infantry, movement_points: 3 }\n";++ static Content Load() => ContentLoader.Load(Fixture.Write(Fixture.Gdd(new Dictionary<string, string>+ {+ ["unit_types.yaml"] = Types, ["map/hexes.json"] = MovementTests.Board(), ["corridors.yaml"] = "corridors: []\n"+ })));++ static Unit Soldier(int id, int q, int r, int strength) => new Unit(id, "soldier", q, r, side: "israel", strength: strength);+ static Unit Enemy(int id, int q, int r, int strength) => new Unit(id, "enemy", q, r, side: "egypt", strength: strength);++ // R-ENEMY-001, R-ENEMY-003: the helper names town_a (the fixture's city, on 1,0) exactly when the turn's own step 1+ // orders an attack on its hex, and it changes nothing.+ [Theory]+ [InlineData(20, 1, true)]+ [InlineData(1, 20, false)]+ public void NamesTheCityTheEnemyAttacksAndChangesNothing(int enemy, int defender, bool atRisk)+ {+ var c = Load();+ var s = CombatTests.With(c, Soldier(1, 1, 0, defender), Enemy(2, 2, 0, enemy));+ var hash = s.Hash();+ var risk = Threats.CitiesAtRisk(c, s);+ Assert.Equal(hash, s.Hash());+ Assert.Equal(atRisk ? new[] { "town_a" } : Array.Empty<string>(), risk);+ var turn = Simulation.Step(c, s, Array.Empty<Command>());+ Assert.Equal(atRisk, turn.Why.Any(w => w.Key == WhyKeys.AttackOrdered && w.Message.EndsWith("hex 1,0", StringComparison.Ordinal)));+ }++ [Fact]+ public void NothingOnceTheGameIsOver()+ {+ var c = Load();+ var s = CombatTests.With(c, Soldier(1, 1, 0, 1), Enemy(2, 2, 0, 20));+ s.GameOver = GameOverCodes.DefeatWar;+ Assert.Empty(Threats.CitiesAtRisk(c, s));+ }++ // The turn's end is Step's own: a turn run after TurnEnd is called ends on that date.+ [Fact]+ public void TurnEndIsTheDateTheTurnEndsOn()+ {+ var c = Load();+ var s = CombatTests.With(c);+ var end = Simulation.TurnEnd(c, s);+ Assert.Equal(end, Simulation.Step(c, s, Array.Empty<Command>()).State.Date);+ }+}diff --git a/sim/Statecraft.Sim/Simulation.cs b/sim/Statecraft.Sim/Simulation.csindex 2c5bc6ee5..f4404be7e 100644--- a/sim/Statecraft.Sim/Simulation.cs+++ b/sim/Statecraft.Sim/Simulation.cs@@ -52,8 +52,7 @@ namespace Statecraft.Sim// The next era's start, or the game's end (the last era's end, or mvp.end in the GDD loop, R-LOOP-004), is a// boundary: the turn that reaches it ends on it, with that turn's real days (spec 4.1, 4.5).var end = c.End;- var boundary = c.Eras.Select(e => e.From).Where(f => f > s.Date).Concat(new[] { end }).Min();- var next = SimTime.AdvanceTo(s.Date, len, boundary);+ var next = TurnEnd(c, s);if (c.IsGdd) SettlementControl.Stamp(s, next); // R-CTRL-004: dates for captures made during the ordersvar adjusted = next != SimTime.Advance(s.Date, len);var days = (next - s.Date).Days;@@ -82,6 +81,16 @@ namespace Statecraft.Simreturn new StepResult(s, why, rejected);}+ /// <summary>The date the turn starting on <paramref name="s"/>'s date ends on: its turn length from the war state,+ /// cut at the next era's start or the game's end when it comes first (spec 4.1, 4.5). Step's own rule; the advisors'+ /// city warning (Threats) reads it too.</summary>+ public static DateTime TurnEnd(Content c, GameState s)+ {+ var len = SecuritySystem.TurnLengthFor(c, s.Date, s.Rung);+ var boundary = c.Eras.Select(e => e.From).Where(f => f > s.Date).Concat(new[] { c.End }).Min();+ return SimTime.AdvanceTo(s.Date, len, boundary);+ }+/// <summary>Who End Turn would hand the event to (EventSystem.Delegate) if the turn ended with these orders: runs/// them on a clone, as <see cref="Step"/> does, then asks EventSystem.DelegatedTo. Never mutates <paramref name="state"/>.</summary>public static Person? DelegatedTo(Content c, GameState state, IReadOnlyList<Command> commands, string eventId)diff --git a/sim/Statecraft.Sim/Threats.cs b/sim/Statecraft.Sim/Threats.csnew file mode 100644index 000000000..0ece78000--- /dev/null+++ b/sim/Statecraft.Sim/Threats.cs@@ -0,0 +1,33 @@+using System;+using System.Collections.Generic;+using System.Globalization;+using System.Linq;++namespace Statecraft.Sim+{+ /// <summary>The advisors' one sim helper (docs/superpowers/specs/2026-10-23-advisors-design.md, section 5): the player's+ /// cities the enemy is likely to attack or enter this turn. It runs resolver step 1 (EnemyAI.Order, R-ENEMY-001) on a+ /// copy of the state and reads the copy: an attack order on a city's hex, or an enemy unit standing on one. It changes+ /// nothing and draws no random number. "Likely": the player's own orders this turn can still change the board.</summary>+ public static class Threats+ {+ public static IReadOnlyList<string> CitiesAtRisk(Content c, GameState s)+ {+ if (!c.IsGdd || s.GameOver != null) return Array.Empty<string>();+ var copy = s.Clone();+ EnemyAI.Order(c, copy, new List<Cause>(), Simulation.TurnEnd(c, s));+ var cityAt = new Dictionary<string, string>(StringComparer.Ordinal);+ foreach (var h in c.SettlementHexes) if (s.Cities.ContainsKey(h.Id)) cityAt[Hex(h.Q, h.R)] = h.Id;+ var risk = new SortedSet<string>(StringComparer.Ordinal);+ foreach (var u in copy.Units.Values)+ {+ if (u.Side == "" || u.Side == Movement.PlayerSide) continue;+ if (u.Order == Combat.Attack && cityAt.TryGetValue(u.OrderTarget, out var attacked)) risk.Add(attacked);+ if (cityAt.TryGetValue(Hex(u.Q, u.R), out var entered)) risk.Add(entered);+ }+ return risk.ToList();+ }++ static string Hex(int q, int r) => string.Format(CultureInfo.InvariantCulture, "{0},{1}", q, r);+ }+}