A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
sim: the war loss ends the game (R-END-003, refs #8) (#1246)
agamrafaeli-instinctafa95994dc45
diff --git a/changelog.d/sim-defeat-war-8.md b/changelog.d/sim-defeat-war-8.mdnew file mode 100644index 000000000..6eccf8aa2--- /dev/null+++ b/changelog.d/sim-defeat-war-8.md@@ -0,0 +1,6 @@+- GDD Phase 7 PR 5, the war loss (refs #8, #387): an enemy military unit holding the hex of one of the player's GDD+ cities at the end of a turn ends the game at once with `defeat_war` (R-END-003; Q-WAR-5 and Q-DEF-3, owner, 17 October+ 2026, #784: one turn is enough, any of the GDD cities, truces included), with its own reason line. The check runs in+ resolver step 11 after the economic count, so a turn the economic defeat also ends keeps `defeat_economy`. The+ reference run is never defeated, so no golden moves. The end-screen history note beside the cause (Q-DEF-4 (b)) goes+ with #812.diff --git a/docs/rules/mvp.md b/docs/rules/mvp.mdindex 05273e36d..bd3d4ceed 100644--- a/docs/rules/mvp.md+++ b/docs/rules/mvp.md@@ -286,15 +286,17 @@ R-ARMS-003: Recruit training (R-TRAIN-001) takes `arms_per_recruit_group` (1) Ar## Defeat-The game can be lost (D-B5, decided: "Money and Food exhausted for N turns, or a war loss. Values for the owner. Coalition collapse is off in the MVP"; Phase 7 PR 5, #8). This part covers the economic defeat; the war loss waits for Phase 5's enemy units (#749). The defeat texts are the owner's pick (Q-DEF-4, #784, open): until then the game ends with the code `defeat_economy`, and its reason reads "Money and Food ran out", the plan's short option (a).+The game can be lost (D-B5, decided: "Money and Food exhausted for N turns, or a war loss. Values for the owner. Coalition collapse is off in the MVP"; Phase 7 PR 5, #8). This part covers both defeats: the economic one (R-END-001, R-END-002) and the war loss (R-END-003). The defeat texts are decided (Q-DEF-4 (b), owner, 18 October 2026, #784): a short factual line per cause - "Money and Food ran out" and "The enemy holds a city" - plus one sourced history note beside each, which goes on the end screen with #812.R-END-001: Money is exhausted when the debt is at or above `debt_ceiling` (Q-DEF-1, owner, 17 October 2026, #784: a debt ceiling; below it borrowing goes on as R-MONEY-003 allows). Food is exhausted when the national stock is 0 (R-FOOD-002; Q-POOL-5, "The stock stops at 0"). One of them alone ends nothing. `debt_ceiling` is IL 250m, a labelled design value (the recommended first value; the Phase 7 balance pass, PR 12, sets the final number), required in the GDD loop, a finite number >= 0.R-END-002: In the resolver's last step, after the objectives and the score (spec §2.3, step 11), a turn end with Money and Food both exhausted adds 1 to `ExhaustedTurns`, and any other turn end sets it back to 0. When it reaches `defeat_turns`, the game ends with `defeat_economy` and a cause in the turn's causes, under the game's end (`era`) like the MVP end. `defeat_turns` is 3, a labelled design value (Q-DEF-2, owner, 18 October 2026, #784: "3 turns"), required in the GDD loop, at least 1. The step reads the debt as it finds it: the turn's deficit is borrowed after the resolution, so it counts from the next turn's end. A defeat on the game's last turn ends it as a defeat. `ExhaustedTurns` is in the state hash only when above 0, so a run that never has both exhausted hashes as before; the legacy loop never counts.+R-END-003: When an enemy military unit (a unit with a `side` other than the player's; a halutz has no side, R-UNIT-001) holds the hex of one of the player's GDD cities (`cities.yaml`; the hex in `map/settlement_hexes.yaml`) at the end of a turn, the game ends at once with `defeat_war` and a cause in the turn's causes, under the game's end (`era`). One turn is enough, any of the GDD cities, at any time, truces included (Q-WAR-5; Q-DEF-3, owner, 17 October 2026, #784). The check runs in the resolver's last step after the economic count (R-END-002), so a turn the economic defeat also ends keeps `defeat_economy`.+## The end report-D-H4 as decided (owner, 17 October 2026, #784: Q-END-1 and Q-END-2 as recommended): #19's legacy report folds into the end screen, which reads one sim helper, `EndReport.For` (Phase 7 PR 7, #810). This first slice reads only what the state already holds; the score's parts wait for #809, and the war loss for #8.+D-H4 as decided (owner, 17 October 2026, #784: Q-END-1 and Q-END-2 as recommended): #19's legacy report folds into the end screen, which reads one sim helper, `EndReport.For` (Phase 7 PR 7, #810). This first slice reads only what the state already holds; the score's parts wait for #809.R-END-005: Once the game has ended, the end report lists the outcome (the `GameOver` code), the end date, the state's score as it stands, every objective in `objectives.yaml` order with its historical date and, when completed, the player's date and the months from history (`Objectives.MonthsFromHistory`), and the two national parameters, Social cohesion and International Support, with no historical value. It is pure: it computes no rule, writes nothing, and two calls give the same report with the state hash unchanged. A game still on has no report.diff --git a/sim/Statecraft.Sim.Tests/DefeatTests.cs b/sim/Statecraft.Sim.Tests/DefeatTests.csindex ddc65d574..16f44866b 100644--- a/sim/Statecraft.Sim.Tests/DefeatTests.cs+++ b/sim/Statecraft.Sim.Tests/DefeatTests.cs@@ -3,10 +3,11 @@ using System.Collections.Generic;using System.Linq;using Statecraft.Sim;-/// <summary>The economic defeat (GDD Phase 7 PR 5, D-B5; #8): Money is exhausted when the debt is at or above+/// <summary>The defeat (GDD Phase 7 PR 5, D-B5; #8). The economic half: Money is exhausted when the debt is at or above/// mvp.debt_ceiling (Q-DEF-1) and Food when the national stock is 0 (R-END-001); after mvp.defeat_turns consecutive turn/// ends with both, the game ends with defeat_economy (Q-DEF-2, N = 3), and a turn with either one not exhausted resets the-/// count (R-END-002). The war loss waits for Phase 5's enemy units (#749).</summary>+/// count (R-END-002). The war loss (R-END-003): an enemy military unit holding a GDD city's hex at the turn's end ends the+/// game at once with defeat_war (Q-WAR-5; Q-DEF-3: one turn is enough, any of the GDD cities, truces included).</summary>public class DefeatTests{static readonly Content Real = ContentLoader.Load(RealContentTests.ContentRoot());@@ -140,11 +141,19 @@ public class DefeatTestsAssert.Contains(reason, e.Message);}- // R-END-002 (plan risk 3): the reference run and the Historical-mode run with no orders reach 15 January 1952 and- // are never defeated, so the existing goldens do not move.+ // R-END-002 (plan risk 3), R-END-003: the reference run and the Historical-mode run with no orders reach+ // 15 January 1952 and are never defeated, so the existing goldens do not move. The war-loss assertion guards a+ // sourced enemy start on a city's hex (plan PR 5, Review focus 3): reaching the MVP end also proves it, since one+ // enemy unit on a city's hex at any turn end would end the run as a defeat.[Fact]public void TheHistoricalRunIsNeverDefeated(){+ var cities = new HashSet<(int Q, int R)>();+ foreach (var city in Real.Cities)+ {+ var h = Real.SettlementHexes.First(x => x.Id == city.Id);+ cities.Add((h.Q, h.R));+ }foreach (var (name, orders) in new (string, Func<GameState, IReadOnlyList<Command>>)[]{("reference", s => GoldenRun.Orders(Real, s)),@@ -156,12 +165,63 @@ public class DefeatTests{s = Simulation.Step(Real, s, orders(s)).State;Assert.True(s.ExhaustedTurns == 0, $"{name}: Money and Food both exhausted on {s.Date:yyyy-MM-dd}");+ var onACity = s.Units.Values.FirstOrDefault(u => u.Side != "" && u.Side != Movement.PlayerSide && cities.Contains((u.Q, u.R)));+ Assert.True(onACity == null, $"{name}: enemy unit {onACity?.Id} holds a city's hex on {s.Date:yyyy-MM-dd}");}Assert.Equal(GameOverCodes.MvpEnded, s.GameOver);Assert.Equal(Real.End, s.Date);}}+ // R-END-003: an enemy military unit on a GDD city's hex at the turn's end ends the game at once, on the first turn+ // it happens. The fixture's town_a is on (1, 0).+ [Fact]+ public void AnEnemyInACityEndsTheGame()+ {+ var c = GddWithArmy();+ var s = Simulation.NewGame(c, 1, Mode.Historical);+ s.Units.Add(1, new Unit(1, "enemy", 1, 0, side: "egypt", strength: 3));+ var r = Simulation.Step(c, s, Array.Empty<Command>());+ Assert.Equal(GameOverCodes.DefeatWar, r.State.GameOver);+ Assert.Contains(r.Why, w => w.System == "era" && w.Target == GameOverCodes.DefeatWar);+ // The game is over: an order is refused with the reason, and the turn does not advance.+ var after = Simulation.Step(c, r.State, new Command[] { new SetMobilization(0.5) });+ Assert.Single(after.Rejected);+ Assert.Equal(r.State.Date, after.State.Date);++ // The player's own unit or a halutz on the hex, or an enemy next to the city, ends nothing (R-UNIT-001: a+ // halutz has no side).+ foreach (var u in new Unit[]+ {+ new Unit(1, "soldier", 1, 0, side: "israel", strength: 3),+ new Unit(1, "halutz", 1, 0),+ new Unit(1, "enemy", 2, 0, side: "egypt", strength: 3),+ })+ {+ var t2 = Simulation.NewGame(c, 1, Mode.Historical);+ t2.Units.Add(u.Id, u);+ Assert.Null(Step(c, t2).GameOver);+ }++ // The economic count is read first, so a turn that ends the game both ways keeps defeat_economy.+ var b = Simulation.NewGame(c, 1, Mode.Historical);+ Broke(b); Starving(b);+ b = Step(c, Step(c, b));+ Assert.Equal(2, b.ExhaustedTurns);+ b.Units.Add(1, new Unit(1, "enemy", 1, 0, side: "egypt", strength: 3));+ Assert.Equal(GameOverCodes.DefeatEconomy, Step(c, b).GameOver);+ }++ /// <summary>The GDD fixture with a military type per side (fixture values); town_a is on (1, 0) of a coastal plain.</summary>+ static Content GddWithArmy() => ContentLoader.Load(Fixture.Write(Fixture.Gdd(new Dictionary<string, string>+ {+ ["unit_types.yaml"] = 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",+ ["map/hexes.json"] = MovementTests.Board(),+ ["corridors.yaml"] = "corridors: []\n",+ })));+// R-END-001, R-END-002: scenario-mvp-defeat overspends and refuses foreign currency until it is defeated.[Fact]public void TheDefeatScenarioEndsInDefeat()diff --git a/sim/Statecraft.Sim/GameState.cs b/sim/Statecraft.Sim/GameState.csindex 7935f0af2..343274b17 100644--- a/sim/Statecraft.Sim/GameState.cs+++ b/sim/Statecraft.Sim/GameState.cs@@ -17,6 +17,8 @@ namespace Statecraft.Simpublic const string CoalitionCollapse = "coalition_collapse";/// <summary>Money and Food were both exhausted for mvp.defeat_turns turn ends in a row (R-END-002; D-B5).</summary>public const string DefeatEconomy = "defeat_economy";+ /// <summary>An enemy military unit holds a GDD city's hex at the turn's end (R-END-003; Q-WAR-5, Q-DEF-3).</summary>+ public const string DefeatWar = "defeat_war";}public enum Mode { Historical, Open }diff --git a/sim/Statecraft.Sim/PlayerText.cs b/sim/Statecraft.Sim/PlayerText.csindex d310678ec..e332fa6c4 100644--- a/sim/Statecraft.Sim/PlayerText.cs+++ b/sim/Statecraft.Sim/PlayerText.cs@@ -21,8 +21,10 @@ namespace Statecraft.SimGameOverCodes.EraEnded => "The chapter has ended",GameOverCodes.MvpEnded => "The game has reached its last turn",GameOverCodes.CoalitionCollapse => "The coalition collapsed",- // Interim: the plan's short option (a) wording. The defeat texts are the owner's pick (Q-DEF-4, #784, open).+ // The short factual line per cause (Q-DEF-4 (b), owner, 18 Oct 2026, #784). The sourced history note+ // beside each cause goes on the end screen with #812.GameOverCodes.DefeatEconomy => "Money and Food ran out",+ GameOverCodes.DefeatWar => "The enemy holds a city",_ => throw Unknown("game over reason", code),};diff --git a/sim/Statecraft.Sim/TurnResolver.cs b/sim/Statecraft.Sim/TurnResolver.csindex 16d3ccfab..eb50f3f23 100644--- a/sim/Statecraft.Sim/TurnResolver.cs+++ b/sim/Statecraft.Sim/TurnResolver.cs@@ -1,5 +1,6 @@using System;using System.Collections.Generic;+using System.Linq;namespace Statecraft.Sim{@@ -31,7 +32,8 @@ namespace Statecraft.SimCities.Arrive(c, s, r, next, why); // 9. arrivals// 10. Occupation timers: Phase 6.Objectives.Complete(c, s, r, next, why); // 11. objectives and score- Defeat(s, r, why); // then the defeat (D-B5); the war loss waits for Phase 5+ Defeat(s, r, why); // then the economic defeat (D-B5, R-END-002)+ WarLoss(c, s, why); // and the war loss (R-END-003)}/// <summary>R-END-001, R-END-002: counts the turn ends with Money (the debt at or above mvp.debt_ceiling) and Food (an@@ -45,5 +47,28 @@ namespace Statecraft.Sims.GameOver = GameOverCodes.DefeatEconomy;why.Add(new Cause("era", s.GameOver, s.ExhaustedTurns, PlayerText.GameOver(s.GameOver))); // the game's end, grouped with the MVP end}++ /// <summary>R-END-003: an enemy military unit holding the hex of one of the player's GDD cities at the turn's+ /// end ends the game at once with defeat_war (Q-WAR-5; Q-DEF-3, owner, 17 Oct 2026, #784: one turn is enough, any+ /// of the GDD cities, at any time, truces included). A halutz or the player's own unit on the hex ends nothing+ /// (R-UNIT-001: a halutz has no side), and a game the economic count has just ended this turn keeps its code.</summary>+ static void WarLoss(Content c, GameState s, List<Cause> why)+ {+ if (s.GameOver != null) return;+ var cities = new HashSet<(int Q, int R)>();+ foreach (var city in c.Cities)+ {+ var h = c.SettlementHexes.First(x => x.Id == city.Id); // every city has one (ValidateGdd)+ cities.Add((h.Q, h.R));+ }+ foreach (var u in s.Units.Values)+ {+ if (u.Side == "" || u.Side == Movement.PlayerSide) continue; // not an enemy (Movement's reading of "the enemy")+ if (!cities.Contains((u.Q, u.R))) continue;+ s.GameOver = GameOverCodes.DefeatWar;+ why.Add(new Cause("era", s.GameOver, u.Id, PlayerText.GameOver(s.GameOver))); // the game's end, grouped with the MVP end+ return;+ }+ }}}