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.

fix(ui): defence rules come from SecuritySystem helpers, ceasefire label follows queued orders, no-op call-ups withdrawn

eyalgolan6f80d854c83f
diff --git a/sim/Statecraft.Sim.Tests/SecurityTests.cs b/sim/Statecraft.Sim.Tests/SecurityTests.cs
index 30b907bb7..0514f91ac 100644
--- a/sim/Statecraft.Sim.Tests/SecurityTests.cs
+++ b/sim/Statecraft.Sim.Tests/SecurityTests.cs
@@ -97,4 +97,45 @@ public class SecurityTests
Assert.Null(SecuritySystem.SetMobilization(C, s, 0.0, why)); // reaches RaiseToFloor
Assert.Equal(0.8, s.Mobilization);
}
+
+ [Theory]
+ [InlineData(-3, 1)] [InlineData(0, 1)] [InlineData(1, 1)] [InlineData(5, 5)] [InlineData(9, 5)]
+ public void ClampRungKeepsTheLadderInRange(int rung, int expected) => Assert.Equal(expected, SecuritySystem.ClampRung(rung));
+
+ [Theory]
+ [InlineData(1, 0.02)] [InlineData(4, 0.4)] [InlineData(5, 0.8)] [InlineData(9, 0.8)] [InlineData(0, 0.02)]
+ public void MinMobilizationIsTheRungsFloor(int rung, double expected) => Assert.Equal(expected, SecuritySystem.MinMobilization(C, rung));
+
+ [Fact]
+ public void EffectiveMobilizationIsNeverBelowTheFloor()
+ {
+ Assert.Equal(0.8, SecuritySystem.EffectiveMobilization(C, 5, 0.1));
+ Assert.Equal(0.9, SecuritySystem.EffectiveMobilization(C, 5, 0.9));
+ Assert.Equal(0.4, SecuritySystem.EffectiveMobilization(C, 4, 0.0));
+ }
+
+ [Fact]
+ public void EffectiveMobilizationMatchesWhatTheSimDoes()
+ {
+ foreach (var rung in new[] { 1, 3, 5 })
+ foreach (var level in new[] { 0.0, 0.3, 1.0 })
+ {
+ var s = new GameState { Rung = rung, Mobilization = 0 };
+ Assert.Null(SecuritySystem.SetMobilization(C, s, level, new List<Cause>()));
+ Assert.Equal(SecuritySystem.EffectiveMobilization(C, rung, level), s.Mobilization);
+ }
+ }
+
+ [Theory]
+ [InlineData(3, 0, 5, 4)] // entering full war starts the clock
+ [InlineData(5, 2, 5, 2)] // staying keeps it
+ [InlineData(5, 2, 4, 0)] // leaving ends it
+ [InlineData(2, 0, 3, 0)]
+ public void CeasefireWeeksAfterAnOrderFollowsSetRung(int from, int left, int to, int expected)
+ {
+ Assert.Equal(expected, SecuritySystem.CeasefireWeeksAfter(C, from, left, to));
+ var s = new GameState { Rung = from, CeasefireWeeksLeft = left };
+ SecuritySystem.SetRung(C, s, to, new List<Cause>());
+ Assert.Equal(expected, s.CeasefireWeeksLeft);
+ }
}
diff --git a/sim/Statecraft.Sim/HistoricalAI.cs b/sim/Statecraft.Sim/HistoricalAI.cs
index 80d6a5dca..5b7e5903e 100644
--- a/sim/Statecraft.Sim/HistoricalAI.cs
+++ b/sim/Statecraft.Sim/HistoricalAI.cs
@@ -15,7 +15,7 @@ namespace Statecraft.Sim
.Select(id => c.Events.First(e => e.Id == id))
.Select(e => (Command)new ChooseEventOption(e.Id, (e.Options.FirstOrDefault(o => o.Historical) ?? e.Options[0]).Id))
.ToList();
- var floor = c.Rules.RungMinMobilization[Math.Max(1, Math.Min(5, s.Rung)) - 1];
+ var floor = SecuritySystem.MinMobilization(c, s.Rung);
if (s.Mobilization > floor) list.Add(new SetMobilization(floor));
return list;
}
diff --git a/sim/Statecraft.Sim/SecuritySystem.cs b/sim/Statecraft.Sim/SecuritySystem.cs
index 015e02d02..8325ae54d 100644
--- a/sim/Statecraft.Sim/SecuritySystem.cs
+++ b/sim/Statecraft.Sim/SecuritySystem.cs
@@ -11,13 +11,30 @@ namespace Statecraft.Sim
static int Weeks(TurnLength len) => len == TurnLength.Week ? 1 : len == TurnLength.Month ? 4 : 13;
+ /// <summary>The ladder has rungs 1..5; anything outside is clamped (a bad save or a large order delta).</summary>
+ public static int ClampRung(int rung) => Math.Max(1, Math.Min(5, rung));
+
+ /// <summary>The least reserve call-up the rung allows (rules.yaml rung_min_mobilization).</summary>
+ public static double MinMobilization(Content c, int rung) => c.Rules.RungMinMobilization[ClampRung(rung) - 1];
+
+ /// <summary>The call-up a level ends up at on this rung: the sim raises a lower order to the rung's minimum.</summary>
+ public static double EffectiveMobilization(Content c, int rung, double level) => Math.Max(level, MinMobilization(c, rung));
+
+ /// <summary>Weeks left on the UN ceasefire clock after moving from <paramref name="fromRung"/> (with
+ /// <paramref name="weeksLeft"/> on the clock) to <paramref name="toRung"/>: entering full war starts it, leaving ends it.</summary>
+ public static int CeasefireWeeksAfter(Content c, int fromRung, int weeksLeft, int toRung)
+ {
+ toRung = ClampRung(toRung);
+ if (toRung != 5) return 0;
+ return fromRung != 5 ? c.Rules.CeasefireWeeks : weeksLeft;
+ }
+
public static void Init(Content c, GameState s, List<Cause> why) => SetRung(c, s, c.Rules.StartRung, why);
public static void SetRung(Content c, GameState s, int rung, List<Cause> why)
{
- rung = Math.Max(1, Math.Min(5, rung));
- if (rung == 5 && s.Rung != 5) s.CeasefireWeeksLeft = c.Rules.CeasefireWeeks;
- if (rung != 5) s.CeasefireWeeksLeft = 0;
+ rung = ClampRung(rung);
+ s.CeasefireWeeksLeft = CeasefireWeeksAfter(c, s.Rung, s.CeasefireWeeksLeft, rung);
if (rung != s.Rung) why.Add(new Cause("security", "rung", rung - s.Rung, FormattableString.Invariant($"security level {s.Rung} -> {rung}")));
s.Rung = rung;
RaiseToFloor(c, s, why);
@@ -35,8 +52,8 @@ namespace Statecraft.Sim
static void RaiseToFloor(Content c, GameState s, List<Cause> why)
{
- var rung = Math.Max(1, Math.Min(5, s.Rung)); // a bad save cannot index past the table
- var floor = c.Rules.RungMinMobilization[rung - 1];
+ var rung = ClampRung(s.Rung); // a bad save cannot index past the table
+ var floor = MinMobilization(c, rung);
if (s.Mobilization < floor)
{
why.Add(new Cause("security", "mobilization", floor - s.Mobilization, FormattableString.Invariant($"reserve call-up raised to {floor * 100:0}%, the minimum for security level {rung}")));
diff --git a/unity/Assets/Scripts/UI/DefencePresenter.cs b/unity/Assets/Scripts/UI/DefencePresenter.cs
index 233388066..ae9ceecbf 100644
--- a/unity/Assets/Scripts/UI/DefencePresenter.cs
+++ b/unity/Assets/Scripts/UI/DefencePresenter.cs
@@ -39,11 +39,15 @@ namespace Statecraft.Game.UI
/// <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;
+ /// <summary>The security level the turn will end with: today's, moved by the queued order.</summary>
+ int TargetRung => SecuritySystem.ClampRung(bridge.State.Rung + QueuedDelta);
+
void Order(int step)
{
var s = bridge.State;
- var target = Math.Max(1, Math.Min(5, s.Rung + QueuedDelta + step));
+ var target = SecuritySystem.ClampRung(s.Rung + QueuedDelta + step);
if (target == s.Rung) Withdraw(new SecurityOrder(0)); else bridge.Queue(new SecurityOrder(target - s.Rung));
+ DropNoOpCallUp();
}
void Withdraw(Command like)
@@ -52,13 +56,35 @@ namespace Statecraft.Game.UI
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];
+ /// <summary>The call-up the turn will end with if <paramref name="level"/> is ordered (the sim raises a lower one to the minimum).</summary>
+ double EffectiveAt(double level) => SecuritySystem.EffectiveMobilization(bridge.Content, TargetRung, level);
+
+ bool SameAsToday(double level) => Math.Abs(EffectiveAt(level) - EffectiveAt(bridge.State.Mobilization)) < 1e-9;
+
+ /// <summary>A queued call-up order that would change nothing (after the minimum is applied) is withdrawn, so it
+ /// neither costs attention nor shows a stale proposed level.</summary>
+ void DropNoOpCallUp()
+ {
+ if (bridge.QueuedLike(new SetMobilization(0)) is SetMobilization q && SameAsToday(q.Level)) bridge.Withdraw(q);
+ }
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));
+ var floor = SecuritySystem.MinMobilization(bridge.Content, TargetRung);
+ var level = Math.Max(floor, Math.Min(1, Math.Round((EffectiveAt(ProposedMobilization) + step) / MobilizationStep) * MobilizationStep));
+ if (SameAsToday(level)) Withdraw(new SetMobilization(0)); else bridge.Queue(new SetMobilization(level));
+ }
+
+ /// <summary>The string key for a turn length. A switch, so a new TurnLength value fails loudly (and in a test).</summary>
+ public static string TurnKey(TurnLength length)
+ {
+ switch (length)
+ {
+ case TurnLength.Quarter: return "defence.turn_quarter";
+ case TurnLength.Month: return "defence.turn_month";
+ case TurnLength.Week: return "defence.turn_week";
+ default: throw new ArgumentOutOfRangeException(nameof(length), length, "no string for this turn length");
+ }
}
public void Render()
@@ -67,22 +93,26 @@ namespace Statecraft.Game.UI
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<Label>($"rung-{rung}-turn").text = text[TurnKey(SecuritySystem.TurnLengthFor(rung))];
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);
+ var target = TargetRung;
+ var ceasefire = screen.Q<Label>("ceasefire");
+ if (s.Rung == 5 && target == 5) ceasefire.text = text.Format("defence.ceasefire", s.CeasefireWeeksLeft);
+ else if (s.Rung == 5) ceasefire.text = text["defence.ceasefire_stops"]; // a lowering is queued
+ else if (target == 5) ceasefire.text = text.Format("defence.ceasefire_starts", SecuritySystem.CeasefireWeeksAfter(c, s.Rung, s.CeasefireWeeksLeft, target));
+ else ceasefire.text = "";
+ ceasefire.style.display = ceasefire.text == "" ? DisplayStyle.None : DisplayStyle.Flex;
+ screen.Q<Label>("order-pending").text = delta == 0 ? "" : text.Format("defence.order", s.Rung, target);
+ var floor = SecuritySystem.MinMobilization(c, target);
+ var proposed = EffectiveAt(ProposedMobilization); // what the call-up will be: the sim raises a lower order to the minimum
+ screen.Q<Label>("mob-level").text = text.Format("defence.mob", proposed * 100, target, floor * 100);
+ screen.Q<Label>("mob-cost").text = text.Format("defence.mob_cost", c.Rules.MobilizationCostPerYear * proposed);
+ screen.Q<Button>("escalate").SetEnabled(target < 5);
+ screen.Q<Button>("de-escalate").SetEnabled(target > 1);
}
}
}
diff --git a/unity/Assets/Tests/EditMode/DefenceScreenTests.cs b/unity/Assets/Tests/EditMode/DefenceScreenTests.cs
index e328f5320..1cddecf84 100644
--- a/unity/Assets/Tests/EditMode/DefenceScreenTests.cs
+++ b/unity/Assets/Tests/EditMode/DefenceScreenTests.cs
@@ -119,4 +119,86 @@ public class DefenceScreenTests
Assert.AreEqual("untouched", ui.Q<Label>("defence-command").text);
}
}
+
+ [Test]
+ public void CallUpCanBeRaisedButNotPast100Percent()
+ {
+ var (ui, bridge, defence) = Open();
+ using (ui)
+ {
+ for (int i = 0; i < 6; i++) ui.Press("mob-up");
+ Assert.AreEqual(1.0, defence.ProposedMobilization, 1e-9);
+ StringAssert.StartsWith("100% of reserves called up", ui.Q<Label>("mob-level").text);
+ bridge.EndTurn();
+ Assert.AreEqual(1.0, bridge.State.Mobilization, 1e-9);
+ }
+ }
+
+ [Test]
+ public void TheCeasefireClockCountsDownEachTurn()
+ {
+ var (ui, bridge, _) = Open();
+ using (ui)
+ {
+ bridge.EndTurn();
+ Assert.AreEqual("A UN ceasefire will be imposed in 51 weeks", ui.Q<Label>("ceasefire").text);
+ Assert.AreEqual(51, bridge.State.CeasefireWeeksLeft);
+ }
+ }
+
+ [Test]
+ public void AQueuedDropFromFullWarSaysTheClockStops()
+ {
+ var (ui, bridge, _) = Open();
+ using (ui)
+ {
+ ui.Press("de-escalate");
+ Assert.AreEqual(DisplayStyle.Flex, ui.Q<Label>("ceasefire").style.display.value);
+ Assert.AreEqual("The UN ceasefire clock stops if the level is lowered", ui.Q<Label>("ceasefire").text);
+ ui.Press("escalate");
+ Assert.AreEqual("A UN ceasefire will be imposed in 52 weeks", ui.Q<Label>("ceasefire").text);
+ }
+ }
+
+ [Test]
+ public void AQueuedRaiseToFullWarShowsTheClockItWillStart()
+ {
+ var (ui, bridge, _) = Open();
+ using (ui)
+ {
+ ui.Press("de-escalate");
+ bridge.EndTurn();
+ Assert.AreEqual(DisplayStyle.None, ui.Q<Label>("ceasefire").style.display.value);
+ ui.Press("escalate");
+ Assert.AreEqual(DisplayStyle.Flex, ui.Q<Label>("ceasefire").style.display.value);
+ Assert.AreEqual($"UN ceasefire: {C.Rules.CeasefireWeeks} weeks once level 5 is reached", ui.Q<Label>("ceasefire").text);
+ bridge.EndTurn();
+ Assert.AreEqual(5, bridge.State.Rung);
+ Assert.AreEqual(C.Rules.CeasefireWeeks - 1, bridge.State.CeasefireWeeksLeft); // the clock started and ran one week
+ }
+ }
+
+ [Test]
+ public void ACallUpOrderThatBecomesANoOpIsWithdrawn()
+ {
+ var (ui, bridge, defence) = Open();
+ using (ui)
+ {
+ ui.Press("de-escalate");
+ ui.Press("mob-down");
+ Assert.AreEqual(2, bridge.Queued.Count);
+ ui.Press("escalate"); // back at level 5, where 75% is raised to 80% anyway
+ Assert.AreEqual(0, bridge.Queued.Count);
+ Assert.AreEqual(bridge.State.Mobilization, defence.ProposedMobilization, 1e-9);
+ }
+ }
+
+ [Test]
+ public void EveryTurnLengthHasAStringAndAnUnknownOneFailsLoudly()
+ {
+ var text = Text;
+ foreach (System.Enum v in System.Enum.GetValues(typeof(TurnLength)))
+ Assert.DoesNotThrow(() => { var _ = text[DefencePresenter.TurnKey((TurnLength)v)]; }, v.ToString());
+ Assert.Throws<System.ArgumentOutOfRangeException>(() => DefencePresenter.TurnKey((TurnLength)99));
+ }
}
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index bc8ce2b12..e80b643cd 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -32,3 +32,5 @@ 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"
+defence.ceasefire_stops: "The UN ceasefire clock stops if the level is lowered"
+defence.ceasefire_starts: "UN ceasefire: {0} weeks once level 5 is reached"