A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
R-UNIT-005: an attack keeps its target's front for the turn (Fixes #2650) (#2683)
Eyal Golanae4bfdaab5e9
diff --git a/changelog.d/2650.md b/changelog.d/2650.mdnew file mode 100644index 000000000..d99f331dd--- /dev/null+++ b/changelog.d/2650.md@@ -0,0 +1,4 @@+### Fixed++- Keep the front of a unit's attack for the rest of the turn when combat destroys, surrenders or drives off the defenders,+ so Send home still waits for that front's armistice.diff --git a/sim/Statecraft.Sim.Tests/ArtilleryTests.cs b/sim/Statecraft.Sim.Tests/ArtilleryTests.csindex c4b5e8390..632d5fb0e 100644--- a/sim/Statecraft.Sim.Tests/ArtilleryTests.cs+++ b/sim/Statecraft.Sim.Tests/ArtilleryTests.cs@@ -57,7 +57,8 @@ public class ArtilleryTestsAssert.Contains(now.Why, w => w.Key == WhyKeys.Engagement);Assert.Equal(arms - 3, now.State.Arms);Assert.Equal(2, now.State.Units[1].Strength);- Assert.Equal((2, 0), now.Attacked[1]);+ var attack = now.Attacked[1];+ Assert.Equal((2, 0, 2, "egypt"), (attack.Q, attack.R, attack.Unit, attack.Front));}// R-UNIT-005 (spec 4.6): a gun's front is that of the enemy it attacked this turn, two hexes away, though it borders none.diff --git a/sim/Statecraft.Sim.Tests/CombatTests.cs b/sim/Statecraft.Sim.Tests/CombatTests.csindex dec0cef1e..0ebe54ab3 100644--- a/sim/Statecraft.Sim.Tests/CombatTests.cs+++ b/sim/Statecraft.Sim.Tests/CombatTests.cs@@ -334,7 +334,7 @@ public class CombatTestswhile ((int)(new Rng(seed).NextDouble() * faces) != face) seed++;s.RngState = seed;var why = new List<Cause>();- Assert.Null(Combat.Strike(c, s, 1, 3, 1, new Dictionary<int, int>(), new Dictionary<int, (int Q, int R)>(), why));+ Assert.Null(Combat.Strike(c, s, 1, 3, 1, new Dictionary<int, int>(), new Dictionary<int, AttackRecord>(), why));var fight = why.Single(w => w.Key == WhyKeys.Engagement);real.Add((int.Parse(fight.Args[2]), int.Parse(fight.Args[3]),why.Any(w => w.Key == WhyKeys.Retreated || w.Key == WhyKeys.SurrenderedPrisoners || w.Key == WhyKeys.SurrenderedHeld)));diff --git a/sim/Statecraft.Sim.Tests/DemobilizationTests.cs b/sim/Statecraft.Sim.Tests/DemobilizationTests.csindex c82e23577..8e981086a 100644--- a/sim/Statecraft.Sim.Tests/DemobilizationTests.cs+++ b/sim/Statecraft.Sim.Tests/DemobilizationTests.cs@@ -164,16 +164,15 @@ public class DemobilizationTestsAssert.Null(Demobilization.Check(c, bordered, 1));}- // R-UNIT-005: the attack-target clause of Front() matters on its own. A gun attacks beyond the next hex- // (Combat.InReach), so an enemy two hexes away that nothing borders still names the front: drop- // `|| (attacks != null && ...)` and this becomes the any-armistice case and goes green after the Jordan signature.+ // R-UNIT-005: the attack record matters on its own. A gun attacks beyond the next hex (Combat.InReach), so an enemy+ // two hexes away that nothing borders still names the front; without the record the Jordan signature would open it.[Fact]public void AnAttackTargetTwoHexesAwayNamesItsFront(){var c = Load();var s = State(c, new Unit(1, "gun", 3, 0, side: "israel", strength: 2, order: Combat.Attack, orderTarget: "1,0"),new Unit(2, "enemy", 1, 0, side: "egypt", strength: 3));- var attacked = new Dictionary<int, (int Q, int R)> { [1] = (1, 0) };+ var attacked = new Dictionary<int, AttackRecord> { [1] = new AttackRecord(1, 0, 2, "egypt") };Assert.Equal("The armistice with Egypt is not signed", Demobilization.Check(c, s, 1, attacked));s.ChosenOptions["armistice_jordan"] = "sign"; // would open the order if the target did not countAssert.Equal("The armistice with Egypt is not signed", Demobilization.Check(c, s, 1, attacked));@@ -194,6 +193,78 @@ public class DemobilizationTestsvar attacking = State(c, Soldier(1, 3, 0), new Unit(2, "enemy", 2, 0, side: "egypt", strength: 3));Assert.Equal("The armistice with Egypt is not signed", Demobilization.Check(c, attacking, 1,- new Dictionary<int, (int Q, int R)> { [1] = (2, 0) }));+ new Dictionary<int, AttackRecord> { [1] = new AttackRecord(2, 0, 2, "egypt") }));+ }++ // R-UNIT-005: an accepted attack keeps its front for the rest of the turn even when combat removes the defender.+ [Fact]+ public void ADestroyedAttackTargetStillNamesItsFront()+ {+ var c = Load();+ var s = State(c, new Unit(1, "gun", 3, 0, side: "israel", strength: 2),+ new Unit(2, "enemy", 1, 0, side: "egypt", strength: 1));+ s.ChosenOptions["armistice_syria"] = "sign";++ var result = Step(c, s, new UnitOrder(1, Combat.Attack, 1, 0),+ new UnitOrder(1, Demobilization.SendHome));++ Assert.DoesNotContain(2, result.State.Units.Keys);+ Assert.Contains(1, result.State.Units.Keys);+ Assert.Equal("The armistice with Egypt is not signed", Assert.Single(result.Rejected).Reason);+ }++ // R-UNIT-005: retreating away from the attacked hex does not erase the attacker's front during the turn.+ [Fact]+ public void ARetreatedAttackTargetStillNamesItsFront()+ {+ var c = Load();+ c.Rules.Mvp!.CombatTable[0].DefenderLoss = new List<int> { 0, 0 };+ c.Rules.Mvp!.CombatTable[0].DefenderRetreats = new List<int> { 1, 1 };+ var s = State(c, new Unit(1, "gun", 3, 0, side: "israel", strength: 2),+ new Unit(2, "enemy", 1, 0, side: "egypt", strength: 3), Soldier(3, 2, 0));+ s.ChosenOptions["armistice_syria"] = "sign";++ var result = Step(c, s, new UnitOrder(1, Combat.Attack, 1, 0),+ new UnitOrder(1, Demobilization.SendHome));++ Assert.Equal((0, 0), (result.State.Units[2].Q, result.State.Units[2].R));+ Assert.Contains(1, result.State.Units.Keys);+ Assert.Equal("The armistice with Egypt is not signed", Assert.Single(result.Rejected).Reason);+ }++ // R-UNIT-005 ties (#2683 review): two front-bearing defenders on the attacked hex name the lower unit id's front,+ // as the unit-by-unit choice did before the attack record kept only one of them.+ [Fact]+ public void TwoDefendersOnTheAttackedHexNameTheLowerIdsFront()+ {+ var c = Load();+ var s = State(c, new Unit(1, "gun", 3, 0, side: "israel", strength: 2),+ new Unit(2, "enemy", 1, 0, side: "egypt", strength: 30), new Unit(3, "syrian", 1, 0, side: "syria", strength: 30));+ s.ChosenOptions["armistice_syria"] = "sign";++ var result = Step(c, s, new UnitOrder(1, Combat.Attack, 1, 0), new UnitOrder(1, Demobilization.SendHome));++ Assert.Equal("The armistice with Egypt is not signed", Assert.Single(result.Rejected).Reason);+ }++ // R-UNIT-005 ties (#2683 review): a nearer bordered enemy names the front before a ranged attack's target.+ [Fact]+ public void ANearerBorderedEnemyNamesTheFrontBeforeARangedTarget()+ {+ var c = Load();+ GameState Start(string signed)+ {+ var s = State(c, new Unit(1, "gun", 3, 0, side: "israel", strength: 2),+ new Unit(2, "enemy", 1, 0, side: "egypt", strength: 30), new Unit(3, "syrian", 2, 0, side: "syria", strength: 30));+ s.ChosenOptions[signed] = "sign";+ return s;+ }+ var cmds = new Command[] { new UnitOrder(1, Combat.Attack, 1, 0), new UnitOrder(1, Demobilization.SendHome) };++ var egyptSigned = Step(c, Start("armistice_egypt"), cmds);+ Assert.Equal("The armistice with Syria is not signed", Assert.Single(egyptSigned.Rejected).Reason);+ var syriaSigned = Step(c, Start("armistice_syria"), cmds);+ Assert.Empty(syriaSigned.Rejected);+ Assert.DoesNotContain(1, syriaSigned.State.Units.Keys);}}diff --git a/sim/Statecraft.Sim.Tests/Gdd03MovementFortTests.cs b/sim/Statecraft.Sim.Tests/Gdd03MovementFortTests.csindex 6b0569fea..b3b57a544 100644--- a/sim/Statecraft.Sim.Tests/Gdd03MovementFortTests.cs+++ b/sim/Statecraft.Sim.Tests/Gdd03MovementFortTests.cs@@ -164,7 +164,7 @@ public class Gdd03MovementFortTestsAssert.Null(Movement.Move(c, s, 1, 3, 1, spent, why));Assert.Equal((3, 1), (s.Units[1].Q, s.Units[1].R));Assert.Equal(1, Movement.PointsLeft(c, s, 1, spent));- Assert.Null(Combat.Strike(c, s, 1, 5, 1, spent, new Dictionary<int, (int Q, int R)>(), why)); // range 2, shared budget+ Assert.Null(Combat.Strike(c, s, 1, 5, 1, spent, new Dictionary<int, AttackRecord>(), why)); // range 2, shared budgetAssert.Equal(0, Movement.PointsLeft(c, s, 1, spent));Assert.Equal("That unit has no movement points left this turn", Combat.Check(c, s, 1, 5, 1, spent: spent));Assert.Empty(Movement.Reachable(c, s, 1, spent));@@ -333,7 +333,7 @@ public class Gdd03MovementFortTestsvar before = s.RngState; var rng = new Rng(before); rng.NextDouble();Assert.Equal(25, Combat.Preview(c, s, 1, 3, 1)!.Value.RatioPercent);var why = new List<Cause>();- Assert.Null(Combat.Strike(c, s, 1, 3, 1, new Dictionary<int, int>(), new Dictionary<int, (int Q, int R)>(), why));+ Assert.Null(Combat.Strike(c, s, 1, 3, 1, new Dictionary<int, int>(), new Dictionary<int, AttackRecord>(), why));Assert.Equal(rng.State, s.RngState);Assert.Contains(why, line => line.Key == WhyKeys.Engagement && line.Args[1] == "25");Assert.True(c.HasPermanentFort(3, 1));@@ -429,7 +429,7 @@ public class Gdd03MovementFortTestsvar state = With(c, Of("recruit"), Of("enemy", 2, 3, side: "egypt"));Assert.Equal(100, Combat.Preview(c, state, 1, 3, 1)!.Value.RatioPercent);var before = state.Hash();- Assert.NotNull(Combat.Strike(c, state, 1, 5, 1, new Dictionary<int,int>(),new Dictionary<int,(int Q,int R)>(),new List<Cause>()));+ Assert.NotNull(Combat.Strike(c, state, 1, 5, 1, new Dictionary<int,int>(),new Dictionary<int,AttackRecord>(),new List<Cause>()));Assert.Equal(before,state.Hash()); Assert.True(c.HasPermanentFort(2,1));}diff --git a/sim/Statecraft.Sim/Combat.cs b/sim/Statecraft.Sim/Combat.csindex e51f11cf9..4abf708a2 100644--- a/sim/Statecraft.Sim/Combat.cs+++ b/sim/Statecraft.Sim/Combat.cs@@ -135,7 +135,7 @@ namespace Statecraft.Sim/// refused, the state untouched, with <see cref="Check"/>'s reason; else it pays its Arms (R-COMBAT-004; not under gdd-0.3), loses its/// order and the rest of its movement points, and fights one engagement alone (OQ-1), drawing once.</summary>internal static string? Strike(Content c, GameState s, int unitId, int? q, int? r, Dictionary<int, int> spent,- Dictionary<int, (int Q, int R)> attacked, List<Cause> why)+ Dictionary<int, AttackRecord> attacked, List<Cause> why){var refused = Check(c, s, unitId, q, r, spent: spent);if (refused != null) return refused;@@ -148,8 +148,11 @@ namespace Statecraft.Simwhy.Add(UnitLines.Line(WhyKeys.FortifyLost,$"Unit {unitId} left hex {u.OrderTarget} and is no longer fortified", new[] { u }, u.OrderTarget));spent[unitId] = Movement.Points(c, attacker);- attacked[unitId] = (q!.Value, r!.Value);- Engage(c, s, mvp, new List<Unit> { attacker }, Defenders(s, attacker, q.Value, r.Value), q.Value, r.Value, why);+ var defenders = Defenders(s, attacker, q!.Value, r!.Value);+ var target = defenders.Where(e => Prisoners.FrontOf.ContainsKey(e.Side)).OrderBy(e => e.Id).FirstOrDefault();+ attacked[unitId] = new AttackRecord(q.Value, r.Value, target?.Id,+ target == null ? null : Prisoners.FrontOf[target.Side]);+ Engage(c, s, mvp, new List<Unit> { attacker }, defenders, q.Value, r.Value, why);return null;}diff --git a/sim/Statecraft.Sim/Commands.cs b/sim/Statecraft.Sim/Commands.csindex c948145d4..5e992eeb1 100644--- a/sim/Statecraft.Sim/Commands.cs+++ b/sim/Statecraft.Sim/Commands.cs@@ -230,16 +230,30 @@ namespace Statecraft.Simpublic StepResult(GameState state, IReadOnlyList<Cause> why, IReadOnlyList<Rejection> rejected) { State = state; Why = why; Rejected = rejected; }}+ /// <summary>A player's accepted attack this turn, captured before combat changes or removes its front-bearing target.</summary>+ public readonly struct AttackRecord+ {+ public int Q { get; }+ public int R { get; }+ public int? Unit { get; }+ public string? Front { get; }++ public AttackRecord(int q, int r, int? unit, string? front)+ {+ Q = q; R = r; Unit = unit; Front = front;+ }+ }+/// <summary>The state after the turn's committed orders (spec 2026-10-23 section 3), before End turn: what/// the screen shows, the movement points each unit has spent, the turn's Why lines so far and the refused orders.</summary>public sealed class PreviewResult{public GameState State { get; }public IReadOnlyDictionary<int, int> Spent { get; }- public IReadOnlyDictionary<int, (int Q, int R)> Attacked { get; }+ public IReadOnlyDictionary<int, AttackRecord> Attacked { get; }public IReadOnlyList<Cause> Why { get; }public IReadOnlyList<Rejection> Rejected { get; }- public PreviewResult(GameState state, IReadOnlyDictionary<int, int> spent, IReadOnlyDictionary<int, (int Q, int R)> attacked,+ public PreviewResult(GameState state, IReadOnlyDictionary<int, int> spent, IReadOnlyDictionary<int, AttackRecord> attacked,IReadOnlyList<Cause> why, IReadOnlyList<Rejection> rejected){ State = state; Spent = spent; Attacked = attacked; Why = why; Rejected = rejected; }}diff --git a/sim/Statecraft.Sim/Demobilization.cs b/sim/Statecraft.Sim/Demobilization.csindex f46a12355..7ceac3f70 100644--- a/sim/Statecraft.Sim/Demobilization.cs+++ b/sim/Statecraft.Sim/Demobilization.cs@@ -24,17 +24,18 @@ namespace Statecraft.Sim/// one's, ties by the lowest unit id. Null when no front-bearing enemy unit is attacked or bordered: the ruling's/// "no enemy units in play" case, which any signed armistice covers (accepted per maintainer comment 6341877062/// on #1309: a frontless enemy counts as none in play).</summary>- internal static string? Front(GameState s, Unit u, IReadOnlyDictionary<int, (int Q, int R)>? attacked = null)+ internal static string? Front(GameState s, Unit u, IReadOnlyDictionary<int, AttackRecord>? attacked = null){- (int Q, int R)? attacks = null;- if (attacked != null && attacked.TryGetValue(u.Id, out var target)) attacks = target;var candidates = s.Units.Values.Where(e => e.Side != "" && e.Side != Movement.PlayerSide && Prisoners.FrontOf.ContainsKey(e.Side))- .Where(e => MapActions.Distance(u.Q, u.R, e.Q, e.R) == 1 || (attacks != null && attacks.Value == (e.Q, e.R)))+ .Where(e => MapActions.Distance(u.Q, u.R, e.Q, e.R) == 1)+ .Select(e => (Distance: 1, Unit: e.Id, Front: Prisoners.FrontOf[e.Side])).ToList();+ if (attacked != null && attacked.TryGetValue(u.Id, out var target)+ && target.Unit is int targetUnit && target.Front is string targetFront)+ candidates.Add((MapActions.Distance(u.Q, u.R, target.Q, target.R), targetUnit, targetFront));if (candidates.Count == 0) return null;- var pick = candidates.OrderBy(e => MapActions.Distance(u.Q, u.R, e.Q, e.R)).ThenBy(e => e.Id).First();- return Prisoners.FrontOf[pick.Side];+ return candidates.OrderBy(e => e.Distance).ThenBy(e => e.Unit).First().Front;}/// <summary>R-UNIT-005's refusals: null when <see cref="Give"/> takes the order, else the reason shown to the player.@@ -42,7 +43,7 @@ namespace Statecraft.Sim/// side (a halutz) or an enemy unit, as the standing orders are; then while the armistice on the unit's front is/// unsigned, or - no enemy attacked or bordered - while no armistice is signed at all.</summary>public static string? Check(Content c, GameState s, int unitId,- IReadOnlyDictionary<int, (int Q, int R)>? attacked = null)+ IReadOnlyDictionary<int, AttackRecord>? attacked = null){if (!s.Units.TryGetValue(unitId, out var u)) return "There is no such unit";if (u.Side == "") return "Only a military unit takes that order";@@ -58,7 +59,7 @@ namespace Statecraft.Sim/// in the city's jobs and housing rules from the same turn. Refused, state untouched, with <see cref="Check"/>'s/// reason.</summary>internal static string? Give(Content c, GameState s, int unitId,- IReadOnlyDictionary<int, (int Q, int R)>? attacked, List<Cause> why)+ IReadOnlyDictionary<int, AttackRecord>? attacked, List<Cause> why){PopulationLedger.RefuseAggregate(s);var refused = Check(c, s, unitId, attacked);diff --git a/sim/Statecraft.Sim/Simulation.cs b/sim/Statecraft.Sim/Simulation.csindex 65ebe6f41..d0270d6a4 100644--- a/sim/Statecraft.Sim/Simulation.cs+++ b/sim/Statecraft.Sim/Simulation.cs@@ -48,7 +48,7 @@ namespace Statecraft.Sim}var spent = new Dictionary<int, int>();- RunOrders(c, s, commands, spent, new Dictionary<int, (int Q, int R)>(), why, rejected);+ RunOrders(c, s, commands, spent, new Dictionary<int, AttackRecord>(), why, rejected);EventSystem.Delegate(c, s, why, spent, rejected); // a delegated card's unit orders share the turn's movement (R-ENEMY-005)var len = SecuritySystem.TurnLengthFor(c, s.Date, s.Rung);@@ -108,7 +108,7 @@ namespace Statecraft.SimPopulationLedger.RefuseAggregate(state);CommandText.Validate(commands);var s = state.Clone();- if (s.GameOver == null) RunOrders(c, s, commands, new Dictionary<int, int>(), new Dictionary<int, (int Q, int R)>(), new List<Cause>(), new List<Rejection>());+ if (s.GameOver == null) RunOrders(c, s, commands, new Dictionary<int, int>(), new Dictionary<int, AttackRecord>(), new List<Cause>(), new List<Rejection>());return EventSystem.DelegatedTo(c, s, c.Events.First(e => e.Id == eventId));}@@ -125,7 +125,7 @@ namespace Statecraft.SimCommandText.Validate(commands);var s = state.Clone();var why = new List<Cause>(); var rejected = new List<Rejection>(); var spent = new Dictionary<int, int>();- var attacked = new Dictionary<int, (int Q, int R)>();+ var attacked = new Dictionary<int, AttackRecord>();if (s.GameOver != null)foreach (var cmd in commands) rejected.Add(new Rejection(cmd, $"The game is over: {PlayerText.GameOver(s.GameOver)}"));else RunOrders(c, s, commands, spent, attacked, why, rejected);@@ -140,7 +140,7 @@ namespace Statecraft.Sim/// Attention first; one that fails validation is refunded and rejected with its reason. Step, DelegatedTo and Preview/// share it, so every dry run sees what End Turn will.</summary>static void RunOrders(Content c, GameState s, IReadOnlyList<Command> commands, Dictionary<int, int> spent,- Dictionary<int, (int Q, int R)> attacked, List<Cause> why, List<Rejection> rejected)+ Dictionary<int, AttackRecord> attacked, List<Cause> why, List<Rejection> rejected){if (c.IsGdd) Movement.ContinueRoutes(c, s, spent, why, rejected); // R-MOVE-009: the route step, before the turn's ordersif (c.IsGdd) SettlementControl.CaptureHeld(c, s, why);@@ -184,7 +184,7 @@ namespace Statecraft.Sim/// reported as a politics cause), or the reason it failed validation. A command that fails validation leaves/// the state as it found it (attention is refunded by the caller).</summary>static string? Execute(Content c, GameState s, Command cmd, Dictionary<int, int> spent,- Dictionary<int, (int Q, int R)> attacked, List<Cause> why, int startRung, int dollarsAtStart,+ Dictionary<int, AttackRecord> attacked, List<Cause> why, int startRung, int dollarsAtStart,List<Rejection> rejected){switch (cmd)