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.

feat(ui): Defence screen: escalation ladder, ceasefire clock, security orders, reserve call-up

eyalgolan0de163bb03b3
diff --git a/unity/Assets/Scripts/UI/DefencePresenter.cs b/unity/Assets/Scripts/UI/DefencePresenter.cs
new file mode 100644
index 000000000..233388066
--- /dev/null
+++ b/unity/Assets/Scripts/UI/DefencePresenter.cs
@@ -0,0 +1,88 @@
+#nullable enable
+using System;
+using System.Linq;
+using Statecraft.Sim;
+using UnityEngine.UIElements;
+
+namespace Statecraft.Game.UI
+{
+ /// <summary>The Defence screen: the always-on escalation ladder (spec section 3) with the current level and its turn
+ /// length, the UN ceasefire clock in full war, security orders to raise or lower the level, and the reserve call-up
+ /// with its yearly cost. Orders are queued for End Turn.</summary>
+ public sealed class DefencePresenter : IDisposable
+ {
+ public const double MobilizationStep = 0.05;
+ readonly VisualElement screen; readonly SimBridge bridge; readonly UiText text;
+ readonly Button escalate, deEscalate, mobUp, mobDown; readonly Action onEscalate, onDeEscalate, onMobUp, onMobDown;
+
+ public DefencePresenter(VisualElement root, SimBridge bridge, UiText text)
+ {
+ screen = root.Q<VisualElement>("screen-defence"); this.bridge = bridge; this.text = text;
+ escalate = screen.Q<Button>("escalate"); deEscalate = screen.Q<Button>("de-escalate");
+ mobUp = screen.Q<Button>("mob-up"); mobDown = screen.Q<Button>("mob-down");
+ onEscalate = () => Order(+1); onDeEscalate = () => Order(-1);
+ onMobUp = () => Mobilize(+MobilizationStep); onMobDown = () => Mobilize(-MobilizationStep);
+ escalate.clicked += onEscalate; deEscalate.clicked += onDeEscalate; mobUp.clicked += onMobUp; mobDown.clicked += onMobDown;
+ bridge.Changed += Render;
+ Render();
+ }
+
+ /// <summary>Unsubscribes from the bridge and the buttons (the owner calls this when disabled).</summary>
+ public void Dispose()
+ {
+ bridge.Changed -= Render;
+ escalate.clicked -= onEscalate; deEscalate.clicked -= onDeEscalate; mobUp.clicked -= onMobUp; mobDown.clicked -= onMobDown;
+ }
+
+ int QueuedDelta => (bridge.QueuedLike(new SecurityOrder(0)) as SecurityOrder)?.Delta ?? 0;
+
+ /// <summary>The call-up level the turn will end with: the queued order's, else today's.</summary>
+ public double ProposedMobilization => (bridge.QueuedLike(new SetMobilization(0)) as SetMobilization)?.Level ?? bridge.State.Mobilization;
+
+ void Order(int step)
+ {
+ var s = bridge.State;
+ var target = Math.Max(1, Math.Min(5, s.Rung + QueuedDelta + step));
+ if (target == s.Rung) Withdraw(new SecurityOrder(0)); else bridge.Queue(new SecurityOrder(target - s.Rung));
+ }
+
+ void Withdraw(Command like)
+ {
+ var queued = bridge.QueuedLike(like);
+ if (queued != null) bridge.Withdraw(queued);
+ }
+
+ /// <summary>The call-up minimum at the level the turn will end with (the sim raises a lower call-up to it).</summary>
+ double Floor => bridge.Content.Rules.RungMinMobilization[Math.Max(1, Math.Min(5, bridge.State.Rung + QueuedDelta)) - 1];
+
+ void Mobilize(double step)
+ {
+ var level = Math.Max(Floor, Math.Min(1, Math.Round((Math.Max(ProposedMobilization, Floor) + step) / MobilizationStep) * MobilizationStep));
+ if (Math.Abs(level - bridge.State.Mobilization) < 1e-9) Withdraw(new SetMobilization(0)); else bridge.Queue(new SetMobilization(level));
+ }
+
+ public void Render()
+ {
+ var c = bridge.Content; var s = bridge.State;
+ for (int rung = 1; rung <= 5; rung++)
+ {
+ screen.Q<Label>($"rung-{rung}-name").text = text[$"defence.rung{rung}"];
+ screen.Q<Label>($"rung-{rung}-turn").text = text["defence.turn_" + SecuritySystem.TurnLengthFor(rung).ToString().ToLowerInvariant()];
+ screen.Q<VisualElement>($"rung-{rung}").EnableInClassList("rung-current", rung == s.Rung);
+ }
+ screen.Q<Label>("defence-command").text = text.Format("defence.command",
+ Cabinet.Holder(c, s, "defense_minister")?.Name ?? "-", Cabinet.Holder(c, s, "chief_of_staff")?.Name ?? "-");
+ var ceasefire = screen.Q<Label>("ceasefire");
+ ceasefire.text = s.Rung == 5 ? text.Format("defence.ceasefire", s.CeasefireWeeksLeft) : "";
+ ceasefire.style.display = s.Rung == 5 ? DisplayStyle.Flex : DisplayStyle.None;
+ var delta = QueuedDelta;
+ screen.Q<Label>("order-pending").text = delta == 0 ? "" : text.Format("defence.order", s.Rung, s.Rung + delta);
+ var floor = Floor;
+ var proposed = Math.Max(ProposedMobilization, floor); // what the call-up will be: the sim raises a lower order to the floor
+ screen.Q<Label>("mob-level").text = text.Format("defence.mob", proposed * 100, s.Rung + delta, floor * 100);
+ screen.Q<Label>("mob-cost").text = text.Format("defence.mob_cost", c.Rules.MobilizationCostPerYear * Math.Max(proposed, floor));
+ screen.Q<Button>("escalate").SetEnabled(s.Rung + delta < 5);
+ screen.Q<Button>("de-escalate").SetEnabled(s.Rung + delta > 1);
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/UI/DefencePresenter.cs.meta b/unity/Assets/Scripts/UI/DefencePresenter.cs.meta
new file mode 100644
index 000000000..f0f242f43
--- /dev/null
+++ b/unity/Assets/Scripts/UI/DefencePresenter.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 297e05422d7a64df3861256ed93bfa69
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/DefenceScreenTests.cs b/unity/Assets/Tests/EditMode/DefenceScreenTests.cs
new file mode 100644
index 000000000..e328f5320
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/DefenceScreenTests.cs
@@ -0,0 +1,122 @@
+using NUnit.Framework;
+using Statecraft.Game;
+using Statecraft.Game.UI;
+using Statecraft.Sim;
+using UnityEditor;
+using UnityEngine;
+using UnityEngine.UIElements;
+
+public class DefenceScreenTests
+{
+ static readonly Content C = ContentLoader.Load(Paths.Content);
+ static UiText Text => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
+
+ static (UiTestKit ui, SimBridge bridge, DefencePresenter defence) Open()
+ {
+ var ui = new UiTestKit();
+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);
+ new ToastPresenter(ui.Root, bridge, Text);
+ return (ui, bridge, new DefencePresenter(ui.Root, bridge, Text));
+ }
+
+ [Test]
+ public void TheLadderShowsFullWarWithTheCeasefireClock()
+ {
+ var (ui, _, _) = Open();
+ using (ui)
+ {
+ Assert.IsTrue(ui.Q<VisualElement>("rung-5").ClassListContains("rung-current"));
+ Assert.IsFalse(ui.Q<VisualElement>("rung-2").ClassListContains("rung-current"));
+ Assert.AreEqual("turns of a week", ui.Q<Label>("rung-5-turn").text);
+ Assert.AreEqual("turns of a quarter", ui.Q<Label>("rung-1-turn").text);
+ Assert.AreEqual("A UN ceasefire will be imposed in 52 weeks", ui.Q<Label>("ceasefire").text);
+ Assert.AreEqual("Minister of Defense David Ben-Gurion · Chief of Staff Yaakov Dori", ui.Q<Label>("defence-command").text);
+ Assert.AreEqual("80% of reserves called up (level 5 needs at least 80%)", ui.Q<Label>("mob-level").text);
+ Assert.AreEqual("Costs 48.0m a year in pay and lost work", ui.Q<Label>("mob-cost").text);
+ Assert.IsFalse(ui.Q<Button>("escalate").enabledSelf);
+ }
+ }
+
+ [Test]
+ public void OrdersAreQueuedAndTheTurnCarriesThemOut()
+ {
+ var (ui, bridge, defence) = Open();
+ using (ui)
+ {
+ ui.Press("de-escalate");
+ Assert.AreEqual("Ordered: level 5 to 4", ui.Q<Label>("order-pending").text);
+ Assert.AreEqual("80% of reserves called up (level 4 needs at least 40%)", ui.Q<Label>("mob-level").text);
+ ui.Press("mob-down");
+ Assert.AreEqual(0.75, defence.ProposedMobilization, 1e-9);
+ bridge.EndTurn();
+ Assert.AreEqual(4, bridge.State.Rung);
+ Assert.AreEqual(0.75, bridge.State.Mobilization, 1e-9);
+ Assert.IsTrue(ui.Q<VisualElement>("rung-4").ClassListContains("rung-current"));
+ Assert.AreEqual(DisplayStyle.None, ui.Q<Label>("ceasefire").style.display.value);
+ }
+ }
+
+ [Test]
+ public void PressingBackToTodaysLevelWithdrawsTheOrder()
+ {
+ var (ui, bridge, _) = Open();
+ using (ui)
+ {
+ ui.Press("de-escalate");
+ ui.Press("escalate");
+ Assert.AreEqual(0, bridge.Queued.Count);
+ Assert.AreEqual("", ui.Q<Label>("order-pending").text);
+ }
+ }
+
+ [Test]
+ public void RaisingTheLevelShowsTheCallUpTheSimWillForce()
+ {
+ var (ui, bridge, defence) = Open();
+ using (ui)
+ {
+ ui.Press("de-escalate");
+ for (int i = 0; i < 8; i++) ui.Press("mob-down"); // 80% down to level 4's floor of 40%
+ Assert.AreEqual(0.4, defence.ProposedMobilization, 1e-9);
+ ui.Press("mob-down"); // cannot go below the floor
+ Assert.AreEqual(0.4, defence.ProposedMobilization, 1e-9);
+ bridge.EndTurn();
+ Assert.AreEqual(0.4, bridge.State.Mobilization, 1e-9);
+ ui.Press("escalate"); // back to full war needs 80% again
+ StringAssert.StartsWith("80% of reserves called up (level 5 needs at least 80%)", ui.Q<Label>("mob-level").text);
+ Assert.AreEqual("Costs 48.0m a year in pay and lost work", ui.Q<Label>("mob-cost").text);
+ bridge.EndTurn();
+ Assert.AreEqual(0.8, bridge.State.Mobilization, 1e-9);
+ Assert.AreEqual(5, bridge.State.Rung);
+ }
+ }
+
+ [Test]
+ public void AnOrderTheAttentionCannotPayForIsRefusedWithTheReason()
+ {
+ var (ui, bridge, _) = Open();
+ using (ui)
+ {
+ Assert.IsNull(bridge.Queue(new SetPolicy(C.Policies[0].Id, 0.5)));
+ Assert.IsNull(bridge.Queue(new SetPolicy(C.Policies[1].Id, 0.5)));
+ ui.Press("de-escalate");
+ Assert.AreEqual("Not done: Not enough attention (need 2.0, have 0.0)", ui.Q<Label>("toast").text);
+ Assert.AreEqual("", ui.Q<Label>("order-pending").text);
+ }
+ }
+
+ [Test]
+ public void ADisposedScreenHearsNothing()
+ {
+ var (ui, bridge, defence) = Open();
+ using (ui)
+ {
+ defence.Dispose();
+ ui.Q<Label>("defence-command").text = "untouched";
+ ui.Press("de-escalate");
+ Assert.AreEqual(0, bridge.Queued.Count);
+ bridge.EndTurn();
+ Assert.AreEqual("untouched", ui.Q<Label>("defence-command").text);
+ }
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/DefenceScreenTests.cs.meta b/unity/Assets/Tests/EditMode/DefenceScreenTests.cs.meta
new file mode 100644
index 000000000..7ace4c99a
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/DefenceScreenTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 2647183a6c62444e1b78e05497e8e2d7
\ No newline at end of file
diff --git a/unity/Assets/UI/Game.uxml b/unity/Assets/UI/Game.uxml
index b3a446b7e..446b6d5cf 100644
--- a/unity/Assets/UI/Game.uxml
+++ b/unity/Assets/UI/Game.uxml
@@ -58,7 +58,30 @@
<ui:VisualElement name="minimap" class="panel minimap" />
<ui:VisualElement name="screen-cabinet" class="panel screen" />
<ui:VisualElement name="screen-budget" class="panel screen" />
- <ui:VisualElement name="screen-defence" class="panel screen" />
+ <ui:VisualElement name="screen-defence" class="panel screen">
+ <ui:VisualElement class="screen-header"><ui:Label text="Defence" class="title" /><ui:Label name="defence-command" class="muted" /></ui:VisualElement>
+ <ui:Label text="Security level" class="meter-name" />
+ <ui:VisualElement name="ladder">
+ <ui:VisualElement name="rung-5" class="rung"><ui:Label name="rung-5-name" class="grow" /><ui:Label name="rung-5-turn" class="muted" /></ui:VisualElement>
+ <ui:VisualElement name="rung-4" class="rung"><ui:Label name="rung-4-name" class="grow" /><ui:Label name="rung-4-turn" class="muted" /></ui:VisualElement>
+ <ui:VisualElement name="rung-3" class="rung"><ui:Label name="rung-3-name" class="grow" /><ui:Label name="rung-3-turn" class="muted" /></ui:VisualElement>
+ <ui:VisualElement name="rung-2" class="rung"><ui:Label name="rung-2-name" class="grow" /><ui:Label name="rung-2-turn" class="muted" /></ui:VisualElement>
+ <ui:VisualElement name="rung-1" class="rung"><ui:Label name="rung-1-name" class="grow" /><ui:Label name="rung-1-turn" class="muted" /></ui:VisualElement>
+ </ui:VisualElement>
+ <ui:Label name="ceasefire" class="muted" />
+ <ui:VisualElement class="row">
+ <ui:Button name="de-escalate" text="Lower the level" />
+ <ui:Button name="escalate" text="Raise the level" />
+ <ui:Label name="order-pending" class="muted grow" />
+ </ui:VisualElement>
+ <ui:Label text="Reserve call-up" class="meter-name" />
+ <ui:VisualElement class="row">
+ <ui:Button name="mob-down" text="-5%" />
+ <ui:Label name="mob-level" class="grow" />
+ <ui:Button name="mob-up" text="+5%" />
+ </ui:VisualElement>
+ <ui:Label name="mob-cost" class="muted" />
+ </ui:VisualElement>
<ui:VisualElement name="screen-why" class="panel screen" />
<ui:Label name="toast" class="panel toast" />
</ui:VisualElement>
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index 2bcba71c7..bc8ce2b12 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -19,3 +19,16 @@ order.no_attention: "Not enough attention (need {0:0.0}, have {1:0.0})"
order.unknown: "Unknown {0} '{1}'"
order.invalid: "That order is not valid"
order.failed: "The turn could not run: {0}"
+defence.rung1: "1 Routine threat: border incidents, deterrence upkeep"
+defence.rung2: "2 Wave: terror or infiltration, hostile buildup"
+defence.rung3: "3 Limited operation: raids, reprisals"
+defence.rung4: "4 Prolonged conflict: attrition, long call-ups"
+defence.rung5: "5 Full war"
+defence.turn_quarter: "turns of a quarter"
+defence.turn_month: "turns of a month"
+defence.turn_week: "turns of a week"
+defence.command: "Minister of Defense {0} · Chief of Staff {1}"
+defence.ceasefire: "A UN ceasefire will be imposed in {0} weeks"
+defence.order: "Ordered: level {0} to {1}"
+defence.mob: "{0:0}% of reserves called up (level {1} needs at least {2:0}%)"
+defence.mob_cost: "Costs {0:0.0}m a year in pay and lost work"