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 unit order names both hex coordinates or neither (Refs #2526) (#3041)
Eyal Golan9cfe2c778cea
diff --git a/changelog.d/2526-half-hex.md b/changelog.d/2526-half-hex.mdnew file mode 100644index 000000000..57aa3d132--- /dev/null+++ b/changelog.d/2526-half-hex.md@@ -0,0 +1,4 @@+### Fixed+- A unit order that names only one of its two hex coordinates can no longer be made, in code or in a scenario file, so+ a saved game can no longer replay such an order differently from the turn that ran it (#2526; reported and first+ fixed by LetMeSleep8h in #2562).diff --git a/docs/rules/mvp.md b/docs/rules/mvp.mdindex e25539da5..0b1993ea2 100644--- a/docs/rules/mvp.md+++ b/docs/rules/mvp.md@@ -326,7 +326,7 @@ R-MOVE-010: A unit needs orders when it is one of the player's military units, iPhase 5 PR 7 (#746). Q-HOLD-1, "(a) 1 turn, free, lost on move", and Q-HOLD-2, "(a) Own hex, no interception" (owner, 15 October 2026, #375). The two percents are design values this PR proposes from avlco's option 1 on #746; the owner may override them.-R-ORDER-001: `UnitOrder(unit, order)` gives one of the player's military units the standing order `fortify` or `hold`, replacing the one it had. A new order replaces a go-to too. The unit keeps its order, turn after turn, until the player gives another; moving clears fortify (Q-HOLD-1): in the resolver after combat (step 2), in id order, a unit no longer on the hex its fortify names (`OrderTarget`, "q,r") loses the order. The order is refused with the state unchanged and a reason for an unknown order or unit, a unit with no side (a halutz), a unit of another side, a hold for artillery (R-COMBAT-007), or the order the unit already has (fortify on the hex it fortifies). It costs `attention_cost.unit_order` (0, D-A6) and is refused in the legacy loop.+R-ORDER-001: `UnitOrder(unit, order)` gives one of the player's military units the standing order `fortify` or `hold`, replacing the one it had. A new order replaces a go-to too. The unit keeps its order, turn after turn, until the player gives another; moving clears fortify (Q-HOLD-1): in the resolver after combat (step 2), in id order, a unit no longer on the hex its fortify names (`OrderTarget`, "q,r") loses the order. The order is refused with the state unchanged and a reason for an unknown order or unit, a unit with no side (a halutz), a unit of another side, a hold for artillery (R-COMBAT-007), or the order the unit already has (fortify on the hex it fortifies). It costs `attention_cost.unit_order` (0, D-A6) and is refused in the legacy loop. A unit order names both hex coordinates or neither; one that names only one cannot be built (#2526), so no save can carry an order that live play refused.R-ORDER-002: Fortify takes one turn to set and costs no Arms or Money (Q-HOLD-1). Given, it reads `fortifying` and names the unit's hex; in the same resolver step it becomes `fortify`, so the combat of the turn it is given sees no bonus. A fortified unit on its hex defends at `mvp.fortify_defence_percent` of its strength (`Orders.DefencePercent`; real value 125), and from the moment it leaves the hex at 100. Under `gdd-0.3`, the catalogue's Agreed `military.fortify_turns` must remain 1; settled fortify on its named hex adds the Agreed `military.fortify_bonus` (25) (O1, owner-approved 1 November 2026, #1668) to the other defence bonuses, once (R-COMBAT-002). `fortifying` adds nothing.diff --git a/sim/Statecraft.Sim.Tests/SaveGameTests.cs b/sim/Statecraft.Sim.Tests/SaveGameTests.csindex 035088743..ceca97063 100644--- a/sim/Statecraft.Sim.Tests/SaveGameTests.cs+++ b/sim/Statecraft.Sim.Tests/SaveGameTests.cs@@ -246,6 +246,22 @@ public class SaveGameTestsAssert.Equal(GoldenRun.Hash1952, loaded.State.Hash());}+ // #2526 (R-ORDER-001, R-SAVE-003): a hold that names a full hex is refused live and written with its hex, so the+ // save replays the same refusal and ends on the same hash. A half hex can no longer be built (UnitOrderHalfHexTests).+ [Fact]+ public void AFullHexHoldReplaysAsItWasRefusedLive()+ {+ var c = ContentLoader.Load(RealContentTests.ContentRoot());+ var g = GameSession.New(c, 1, Mode.Historical);+ var r = g.Step(new Command[] { new UnitOrder(0, Orders.Hold, 2, 0) });+ Assert.Equal(Orders.RefusedNamesNoHex, Assert.Single(r.Rejected).Reason);+ var text = g.Save.Write();+ Assert.Contains("\norder 0 hold 2 0\n", text);+ var loaded = GameSession.Load(c, text, out var warnings);+ Assert.Empty(warnings);+ Assert.Equal(g.State.Hash(), loaded.State.Hash());+ }+[Fact]public void OtherContentLoadsWithAWarning(){diff --git a/sim/Statecraft.Sim.Tests/ScenarioTests.cs b/sim/Statecraft.Sim.Tests/ScenarioTests.csindex f8f706029..2d376957c 100644--- a/sim/Statecraft.Sim.Tests/ScenarioTests.cs+++ b/sim/Statecraft.Sim.Tests/ScenarioTests.cs@@ -88,6 +88,9 @@ public class ScenarioTests[InlineData(" - { kind: set_policy, policy: rationing, level: 1, extra: 2 }\n", "unknown field 'extra'")][InlineData(" - { policy: rationing }\n", "needs 'kind'")][InlineData(" - { kind: set_policy, policy: rationing, level: lots }\n", "must be a number")]+ // #2526 (R-ORDER-001): a unit order names both hex coordinates or neither.+ [InlineData(" - { kind: unit_order, unit: 0, order: hold, q: 1 }\n", "needs both 'q' and 'r', or neither")]+ [InlineData(" - { kind: unit_order, unit: 0, order: hold, r: 1 }\n", "needs both 'q' and 'r', or neither")]public void AMalformedCommandIsRefusedWithTheProblemNamed(string command, string expected) =>Assert.Contains(expected, Assert.Throws<InvalidDataException>(() => Scenario.Parse(Head + command)).Message);diff --git a/sim/Statecraft.Sim.Tests/UnitOrderHalfHexTests.cs b/sim/Statecraft.Sim.Tests/UnitOrderHalfHexTests.csnew file mode 100644index 000000000..2d8aa47e1--- /dev/null+++ b/sim/Statecraft.Sim.Tests/UnitOrderHalfHexTests.cs@@ -0,0 +1,20 @@+using System;+using Statecraft.Sim;++/// <summary>#2526: a save writes a unit order's hex only when both coordinates are set, while live play refuses a+/// non-hex order that carries either one, so an order with half a hex would replay differently from the game that+/// ran it. The order now names both coordinates or neither (R-ORDER-001). First fixed by LetMeSleep8h in #2562.</summary>+public class UnitOrderHalfHexTests+{+ [Fact]+ public void AHalfGivenHexIsRefusedAtConstruction()+ {+ Assert.Throws<ArgumentException>(() => new UnitOrder(1, Orders.Hold, q: 1));+ Assert.Throws<ArgumentException>(() => new UnitOrder(1, Orders.Hold, r: 2));+ Assert.Throws<ArgumentException>(() => new UnitOrder(1, Movement.GoTo, q: 1));+ Assert.Throws<ArgumentException>(() => new UnitOrder(1, Combat.Attack, r: 0));+ // Both or neither still builds.+ Assert.Null(new UnitOrder(1, Orders.Hold).Q);+ Assert.Equal(2, new UnitOrder(1, Movement.GoTo, 1, 2).R);+ }+}diff --git a/sim/Statecraft.Sim/Commands.cs b/sim/Statecraft.Sim/Commands.csindex a452d9569..7b66c6ecb 100644--- a/sim/Statecraft.Sim/Commands.cs+++ b/sim/Statecraft.Sim/Commands.cs@@ -148,9 +148,14 @@ namespace Statecraft.Sim{public override string Kind => Orders.Kind;public int Unit { get; } public string Order { get; }- /// <summary>The hex an attack names; null for fortify and hold.</summary>+ /// <summary>The hex an attack or a go-to names; null for the orders that name none. Both coordinates or neither: a+ /// half-given hex is refused at construction, so a save cannot carry an order live play refused (#2526).</summary>public int? Q { get; } public int? R { get; }- public UnitOrder(int unit, string order, int? q = null, int? r = null) { Unit = unit; Order = order; Q = q; R = r; }+ public UnitOrder(int unit, string order, int? q = null, int? r = null)+ {+ if (q is null != (r is null)) throw new ArgumentException("A unit order names both hex coordinates or neither (#2526).");+ Unit = unit; Order = order; Q = q; R = r;+ }}/// <summary>Starts city project <see cref="ProjectKind"/> (a projects.yaml id) for <see cref="City"/> on hex (<see cref="Q"/>,diff --git a/sim/Statecraft.Sim/Scenario.cs b/sim/Statecraft.Sim/Scenario.csindex 35b6f0ccf..4b3cad360 100644--- a/sim/Statecraft.Sim/Scenario.cs+++ b/sim/Statecraft.Sim/Scenario.cs@@ -106,7 +106,9 @@ namespace Statecraft.Simcase "reinforce": return new Reinforce(Whole(c, "unit"), Str(c, "city"), Whole(c, "count"));case "pioneer_work": return new PioneerWork(Whole(c, "unit"), Str(c, "action"), Str(c, "site"));case "stop_work": return new StopWork(Whole(c, "unit"));- case "unit_order": return new UnitOrder(Whole(c, "unit"), Str(c, "order"), c.ContainsKey("q") ? Whole(c, "q") : (int?)null, c.ContainsKey("r") ? Whole(c, "r") : (int?)null);+ case "unit_order":+ if (c.ContainsKey("q") != c.ContainsKey("r")) throw new InvalidDataException("command 'unit_order' needs both 'q' and 'r', or neither");+ return new UnitOrder(Whole(c, "unit"), Str(c, "order"), c.ContainsKey("q") ? Whole(c, "q") : (int?)null, c.ContainsKey("r") ? Whole(c, "r") : (int?)null);case "start_project": return new StartProject(Str(c, "project"), Str(c, "city"), Whole(c, "q"), Whole(c, "r"));case "cancel_project": return new CancelProject(Whole(c, "project"));case "move": return new Move(Whole(c, "unit"), Whole(c, "q"), Whole(c, "r"));