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.

Hex and info panels: Build is disabled when queued orders would make the sim refuse it (Fixes #2661) (#2679)

Eyal Golan3461df2042ba
diff --git a/changelog.d/2661.md b/changelog.d/2661.md
new file mode 100644
index 000000000..f38ef6aa7
--- /dev/null
+++ b/changelog.d/2661.md
@@ -0,0 +1,4 @@
+### Fixed
+
+- Disabled city-project and legacy map-action Build controls when orders already queued this turn would make the simulation
+ refuse the new order. An order the Attention left cannot pay for keeps its button, and pressing it still says why.
diff --git a/unity/Assets/Scripts/UI/GddHexPanel.cs b/unity/Assets/Scripts/UI/GddHexPanel.cs
index 435c7117f..78daf1cc1 100644
--- a/unity/Assets/Scripts/UI/GddHexPanel.cs
+++ b/unity/Assets/Scripts/UI/GddHexPanel.cs
@@ -21,8 +21,9 @@ namespace Statecraft.Game.UI
/// day it was finished (R-PROJ-001 to R-PROJ-003); under gdd-0.3, on a built arms workshop, its staffing and cycle (#1722,
/// R-ARMS-005 to R-ARMS-007).</para>
/// <para>Every number is the sim's, and every order goes through the bridge's queue: the sim rules on it when the turn
- /// runs. No rule lives here: the limits are the sim's public helpers (Cities.MaxWorkers, Training.Available, Workshops.Check), asked with
- /// the queue, and a button is disabled only where the sim would refuse that order, or another one in the queue, anyway.</para></summary>
+ /// runs. No rule lives here: the limits are the sim's public helpers (Cities.MaxWorkers, Training.Available, Workshops.Check) and
+ /// Simulation.Preview, asked with the exact queue a press would leave; a button is disabled only where the sim would refuse that
+ /// order, or another one in the queue, anyway.</para></summary>
public static class GddHexPanel
{
public const string HostName = "info-gdd";
@@ -351,6 +352,36 @@ namespace Statecraft.Game.UI
var cities = Projects.CitiesFor(c, s, q, r);
if (cities.Count == 0 || kinds.Count == 0) return;
var queued = bridge.QueuedLike(new StartProject("", "", q, r)) as StartProject;
+
+ // Ask the sim about the exact queue a press would leave: replace this hex's order in its current slot, or append it,
+ // after the committed orders. This costs one preview for each project kind the panel considers while it renders, not
+ // on each frame; an earlier order can make a kind that the current state refuses valid.
+ (string? kind, string? words) Refusal(ProjectKind k, string city)
+ {
+ var press = new StartProject(k.Id, city, q, r);
+ var old = bridge.QueuedLike(press);
+ var queue = bridge.Queued.ToList();
+ var slot = old == null ? -1 : queue.IndexOf(old);
+ // An order the Attention left cannot pay for keeps its Build button, as before the preview: the bridge refuses
+ // the press and says why (order.no_attention, SimBridge.Queue). Only the rules are asked, on the state the rest
+ // of the queue leaves (the press replaces this hex's order, so that order is left out too).
+ if (!bridge.CanPay(press))
+ {
+ if (slot >= 0) queue.RemoveAt(slot);
+ var rule = Projects.CheckCityReason(c, Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(queue).ToList()).State,
+ k.Id, city, q, r);
+ return rule == null ? (null, null) : (rule.Kind, ReasonText(rule, text, c, townName));
+ }
+ if (slot >= 0) queue[slot] = press; else queue.Add(press);
+ var preview = Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(queue).ToList());
+ var rejected = preview.Rejected.FirstOrDefault(x => ReferenceEquals(x.Command, press));
+ if (rejected == null) return (null, null);
+
+ var exact = Projects.CheckCityReason(c, preview.State, k.Id, city, q, r);
+ return exact != null
+ ? (exact.Kind, ReasonText(exact, text, c, townName))
+ : (null, SimBridge.ReasonText(text, rejected.Reason, c, bridge.TownNamesHe));
+ }
// Owner ruling on #1150 (21 Oct 2026): under the city's own part the full list ran into the left column on a stacked
// bar or a short window, so there it folds behind one toggle; a refusal is then one tap away. The open state is the
// host's class, kept across renders (InfoPanelPresenter.Show closes it for a newly chosen hex).
@@ -381,13 +412,13 @@ namespace Statecraft.Game.UI
host.Q("city-project-cities")?.Query<Button>().Where(b => b.enabledSelf).First()?.Focus();
}).SetEnabled(id != city);
}
- var refusals = kinds.Select(k => Projects.CheckCityReason(c, s, k.Id, city, q, r)).ToList();
- var words = refusals.Select(x => x == null ? null : ReasonText(x, text, c, townName)).ToList();
+ var refusals = kinds.Select(k => Refusal(k, city)).ToList();
+ var words = refusals.Select(x => x.words).ToList();
// Every kind refused for one reason (not land, a placement there, the turn's limit): said once, with no buttons,
// as the map actions do (#398). A kind this city lacks the resource for (R-PROJ-005: the winery outside Rishon
// LeZion) does not split that line into one per kind, nor does the gdd-0.3 housing repair off its city's own hex
// (R-HOUSE-006: repair_city_hex comes before the shared reasons, so it would hide them on every other hex).
- var shared = words.Where((x, i) => refusals[i]?.Kind != "no_resource" && refusals[i]?.Kind != "repair_city_hex").ToList();
+ var shared = words.Where((x, i) => refusals[i].kind != "no_resource" && refusals[i].kind != "repair_city_hex").ToList();
if (words.All(x => x != null) && shared.Count > 0 && shared.Distinct().Count() == 1) { Line(list, "city-project-reason", shared[0]!); return; }
// A kind the sim refuses shows its reason alone, which names the kind (no disabled Build above it: one line, not two).
for (int i = 0; i < kinds.Count; i++)
diff --git a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
index fbaea0142..fd971e49a 100644
--- a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
+++ b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
@@ -11,7 +11,8 @@ namespace Statecraft.Game.UI
{
/// <summary>The info panel (bottom-left): the selected hex's land, height and the settlements it owns, what the player
/// built there, and the two map actions (transit camp, housing project) with the reason when one is not possible
- /// (MapActions.Check). Tile yields are deliberately not shown (owner decision, M3 rulings 2). A selected unit
+ /// (MapActions.CheckReason and Simulation.Preview of the exact queue a press would leave). Tile yields are deliberately not
+ /// shown (owner decision, M3 rulings 2). A selected unit
/// (<see cref="ShowUnit"/>) shows its card instead (<see cref="UnitCard"/>), with a button to the Defence screen (owner
/// ruling, 2026-10-11) and, in the GDD loop, the Move, Fortify, Hold and Attack orders (GDD Phase 5 PR 15, #754),
/// and Send home after the unit's front signs its armistice (#1309).</summary>
@@ -980,10 +981,37 @@ namespace Statecraft.Game.UI
Control1948.LineDateText(here.Date.ToString("yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), k => text[k])) : ""; // PlayerText.Month is always English (#701)
// The GDD loop refuses every map action and builds with city projects instead (R-PROJ-004): it has no map-action
// button and no reason, so its refusal is not asked for (the UI has no words for it).
- var campReason = c.IsGdd ? null : MapActions.CheckReason(c, s, MapActions.TransitCamp, q, r);
- var housingReason = c.IsGdd ? null : MapActions.CheckReason(c, s, MapActions.Housing, q, r);
- var campWhy = campReason == null ? null : MapActionReasonText(campReason, text);
- var housingWhy = housingReason == null ? null : MapActionReasonText(housingReason, text);
+ string? Refusal(string action)
+ {
+ // SimBridge.Queue replaces this hex's order in place, or appends it. Preview that exact queue after the committed
+ // orders. This costs one preview for each map-action kind the panel considers while the selected-hex panel renders,
+ // never from a per-frame callback; an earlier order can make an action that the current state refuses valid.
+ var press = new MapAction(action, q, r);
+ var old = bridge.QueuedLike(press);
+ var queue = bridge.Queued.ToList();
+ var slot = old == null ? -1 : queue.IndexOf(old);
+ // An action the Attention left cannot pay for keeps its button, as before the preview: the bridge refuses the
+ // press and says why (order.no_attention, SimBridge.Queue). Only the rules are asked, on the state the rest of
+ // the queue leaves (the press replaces this hex's order, so that order is left out too).
+ if (!bridge.CanPay(press))
+ {
+ if (slot >= 0) queue.RemoveAt(slot);
+ var rule = MapActions.CheckReason(c, Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(queue).ToList()).State,
+ action, q, r);
+ return rule == null ? null : MapActionReasonText(rule, text);
+ }
+ if (slot >= 0) queue[slot] = press; else queue.Add(press);
+ var preview = Simulation.Preview(c, bridge.TurnStart, bridge.Session.Committed.Concat(queue).ToList());
+ var rejected = preview.Rejected.FirstOrDefault(x => ReferenceEquals(x.Command, press));
+ if (rejected == null) return null;
+
+ var exact = MapActions.CheckReason(c, preview.State, action, q, r);
+ return exact != null ? MapActionReasonText(exact, text)
+ : SimBridge.ReasonText(text, rejected.Reason, c, bridge.TownNamesHe);
+ }
+
+ var campWhy = c.IsGdd ? null : Refusal(MapActions.TransitCamp);
+ var housingWhy = c.IsGdd ? null : Refusal(MapActions.Housing);
// eras.yaml systems.map_actions (#58): an action the era does not allow has no button and no reason
var allowed = c.IsGdd ? new System.Collections.Generic.List<string>() : c.SystemsAt(s.Date).MapActions;
bool campOn = allowed.Contains(MapActions.TransitCamp), housingOn = allowed.Contains(MapActions.Housing);
diff --git a/unity/Assets/Tests/EditMode/CityProjectPanelTests.cs b/unity/Assets/Tests/EditMode/CityProjectPanelTests.cs
index 12a89d41f..28e807c78 100644
--- a/unity/Assets/Tests/EditMode/CityProjectPanelTests.cs
+++ b/unity/Assets/Tests/EditMode/CityProjectPanelTests.cs
@@ -290,6 +290,30 @@ public class CityProjectPanelTests
finally { C.ProjectKinds.Remove(winery); }
}
+ // Red first on PR #2679 head 4e25f657c: Refusal returns the current state's ma'abarot-policy reason before it previews
+ // the earlier SetPolicy order, so the transit-camp button is absent even though End turn would accept the start.
+ [Test]
+ public void AQueuedMaabarotPolicyEnablesATransitCampProject()
+ {
+ var (q, r) = Lone;
+ var bridge = Bridge(En, s => s.CoalitionStability = 1);
+ Assert.IsTrue(PoliticsSystem.HasMajority(C, bridge.State.Date), "precondition: the policy vote has a majority");
+ Assert.AreEqual("maabarot_policy", Projects.CheckCityReason(C, bridge.State, MapActions.TransitCamp, City, q, r)?.Kind,
+ "precondition: without the queued policy the current state refuses the camp");
+ Assert.IsNull(bridge.Queue(new SetPolicy(MapActions.MaabarotPolicy, 1)));
+ Assert.AreEqual(1.0, Simulation.Preview(C, bridge.TurnStart,
+ bridge.Session.Committed.Concat(bridge.Queued).ToList()).State.Policies[MapActions.MaabarotPolicy],
+ "precondition: the policy vote passes before the project runs");
+
+ using var ui = new UiTestKit();
+ using var panel = Panel(ui, bridge, En);
+ panel.Show(q, r);
+
+ Assert.IsTrue(ui.Q<Button>("city-project-" + MapActions.TransitCamp).enabledSelf);
+ Assert.IsNull(ui.Root.Q("city-project-reason"));
+ Assert.IsNull(ui.Root.Q("city-project-" + MapActions.TransitCamp + "-reason"));
+ }
+
[Test]
public void AnOrderTheAttentionCannotPayIsRefusedByTheBridge()
{
@@ -307,6 +331,36 @@ public class CityProjectPanelTests
Assert.AreEqual(new[] { En.Format("order.no_attention", bridge.Price(order)!.Value, 0.0) }, refusals);
}
+ // A guard for the Attention path (#2679 review): a start the Attention left cannot pay for keeps its Build buttons only
+ // where the rules allow it. With the queued start the turn's limit is reached, so the turn_cap reason shows instead.
+ [Test]
+ public void WithNoAttentionLeftTheQueuedStartsTurnLimitStillShows()
+ {
+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ var s = g.State;
+ s.Policies[MapActions.MaabarotPolicy] = 1; // so the transit camp is not refused for the policy first
+ var hexes = C.Hexes.Where(h => h.Land
+ && Projects.CitiesFor(C, s, h.Q, h.R).SequenceEqual(new[] { City })
+ && Projects.CheckCityReason(C, s, "housing", City, h.Q, h.R) == null).OrderBy(h => h.Q).ThenBy(h => h.R).Take(2).ToList();
+ Assert.AreEqual(2, hexes.Count, "precondition: two hexes only Haifa reaches where it may build");
+ var cap = C.RulesAt(s.Date).MapActionsPerTurn;
+ Assert.Greater(cap, 0, "precondition: the turn allows a start");
+ s.MapActionsTurnDate = s.Date; s.MapActionsThisTurn = cap - 1;
+ s.Attention = AttentionSystem.Price(C, s, new StartProject("housing", City, hexes[0].Q, hexes[0].R));
+
+ var bridge = new SimBridge(g, En);
+ using var ui = new UiTestKit();
+ using var panel = Panel(ui, bridge, En);
+ panel.Show(hexes[0].Q, hexes[0].R);
+ ui.Press("city-project-housing");
+ Assert.AreEqual(1, bridge.Queued.OfType<StartProject>().Count(), "precondition: the Attention paid for the first start");
+ Assert.IsFalse(bridge.CanPay(new StartProject("housing", City, hexes[1].Q, hexes[1].R)), "precondition: none is left for a second");
+
+ panel.Show(hexes[1].Q, hexes[1].R);
+ Assert.AreEqual(En.Format("city.project_reason.turn_cap", cap), ui.Q<Label>("city-project-reason").text);
+ Assert.IsNull(ui.Root.Q("city-project-housing"), "shared refusal: one reason and no Build buttons");
+ }
+
[Test]
public void AHexNoCityReachesShowsNoProjects()
{
@@ -411,6 +465,34 @@ public class WorkshopPanelTests
static AssignWorkers? Queued(SimBridge b) => b.QueuedLike(new AssignWorkers(City, Workshops.Role, 0)) as AssignWorkers;
+ // Red first on origin/main 0c976fb21: after the first press, the second hex still offers enabled Build buttons because the
+ // panel checks the turn-start state without previewing the queued start. Both hexes have exactly Haifa as their first (and
+ // only) Projects.CitiesFor result, so the two UI positions exercise the same city's per-turn cap.
+ [Test]
+ public void AQueuedStartDisablesASecondStartForTheSameCityOnAnotherHex()
+ {
+ var g = GameSession.New(C03, GoldenRun.Seed, Mode.Historical);
+ g.State.Policies[MapActions.MaabarotPolicy] = 1;
+ var hexes = C03.Hexes.Where(h => h.Land
+ && Projects.CitiesFor(C03, g.State, h.Q, h.R).SequenceEqual(new[] { City })
+ && Projects.CheckCityReason(C03, g.State, "industry", City, h.Q, h.R) == null).Take(2).ToList();
+ Assert.AreEqual(2, hexes.Count, "precondition: two hexes whose first city is Haifa");
+
+ var bridge = new SimBridge(g, En) { TownNamesHe = TownsHe };
+ using var ui = new UiTestKit();
+ using var panel = Panel(ui, bridge, En);
+ panel.Show(hexes[0].Q, hexes[0].R);
+ ui.Press("city-project-industry");
+ Assert.AreEqual(City, bridge.Queued.OfType<StartProject>().Single().City,
+ "the first UI press queues Haifa's start");
+ Assert.IsNull(Projects.CheckCityReason(C03, bridge.State, "housing", City, hexes[1].Q, hexes[1].R),
+ "the pre-queue state alone still permits a second start");
+
+ panel.Show(hexes[1].Q, hexes[1].R);
+ Assert.AreEqual(En.Format("city.project_reason.city_cap", Towns[City]), ui.Q<Label>("city-project-reason").text);
+ Assert.IsNull(ui.Root.Q("city-project-housing"), "shared refusal: one reason and no generated Build buttons");
+ }
+
// R-ARMS-005: the control shows on a built arms workshop under gdd-0.3, and nowhere else: not on a workshop being
// built, and not on another built kind. The gdd-0.1 row shows that no gdd-0.1 kind shows it: the loader keeps every
// kind with a worker slot out of gdd-0.1 (R-ARMS-004), so its industry has none. It does not prove WorkshopPart's
diff --git a/unity/Assets/Tests/EditMode/MapActionUiTests.cs b/unity/Assets/Tests/EditMode/MapActionUiTests.cs
index 38b233324..e1ef226c4 100644
--- a/unity/Assets/Tests/EditMode/MapActionUiTests.cs
+++ b/unity/Assets/Tests/EditMode/MapActionUiTests.cs
@@ -99,6 +99,64 @@ public class MapActionUiTests
Assert.AreEqual("A transit camp built here in August 1950", ui.Q<Label>("info-placement").text);
}
+ // Red first on PR #2679 head 0401491db: the whole-queue preview rejected the unaffordable press for its Attention, so both
+ // buttons were hidden behind one shared reason. As on origin/main 0c976fb21, an action the Attention left cannot pay for
+ // keeps its button and the bridge refuses the press with order.no_attention (SimBridge.Queue).
+ [Test]
+ public void NoAttentionKeepsMapActionsAndTheBridgeRefusesTheirPresses()
+ {
+ using var ui = new UiTestKit();
+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ while (g.State.Date < new DateTime(1950, 8, 1)) g.Step(HistoricalAI.Commands(C, g.State));
+ g.State.Attention = 0;
+ var bridge = new SimBridge(g, Text);
+ var refusals = new System.Collections.Generic.List<string>();
+ bridge.Refused += refusals.Add;
+ using var info = Panel(ui, bridge);
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ var orders = new[] { new MapAction(MapActions.TransitCamp, tlv.Q, tlv.R), new MapAction(MapActions.Housing, tlv.Q, tlv.R) };
+ foreach (var order in orders)
+ {
+ Assert.IsNull(MapActions.CheckReason(C, bridge.State, order.Action, tlv.Q, tlv.R), order.Action + ": otherwise legal on this hex");
+ Assert.IsFalse(bridge.CanPay(order), order.Action + ": precondition: no Attention left");
+ }
+
+ info.Show(tlv.Q, tlv.R);
+
+ foreach (var name in new[] { "build-camp", "build-housing" })
+ {
+ Assert.IsTrue(ui.Q<Button>(name).enabledSelf, name + ": the rules allow it, so the button stays");
+ ui.Press(name);
+ }
+ Assert.IsEmpty(bridge.Queued, "the bridge queues no order it cannot pay for");
+ Assert.AreEqual(orders.Select(o => Text.Format("order.no_attention", bridge.Price(o)!.Value, 0.0)).ToArray(), refusals);
+ }
+
+ // Red first on PR #2679 head 4e25f657c: Refusal returns the current state's ma'abarot-policy reason before it previews
+ // the earlier SetPolicy order, so the transit-camp button is disabled even though End turn would accept the action.
+ [Test]
+ public void AQueuedMaabarotPolicyEnablesATransitCampMapAction()
+ {
+ using var ui = new UiTestKit();
+ var bridge = At(new DateTime(1950, 8, 1));
+ bridge.TurnStart.Policies[MapActions.MaabarotPolicy] = 0;
+ bridge.TurnStart.CoalitionStability = 1;
+ Assert.IsTrue(PoliticsSystem.HasMajority(C, bridge.TurnStart.Date), "precondition: the policy vote has a majority");
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ Assert.AreEqual("maabarot_policy", MapActions.CheckReason(C, bridge.State, MapActions.TransitCamp, tlv.Q, tlv.R)?.Kind,
+ "precondition: without the queued policy the current state refuses the camp");
+ Assert.IsNull(bridge.Queue(new SetPolicy(MapActions.MaabarotPolicy, 1)));
+ Assert.AreEqual(1.0, Simulation.Preview(C, bridge.TurnStart,
+ bridge.Session.Committed.Concat(bridge.Queued).ToList()).State.Policies[MapActions.MaabarotPolicy],
+ "precondition: the policy vote passes before the map action runs");
+
+ using var info = Panel(ui, bridge);
+ info.Show(tlv.Q, tlv.R);
+
+ Assert.IsTrue(ui.Q<Button>("build-camp").enabledSelf);
+ Assert.AreEqual("", ui.Q<Label>("info-reason").text);
+ }
+
/// <summary>#701: in the Hebrew UI the build date names its month in Hebrew (date.month_year), not "August 1950". The line
/// is a date label with no verb (Beri, 17 October 2026), since a fixed "נבנה" is wrong for a feminine name ("מעברה").</summary>
[Test]
@@ -243,19 +301,30 @@ public class MapActionUiTests
foreach (var word in new[] { "yield", "food", "production", "gold", "income" }) StringAssert.DoesNotContain(word, shown);
}
+ // Red first on origin/main 0c976fb21: the third hex's buttons remain enabled because the panel checks the turn-start state,
+ // so a third press enters the queue and is refused only at End turn.
[Test]
- public void ThePerTurnCapAndRefusalsComeFromTheSim()
+ public void ThePerTurnCapIncludesActionsAlreadyQueuedThisTurn()
{
using var ui = new UiTestKit();
var bridge = At(new DateTime(1950, 8, 1));
- new ToastPresenter(ui.Root, bridge, Text);
- var info = Panel(ui, bridge);
- var hexes = C.SettlementHexes.Select(h => (h.Q, h.R)).Distinct().Take(3).ToList();
- foreach (var (q, r) in hexes) { info.Show(q, r); ui.Press("build-housing"); }
- Assert.AreEqual(3, bridge.Queued.OfType<MapAction>().Count()); // the sim, not the UI, enforces the cap at End Turn
- bridge.EndTurn();
- Assert.AreEqual(1, bridge.Session.Last!.Rejected.Count);
- StringAssert.Contains($"No more than {C.Rules.MapActionsPerTurn} map actions in one turn", ui.Q<Label>("toast").text);
+ using var info = Panel(ui, bridge);
+ var cap = C.RulesAt(bridge.State.Date).MapActionsPerTurn;
+ Assert.AreEqual(2, cap, "precondition: the legacy map-action cap");
+ var hexes = C.SettlementHexes.Select(h => (h.Q, h.R)).Distinct().Take(cap + 1).ToList();
+ Assert.AreEqual(cap + 1, hexes.Count, "precondition: one distinct target beyond the cap");
+ foreach (var (q, r) in hexes.Take(cap)) { info.Show(q, r); ui.Press("build-housing"); }
+ Assert.AreEqual(cap, bridge.Queued.OfType<MapAction>().Count());
+
+ info.Show(hexes[cap].Q, hexes[cap].R);
+ Assert.AreEqual(Text.Format("info.map_action_reason.turn_cap", cap), ui.Q<Label>("info-reason").text);
+ foreach (var name in new[] { "build-camp", "build-housing" })
+ {
+ Assert.AreEqual(DisplayStyle.None, ui.Q<Button>(name).style.display.value, name + ": shared refusal hides the button");
+ Assert.IsFalse(ui.Q<Button>(name).enabledSelf, name + ": shared refusal disables the button");
+ }
+ Assert.AreEqual(cap, bridge.Queued.OfType<MapAction>().Count(), "the unavailable third action was not queued");
+ Assert.IsEmpty(bridge.EndTurn().Rejected);
}
[Test]