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(sim): events fire when their window overlaps the turn; hard anchors win the cap (M3)

eyalgolan36d990d1206d
diff --git a/sim/Statecraft.Cli/Program.cs b/sim/Statecraft.Cli/Program.cs
index 9c48851b0..1d8a043d7 100644
--- a/sim/Statecraft.Cli/Program.cs
+++ b/sim/Statecraft.Cli/Program.cs
@@ -61,8 +61,9 @@ static void Briefing(Content c, GameState s)
foreach (var id in s.PendingEvents)
{
var e = c.Events.First(x => x.Id == id);
- Console.WriteLine($" EVENT {e.Id}: {Offices.Fill(c, e.Title, s.Date)} - {EventSystem.Render(c, e, s.Date)}");
- foreach (var o in e.Options) Console.WriteLine($" {o.Id}: {Offices.Fill(c, o.Text, s.Date)}");
+ var fillDate = EventSystem.FillDate(e, s.Date); // after the window ends, name the last in-window holder
+ Console.WriteLine($" EVENT {e.Id}: {Offices.Fill(c, e.Title, fillDate)} - {EventSystem.Render(c, e, s.Date)}");
+ foreach (var o in e.Options) Console.WriteLine($" {o.Id}: {Offices.Fill(c, o.Text, fillDate)}");
}
}
diff --git a/sim/Statecraft.Sim.Tests/EventTests.cs b/sim/Statecraft.Sim.Tests/EventTests.cs
index ee21cd7e0..256548433 100644
--- a/sim/Statecraft.Sim.Tests/EventTests.cs
+++ b/sim/Statecraft.Sim.Tests/EventTests.cs
@@ -456,4 +456,62 @@ options:
for (ulong seed = 1; seed <= 60; seed++) picks.Add(DelegatedText(c, 0, seed));
Assert.True(picks.Count > 1, "competence 0 should choose at random among options");
}
+
+ static string Simple(string id, string anchor, string from, string? to = null) =>
+ $"id: {id}\ntitle: '{id} for {{defense_minister}}'\ntext: Something happens.\nanchor: {anchor}\nfrom: {from}\n" +
+ (to == null ? "" : $"to: {to}\n") +
+ "options:\n - id: ok\n text: Accept\n historical: true\n";
+
+ [Fact]
+ public void ShortWindowInsideAQuarterTurnStillFires()
+ {
+ // start_rung 1 gives quarter turns: 1948-05-15 -> 1948-08-15. The window 1948-06-01..06-11 lies wholly inside it.
+ var c = ContentLoader.Load(Fixture.Write(
+ new Dictionary<string, string> { ["rules.yaml"] = Fixture.Files["rules.yaml"].Replace("start_rung: 5", "start_rung: 1") },
+ new Dictionary<string, string> { ["brief.yaml"] = Simple("brief", "soft", "1948-06-01", "1948-06-11") }));
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ Assert.Empty(s.PendingEvents);
+ var r = Simulation.Step(c, s, new List<Command>());
+ Assert.Equal(new DateTime(1948, 8, 15), r.State.Date);
+ Assert.Equal(new[] { "brief" }, r.State.PendingEvents);
+ Assert.Contains(r.Why, w => w.Message == "New: brief for Pm One");
+ }
+
+ [Fact]
+ public void WindowThatEndedBeforeTheTurnDoesNotFire()
+ {
+ var c = ContentLoader.Load(Fixture.Write(
+ new Dictionary<string, string> { ["rules.yaml"] = Fixture.Files["rules.yaml"].Replace("start_rung: 5", "start_rung: 1") },
+ new Dictionary<string, string> { ["old.yaml"] = Simple("old", "soft", "1948-05-14", "1948-05-15") }));
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ Assert.Empty(Simulation.Step(c, s, new List<Command>()).State.PendingEvents);
+ }
+
+ [Fact]
+ public void HardAnchorIsNeverLostToTheCap()
+ {
+ var c = ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string>
+ {
+ ["a.yaml"] = Simple("a", "soft", "1948-05-15"),
+ ["b.yaml"] = Simple("b", "soft", "1948-05-15"),
+ ["c.yaml"] = Simple("c", "soft", "1948-05-15"),
+ ["z.yaml"] = Simple("z", "hard", "1948-05-15"),
+ }));
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ Assert.Equal(new[] { "a", "b", "z" }, s.PendingEvents); // hard first for the cap, then the usual order
+ }
+
+ [Fact]
+ public void EventFiredAfterItsWindowNamesTheInWindowHolder()
+ {
+ // Quarter turns: 1948-11-15 -> 1949-02-15. defense_minister passes from Pm One to Def Two on 1949-01-01.
+ var c = ContentLoader.Load(Fixture.Write(
+ new Dictionary<string, string> { ["rules.yaml"] = Fixture.Files["rules.yaml"].Replace("start_rung: 5", "start_rung: 1") },
+ new Dictionary<string, string> { ["late.yaml"] = Simple("late", "soft", "1948-12-20", "1948-12-30") }));
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ StepResult r = null!;
+ for (int i = 0; i < 3; i++) { r = Simulation.Step(c, s, new List<Command>()); s = r.State; }
+ Assert.Equal(new DateTime(1949, 2, 15), s.Date);
+ Assert.Contains(r.Why, w => w.Message == "New: late for Pm One");
+ }
}
diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.cs
index 7b9a5f8d4..84afb37c9 100644
--- a/sim/Statecraft.Sim/EventSystem.cs
+++ b/sim/Statecraft.Sim/EventSystem.cs
@@ -10,22 +10,32 @@ namespace Statecraft.Sim
static GameEvent Get(Content c, string id) => c.Events.First(e => e.Id == id);
- public static void Trigger(Content c, GameState s, List<Cause> why)
+ /// <summary>Fires every unfired event whose window [From, To) overlaps the turn just played,
+ /// [<paramref name="turnStart"/>, s.Date), or contains s.Date; <paramref name="turnStart"/> defaults to s.Date
+ /// (game start). At most MaxNewPerTurn fire: hard anchors claim the slots first, then the usual order
+ /// (From, then id). The fired events keep the usual order.</summary>
+ public static void Trigger(Content c, GameState s, List<Cause> why, DateTime? turnStart = null)
{
+ var start = turnStart ?? s.Date;
var eligible = c.Events
- .Where(e => !s.FiredEvents.Contains(e.Id) && e.From <= s.Date && (e.To == null || s.Date < e.To.Value))
+ .Where(e => !s.FiredEvents.Contains(e.Id) && e.From <= s.Date && (e.To == null || start < e.To.Value))
.Where(e => e.Conditions.All(k => (!k.Min.HasValue || s.Indicators[k.Indicator] >= k.Min.Value)
&& (!k.Max.HasValue || s.Indicators[k.Indicator] <= k.Max.Value)))
.OrderBy(e => e.From).ThenBy(e => e.Id, StringComparer.Ordinal)
- .Take(MaxNewPerTurn);
- foreach (var e in eligible)
+ .ToList();
+ var chosen = new HashSet<string>(eligible.OrderBy(e => e.Anchor == "hard" ? 0 : 1).Take(MaxNewPerTurn).Select(e => e.Id));
+ foreach (var e in eligible.Where(e => chosen.Contains(e.Id)))
{
s.FiredEvents.Add(e.Id);
s.PendingEvents.Add(e.Id);
- why.Add(new Cause("events", e.Id, 0, $"New: {Offices.Fill(c, e.Title, s.Date)}"));
+ why.Add(new Cause("events", e.Id, 0, $"New: {Offices.Fill(c, e.Title, FillDate(e, s.Date))}"));
}
}
+ /// <summary>The date whose office holders an event's text names: the given date, or the last in-window day
+ /// once the window has ended (load-time coverage guarantees a holder there).</summary>
+ public static DateTime FillDate(GameEvent e, DateTime date) => e.To.HasValue && date >= e.To.Value ? e.To.Value.AddDays(-1) : date;
+
/// <summary>Returns null on success, or the reason the choice was rejected.</summary>
public static string? Choose(Content c, GameState s, string eventId, string optionId, List<Cause> why)
{
@@ -39,10 +49,7 @@ namespace Statecraft.Sim
static void Resolve(Content c, GameState s, string eventId, EventOption o, string how, List<Cause> why)
{
s.PendingEvents.Remove(eventId);
- // Resolved on or after the window end: name whoever held the office on the last in-window day
- // (load-time coverage guarantees a holder there); otherwise today's holder.
- var e = Get(c, eventId);
- var fillDate = e.To.HasValue && s.Date >= e.To.Value ? e.To.Value.AddDays(-1) : s.Date;
+ var fillDate = FillDate(Get(c, eventId), s.Date);
why.Add(new Cause("events", eventId, 0, $"{how}: {Offices.Fill(c, o.Text, fillDate)}"));
foreach (var fx in o.Effects)
{
@@ -89,6 +96,6 @@ namespace Statecraft.Sim
s.RngState = rng.State;
}
- public static string Render(Content c, GameEvent e, DateTime date) => Offices.Fill(c, e.Text, date);
+ public static string Render(Content c, GameEvent e, DateTime date) => Offices.Fill(c, e.Text, FillDate(e, date));
}
}
diff --git a/sim/Statecraft.Sim/Simulation.cs b/sim/Statecraft.Sim/Simulation.cs
index 703ac084c..fb9501d8b 100644
--- a/sim/Statecraft.Sim/Simulation.cs
+++ b/sim/Statecraft.Sim/Simulation.cs
@@ -59,10 +59,11 @@ namespace Statecraft.Sim
PoliticsSystem.UpdateStability(c, s, yf, why);
SecuritySystem.Apply(c, s, len, why);
PoliticsSystem.ReportChanges(c, s.Date, next, why);
+ var turnStart = s.Date;
s.Date = next;
s.Turn++;
AttentionSystem.Refill(c, s);
- if (s.GameOver == null) EventSystem.Trigger(c, s, why); // a game that just ended raises no new events
+ if (s.GameOver == null) EventSystem.Trigger(c, s, why, turnStart); // a game that just ended raises no new events
return new StepResult(s, why, rejected);
}