A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
The Arms ceiling check counts the turn's queued dollar conversion (#2204 ruling B, ceiling part) (#2250)
Eyal Golan97ede70c79b5
diff --git a/changelog.d/2206-ceiling-warning-conversion.md b/changelog.d/2206-ceiling-warning-conversion.mdnew file mode 100644index 000000000..5ef1b8ac9--- /dev/null+++ b/changelog.d/2206-ceiling-warning-conversion.md@@ -0,0 +1,3 @@+- The Defence screen's Arms debt-ceiling warning runs the turn's orders as End turn will, so the dollar conversion queued this+ turn (which End turn runs before the purchase) and any spending queued before the purchase count: it warns exactly when End+ turn would refuse the amount for the ceiling (R-ARMS-002, R-MONEY-004; #2206).diff --git a/docs/rules/mvp.md b/docs/rules/mvp.mdindex d1b7aefa7..1eb4ce043 100644--- a/docs/rules/mvp.md+++ b/docs/rules/mvp.md@@ -422,7 +422,7 @@ The national Arms pool (D-G6, decided 15 October 2026: "From events (sourced armR-ARMS-001: Arms is a national stock of whole units (`GameState.Arms`, hashed as `arms=` only when not 0); one Arms is the equipment package for one recruit group of `people_per_unit` (1,000) people. A new game starts the stock at `arms_start` (12). It never goes below 0. `arms_start`, `arms_price` and `arms_per_recruit_group` are required in the GDD loop (`rules.yaml` `mvp:`); a missing or negative value is a load error.-R-ARMS-002: `BuyArms(count)` adds `count` units to the stock at once, with no delivery delay, and the Treasury pays `count * arms_price` (IL 1m an Arms), borrowing what it lacks (R-MONEY-003). It is refused, state and Attention unchanged, for fewer than one unit, a stock that would pass a whole number's range, or a purchase whose borrowing would take the debt past `debt_ceiling` (#2198; owner, 2 November 2026: "Refuse buys over ceiling (Recommended)", https://github.com/eyalgolan/statecraft-israel/issues/2198#issuecomment-6564721486), with the reason "Buying that many Arms would take the debt past its ceiling"; the legacy loop refuses it before charging Attention. The debt after is R-MONEY-003's: the debt plus what the Treasury lacks (`Debt + max(0, cost - Treasury)`). A debt exactly at the ceiling is allowed, and a purchase the Treasury pays in full borrows nothing and is never refused at it, even with the debt already past it. This overrides Q22 for Arms purchases only; every other cost still borrows past the ceiling (R-MONEY-003). The Defence screen shows the same reason before End turn when the amount would be refused at the current Treasury and debt. The turn's dollar conversions (R-MONEY-004) run before its Arms purchases, whatever the queue order: a conversion queued after the turn's first `BuyArms` runs just before it, with the other such conversions in their queue order, and every other order keeps its queue place (#2206). Its Attention cost is `attention_cost.buy_arms` (1), whatever the count.+R-ARMS-002: `BuyArms(count)` adds `count` units to the stock at once, with no delivery delay, and the Treasury pays `count * arms_price` (IL 1m an Arms), borrowing what it lacks (R-MONEY-003). It is refused, state and Attention unchanged, for fewer than one unit, a stock that would pass a whole number's range, or a purchase whose borrowing would take the debt past `debt_ceiling` (#2198; owner, 2 November 2026: "Refuse buys over ceiling (Recommended)", https://github.com/eyalgolan/statecraft-israel/issues/2198#issuecomment-6564721486), with the reason "Buying that many Arms would take the debt past its ceiling"; the legacy loop refuses it before charging Attention. The debt after is R-MONEY-003's: the debt plus what the Treasury lacks (`Debt + max(0, cost - Treasury)`). A debt exactly at the ceiling is allowed, and a purchase the Treasury pays in full borrows nothing and is never refused at it, even with the debt already past it. This overrides Q22 for Arms purchases only; every other cost still borrows past the ceiling (R-MONEY-003). The Defence screen shows the same reason before End turn when End turn would refuse the amount for the ceiling with the turn's orders as they stand: it runs them from the turn's start on a copy, as End turn runs them, with this purchase in place of a queued one or after them, so the dollar conversions and any other spending queued before the purchase count (#2206). The turn's dollar conversions (R-MONEY-004) run before its Arms purchases, whatever the queue order: a conversion queued after the turn's first `BuyArms` runs just before it, with the other such conversions in their queue order, and every other order keeps its queue place (#2206). Its Attention cost is `attention_cost.buy_arms` (1), whatever the count.R-ARMS-003: Recruit training (R-TRAIN-001) takes `arms_per_recruit_group` (1) Arms for each group when it starts, after its other checks and before it pays. A stock short of that refuses the order with a reason naming the need and the stock, state and Attention unchanged (Q-UNIT-3). A gun or armoured car crew takes its type's `training_arms` a group instead, by the same rule (R-UNIT-008). Pioneer training takes no Arms. The cohort keeps the Arms it took, and the state hash writes them with it; cancelling the cohort (R-TRAIN-002) returns them to the pool, up to the most the stock can hold, although the Money stays paid (owner, 18 October 2026, #1047: "Refund on cancel"). Replacement groups (R-TRAIN-004) take their course's Arms by the same rule (owner, 23 October 2026, #1510: "Replacements cost Arms").diff --git a/sim/Statecraft.Sim.Tests/DollarsTests.cs b/sim/Statecraft.Sim.Tests/DollarsTests.csindex 65762b3fb..05aa008c7 100644--- a/sim/Statecraft.Sim.Tests/DollarsTests.cs+++ b/sim/Statecraft.Sim.Tests/DollarsTests.cs@@ -279,13 +279,16 @@ options:// R-MONEY-004, R-ARMS-002 (#2206; owner, 2 November 2026: "Conversion counts first (Recommended)"): a turn's dollar// conversions run before its Arms purchases, so the purchase borrows only what the conversion leaves it short, whatever// the queue order. The real content's first turn: rung 5, Treasury IL 20m, no debt, IL 1m an Arms, IL 0.333 per $.- // Queued before or after $4m (IL 1.332m), 271 Arms give the same turn: the same state, causes and rejections.+ // Queued before or after $4m (IL 1.332m), 271 Arms give the same turn: the same state, causes and rejections. With the+ // IL 250m debt ceiling (#2203), 271 Arms alone borrow IL 251m and are refused; after the conversion they borrow IL 249.668m,+ // so a purchase queued BEFORE the conversion is accepted too.[Fact]public void AConversionCountsForAnArmsPurchaseWhateverTheQueueOrder(){var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);var m = Real.RulesAt(s.Date).Mvp!;- Assert.Equal((5, 20.0, 0.0, 1.0, 0.333), (s.Rung, s.Treasury, s.Debt, m.ArmsPrice!.Value, m.IlPerDollarOn(s.Date)));+ Assert.Equal((5, 20.0, 0.0, 250.0, 1.0, 0.333), (s.Rung, s.Treasury, s.Debt, m.DebtCeiling!.Value, m.ArmsPrice!.Value, m.IlPerDollarOn(s.Date)));+ Assert.Equal(Pools.RefusedDebtCeiling, Assert.Single(Simulation.Step(Real, s, new Command[] { new BuyArms(271) }).Rejected).Reason);var first = Simulation.Step(Real, s, new Command[] { new ConvertDollars(4), new BuyArms(271) });var last = Simulation.Step(Real, s, new Command[] { new BuyArms(271), new ConvertDollars(4) });Assert.Empty(first.Rejected);@@ -300,6 +303,97 @@ options:Assert.Equal(271 - 20 - 4 * 0.333, orders.Debt, 9);var none = Simulation.Step(Real, s, Array.Empty<Command>()).State;Assert.Equal(none.Treasury - none.Debt - 271 + 4 * 0.333, last.State.Treasury - last.State.Debt, 9);+ // One Arms more passes the ceiling in either order, refused with the same reason, and the conversion stands.+ var over = Simulation.Step(Real, s, new Command[] { new BuyArms(272), new ConvertDollars(4) });+ var overFirst = Simulation.Step(Real, s, new Command[] { new ConvertDollars(4), new BuyArms(272) });+ Assert.Equal(Pools.RefusedDebtCeiling, Assert.Single(over.Rejected).Reason);+ Assert.Equal(Pools.RefusedDebtCeiling, Assert.Single(overFirst.Rejected).Reason);+ Assert.Equal(overFirst.State.Hash(), over.State.Hash());+ Assert.Equal(4, over.State.DollarsConverted);+ }++ // R-ARMS-002, R-MONEY-004 (#2206): the Defence screen's ceiling warning counts the conversions queued this turn, as End+ // turn does (Pools.BorrowsPastDebtCeiling with the queue): after $4m, 271 Arms are allowed and 272 refused. A conversion+ // End turn refuses (past the month's $4m) counts for nothing.+ [Fact]+ public void TheCeilingCheckCountsTheQueuedConversionsAsEndTurnDoes()+ {+ var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);+ // BuyArms(1) is the purchase already queued; the amount checked replaces it in its slot, as Buy does (SimBridge.Queue).+ var four = new Command[] { new BuyArms(1), new ConvertDollars(4) };+ Assert.True(Pools.BorrowsPastDebtCeiling(Real, s, Array.Empty<Command>(), 271));+ Assert.False(Pools.BorrowsPastDebtCeiling(Real, s, four, 271));+ Assert.True(Pools.BorrowsPastDebtCeiling(Real, s, four, 272));+ Assert.True(Pools.BorrowsPastDebtCeiling(Real, s, new Command[] { new ConvertDollars(5) }, 271));+ foreach (var count in new[] { 271, 272 })+ Assert.Equal(Pools.BorrowsPastDebtCeiling(Real, s, four, count),+ Simulation.Step(Real, s, new Command[] { new BuyArms(count), new ConvertDollars(4) }).Rejected.Any(x => x.Reason == Pools.RefusedDebtCeiling));+ Assert.Equal((20.0, 0), (s.Treasury, s.DollarsConverted)); // the queue is judged on a copy+ }++ // R-ARMS-002 (#2206): the warning runs the whole queue as End turn will, so spending queued before the purchase counts+ // too. On the first turn (Treasury IL 20m, no debt, the IL 250m ceiling, IL 1m an Arms, IL 0.333 per $) one halutz group+ // (IL 0.5m, paid or borrowed at once) and $4m (IL 1.332m) leave IL 20.832m: 271 Arms borrow IL 250.168m, past the+ // ceiling, so End turn refuses them; 270 borrow IL 249.168m and pass. Counting only the conversion said IL 249.668m.+ [Fact]+ public void TheCeilingCheckCountsTheSpendingQueuedBeforeThePurchase()+ {+ var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);+ Assert.Equal(0.5, Real.UnitTypes.Single(t => t.Id == "halutz").TrainingCost);+ var queued = new Command[] { new Train("tel_aviv", "halutz", 1), new ConvertDollars(4) };+ Assert.True(Pools.BorrowsPastDebtCeiling(Real, s, queued, 271));+ Assert.Equal(Pools.RefusedDebtCeiling, Assert.Single(Simulation.Step(Real, s, queued.Append(new BuyArms(271)).ToList()).Rejected).Reason);+ Assert.False(Pools.BorrowsPastDebtCeiling(Real, s, queued, 270));+ Assert.Empty(Simulation.Step(Real, s, queued.Append(new BuyArms(270)).ToList()).Rejected);+ // The conversion alone: 271 Arms borrow IL 249.668m and pass.+ Assert.False(Pools.BorrowsPastDebtCeiling(Real, s, new Command[] { new ConvertDollars(4) }, 271));+ }++ // #2206: for each queue on the Defence screen, the warning says "refused" for exactly the amounts End turn refuses for the+ // ceiling. Each row gives the queue and the batch End turn runs after Buy queues n Arms: the purchase takes a queued+ // purchase's slot, else goes last (SimBridge.Queue). The first refused amount is 271 - (IL 0.5m a halutz group, IL 2m+ // for industry, paid before the purchase) + 1 for the $4m (IL 1.332m), rounded down.+ [Fact]+ public void TheCeilingWarningAgreesWithEndTurnOnEveryQueue()+ {+ var s = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);+ var hash = s.Hash();+ Command One() => new Train("tel_aviv", "halutz", 1);+ Command Ten() => new Train("tel_aviv", "halutz", 10);+ Command Four() => new ConvertDollars(4);+ // A hex the game accepts for Tel Aviv's industry, taken from the map rather than written in, so a regrid (#1796's+ // 4.4 km grid made the old (-4, 20) sea) does not turn this row into a refused order that spends nothing.+ var site = Real.Hexes.OrderBy(h => h.Q).ThenBy(h => h.R)+ .First(h => Projects.CheckCityReason(Real, s, "industry", "tel_aviv", h.Q, h.R) == null);+ Command Industry() => new StartProject("industry", "tel_aviv", site.Q, site.R);+ Command Option() => new ChooseEventOption("idf_founding", "one_army");+ var rows = new (Command[] Queued, Func<int, Command[]> EndTurn, int FirstRefused)[]+ {+ (new Command[0], n => new Command[] { new BuyArms(n) }, 271),+ (new[] { Four() }, n => new[] { Four(), new BuyArms(n) }, 272),+ (new[] { One() }, n => new[] { One(), new BuyArms(n) }, 270),+ (new[] { One(), Four() }, n => new[] { One(), Four(), new BuyArms(n) }, 271),+ (new[] { Four(), One() }, n => new[] { Four(), One(), new BuyArms(n) }, 271),+ (new[] { Ten(), Four() }, n => new[] { Ten(), Four(), new BuyArms(n) }, 267),+ (new[] { Industry(), Four() }, n => new[] { Industry(), Four(), new BuyArms(n) }, 270),+ (new[] { Option(), Four() }, n => new[] { Option(), Four(), new BuyArms(n) }, 272),+ // the option and the training use up the turn's Attention: End turn refuses the purchase for that, never for the+ // ceiling, so no amount warns here (the screen's Attention line says why)+ (new[] { Option(), One(), Four() }, n => new[] { Option(), One(), Four(), new BuyArms(n) }, int.MaxValue),+ // the queued purchase runs before the training, which then counts for nothing; the conversion still runs first+ (new[] { new BuyArms(1), One(), Four() }, n => new[] { new BuyArms(n), One(), Four() }, 272),+ (new[] { One(), new BuyArms(1), Four() }, n => new[] { One(), new BuyArms(n), Four() }, 271),+ };+ foreach (var (queued, endTurn, firstRefused) in rows)+ for (int n = 265; n <= 274; n++)+ {+ var turn = Simulation.Step(Real, s, endTurn(n));+ Assert.DoesNotContain(turn.Rejected, x => x.Command is not BuyArms);+ var refused = turn.Rejected.Any(x => x.Command is BuyArms && x.Reason == Pools.RefusedDebtCeiling);+ Assert.Equal(n >= firstRefused, refused);+ Assert.Equal(refused, Pools.BorrowsPastDebtCeiling(Real, s, queued, n));+ }+ Assert.Equal(hash, s.Hash()); // every check runs on a copy}// #2206: only the conversions queued after the turn's first Arms purchase move, to just before it, in their queue order;diff --git a/sim/Statecraft.Sim/Pools.cs b/sim/Statecraft.Sim/Pools.csindex a99b78921..0537bea2a 100644--- a/sim/Statecraft.Sim/Pools.cs+++ b/sim/Statecraft.Sim/Pools.cs@@ -68,6 +68,20 @@ namespace Statecraft.Simreturn 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)+ {+ 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);+ }+/// <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.csindex 9c166046e..5ec6e229c 100644--- a/unity/Assets/Scripts/UI/ArmsPurchasePresenter.cs+++ b/unity/Assets/Scripts/UI/ArmsPurchasePresenter.cs@@ -1,5 +1,6 @@#nullable enableusing System;+using System.Linq;using Statecraft.Sim;using UnityEngine.UIElements;@@ -71,8 +72,10 @@ namespace Statecraft.Game.UIpanel.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: the sim refuses this amount at End turn if the state stays as it is now (Pools.BuyArms).- : Pools.BorrowsPastDebtCeiling(c, s, count.value) ? text["arms.refused_debt_ceiling"] : "";+ // #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"] : "";}public void Dispose()diff --git a/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs b/unity/Assets/Tests/EditMode/FoodArmsHudTests.csindex 990650397..f3b683c11 100644--- a/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs+++ b/unity/Assets/Tests/EditMode/FoodArmsHudTests.cs@@ -249,6 +249,56 @@ public class FoodArmsHudTestsAssert.IsEmpty(ui.Q<Label>("arms-refusal").text, "paid from the Treasury, nothing is borrowed");}+ // #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.+ [TestCase(false, false)] [TestCase(false, true)]+ [TestCase(true, false)] [TestCase(true, true)]+ public void TheCeilingWarningCountsTheQueuedConversion(bool modern, bool he)+ {+ var b = Bridge(modern, he); var t = Text(he);+ using var ui = new UiTestKit();+ using var defence = new DefencePresenter(ui.Root, b, t);+ var r = b.Content.RulesAt(b.State.Date).Mvp!;+ Assert.AreEqual((20.0, 0.0, 250.0, 1.0), (b.State.Treasury, b.State.Debt, r.DebtCeiling!.Value, r.ArmsPrice!.Value));+ var count = ui.Q<IntegerField>("arms-count");+ var warning = ui.Q<Label>("arms-refusal");+ count.value = 271;+ Assert.AreEqual(t["arms.refused_debt_ceiling"], warning.text, "no conversion queued");+ ui.Q<IntegerField>("dollars-amount").value = 4;+ ui.Press("convert-dollars");+ Assert.AreEqual(4, ((ConvertDollars)b.Queued.Single()).Amount);+ Assert.IsEmpty(warning.text, "End turn converts first, so 271 Arms stay within the ceiling");+ count.value = 272;+ Assert.AreEqual(t["arms.refused_debt_ceiling"], warning.text, "272 Arms still pass the ceiling");+ count.value = 271;+ ui.Press("withdraw-dollars");+ Assert.IsEmpty(b.Queued);+ Assert.AreEqual(t["arms.refused_debt_ceiling"], warning.text, "the conversion withdrawn");+ }++ // #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.+ [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)+ {+ var b = Bridge(modern, he); var t = Text(he);+ using var ui = new UiTestKit();+ using var defence = new DefencePresenter(ui.Root, b, t);+ Assert.IsNull(b.Queue(new Train("tel_aviv", "halutz", 1)));+ ui.Q<IntegerField>("dollars-amount").value = 4;+ ui.Press("convert-dollars");+ ui.Q<IntegerField>("arms-count").value = arms;+ 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);+ var turn = b.EndTurn();+ Assert.AreEqual(refused ? new[] { Pools.RefusedDebtCeiling } : new string[0], turn.Rejected.Select(x => x.Reason).ToArray());+ }+[Test]public void PoolCausesIgnoreTheSupplyIndicatorAndKeepTheSimTieOrder(){