A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
sim: GDD Phase 7 PR 1, the event rules in the GDD loop with IDs (D-H1, refs #805) (#898)
Beri956bdd268440
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5e280ec66..557fbc1f8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -198,6 +198,10 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIBthe reference player's halutz group becomes a veteran after Beit Kama and builds Gilat, Mefalsim and Re'im in 3 turnseach, so the first new yishuv completes on 1949-09-15 (was 1950-03-15), and by 1950 the score is 100 and the debt isIL 2m higher. The 1952 score and debt do not change.+- The GDD loop's event rules have IDs and tests (D-H1; GDD Phase 7 PR 1, refs #805): R-EVT-001 to R-EVT-005 in+ `docs/rules/mvp.md`, as the owner decided Q-EVT-1 to Q-EVT-4 on 17 October 2026 (#784). Hard anchors always fire, at+ most 3 soft events a turn in order of `from` then id, unanswered events are delegated, and no effect is delayed; no+ behaviour changes and no golden moves. `EventSystem.OptionEffects` lists every effect of an option for the event card.- City and town pills start with their population tier, a gold disc with a number from 1 to 9 (refs #710). The tier isstatic: it comes from the city's 1948 people in `content/cities.yaml` (#821), not from the sim's live count (owner,15 October 2026). The scale is the design value proposed on #710 and not yet ruled: tiers double from 1,000, so underdiff --git a/docs/gdd/ministers-events-end.md b/docs/gdd/ministers-events-end.mdindex 700f01c7b..86f26fd00 100644--- a/docs/gdd/ministers-events-end.md+++ b/docs/gdd/ministers-events-end.md@@ -7,10 +7,11 @@ As a player of the MVP, I want the game to run its events and my chosen minister## Rules- The MVP runs 44 turns (monthly), from 15 May 1948 to 15 January 1952: D-B1 fixes the start and the monthly grid [S2], and the owner ended the MVP on 15 January 1952, keeping the 15th grid with no adjusted turn (D-E5, decided 15 October 2026 [S2]). The GDD text still says "approximately 48 turns" [S1]; the owner settled 44 turns [S2].-- Objective cards and scoring belong to the first phase (#379, `objectives-and-score.md`; D-B2, D-B6 [S2]) and are not restated here. This section covers only what the final phase adds: every card complete, the end screen (D-H4, open), defeat (D-B5, decided 15 October 2026 [S2]), the events (D-H1, D-H2, open) and the ministers (D-H3, open) [S2].+- Objective cards and scoring belong to the first phase (#379, `objectives-and-score.md`; D-B2, D-B6 [S2]) and are not restated here. This section covers only what the final phase adds: every card complete, the end screen (D-H4, open), defeat (D-B5, decided 15 October 2026 [S2]), the events (D-H1, decided 17 October 2026 [S5]; D-H2, open) and the ministers (D-H3, open) [S2].- Defeat: not all objectives are required to finish. The game is lost when Money and Food are exhausted for several turns in a row or on a war loss, and coalition collapse is off in the MVP (D-B5, decided 15 October 2026 [S2]). The defeat is built in this phase, with the counter set then (`Q-GOV-2`, decided 15 October 2026 [S4]). A war loss is enemy units holding the hex of one of the player's cities at turn end (`Q-WAR-5`, decided 15 October 2026 [S4]); what "Money exhausted" means now that borrowing is allowed stays open.- The war-survival objective completes on the last armistice event, the Syria-Israel agreement of 20 July 1949 (D-B2 [S2]; [S3]). The four signings were Egypt on 24 February 1949, Lebanon on 23 March 1949, Jordan on 3 April 1949 and Syria on 20 July 1949 [S3].- The existing cabinet and Attention stay (D-C5 [S2]). Minister roles, candidates and effects are D-H3, open [S2].+- Events (D-H1): the owner took the recommendations for `Q-EVT-1` to `Q-EVT-4` on 17 October 2026 [S5]. Windows, hard anchors and a cap of three soft events a turn stay; an unanswered event is delegated to its office's holder; no effect is delayed; the card shows every effect of every option from the sim (`R-EVT-001` to `R-EVT-005`, `docs/rules/mvp.md`). An early armistice's place ahead of the other soft events comes with `front_quiet`, in the war-objective PR of the military phase (#752).## Data touched@@ -26,7 +27,7 @@ As a player of the MVP, I want the game to run its events and my chosen minister## Out of scope-- Event frequency, windows and caps (D-H1), event effect targets (D-H2) and minister roles and effects (D-H3): all open.+- Event effect targets (D-H2) and minister roles and effects (D-H3): open.- The objectives panel UI and the score's presentation in Phase 1.- The exact completion conditions of each objective (GDD "MVP completion rules" [S1]): open.- Objective 1's bonus for capturing enemy soldiers lands at the prisoners' return (the plan, section 4.3 [S2]): Phase 5/6.@@ -37,10 +38,11 @@ As a player of the MVP, I want the game to run its events and my chosen minister- [S2] GDD v0.1 impact analysis and phased plan, sections 5 and 6, docs/superpowers/specs/2026-10-12-gdd-v0.1-impact-and-plan.md- [S3] The 1949 armistice layer, content/map/armistice_hexes_1949.yaml (signing dates per agreement)- [S4] The owner's phase answers announced on #375, 15 October 2026, https://github.com/eyalgolan/statecraft-israel/issues/375#issuecomment-6180592337, checked 2026-10-15+- [S5] The owner's Phase 7 decision on #784, 17 October 2026, https://github.com/eyalgolan/statecraft-israel/issues/784#issuecomment-6232838937, checked 2026-10-17## Open questions-- Minister roles, candidates and effects (D-H3); event frequency and caps (D-H1); event effects on units, cities and projects (D-H2).+- Minister roles, candidates and effects (D-H3); event effects on units, cities and projects (D-H2).- Bonus amounts and the timing formula's remaining parts (GDD "MVP completion rules" [S1]).- What "Money exhausted" means for defeat now that borrowing is allowed (D-B5 follow-up [S4]).- The GDD's "approximately 48 turns" [S1] against the decided 44 turns [S2]: a one-line GDD fix for the owner.diff --git a/docs/rules/mvp.md b/docs/rules/mvp.mdindex 8711b3286..b88f6fc92 100644--- a/docs/rules/mvp.md+++ b/docs/rules/mvp.md@@ -133,3 +133,17 @@ R-NAT-004: "Low international support" (`low_intl_support`) is a situation on `iR-EFX-001: An option's effect may target an indicator id from `model.yaml`, `treasury` or `fx_reserves` (game money), or `food`, the national Food stock: the effect moves the stock by whole units (the delta rounds half away from zero) and the stock never drops below 0, so a cut larger than the stock applies only to what is there. Any other target is a load error naming file and field. The Arms stock becomes a target with its pool (#781).R-EFX-002: An option of an event tagged `holocaust`, `terror`, `massacre` or `war-casualties` cannot raise `treasury`, `fx_reserves`, `food`, `food_supply`, `morale` or `intl_standing` (Q-EFX-2, decided 15 October 2026: the no-gain rule covers all five resources, with the legacy indicators standing for Food, Social cohesion and International Support; `food` is Food's own target, and Arms joins the rule with its pool, #781). A cost stays allowed. The rule for the Phase 6 `displacement` tag is Q-DEP-6 (open).++## Events++D-H1 in the GDD loop (GDD Phase 7 PR 1, #805). The owner took the plan's recommendations for Q-EVT-1 to Q-EVT-4 on 17 October 2026 (#784). As the plan's Decided rows record them (`docs/superpowers/plans/2026-10-15-gdd-phase-7.md`), that keeps windows, hard anchors and the cap of 3 soft events a turn, keeps delegation, adds no delayed effects in the MVP, and shows every effect of every option from the sim's preview. These rules state what the code already does; no behaviour changed.++R-EVT-001: An event fires on the first turn whose span overlaps its window `[from, to)` (or at the new game, when the start date is in it), once its `requires` is met and its conditions hold; an objective's card also waits for its objective (R-OBJ-003). Every eligible hard anchor fires. At most 3 soft events fire a turn (`EventSystem.MaxSoftEventsPerTurn`); they take the slots the hard anchors leave, `max(0, 3 - hard anchors)`, and the rest wait for a later turn (Q-CUT-6).++R-EVT-002: The soft events take the free slots in order of `from`, then id (ordinal). The decided place of an early armistice ahead of the other soft events (Q-EVT-1, through `front_quiet`, Q-OBJ-1) lands with that key in Phase 5 PR 13 (#752), and this rule is reworded then.++R-EVT-003: An event the player leaves unanswered at End Turn is delegated to its office's holder (Q-EVT-2): with probability competence/10 the holder picks the recommended option (else the first), otherwise a random option. In Historical mode a hard anchor takes its historical option, and an objective's card its one option, with no RNG draw.++R-EVT-004: An option's effects apply when it is chosen or delegated, on that turn, and never later; an option has no delayed form, and the loader refuses an unknown key such as `delay` (Q-EVT-3).++R-EVT-005: One sim helper, `EventSystem.OptionEffects`, lists every effect of an option in the order choosing it applies them: the numeric effects, the policies it sets (by id), then the security level, each with the option's own amount (Q-EVT-4). The event card will show this list (GDD Phase 7 PR 8), so no number will be computed in Unity.diff --git a/sim/Statecraft.Sim.Tests/EventRulesGddTests.cs b/sim/Statecraft.Sim.Tests/EventRulesGddTests.csnew file mode 100644index 000000000..faafcc207--- /dev/null+++ b/sim/Statecraft.Sim.Tests/EventRulesGddTests.cs@@ -0,0 +1,213 @@+using System;+using System.Collections.Generic;+using System.Linq;+using Statecraft.Sim;++/// <summary>The event rules of the GDD loop (D-H1; GDD Phase 7 PR 1, #805). The owner took the plan's recommendations+/// for Q-EVT-1 to Q-EVT-4 on 17 October 2026 (#784): keep windows, hard anchors, the cap of 3 soft events a turn and+/// delegation, add no delayed effects, and show every effect of every option. These tests pin those rules on the real+/// monthly content (the mvp-end run) and on small fixtures. The armistice's place ahead of other soft events+/// (TheCapNeverDelaysAnArmistice) needs `front_quiet` and lands with Phase 5 PR 13 (#752).</summary>+public class EventRulesGddTests+{+ static readonly Content C = ContentLoader.Load(RealContentTests.ContentRoot());++ /// <summary>Every state of the mvp-end run (GoldenRun.Orders, seed 1948, Historical mode), the new game first.</summary>+ static readonly List<GameState> Run = Play();++ static List<GameState> Play()+ {+ var s = Simulation.NewGame(C, GoldenRun.Seed, Mode.Historical);+ var run = new List<GameState> { s };+ while (s.GameOver == null && run.Count < 1000) { s = Simulation.Step(C, s, GoldenRun.Orders(C, s)).State; run.Add(s); }+ return run;+ }++ static GameEvent Event(string id) => C.Events.First(e => e.Id == id);++ /// <summary>The events that fired on the turn that ended in Run[i] (for i = 0, those raised by the new game).</summary>+ static List<GameEvent> FiredOn(int i) =>+ Run[i].FiredEvents.Where(id => i == 0 || !Run[i - 1].FiredEvents.Contains(id)).Select(Event).ToList();++ // R-EVT-001: every hard anchor fires on the first turn its window overlaps, when nothing else (requires, conditions,+ // an objective) holds it back.+ [Fact]+ public void HardAnchorsAlwaysFire()+ {+ Assert.Equal(GameOverCodes.MvpEnded, Run[Run.Count - 1].GameOver);+ var plain = C.Events.Where(e => e.Anchor == "hard" && e.Requires == null && e.Conditions.Count == 0 && e.Objective == "").ToList();+ Assert.NotEmpty(plain);+ foreach (var e in plain)+ {+ // The first state whose turn overlaps the window: [previous date, date) meets [From, To), or the new game is in it.+ var first = Enumerable.Range(0, Run.Count).First(i => e.From <= Run[i].Date && (e.To == null || (i == 0 ? Run[0].Date : Run[i - 1].Date) < e.To.Value));+ Assert.True(FiredOn(first).Contains(e), $"{e.Id} did not fire on {Run[first].Date:yyyy-MM-dd}, the first turn its window overlaps");+ }+ }++ // R-EVT-001: at most MaxSoftEventsPerTurn (3) soft events fire on any turn of the mvp-end run.+ [Fact]+ public void AtMostThreeSoftEventsATurn()+ {+ Assert.Equal(3, EventSystem.MaxSoftEventsPerTurn);+ for (int i = 0; i < Run.Count; i++)+ {+ var soft = FiredOn(i).Where(e => e.Anchor != "hard").Select(e => e.Id).ToList();+ var hard = FiredOn(i).Count(e => e.Anchor == "hard");+ // the soft events take only the slots the hard anchors leave+ Assert.True(soft.Count <= Math.Max(0, EventSystem.MaxSoftEventsPerTurn - hard), $"{Run[i].Date:yyyy-MM-dd}: {hard} hard, soft {string.Join(", ", soft)}");+ }+ }++ static string Soft(string id, string from) =>+ $"id: {id}\ntitle: '{id}'\ntext: Something happens.\nfrom: {from}\noptions:\n - id: ok\n text: Accept\n historical: true\n";++ // R-EVT-001: the soft events take only the slots the hard anchors leave. With 1 hard anchor and 3 soft events+ // eligible on one turn, exactly 2 soft events fire (the mvp-end run has no such turn, so a fixture pins it).+ [Fact]+ public void HardAnchorsUseUpSoftSlots()+ {+ var c = ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string>+ {+ ["a.yaml"] = Soft("a", "1948-05-15"),+ ["b.yaml"] = Soft("b", "1948-05-15"),+ ["c.yaml"] = Soft("c", "1948-05-15"),+ ["h.yaml"] = Soft("h", "1948-05-15").Replace("from:", "anchor: hard\nfrom:"),+ }));+ var s = Simulation.NewGame(c, 1, Mode.Historical);+ Assert.Equal(new[] { "a", "b", "h" }, s.PendingEvents); // h takes one of the 3 slots; c waits+ }++ // R-EVT-002: the soft events take the free slots in order of From, then id; the one left over waits for the next turn.+ [Fact]+ public void SoftEventsTakeTheSlotsInOrderOfFromThenId()+ {+ var c = ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string>+ {+ ["a.yaml"] = Soft("a", "1948-05-15"),+ ["b.yaml"] = Soft("b", "1948-05-15"),+ ["c.yaml"] = Soft("c", "1948-05-15"),+ ["d.yaml"] = Soft("d", "1948-05-01"),+ }));+ var s = Simulation.NewGame(c, 1, Mode.Historical);+ Assert.Equal(new[] { "d", "a", "b" }, s.PendingEvents); // d opened first; then a and b by id; c finds no slot+ var next = Simulation.Step(c, s, new List<Command>()).State;+ Assert.Equal(new[] { "c" }, next.PendingEvents);+ }++ const string Choice = @"+id: choice+title: A choice+text: '{finance_minister} asks for a decision.'+office: finance_minister+anchor: {ANCHOR}+from: 1948-05-15+options:+ - id: first+ text: The first way+ recommended: true+ effects:+ - target: fx_reserves+ delta: -10+ - id: history+ text: The way it went+ historical: true+ effects:+ - target: food_supply+ delta: 0.1+ set_policy:+ rationing: 0.4+ set_rung: 3+";++ static Content ChoiceContent(string anchor) =>+ ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["choice.yaml"] = Choice.Replace("{ANCHOR}", anchor) }));++ // R-EVT-003: an event the player leaves unanswered at End Turn is delegated to its office's holder; in Historical+ // mode a hard anchor takes its historical option with no RNG draw.+ [Fact]+ public void AnUnansweredEventIsDelegated()+ {+ var c = ChoiceContent("soft");+ var s = Simulation.NewGame(c, 1, Mode.Historical);+ Assert.Equal(new[] { "choice" }, s.PendingEvents);+ var r = Simulation.Step(c, s, new List<Command>());+ Assert.Empty(r.State.PendingEvents);+ Assert.True(r.State.ChosenOptions.ContainsKey("choice"));+ Assert.Contains(r.Why, w => w.System == "events" && w.Target == "choice" && w.Message.StartsWith("Delegated to "));++ var hard = ChoiceContent("hard");+ var h = Simulation.NewGame(hard, 1, Mode.Historical);+ var rng = h.RngState;+ EventSystem.Delegate(hard, h, new List<Cause>());+ Assert.Equal("history", h.ChosenOptions["choice"]); // not the recommended one: the historical option, always+ Assert.Equal(rng, h.RngState); // and no draw+ }++ // R-EVT-004: an option's effects apply on the turn it is chosen, never later; no option can carry a delay.+ [Fact]+ public void NoEffectArrivesLater()+ {+ var c = ChoiceContent("soft");+ var s = Simulation.NewGame(c, 1, Mode.Historical);+ var first = Simulation.Step(c, s, new List<Command> { new ChooseEventOption("choice", "first") });+ Assert.Contains(first.Why, w => w.System == "events" && w.Target == "fx_reserves" && w.Delta == -10);+ var later = first.State;+ for (int turn = 0; turn < 3; turn++)+ {+ var r = Simulation.Step(c, later, new List<Command>());+ // Nothing of the event comes back on a later turn. An effect is applied only in Resolve, which writes an+ // "events" cause for each change, so the causes are the record to check.+ Assert.DoesNotContain(r.Why, w => w.System == "events");+ later = r.State;+ }+ var delayed = Choice.Replace("{ANCHOR}", "soft").Replace(" delta: -10\n", " delta: -10\n delay: 2\n");+ Assert.Contains("delay", delayed);+ var refused = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["choice.yaml"] = delayed })));+ Assert.Contains("delay", refused.Message);+ }++ // R-EVT-005: one sim helper lists every effect of an option, in the order they apply: the numeric effects, the+ // policies it sets, then the security level.+ [Fact]+ public void ThePreviewListsEveryEffectOfAnOption()+ {+ var c = ChoiceContent("soft");+ var s = Simulation.NewGame(c, 1, Mode.Historical);+ var e = c.Events.First(x => x.Id == "choice");+ var lines = EventSystem.OptionEffects(c, s, e.Options[1]);+ Assert.Equal(new[] { "food_supply", "rationing", "rung" }, lines.Select(l => l.Target));+ Assert.Equal(new[] { 0.1, 0.4, 3.0 }, lines.Select(l => l.Delta));+ Assert.Equal(PlayerText.Capitalised(PlayerText.Change(c, "food_supply", 0.1)), lines[0].Message);+ Assert.All(lines, l => Assert.Equal("events", l.System));+ var money = EventSystem.OptionEffects(c, s, e.Options[0]).Single();+ Assert.Equal(("fx_reserves", -10.0), (money.Target, money.Delta));++ // What the helper lists is what choosing the option applies.+ var why = new List<Cause>();+ Assert.Null(EventSystem.Choose(c, s, "choice", "history", why));+ Assert.Equal(new[] { "food_supply", "rationing" }, why.Where(w => w.System == "events" && w.Target != "choice").Select(w => w.Target));+ Assert.Equal(3, s.Rung);+ }++ // R-EVT-005: on the real content, for every option of every event on the turn its card reaches the player, the+ // helper lists exactly the option's effects, policies and security level.+ [Fact]+ public void ThePreviewListsEveryEffectOnTheRealContent()+ {+ var checkedOptions = 0;+ foreach (var e in C.Events)+ {+ var s = Run.First(x => x.FiredEvents.Contains(e.Id)); // every event fires in the run (EventRetimingTests)+ foreach (var o in e.Options)+ {+ var expected = o.Effects.Select(fx => (fx.Target, fx.Delta))+ .Concat(o.SetPolicy.OrderBy(k => k.Key, StringComparer.Ordinal).Select(k => (k.Key, k.Value)))+ .Concat(o.SetRung.HasValue ? new[] { ("rung", (double)o.SetRung.Value) } : new (string, double)[0]);+ Assert.Equal(expected, EventSystem.OptionEffects(C, s, o).Select(l => (l.Target, l.Delta)));+ checkedOptions++;+ }+ }+ Assert.True(checkedOptions > 60, $"only {checkedOptions} options");+ }+}diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.csindex d622f383c..642262553 100644--- a/sim/Statecraft.Sim/EventSystem.cs+++ b/sim/Statecraft.Sim/EventSystem.cs@@ -63,8 +63,7 @@ namespace Statecraft.Simvar fillDate = FillDate(Get(c, eventId), s.Date);var title = Cabinet.Fill(c, s, Get(c, eventId).Title, fillDate);why.Add(new Cause("events", eventId, 0, $"{how}: {Cabinet.Fill(c, s, o.Text, fillDate)}"));- // A late-fired event can be resolved after a dated record's window has ended (chapters design 3.2): nothing reaches it.- foreach (var fx in o.Effects.Where(fx => EventRules.SpecialTargets.Contains(fx.Target) || s.Indicators.ContainsKey(fx.Target)))+ foreach (var fx in o.Effects.Where(fx => Reaches(s, fx.Target))){var change = fx.Delta;if (fx.Target == "treasury")@@ -97,6 +96,24 @@ namespace Statecraft.Simif (o.SetRung.HasValue) SecuritySystem.SetRung(c, s, o.SetRung.Value, why);}+ /// <summary>A late-fired event can be resolved after a dated record's window has ended (chapters design 3.2): an+ /// effect on a target that no longer exists reaches nothing.</summary>+ static bool Reaches(GameState s, string target) => EventRules.SpecialTargets.Contains(target) || s.Indicators.ContainsKey(target);++ /// <summary>R-EVT-005: every effect of an option, in the order choosing it applies them (Resolve): the numeric+ /// effects, the policies it sets (by id), then the security level. Each amount is the option's own; Resolve may+ /// apply less where an indicator meets its bound or the Food stock its floor, and rounds Food to whole units. The+ /// event card will read this list (Q-EVT-4, owner 17 October 2026; Phase 7 PR 8), so no number is computed in Unity.</summary>+ public static List<Cause> OptionEffects(Content c, GameState s, EventOption o)+ {+ var lines = o.Effects.Where(fx => Reaches(s, fx.Target))+ .Select(fx => new Cause("events", fx.Target, fx.Delta, PlayerText.Change(c, fx.Target, fx.Delta))).ToList();+ lines.AddRange(o.SetPolicy.Where(k => s.Policies.ContainsKey(k.Key)).OrderBy(k => k.Key, StringComparer.Ordinal)+ .Select(k => new Cause("events", k.Key, k.Value, $"{PlayerText.Target(c, k.Key)} set to {PlayerText.Percent(k.Value)}")));+ if (o.SetRung.HasValue) lines.Add(new Cause("events", "rung", o.SetRung.Value, FormattableString.Invariant($"Security level set to {o.SetRung.Value}")));+ return lines;+ }+/// <summary>Unanswered events go to the responsible minister: with probability competence/10 they pick the/// 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). An objective's card has one