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(sim): security escalation ladder with ceasefire clock

eyalgolan55820b8820c3
diff --git a/sim/Statecraft.Sim.Tests/Fixture.cs b/sim/Statecraft.Sim.Tests/Fixture.cs
index b51978b2c..8b6b32bcc 100644
--- a/sim/Statecraft.Sim.Tests/Fixture.cs
+++ b/sim/Statecraft.Sim.Tests/Fixture.cs
@@ -117,6 +117,7 @@ attention_per_turn: 3
attention_cost: { set_policy: 1, allocate_budget: 1, choose_event_option: 1, set_mobilization: 1, security_order: 1 }
rung_attention_step: 0.25
ceasefire_weeks: 4
+rung_min_mobilization: [0.02, 0.05, 0.15, 0.4, 0.8]
treasury_start: 100
fx_start: 50
tax_per_capita_per_year: 0.0004
diff --git a/sim/Statecraft.Sim.Tests/SecurityTests.cs b/sim/Statecraft.Sim.Tests/SecurityTests.cs
new file mode 100644
index 000000000..2ba5aa51c
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/SecurityTests.cs
@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using Statecraft.Sim;
+
+public class SecurityTests
+{
+ static readonly Content C = ContentLoader.Load(Fixture.Write());
+
+ [Theory]
+ [InlineData(1, TurnLength.Quarter)] [InlineData(3, TurnLength.Quarter)]
+ [InlineData(4, TurnLength.Month)] [InlineData(5, TurnLength.Week)]
+ public void TurnLengthByRung(int rung, TurnLength expected) => Assert.Equal(expected, SecuritySystem.TurnLengthFor(rung));
+
+ [Fact]
+ public void StartAtWarSetsCeasefireClockAndCallUp()
+ {
+ var s = new GameState(); SecuritySystem.Init(C, s, new List<Cause>());
+ Assert.Equal(5, s.Rung); Assert.Equal(4, s.CeasefireWeeksLeft); Assert.Equal(0.8, s.Mobilization);
+ }
+
+ [Fact]
+ public void CeasefireClockForcesDeescalation()
+ {
+ var s = new GameState(); var why = new List<Cause>();
+ SecuritySystem.Init(C, s, why);
+ for (int i = 0; i < 3; i++) SecuritySystem.Apply(C, s, TurnLength.Week, why);
+ Assert.Equal(5, s.Rung);
+ SecuritySystem.Apply(C, s, TurnLength.Week, why);
+ Assert.Equal(3, s.Rung); Assert.Equal(0, s.CeasefireWeeksLeft);
+ Assert.Contains(why, w => w.Target == "rung" && w.Message.Contains("ceasefire"));
+ Assert.Equal(0.8, s.Mobilization); // de-escalation does not demobilize
+ }
+
+ [Fact]
+ public void EscalationRaisesCallUpFloor()
+ {
+ var s = new GameState { Rung = 1 }; var why = new List<Cause>();
+ SecuritySystem.SetRung(C, s, 4, why);
+ Assert.Equal(0.4, s.Mobilization);
+ SecuritySystem.SetRung(C, s, 9, why);
+ Assert.Equal(5, s.Rung); Assert.Equal(4, s.CeasefireWeeksLeft);
+ }
+
+ [Fact]
+ public void MobilizationOrdersAreValidatedAndFloored()
+ {
+ var s = new GameState { Rung = 3 }; var why = new List<Cause>();
+ Assert.Equal("Mobilization must be between 0 and 1", SecuritySystem.SetMobilization(C, s, 1.5, why));
+ Assert.Null(SecuritySystem.SetMobilization(C, s, 0.0, why));
+ Assert.Equal(0.15, s.Mobilization);
+ }
+
+ [Fact]
+ public void LoaderRequiresFiveRungFloors()
+ {
+ var bad = Fixture.Files["rules.yaml"].Replace("[0.02, 0.05, 0.15, 0.4, 0.8]", "[0.1]");
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = bad })));
+ Assert.Equal("rung_min_mobilization", ex.Field);
+ }
+}
diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.cs
index 7e84b8a01..22a5ea691 100644
--- a/sim/Statecraft.Sim/Content.cs
+++ b/sim/Statecraft.Sim/Content.cs
@@ -103,6 +103,7 @@ namespace Statecraft.Sim
public Dictionary<string, double> AttentionCost { get; set; } = new Dictionary<string, double>();
public double RungAttentionStep { get; set; }
public int CeasefireWeeks { get; set; }
+ public List<double> RungMinMobilization { get; set; } = new List<double>();
public double TreasuryStart { get; set; }
public double FxStart { get; set; }
public double TaxPerCapitaPerYear { get; set; }
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index a7eda84bb..9b21c388f 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -133,6 +133,7 @@ namespace Statecraft.Sim
foreach (var v in c.VoterGroups)
foreach (var k in v.Weights.Keys)
if (!indicators.Contains(k)) throw new ContentException("model.yaml", $"voter_groups[{v.Id}].weights.{k}", "unknown indicator");
+ if (c.Rules.RungMinMobilization.Count != 5) throw new ContentException("rules.yaml", "rung_min_mobilization", "needs exactly 5 values (rungs 1-5)");
foreach (var k in c.Rules.BudgetStart.Keys)
if (!offices.Contains(k)) throw new ContentException("rules.yaml", $"budget_start.{k}", "unknown office");
EventRules.Validate(c, offices, indicators);
diff --git a/sim/Statecraft.Sim/SecuritySystem.cs b/sim/Statecraft.Sim/SecuritySystem.cs
new file mode 100644
index 000000000..ef708dba3
--- /dev/null
+++ b/sim/Statecraft.Sim/SecuritySystem.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+
+namespace Statecraft.Sim
+{
+ /// <summary>The always-on escalation ladder: 1 routine, 2 wave, 3 limited operation, 4 prolonged conflict, 5 full war.</summary>
+ public static class SecuritySystem
+ {
+ public static TurnLength TurnLengthFor(int rung) =>
+ rung >= 5 ? TurnLength.Week : rung == 4 ? TurnLength.Month : TurnLength.Quarter;
+
+ static int Weeks(TurnLength len) => len == TurnLength.Week ? 1 : len == TurnLength.Month ? 4 : 13;
+
+ 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;
+ if (rung != s.Rung) why.Add(new Cause("security", "rung", rung - s.Rung, $"security level {s.Rung} -> {rung}"));
+ s.Rung = rung;
+ RaiseToFloor(c, s, why);
+ }
+
+ /// <summary>Returns null on success, or the reason the order was rejected.</summary>
+ public static string? SetMobilization(Content c, GameState s, double level, List<Cause> why)
+ {
+ if (level < 0 || level > 1) return "Mobilization must be between 0 and 1";
+ s.Mobilization = level;
+ RaiseToFloor(c, s, why);
+ return null;
+ }
+
+ static void RaiseToFloor(Content c, GameState s, List<Cause> why)
+ {
+ var floor = c.Rules.RungMinMobilization[s.Rung - 1];
+ if (s.Mobilization < floor)
+ {
+ why.Add(new Cause("security", "mobilization", floor - s.Mobilization, $"reserve call-up raised to {floor:P0}, the minimum for security level {s.Rung}"));
+ s.Mobilization = floor;
+ }
+ }
+
+ public static void Apply(Content c, GameState s, TurnLength len, List<Cause> why)
+ {
+ if (s.Rung != 5) return;
+ s.CeasefireWeeksLeft = Math.Max(0, s.CeasefireWeeksLeft - Weeks(len));
+ if (s.CeasefireWeeksLeft == 0)
+ {
+ why.Add(new Cause("security", "rung", -2, "UN ceasefire imposed: full war ends"));
+ SetRung(c, s, 3, why);
+ }
+ }
+ }
+}