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.

fix: the tour's move step skips a unit that spent its movement (#1638)

Eyal Golanf3b967402bf9
diff --git a/changelog.d/tour-move-step-spent.md b/changelog.d/tour-move-step-spent.md
new file mode 100644
index 000000000..1b95f68ac
--- /dev/null
+++ b/changelog.d/tour-move-step-spent.md
@@ -0,0 +1,4 @@
+### Fixed
+- How to play's "Moving a unit" step no longer picks a unit whose card has no Move button: one that already
+ spent its movement this turn, or one with Send home queued. It shows the next unit that can still move, or just its
+ text when none can.
diff --git a/unity/Assets/Scripts/UI/AdvisorTargets.cs b/unity/Assets/Scripts/UI/AdvisorTargets.cs
index 539e6f13f..2c8ec2bfc 100644
--- a/unity/Assets/Scripts/UI/AdvisorTargets.cs
+++ b/unity/Assets/Scripts/UI/AdvisorTargets.cs
@@ -78,14 +78,15 @@ namespace Statecraft.Game.UI
}
/// <summary>The unit the opening chain shows on its move step: the first of <paramref name="units"/> (the drawn tokens' rows, in
- /// units.yaml order) whose sim unit has a hex in Movement.Reachable now, so its card shows Move
- /// (InfoPanelPresenter.MoveTargets' rule); its id, or null with none (the legacy loop, or no unit able to move).</summary>
- public static string? UnitToShow(Content c, GameState s, IEnumerable<UnitEntry> units) =>
- units.FirstOrDefault(u => UnitCard.SimUnit(c, s, u) is int id && Movement.Reachable(c, s, id).Count > 0)?.Id;
+ /// units.yaml order) whose card shows Move now (InfoPanelPresenter.MoveReach, the card's own rule: the turn's spent points
+ /// count, as moves are instant since #1527, and a unit going home has none); its id, or null with none (the legacy loop,
+ /// or no unit able to move: the step shows its text without a target).</summary>
+ public static string? UnitToShow(SimBridge bridge, IEnumerable<UnitEntry> units) =>
+ units.FirstOrDefault(u => UnitCard.SimUnit(bridge.Content, bridge.State, u) is int id && InfoPanelPresenter.MoveReach(bridge, id) != null)?.Id;
/// <summary>The drawn-token form used by the game. A raised token carries its sim id even without a starting row.</summary>
- public static string? UnitToShow(Content c, GameState s, IEnumerable<UnitToken> units) =>
- units.FirstOrDefault(u => UnitCard.SimUnit(c, s, u) is int id && Movement.Reachable(c, s, id).Count > 0)?.Unit.Id;
+ public static string? UnitToShow(SimBridge bridge, IEnumerable<UnitToken> units) =>
+ units.FirstOrDefault(u => UnitCard.SimUnit(bridge.Content, bridge.State, u) is int id && InfoPanelPresenter.MoveReach(bridge, id) != null)?.Unit.Id;
}
}
diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.cs
index c7e98b17f..3e7fe4481 100644
--- a/unity/Assets/Scripts/UI/GameRoot.cs
+++ b/unity/Assets/Scripts/UI/GameRoot.cs
@@ -517,7 +517,7 @@ namespace Statecraft.Game.UI
};
Action showUnit = () =>
{
- if (AdvisorTargets.UnitToShow(bridge.Content, bridge.State, unitLayer.Tokens.Select(t => t.Token)) is not string id) return;
+ if (AdvisorTargets.UnitToShow(bridge, unitLayer.Tokens.Select(t => t.Token)) is not string id) return;
camRig.Distance = Mathf.Min(camRig.Distance, AdvisorTargets.MoveStepDistanceM);
FocusUnit(id);
};
@@ -528,7 +528,7 @@ namespace Statecraft.Game.UI
var hex = info.ShowsUnit ? null : info.Selected;
var unit = info.ShowsUnit ? unitLayer.Selected : null;
var city = AdvisorTargets.CityHex(bridge.Content, bridge.State);
- var shownUnit = AdvisorTargets.UnitToShow(bridge.Content, bridge.State, unitLayer.Tokens.Select(t => t.Token));
+ var shownUnit = AdvisorTargets.UnitToShow(bridge, unitLayer.Tokens.Select(t => t.Token));
// It reads the UI of the Begin it runs in, so after a language switch rebuilt the UI it still puts back what
// the advisor found before (AdvisorSession.ChainState.PutBack).
return () =>
diff --git a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
index 6692db823..a4db3ee16 100644
--- a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
+++ b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
@@ -204,10 +204,18 @@ namespace Statecraft.Game.UI
(int unit, IReadOnlyDictionary<(int Q, int R), int> hexes)? MoveTargets()
{
var unit = unitMode ? selectedUnit() : null;
- if (unit == null || bridge.State.GameOver != null || !(UnitCard.SimUnit(bridge.Content, bridge.State, unit.Value.Token) is int id)
- || bridge.GoingHome(id)) return null;
+ if (unit == null || !(UnitCard.SimUnit(bridge.Content, bridge.State, unit.Value.Token) is int id)) return null;
+ return MoveReach(bridge, id) is { } reach ? (id, reach) : null;
+ }
+
+ /// <summary>The hexes sim unit <paramref name="id"/>'s Move can go to now, as its card offers them (Movement.Reachable with
+ /// the turn's spent points), or null when its card has no Move: the game is over, Send home is queued for it, or no hex
+ /// is in reach. The advisor's move step picks its unit by it too (AdvisorTargets.UnitToShow).</summary>
+ public static IReadOnlyDictionary<(int Q, int R), int>? MoveReach(SimBridge bridge, int id)
+ {
+ if (bridge.State.GameOver != null || bridge.GoingHome(id)) return null;
var reach = Movement.Reachable(bridge.Content, bridge.State, id, bridge.Spent);
- return reach.Count == 0 ? null : (id, reach);
+ return reach.Count == 0 ? null : reach;
}
/// <summary>The shown unit's sim id, the hexes its attack can name (each unit's hex the sim's Combat.Check takes: the
diff --git a/unity/Assets/Tests/EditMode/AdvisorOpeningParityTests.cs b/unity/Assets/Tests/EditMode/AdvisorOpeningParityTests.cs
index 45755876b..89408489c 100644
--- a/unity/Assets/Tests/EditMode/AdvisorOpeningParityTests.cs
+++ b/unity/Assets/Tests/EditMode/AdvisorOpeningParityTests.cs
@@ -54,7 +54,7 @@ public class AdvisorOpeningParityTests
ShowCity = () => { if (AdvisorTargets.CityHex(bridge.Content, bridge.State) is (int q, int r)) info.Show(q, r); },
ShowUnit = () =>
{
- if (AdvisorTargets.UnitToShow(bridge.Content, bridge.State, tokens) is not string id) return;
+ if (AdvisorTargets.UnitToShow(bridge, tokens) is not string id) return;
shown = new PlacedToken(tokens.First(t => t.Unit.Id == id), new AnchorSpot(0, 0, 0, 0, false)); info.ShowUnit();
},
});
@@ -258,16 +258,56 @@ public class AdvisorOpeningParityTests
[Test]
public void TheMoveStepsUnitCanMove()
{
- var s = GameSession.New(C, GoldenRun.Seed, Mode.Historical).State;
+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), En);
+ var s = bridge.State;
var rows = UnitRoster.FromState(UnitRows, C, s, _ => true, (row, a, q, r) => a).Select(t => t.Unit).ToList();
- var id = AdvisorTargets.UnitToShow(C, s, rows);
+ var id = AdvisorTargets.UnitToShow(bridge, rows);
Assert.IsNotNull(id, "a new game has a unit that can move");
var row = rows.Single(u => u.Id == id);
Assert.AreEqual(UnitData.Israel, row.Side);
Assert.IsNotEmpty(Movement.Reachable(C, s, UnitCard.SimUnit(C, s, row)!.Value), "the sim has a hex for it");
var first = rows.First(u => UnitCard.SimUnit(C, s, u) is int i && Movement.Reachable(C, s, i).Count > 0);
Assert.AreEqual(first.Id, id, "the first such row");
- Assert.IsNull(AdvisorTargets.UnitToShow(SimBridgeGddTests.Legacy, s, rows), "the legacy loop: no unit to show");
+ var legacy = new SimBridge(GameSession.New(SimBridgeGddTests.Legacy, GoldenRun.Seed, Mode.Historical), En);
+ Assert.IsNull(AdvisorTargets.UnitToShow(legacy, rows), "the legacy loop: no unit to show");
+ }
+
+ /// <summary>Since moves are instant (#1527), a unit can spend its movement, or have Send home queued, before How to play
+ /// reopens the tour: the move step then shows the next unit whose card still has Move (InfoPanelPresenter.MoveReach, the
+ /// card's rule), and none once no unit can move, as in the legacy loop (the step's text without a target).</summary>
+ [Test]
+ public void TheMoveStepSkipsAUnitWhoseCardHasNoMove()
+ {
+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), En);
+ var rows = UnitRoster.FromState(UnitRows, C, bridge.State, _ => true, (row, a, q, r) => a).Select(t => t.Unit).ToList();
+ int Sim(string row) => UnitCard.SimUnit(C, bridge.State, rows.Single(u => u.Id == row))!.Value;
+ void SpendAll(string row)
+ {
+ var id = Sim(row);
+ for (int i = 0; i < 20; i++)
+ {
+ var reach = Movement.Reachable(C, bridge.State, id, bridge.Spent);
+ if (reach.Count == 0) return;
+ var far = reach.OrderByDescending(kv => kv.Value).First().Key;
+ Assert.IsNull(bridge.Queue(new Move(id, far.Q, far.R)), $"{row}: the sim takes a move it offered");
+ }
+ Assert.Fail($"{row}: still has movement after 20 moves");
+ }
+ var first = AdvisorTargets.UnitToShow(bridge, rows);
+ Assert.IsNotNull(first, "precondition: a new game has a unit that can move");
+ SpendAll(first!);
+ Assert.IsNotEmpty(Movement.Reachable(C, bridge.State, Sim(first!)), "precondition: with nothing spent the sim would still offer it hexes");
+ var second = AdvisorTargets.UnitToShow(bridge, rows);
+ Assert.IsNotNull(second, "another unit can still move");
+ Assert.AreNotEqual(first, second, "the unit that spent its movement is not shown");
+ Assert.IsNotNull(InfoPanelPresenter.MoveReach(bridge, Sim(second!)), "the shown unit's card has Move");
+ Assert.IsNull(bridge.Queue(new UnitOrder(Sim(second!), Demobilization.SendHome)));
+ Assert.IsNotEmpty(Movement.Reachable(C, bridge.State, Sim(second!), bridge.Spent), "precondition: the sim alone would still move it");
+ var third = AdvisorTargets.UnitToShow(bridge, rows);
+ Assert.IsTrue(third != first && third != second, $"a unit going home is not shown either ({third})");
+ foreach (var row in rows.Where(u => UnitCard.SimUnit(C, bridge.State, u) is int id && InfoPanelPresenter.MoveReach(bridge, id) != null).Select(u => u.Id).ToList())
+ SpendAll(row);
+ Assert.IsNull(AdvisorTargets.UnitToShow(bridge, rows), "no unit can move: none to show");
}
/// <summary>Skip at any step closes it; Next walks the seven steps and Done closes it; Escape skips; How to play's button
@@ -449,7 +489,7 @@ public class AdvisorOpeningParityTests
var move = Array.IndexOf(Steps, "opening.move");
for (int i = 0; i < move; i++) UiTestKit.Press(Ui.Q<Button>("advisor-got-it"));
Assert.AreEqual(move, advisor.ChainStep);
- var id = AdvisorTargets.UnitToShow(bridge.Content, bridge.State, units.Tokens.Select(t => t.Token));
+ var id = AdvisorTargets.UnitToShow(bridge, units.Tokens.Select(t => t.Token));
Assert.IsNotNull(id, "precondition: a drawn unit can move");
Assert.AreEqual(id, units.Selected, "the advisor's unit, with its ring");
Assert.IsTrue(info.ShowsUnit, "its card in the info panel");
@@ -491,7 +531,7 @@ public class AdvisorOpeningParityTests
Assert.AreEqual(move, advisor.ChainStep, "precondition: the advisor is on its move step");
for (int frame = 0; frame < 5; frame++) yield return null;
- var id = AdvisorTargets.UnitToShow(bridge.Content, bridge.State, units.Tokens.Select(t => t.Token));
+ var id = AdvisorTargets.UnitToShow(bridge, units.Tokens.Select(t => t.Token));
Assert.IsNotNull(id, "precondition: a drawn unit can move");
var token = units.Tokens.Single(t => t.Token.Unit.Id == id);
var simUnit = UnitCard.SimUnit(bridge.Content, bridge.State, token.Token);