A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix(sim): hard anchors happen, no events after game over, turn-start attention prices
eyalgolan1942dfb4fa63
diff --git a/sim/Statecraft.Sim.Tests/SimTests.cs b/sim/Statecraft.Sim.Tests/SimTests.csindex fadc969a6..44330b49b 100644--- a/sim/Statecraft.Sim.Tests/SimTests.cs+++ b/sim/Statecraft.Sim.Tests/SimTests.cs@@ -245,4 +245,106 @@ options:Assert.Equal(Play(11), Play(11));Assert.NotEqual(Play(11).hash, Play(12).hash);}++ // ---- Fix round 1 ----++ const string Partition = @"+id: partition+title: The armistice line+text: 'Where should the line run?'+anchor: hard+office: defense_minister+from: 1948-05-15+options:+ - id: hold_north+ text: Hold the north+ recommended: true+ - id: accept_line+ text: Accept the line+ historical: true+ - id: push_on+ text: Push on+";++ [Fact]+ public void HardAnchorDelegatedInHistoricalModeTakesTheHistoricalOptionWithoutRng()+ {+ var c = ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["partition.yaml"] = Partition }));+ for (ulong seed = 1; seed <= 20; seed++)+ {+ var s0 = Simulation.NewGame(c, seed, Mode.Historical);+ Assert.Equal(new[] { "partition" }, s0.PendingEvents);+ var r = Simulation.Step(c, s0, None);+ Assert.Contains(r.Why, w => w.System == "events" && w.Message == "delegated to Pm One: Accept the line");+ Assert.Equal(seed, r.State.RngState);+ }+ }++ [Fact]+ public void HardAnchorOutsideHistoricalModeKeepsCompetenceDelegation()+ {+ var c = ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["partition.yaml"] = Partition }));+ var s0 = Simulation.NewGame(c, 5, Mode.Historical);+ s0.Mode = Mode.Open;+ var r = Simulation.Step(c, s0, None);+ Assert.NotEqual(5UL, r.State.RngState);+ Assert.Contains(r.Why, w => w.System == "events" && w.Message.StartsWith("delegated to Pm One: "));+ }++ const string Late = @"+id: late+title: A later matter+text: 'Something new.'+from: 1948-05-22+options:+ - id: ok+ text: Fine+";++ [Fact]+ public void CollapseTurnTriggersNoNewEvents()+ {+ var c = ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["late.yaml"] = Late }));+ var s0 = Simulation.NewGame(c, 42, Mode.Historical);+ s0.CoalitionStability = 0.2;+ s0.IndicatorShifts["morale"] = -0.45; // approval 0.05 drags stability below the collapse line this turn+ var r = Simulation.Step(c, s0, None);+ Assert.Equal("coalition_collapse", r.State.GameOver);+ Assert.Equal(new DateTime(1948, 5, 22), r.State.Date);+ Assert.Equal(1, r.State.Turn);+ Assert.Empty(r.State.PendingEvents);+ Assert.DoesNotContain("late", r.State.FiredEvents);+ }++ [Fact]+ public void AttentionIsPricedAtTheRungTheTurnStartedAt()+ {+ var s0 = Simulation.NewGame(C, 42, Mode.Historical); // rung 5: every command costs 2 of 3+ var r = Simulation.Step(C, s0, new Command[] { new SecurityOrder(-4), new SetPolicy("rationing", 0.5) });+ Assert.Equal(1, r.State.Rung);+ Assert.Equal(new[] { "Not enough attention (need 2.0, have 1.0)" }, r.Rejected.Select(x => x.Reason));+ Assert.Equal(0.0, r.State.Policies["rationing"]);+ }++ [Fact]+ public void DeterministicWhenPolicyVotesDrawFromTheRng()+ {+ (string hash, ulong rng, string log) Play(ulong seed)+ {+ var s = Simulation.NewGame(C, seed, Mode.Historical);+ s.CoalitionStability = 0.3; // below 0.5: each vote passes with probability 2 * stability+ var lines = new List<string>();+ for (int i = 0; i < 8; i++)+ {+ var r = Simulation.Step(C, s, new Command[] { new SetPolicy("rationing", i % 2 == 0 ? 0.6 : 0.2) });+ lines.AddRange(r.Why.Select(w => w.ToString()));+ lines.AddRange(r.Rejected.Select(x => x.Reason));+ s = r.State;+ }+ return (s.Hash(), s.RngState, string.Join("\n", lines));+ }+ var a = Play(21);+ Assert.Equal(a, Play(21));+ Assert.NotEqual(21UL, a.rng);+ }}diff --git a/sim/Statecraft.Sim/AttentionSystem.cs b/sim/Statecraft.Sim/AttentionSystem.csindex 1c8ee4f43..e9b23039d 100644--- a/sim/Statecraft.Sim/AttentionSystem.cs+++ b/sim/Statecraft.Sim/AttentionSystem.cs@@ -15,13 +15,15 @@ namespace Statecraft.Simpublic static void Refill(Content c, GameState s) => s.Attention = c.Rules.AttentionPerTurn;- public static double Cost(Content c, GameState s, string kind) =>- c.Rules.AttentionCost[kind] * (1 + c.Rules.RungAttentionStep * (s.Rung - 1));+ /// <summary>Cost at <paramref name="rung"/> if given, else at the state's current rung.</summary>+ public static double Cost(Content c, GameState s, string kind, int? rung = null) =>+ c.Rules.AttentionCost[kind] * (1 + c.Rules.RungAttentionStep * ((rung ?? s.Rung) - 1));- /// <summary>Returns null and deducts on success, or the reason it could not be afforded.</summary>- public static string? TrySpend(Content c, GameState s, string kind)+ /// <summary>Returns null and deducts on success, or the reason it could not be afforded.+ /// <paramref name="rung"/> prices the command at that security level (the turn's starting rung).</summary>+ public static string? TrySpend(Content c, GameState s, string kind, int? rung = null){- var cost = Cost(c, s, kind);+ var cost = Cost(c, s, kind, rung);if (cost > s.Attention + 1e-9)return string.Format(CultureInfo.InvariantCulture, "Not enough attention (need {0:0.0}, have {1:0.0})", cost, s.Attention);s.Attention -= cost;diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.csindex f9f2824d6..2bc66e2dd 100644--- a/sim/Statecraft.Sim/EventSystem.cs+++ b/sim/Statecraft.Sim/EventSystem.cs@@ -65,7 +65,8 @@ namespace Statecraft.Sim}/// <summary>Unanswered events go to the responsible minister: with probability competence/10 they pick the- /// recommended option (else the first), otherwise a random option.</summary>+ /// recommended option (else the first), otherwise a random option. In Historical mode a hard-anchor event+ /// resolves to its historical option without drawing from the RNG (major anchors happen).</summary>public static void Delegate(Content c, GameState s, List<Cause> why){var rng = new Rng(s.RngState);@@ -74,9 +75,10 @@ namespace Statecraft.Simvar e = Get(c, id);var minister = e.Office == "" ? null : Offices.Holder(c, e.Office, s.Date);var competence = (minister?.Competence ?? 5) / 10.0;- var option = rng.NextDouble() < competence+ var anchored = s.Mode == Mode.Historical && e.Anchor == "hard" ? e.Options.FirstOrDefault(o => o.Historical) : null;+ var option = anchored ?? (rng.NextDouble() < competence? e.Options.FirstOrDefault(o => o.Recommended) ?? e.Options[0]- : e.Options[(int)(rng.NextDouble() * e.Options.Count)];+ : e.Options[(int)(rng.NextDouble() * e.Options.Count)]);Resolve(c, s, id, option, $"delegated to {minister?.Name ?? "the cabinet"}", why);}s.RngState = rng.State;diff --git a/sim/Statecraft.Sim/Simulation.cs b/sim/Statecraft.Sim/Simulation.csindex c55eb76f5..703ac084c 100644--- a/sim/Statecraft.Sim/Simulation.cs+++ b/sim/Statecraft.Sim/Simulation.cs@@ -40,11 +40,12 @@ namespace Statecraft.Simreturn new StepResult(s, why, rejected);}+ var priceRung = s.Rung; // every command this turn is priced at the rung the turn started atforeach (var cmd in commands){if (!AttentionSystem.IsCommandKind(cmd.Kind)) { rejected.Add(new Rejection(cmd, $"Unknown command '{cmd.Kind}'")); continue; }var before = s.Attention;- var error = AttentionSystem.TrySpend(c, s, cmd.Kind) ?? Execute(c, s, cmd, why);+ var error = AttentionSystem.TrySpend(c, s, cmd.Kind, priceRung) ?? Execute(c, s, cmd, why);if (error != null) { s.Attention = before; rejected.Add(new Rejection(cmd, error)); }}EventSystem.Delegate(c, s, why);@@ -61,7 +62,7 @@ namespace Statecraft.Sims.Date = next;s.Turn++;AttentionSystem.Refill(c, s);- EventSystem.Trigger(c, s, why);+ if (s.GameOver == null) EventSystem.Trigger(c, s, why); // a game that just ended raises no new eventsreturn new StepResult(s, why, rejected);}