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.

Defence screen: never queue an Arms purchase End turn would refuse for the debt ceiling or stock capacity (Fixes #2590) (#2612)

Eyal Golan0803cdf95c09
diff --git a/changelog.d/2590.md b/changelog.d/2590.md
new file mode 100644
index 000000000..3f7d7b93a
--- /dev/null
+++ b/changelog.d/2590.md
@@ -0,0 +1,4 @@
+### Fixed
+
+- The Defence screen no longer queues an Arms purchase that End turn would refuse
+ for the debt ceiling or stock capacity.
diff --git a/sim/Statecraft.Sim.Tests/PoolsTests.cs b/sim/Statecraft.Sim.Tests/PoolsTests.cs
index 703a69c73..6b517cef0 100644
--- a/sim/Statecraft.Sim.Tests/PoolsTests.cs
+++ b/sim/Statecraft.Sim.Tests/PoolsTests.cs
@@ -285,7 +285,8 @@ public class PoolsTests
var c = Gdd();
var s = Simulation.NewGame(c, 1, Mode.Historical);
var after = s.Clone();
- Assert.Equal("That is more Arms than the stock can hold", Pools.BuyArms(c, after, int.MaxValue, new List<Cause>()));
+ Assert.Equal(Pools.RefusedStockCapacity, Pools.BuyArms(c, after, int.MaxValue, new List<Cause>()));
+ Assert.Equal(Pools.RefusedStockCapacity, Pools.BuyArmsRefusal(c, s, Array.Empty<Command>(), int.MaxValue));
Assert.Equal(s.Hash(), after.Hash());
}
diff --git a/sim/Statecraft.Sim/Pools.cs b/sim/Statecraft.Sim/Pools.cs
index 0537bea2a..b3d9430fd 100644
--- a/sim/Statecraft.Sim/Pools.cs
+++ b/sim/Statecraft.Sim/Pools.cs
@@ -39,6 +39,9 @@ namespace Statecraft.Sim
/// <summary>R-ARMS-002's refusal at the debt ceiling (#2198); SimBridge.ReasonKeys gives it the UI's words.</summary>
public const string RefusedDebtCeiling = "Buying that many Arms would take the debt past its ceiling";
+ /// <summary>R-ARMS-002's refusal when a purchase would overflow the Arms stock.</summary>
+ public const string RefusedStockCapacity = "That is more Arms than the stock can hold";
+
/// <summary>R-ARMS-002 (D-G6): <paramref name="count"/> Arms join the stock at once, and the Treasury pays
/// count * arms_price, borrowing what it lacks (R-MONEY-003). Refused, state untouched, for fewer than one unit, a
/// stock that would pass a whole number's range, or borrowing that would take the debt past mvp.debt_ceiling (#2198,
@@ -49,7 +52,7 @@ namespace Statecraft.Sim
{
PopulationLedger.RefuseAggregate(s);
if (count < 1) return "Buy at least 1 Arms";
- if ((long)s.Arms + count > int.MaxValue) return "That is more Arms than the stock can hold";
+ if ((long)s.Arms + count > int.MaxValue) return RefusedStockCapacity;
if (BorrowsPastDebtCeiling(c, s, count)) return RefusedDebtCeiling;
var cost = count * c.RulesAt(s.Date).Mvp!.ArmsPrice!.Value;
EconomySystem.PayOrBorrow(c, s, cost, "for Arms", why, CauseKeys.Purpose.Arms);
@@ -68,20 +71,24 @@ namespace Statecraft.Sim
return left < 0 && s.Debt - left > r.DebtCeiling!.Value; // the loader requires both in the GDD loop
}
- /// <summary>#2206: whether End turn would refuse <paramref name="count"/> Arms for the ceiling (BuyArms, the check above)
- /// if it ran <paramref name="orders"/>, the turn's orders, from <paramref name="turnStart"/>, after Buy queued them: the
- /// purchase takes a queued purchase's slot, else goes last (SimBridge.Queue). The orders run on a copy through
- /// Simulation.Preview, End turn's own order phase (RunOrders: conversions before the purchase, Attention, refusals),
- /// so everything queued before the purchase counts as End turn counts it. The Defence screen's warning.</summary>
- public static bool BorrowsPastDebtCeiling(Content c, GameState turnStart, IReadOnlyList<Command> orders, int count)
+ /// <summary>Returns the reason End turn would refuse <paramref name="count"/> Arms if it ran
+ /// <paramref name="orders"/>, the turn's orders, from <paramref name="turnStart"/>, after Buy queued them. The purchase
+ /// takes a queued purchase's slot, else goes last (SimBridge.Queue). The orders run on a copy through Simulation.Preview,
+ /// End turn's own order phase (RunOrders: conversions before the purchase, Attention, refusals), so everything queued
+ /// before the purchase counts as End turn counts it. The Defence screen's warning and press guard.</summary>
+ public static string? BuyArmsRefusal(Content c, GameState turnStart, IReadOnlyList<Command> orders, int count)
{
var buy = new BuyArms(count);
var batch = orders.ToList();
var slot = batch.FindIndex(x => x is BuyArms);
if (slot >= 0) batch[slot] = buy; else batch.Add(buy);
- return Simulation.Preview(c, turnStart, batch).Rejected.Any(r => ReferenceEquals(r.Command, buy) && r.Reason == RefusedDebtCeiling);
+ return Simulation.Preview(c, turnStart, batch).Rejected.FirstOrDefault(r => ReferenceEquals(r.Command, buy))?.Reason;
}
+ /// <summary>#2206: whether End turn would refuse the replacement Arms purchase for the debt ceiling.</summary>
+ public static bool BorrowsPastDebtCeiling(Content c, GameState turnStart, IReadOnlyList<Command> orders, int count)
+ => BuyArmsRefusal(c, turnStart, orders, count) == RefusedDebtCeiling;
+
/// <summary>The nation's need for one turn: its head count in population units, rounded half away from zero.</summary>
static int Need(GameState s, MvpRules r) => (int)Math.Round(s.TotalPopulation / r.PeoplePerUnit, MidpointRounding.AwayFromZero);
diff --git a/unity/Assets/Scripts/UI/ArmsPurchasePresenter.cs b/unity/Assets/Scripts/UI/ArmsPurchasePresenter.cs
index 5ec6e229c..721e6e4eb 100644
--- a/unity/Assets/Scripts/UI/ArmsPurchasePresenter.cs
+++ b/unity/Assets/Scripts/UI/ArmsPurchasePresenter.cs
@@ -28,9 +28,11 @@ namespace Statecraft.Game.UI
buy.text = text["arms.buy"]; withdraw.text = text["arms.withdraw"];
onBuy = () =>
{
+ // An amount End turn already refuses for capacity or the debt ceiling is never queued.
+ if (EndTurnRefusal() != null) { Render(); return; }
var reason = bridge.Queue(new BuyArms(count.value));
Render();
- // Only Queue's own refusal replaces Render's line: an accepted order keeps its warning (the debt ceiling, #2198).
+ // Only Queue's own refusal replaces Render's line.
if (reason != null) panel.Q<Label>("arms-refusal").text = reason;
};
presses = new PressSource(panel);
@@ -50,6 +52,18 @@ namespace Statecraft.Game.UI
Render();
}
+ string? EndTurnRefusal()
+ {
+ var reason = Pools.BuyArmsRefusal(
+ bridge.Content,
+ bridge.TurnStart,
+ bridge.Session.Committed.Concat(bridge.Queued).ToList(),
+ count.value);
+ return reason == Pools.RefusedDebtCeiling || reason == Pools.RefusedStockCapacity
+ ? SimBridge.ReasonText(text, reason)
+ : null;
+ }
+
public void Render()
{
var c = bridge.Content; var s = bridge.State;
@@ -72,10 +86,9 @@ namespace Statecraft.Game.UI
panel.Q<Label>("arms-refusal").text = s.GameOver != null ? text["order.game_over"]
: count.value < 1 ? text["arms.minimum"]
: !bridge.CanPay(order) ? text["arms.no_attention"]
- // #2198, #2206: End turn refuses this amount for the ceiling (Pools.BuyArms) with the turn's orders as they stand:
- // the committed then queued ones, from the turn's start, as End turn runs them.
- : Pools.BorrowsPastDebtCeiling(c, bridge.TurnStart, bridge.Session.Committed.Concat(bridge.Queued).ToList(), count.value)
- ? text["arms.refused_debt_ceiling"] : "";
+ // #2198, #2206, #2590: End turn refuses this amount for the ceiling or capacity (Pools.BuyArms) with the turn's
+ // orders as they stand: the committed then queued ones, from the turn's start, as End turn runs them.
+ : EndTurnRefusal() ?? "";
}
public void Dispose()
diff --git a/unity/Assets/Scripts/UI/SimBridge.cs b/unity/Assets/Scripts/UI/SimBridge.cs
index 20464d97f..5176fdf0e 100644
--- a/unity/Assets/Scripts/UI/SimBridge.cs
+++ b/unity/Assets/Scripts/UI/SimBridge.cs
@@ -205,7 +205,7 @@ namespace Statecraft.Game.UI
return Unknown("city_project", p.ProjectKind, Content.ProjectKinds.Select(x => x.Id)) ?? Unknown("city", p.City, Content.Cities.Select(x => x.Id));
case CancelProject x:
return Unknown("city_project", x.Project.ToString(System.Globalization.CultureInfo.InvariantCulture), State.CityProjects.Keys.Select(k => k.ToString(System.Globalization.CultureInfo.InvariantCulture)));
- // Buying Arms (#781): no ids; the sim refuses a count below one, and borrowing past the debt ceiling (#2198), itself.
+ // Buying Arms (#781): no ids; the sim refuses the count, stock capacity and debt ceiling itself.
case BuyArms _: return null;
// Answering a capture (#797): the sim refuses a place not awaiting the choice, or an outcome not built yet, with
// its own reason and never throws on the id. The capture dialog is PR 10.
@@ -218,7 +218,8 @@ namespace Statecraft.Game.UI
}
}
- /// <summary>The sim's English refusals that have a UI string: Orders.Check's (#2045) and the Arms purchase refused at the debt ceiling (#2198).
+ /// <summary>The sim's English refusals that have a UI string: Orders.Check's (#2045) and the Arms-purchase refusals
+ /// (#2198, #2590).
/// Keyed on the English, so the same words from another check (Movement, Combat, Training, Demobilization give "That
/// unit is not yours", "There is no such unit" too) read in the UI's language as well.</summary>
static readonly Dictionary<string, string> ReasonKeys = new Dictionary<string, string>
@@ -245,6 +246,7 @@ namespace Statecraft.Game.UI
[Training.RefusedNoTraining] = "unit.reinforce_refused.no_training",
[Training.RefusedCount] = "unit.reinforce_refused.count",
[Pools.RefusedDebtCeiling] = "arms.refused_debt_ceiling",
+ [Pools.RefusedStockCapacity] = "arms.refused_stock_capacity",
[Orders.RefusedCardOrder] = "unit.order_refused.card_order", // R-ENEMY-005: an operation card's refused go-to
[Orders.RefusedLastGarrison] = "unit.order_refused.last_garrison", // R-CTRL-013: a card's go-to held back
};
diff --git a/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs b/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs
index f3b683c11..5776f595c 100644
--- a/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs
+++ b/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs
@@ -222,7 +222,7 @@ public class FoodArmsHudTests
}
// #2198: the panel says ahead of End turn when the amount would borrow past the debt ceiling, in the sim's own check;
- // exactly at the ceiling says nothing. Buy stays available, and the sim decides when the turn runs.
+ // exactly at the ceiling says nothing. Buy stays available to explain the refusal, but queues nothing.
[TestCase(false, false)] [TestCase(false, true)]
[TestCase(true, false)] [TestCase(true, true)]
public void TheAmountThatWouldBorrowPastTheDebtCeilingIsExplained(bool modern, bool he)
@@ -239,16 +239,34 @@ public class FoodArmsHudTests
count.value = 2;
Assert.AreEqual(t["arms.refused_debt_ceiling"], ui.Q<Label>("arms-refusal").text);
Assert.IsTrue(ui.Q<Button>("buy-arms").enabledSelf, "a refusal remains explainable");
- // Pressing Buy queues the order (the sim decides at End turn) and keeps the warning on screen (review of #2203).
ui.Press("buy-arms");
- Assert.AreEqual(2, ((BuyArms)b.Queued.Single()).Count);
- Assert.AreEqual(t["arms.refused_debt_ceiling"], ui.Q<Label>("arms-refusal").text, "Buy does not clear the warning");
- ui.Press("withdraw-arms");
+ Assert.IsEmpty(b.Queued);
+ Assert.AreEqual(t["arms.refused_debt_ceiling"], ui.Q<Label>("arms-refusal").text);
b.State.Treasury = 2 * r.ArmsPrice!.Value;
count.value = 1; count.value = 2;
Assert.IsEmpty(ui.Q<Label>("arms-refusal").text, "paid from the Treasury, nothing is borrowed");
}
+ // #2590 (R-ARMS-002): exactly filling the stock is allowed; an amount that would overflow it remains explainable but is
+ // not queued. Build the Defence screen as GameRoot does and press through its real Buy button.
+ [TestCase(false)] [TestCase(true)]
+ public void AnAmountThatWouldOverflowTheStockIsExplainedAndNotQueued(bool he)
+ {
+ var b = Bridge(false, he); var t = Text(he);
+ using var ui = new UiTestKit();
+ using var defence = new DefencePresenter(ui.Root, b, t);
+ b.State.Arms = int.MaxValue - 1;
+ var count = ui.Q<IntegerField>("arms-count");
+ count.value = 1;
+ Assert.IsEmpty(ui.Q<Label>("arms-refusal").text, "exactly at the stock limit is allowed");
+ count.value = 2;
+ Assert.AreEqual(t["arms.refused_stock_capacity"], ui.Q<Label>("arms-refusal").text);
+ Assert.IsTrue(ui.Q<Button>("buy-arms").enabledSelf, "the refusal remains explainable");
+ ui.Press("buy-arms");
+ Assert.IsEmpty(b.Queued);
+ Assert.AreEqual(t["arms.refused_stock_capacity"], ui.Q<Label>("arms-refusal").text);
+ }
+
// #2206 (R-ARMS-002, R-MONEY-004): the ceiling warning counts the dollar conversion queued this turn, which End turn runs
// before the purchase. On the first turn (Treasury IL 20m, no debt, the IL 250m ceiling, IL 1m an Arms), 271 Arms warn
// alone; after Convert queues $4m they do not, 272 still do, and withdrawing the conversion warns again.
@@ -279,7 +297,7 @@ public class FoodArmsHudTests
// #2206 (R-ARMS-002): the warning runs the whole queue as End turn will, so spending queued before the purchase counts.
// On the first turn one halutz group (IL 0.5m) and $4m (IL 1.332m) leave IL 20.832m: 271 Arms would borrow IL 250.168m,
- // past the IL 250m ceiling, so the screen warns and End turn refuses them; 270 borrow IL 249.168m, and neither does.
+ // past the IL 250m ceiling, so the screen warns and the press queues nothing; 270 borrow IL 249.168m and is queued.
[TestCase(false, false, 270)] [TestCase(false, false, 271)] [TestCase(false, true, 270)] [TestCase(false, true, 271)]
[TestCase(true, false, 270)] [TestCase(true, false, 271)] [TestCase(true, true, 270)] [TestCase(true, true, 271)]
public void TheCeilingWarningCountsTheTrainingQueuedBeforeThePurchase(bool modern, bool he, int arms)
@@ -294,9 +312,9 @@ public class FoodArmsHudTests
var refused = arms == 271;
Assert.AreEqual(refused ? t["arms.refused_debt_ceiling"] : "", ui.Q<Label>("arms-refusal").text);
ui.Press("buy-arms");
- Assert.AreEqual(3, b.Queued.Count);
+ Assert.AreEqual(refused ? 2 : 3, b.Queued.Count);
var turn = b.EndTurn();
- Assert.AreEqual(refused ? new[] { Pools.RefusedDebtCeiling } : new string[0], turn.Rejected.Select(x => x.Reason).ToArray());
+ Assert.IsEmpty(turn.Rejected);
}
[Test]
diff --git a/unity/Assets/Tests/EditMode/HebrewUiTests.cs b/unity/Assets/Tests/EditMode/HebrewUiTests.cs
index 0b5ef2d5d..83b0512ba 100644
--- a/unity/Assets/Tests/EditMode/HebrewUiTests.cs
+++ b/unity/Assets/Tests/EditMode/HebrewUiTests.cs
@@ -104,16 +104,22 @@ public class HebrewUiTests
}
}
- // #2198: the Arms purchase refused at the debt ceiling reaches the end-of-turn toast (ReasonText) in both languages.
+ // #2198, #2590: every localized Arms-purchase refusal reaches the end-of-turn toast (ReasonText) in both languages.
[Test]
- public void TheArmsDebtCeilingRefusalHasAStringInBothLanguages()
+ public void EveryArmsPurchaseRefusalHasAStringInBothLanguages()
{
- var reason = Statecraft.Sim.Pools.RefusedDebtCeiling;
- Assert.AreEqual(reason, SimBridge.ReasonText(En, reason), "the English string is the sim's own");
- var he = SimBridge.ReasonText(He, reason);
- Assert.AreNotEqual(reason, he, "no Hebrew string for: " + reason);
- StringAssert.IsMatch("[א-ת]", he, reason);
- StringAssert.DoesNotMatch("[A-Za-z]", he, reason);
+ foreach (var reason in new[]
+ {
+ Statecraft.Sim.Pools.RefusedDebtCeiling,
+ Statecraft.Sim.Pools.RefusedStockCapacity,
+ })
+ {
+ Assert.AreEqual(reason, SimBridge.ReasonText(En, reason), "the English string is the sim's own");
+ var he = SimBridge.ReasonText(He, reason);
+ Assert.AreNotEqual(reason, he, "no Hebrew string for: " + reason);
+ StringAssert.IsMatch("[א-ת]", he, reason);
+ StringAssert.DoesNotMatch("[A-Za-z]", he, reason);
+ }
}
// #2072: every Combat.Check and Training.Reinforce refusal (their Refused* constants) has a string in both files. Each
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index 293736a61..5098f48c4 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -1048,6 +1048,7 @@ arms.no_attention: "Not enough Attention for this purchase. Withdraw another ord
# #2198 (owner, 2 Nov 2026: "Refuse buys over ceiling (Recommended)"): Pools.BuyArms's refusal, word for word, shown in the
# end-of-turn toast.
arms.refused_debt_ceiling: "Buying that many Arms would take the debt past its ceiling"
+arms.refused_stock_capacity: "That is more Arms than the stock can hold"
# #731 stock summaries in the report and Why.
report.stock_line: "{0}: {1}"
diff --git a/unity/Assets/UI/strings.he.yaml b/unity/Assets/UI/strings.he.yaml
index 875673cfa..4c5c718ff 100644
--- a/unity/Assets/UI/strings.he.yaml
+++ b/unity/Assets/UI/strings.he.yaml
@@ -1268,6 +1268,8 @@ arms.minimum: "יש לרכוש לפחות יחידת נשק אחת. יש להז
arms.no_attention: "אין די תשומת לב לרכישה. יש לבטל הוראה אחרת או להמתין לתור הבא."
# #2198: agent-drafted Hebrew; native review: owner, 2 Nov 2026, "Hebrew is right (Recommended)".
arms.refused_debt_ceiling: "רכישת כמות כזאת של נשק תעלה את החוב מעל התקרה"
+# Agent-drafted Hebrew; for the native read in #564.
+arms.refused_stock_capacity: "כמות זו של נשק גדולה מכפי שהמלאי יכול להכיל"
# #731 stock summaries in the report and Why. Native review: owner, 3 Nov 2026 (HE batch 1: "All fine as they are").
report.stock_line: "{0} במלאי: {1}"