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): HistoricalAI skips orders the turn's attention cannot cover (#13)
eyalgolanfb31ce14ddc0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f528eb566..9e1f74028 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -18,3 +18,7 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB- Unity-MCP 0.93.3 (`com.ivanmurzak.unity.mcp`, from OpenUPM) as the standard AI-agent bridge in the editor, localCustom mode with token auth only (held by `UnityMcpGuard`). It stays offline in batch mode, opens no window there, and isleft out of player builds.++### Changed+- The historical AI no longer sends an order the turn's attention cannot cover (the rule the UI's order queue applies),+ so the sim never has to refuse one; the golden playthroughs are unchanged.diff --git a/sim/Statecraft.Sim.Tests/SimTests.cs b/sim/Statecraft.Sim.Tests/SimTests.csindex 163fa5479..423e3e6a3 100644--- a/sim/Statecraft.Sim.Tests/SimTests.cs+++ b/sim/Statecraft.Sim.Tests/SimTests.cs@@ -251,6 +251,10 @@ options:var c = WithEvents();var s = Simulation.NewGame(c, 42, Mode.Historical);Assert.Equal(new[] { "raid", "shortage" }, s.PendingEvents);+ // 3 attention and each choice costs 2: like the UI's queue, it sends only the choice it can afford.+ var affordable = HistoricalAI.Commands(c, s).Cast<ChooseEventOption>().ToList();+ Assert.Equal(new[] { ("raid", "hold") }, affordable.Select(x => (x.EventId, x.OptionId)));+ s.Attention = 10;var cmds = HistoricalAI.Commands(c, s).Cast<ChooseEventOption>().ToList();Assert.Equal(new[] { ("raid", "hold"), ("shortage", "import") }, cmds.Select(x => (x.EventId, x.OptionId)));}@@ -260,7 +264,8 @@ options:{var c = WithEvents();var s0 = Simulation.NewGame(c, 42, Mode.Historical);- var r = Simulation.Step(c, s0, HistoricalAI.Commands(c, s0)); // 3 attention, each choice costs 2+ var both = new Command[] { new ChooseEventOption("raid", "hold"), new ChooseEventOption("shortage", "import") };+ var r = Simulation.Step(c, s0, both); // 3 attention, each choice costs 2Assert.StartsWith("Not enough attention", r.Rejected.Single().Reason);Assert.Empty(r.State.PendingEvents);Assert.Contains(r.Why, w => w.System == "events" && w.Message.StartsWith("Chosen: "));diff --git a/sim/Statecraft.Sim.Tests/SliceSmokeTests.cs b/sim/Statecraft.Sim.Tests/SliceSmokeTests.csindex 37e1c643b..3f028b7fa 100644--- a/sim/Statecraft.Sim.Tests/SliceSmokeTests.cs+++ b/sim/Statecraft.Sim.Tests/SliceSmokeTests.cs@@ -240,18 +240,7 @@ public class SliceSmokeTestsvar clock = Stopwatch.StartNew();var run = Play(Real, Historical, reload: true, PlayerTextTests.LeakGuard(Real));AssertClean("historical", run, Play(Real, Historical, reload: false, guard: null));- // HistoricalAI does not check attention: on 27 May 1949 it orders a call-up it cannot afford. The UI's queue refuses- // that order (the golden run hands it to Step, which refunds and refuses it, changing nothing), so the hash agrees.- Assert.Equal(new[] { "turn 33 (27 May 1949): set_mobilization needs 1.3 attention, 0.3 left" }, run.QueueRefusals.ToArray());- var golden = Simulation.NewGame(Real, GoldenRun.Seed, Mode.Historical);- var stepRefusals = new List<string>();- while (golden.Date < GoldenRun.Until1952 && golden.GameOver == null)- {- var r = Simulation.Step(Real, golden, HistoricalAI.Commands(Real, golden));- stepRefusals.AddRange(r.Rejected.Select(x => $"turn {r.State.Turn}: {x.Command.Kind}: {x.Reason}"));- golden = r.State;- }- Assert.Equal(new[] { "turn 33: set_mobilization: Not enough attention (need 1.3, have 0.3)" }, stepRefusals.ToArray());+ Assert.True(run.QueueRefusals.Count == 0, "HistoricalAI sent an order the UI would refuse:\n" + string.Join("\n", run.QueueRefusals));Assert.Equal(GoldenRun.Hash1952, run.Final.Hash());Assert.True(clock.Elapsed < TimeSpan.FromMinutes(1), $"took {clock.Elapsed}");}diff --git a/sim/Statecraft.Sim/HistoricalAI.cs b/sim/Statecraft.Sim/HistoricalAI.csindex 5b7e5903e..13e18f521 100644--- a/sim/Statecraft.Sim/HistoricalAI.cs+++ b/sim/Statecraft.Sim/HistoricalAI.cs@@ -6,17 +6,28 @@ namespace Statecraft.Sim{/// <summary>Plays what really happened: picks each pending event's historical option (else its first), then sends/// reserves home down to the current security level's minimum call-up (the army demobilised after the 1949- /// armistices). It sends no budget allocation, so the budget stays at rules.yaml budget_start.</summary>+ /// armistices). It sends no budget allocation, so the budget stays at rules.yaml budget_start. Like the UI's order+ /// queue (SimBridge.Queue), it leaves out an order the turn's remaining attention cannot cover, so Step never has to+ /// refuse one of its orders.</summary>public static class HistoricalAI{public static IReadOnlyList<Command> Commands(Content c, GameState s){- var list = s.PendingEvents+ var wanted = s.PendingEvents.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 = SecuritySystem.MinMobilization(c, s.Rung);- if (s.Mobilization > floor) list.Add(new SetMobilization(floor));+ if (s.Mobilization > floor) wanted.Add(new SetMobilization(floor));+ var list = new List<Command>();+ var left = s.Attention;+ foreach (var cmd in wanted)+ {+ var cost = AttentionSystem.Cost(c, s, cmd.Kind); // the turn's starting rung, as Step prices it+ if (cost > left + 1e-9) continue; // AttentionSystem.TrySpend's rule+ left -= cost;+ list.Add(cmd);+ }return list;}}