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): attention budget per turn
eyalgolan164664e2450d
diff --git a/sim/Statecraft.Sim.Tests/AttentionTests.cs b/sim/Statecraft.Sim.Tests/AttentionTests.csnew file mode 100644index 000000000..2c4b1c6eb--- /dev/null+++ b/sim/Statecraft.Sim.Tests/AttentionTests.cs@@ -0,0 +1,34 @@+using System.Collections.Generic;+using Statecraft.Sim;++public class AttentionTests+{+ static readonly Content C = ContentLoader.Load(Fixture.Write());++ [Fact]+ public void CostRisesWithSecurityLevel()+ {+ var s = new GameState { Rung = 1 };+ Assert.Equal(1.0, AttentionSystem.Cost(C, s, "set_policy"));+ s.Rung = 5;+ Assert.Equal(2.0, AttentionSystem.Cost(C, s, "set_policy")); // 1 * (1 + 0.25 * 4)+ }++ [Fact]+ public void SpendingStopsWhenAttentionRunsOut()+ {+ var s = new GameState { Rung = 5 };+ AttentionSystem.Refill(C, s); // 3+ Assert.Null(AttentionSystem.TrySpend(C, s, "set_policy")); // 3 - 2 = 1+ Assert.Equal("Not enough attention (need 2.0, have 1.0)", AttentionSystem.TrySpend(C, s, "set_policy"));+ Assert.Equal(1.0, s.Attention);+ }++ [Fact]+ public void LoaderRejectsUnknownCommandCost()+ {+ var bad = Fixture.Files["rules.yaml"].Replace("set_policy: 1,", "set_polcy: 1,");+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = bad })));+ Assert.Equal("attention_cost.set_polcy", ex.Field);+ }+}diff --git a/sim/Statecraft.Sim/AttentionSystem.cs b/sim/Statecraft.Sim/AttentionSystem.csnew file mode 100644index 000000000..62ad727d4--- /dev/null+++ b/sim/Statecraft.Sim/AttentionSystem.cs@@ -0,0 +1,27 @@+using System.Collections.Generic;+using System.Globalization;++namespace Statecraft.Sim+{+ /// <summary>The PM's per-turn capacity. Each command costs attention; costs rise with the security level.</summary>+ public static class AttentionSystem+ {+ public static readonly HashSet<string> CommandKinds = new HashSet<string>+ { "set_policy", "allocate_budget", "choose_event_option", "set_mobilization", "security_order" };++ public 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>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)+ {+ var cost = Cost(c, s, kind);+ 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;+ return null;+ }+ }+}diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 8b0b06130..022828a72 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -144,6 +144,10 @@ namespace Statecraft.Simforeach (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.Start == default) throw new ContentException("rules.yaml", "start", "required");+ foreach (var k in c.Rules.AttentionCost.Keys)+ if (!AttentionSystem.CommandKinds.Contains(k)) throw new ContentException("rules.yaml", $"attention_cost.{k}", "unknown command kind");+ foreach (var k in AttentionSystem.CommandKinds)+ if (!c.Rules.AttentionCost.ContainsKey(k)) throw new ContentException("rules.yaml", $"attention_cost.{k}", "missing cost");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");