A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Movement: a go-to route's turn ends before a full friendly stack, as End turn does (Fixes #2548) (#2619)
Beri02a27b900f04
diff --git a/changelog.d/2548-route-full-stack.md b/changelog.d/2548-route-full-stack.mdnew file mode 100644index 000000000..6093fc63f--- /dev/null+++ b/changelog.d/2548-route-full-stack.md@@ -0,0 +1,3 @@+### Fixed+- A go-to route whose day would end on a hex already full of your units now ends that day on the last free hex+ before it, as the unit really does, so the turn shown for each hex and for the arrival is right (#2548).diff --git a/sim/Statecraft.Sim.Tests/GoToTests.cs b/sim/Statecraft.Sim.Tests/GoToTests.csindex 5258f4f67..9c3307598 100644--- a/sim/Statecraft.Sim.Tests/GoToTests.cs+++ b/sim/Statecraft.Sim.Tests/GoToTests.cs@@ -113,6 +113,29 @@ public class GoToTestsAssert.Equal((4, 1), (now.State.Units[1].Q, now.State.Units[1].R));}+ // R-MOVE-002, R-MOVE-003 (#2548): a turn of the route that would end on a hex full of the unit's side ends on the last+ // free hex before it, as the route step does, so the route shows the turn the unit really arrives on.+ [Fact]+ public void ARouteShowsTheTurnLostToAFullStackOnTheWay()+ {+ var c = Board(); // the fixture's stack limit is 2 a side (MovementTests' summary)+ var s = CombatTests.With(c, new Unit(1, "gun", 0, 1, side: "israel", strength: 3), Soldier(2, 2, 1), Soldier(3, 2, 1));+ var shown = Movement.Route(c, s, 1, 4, 1, None);+ // The gun's 2 points would end turn 1 on 2,1, which is full: it stops on 1,1, and 4,1 is a turn later.+ Assert.Equal(new[] { (1, 1, 1), (2, 1, 2), (3, 1, 2), (4, 1, 3) }, shown.Select(h => (h.Q, h.R, h.Turn)));+ var state = Simulation.Step(c, s, new Command[] { new UnitOrder(1, Movement.GoTo, 4, 1) }).State;+ var turn = 1;+ while (true)+ {+ turn++;+ var start = Simulation.Preview(c, state, new Command[0]).State; // the route step has run+ if ((start.Units[1].Q, start.Units[1].R) == (4, 1)) break;+ Assert.True(turn < 5, "the gun never arrived");+ state = Simulation.Step(c, state, new Command[0]).State;+ }+ Assert.Equal(shown.Last().Turn, turn);+ }+// R-SAVE-003: a go-to is a committed unit order with its hex.[Fact]public void AGoToIsSavedAsAUnitOrderWithAHex()diff --git a/sim/Statecraft.Sim/Movement.cs b/sim/Statecraft.Sim/Movement.csindex 2fc8e9dd5..4a2f1dea1 100644--- a/sim/Statecraft.Sim/Movement.cs+++ b/sim/Statecraft.Sim/Movement.cs@@ -102,13 +102,27 @@ namespace Statecraft.Simfor (var h = (q, r); h != (u.Q, u.R); h = prev[h]) hexes.Add(h);hexes.Reverse();spent.TryGetValue(unitId, out var used);- int left = full - used, turn = 1, before = 0;- foreach (var h in hexes)+ int left = full - used, turn = 1, before = 0, first = 0;+ for (int i = 0; i < hexes.Count; i++){- var step = dist[h] - before; before = dist[h];- if (step > left && !(left == full && step > full)) { turn++; left = full; }+ var step = dist[hexes[i]] - before;+ if (step > left && !(left == full && step > full))+ {+ // R-MOVE-003, as Advance moves: a turn that would end on a hex full of the unit's side ends on the last+ // free hex of that turn instead, and the next turn starts from there (#2548). With none free, as before.+ var end = i - 1;+ while (end >= first && Full(c, s, u, hexes[end].Q, hexes[end].R) != null) end--;+ if (end >= first && end < i - 1)+ {+ result.RemoveRange(end + 1, result.Count - end - 1);+ i = end + 1;+ step = dist[hexes[i]] - dist[hexes[end]];+ }+ turn++; left = full; first = i;+ }+ before = dist[hexes[i]];left = step > left ? 0 : left - step;- result.Add((h.Q, h.R, turn));+ result.Add((hexes[i].Q, hexes[i].R, turn));}return result;}