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.

sim: events can require another event's chosen option

eyalgolanacd8ab2abbfd
diff --git a/docs/determinism.md b/docs/determinism.md
index aaf8a0499..5b89a5db8 100644
--- a/docs/determinism.md
+++ b/docs/determinism.md
@@ -22,8 +22,8 @@ with `GoldenRun.Seed = 1948` on the real `content/`, then steps with `Historical
| Run | Ends | Hash |
|---|---|---|
-| to 1950-01-01 (`Hash1950`) | 1950-02-27, turn 35 | `a540b52e10474615b703a2b14b7c742adbde84611496074b93a5116ef654899c` |
-| to 1952-01-01 (`Hash1952`) | 1952-02-27, turn 43 | `16c16d7ec4f5419d31eb537d70924b8681b95ec94d7ed70874c0749292446e60` |
+| to 1950-01-01 (`Hash1950`) | 1950-02-27, turn 35 | `d463d5b22c9f98cf6b604a2f31738f01ca4b72cd3b2fb3c58a6d31790f4ff26e` |
+| to 1952-01-01 (`Hash1952`) | 1952-02-27, turn 43 | `f78fc68d91fce73bc12e31c3a6eade4e889fd7e6d3002c5b9821075e31ab1dfb` |
Each constant is written in three places that must agree:
- `sim/Statecraft.Sim/GoldenRun.cs`, which Unity code and players compare against;
@@ -70,3 +70,5 @@ rules or content, treat it as a determinism bug.
| M3 Task 8 fix | 1952 | `91bb6fb5` | same |
| playtest 1 E | 1950 | `a540b52e` | 1948 war-period events added (playtest 1 E): thirteen events whose windows open between 26 May and 31 December 1948; the second truce puts the run on monthly turns from 17 July to 17 October 1948, so the run ends 1950-02-27 after 35 turns (was 1950-02-26, turn 45) |
| playtest 1 E | 1952 | `16c16d7e` | same; ends 1952-02-27 after 43 turns (was 1952-02-26, turn 53) |
+| playtest 1 E fix 1 | 1950 | `d463d5b2` | events gain `requires: {event, option}`; the hash text gains chosen{event=option;...} (the options each resolved event was resolved with); no change in the run |
+| playtest 1 E fix 1 | 1952 | `f78fc68d` | same |
diff --git a/sim/Statecraft.Sim.Tests/DeterminismTests.cs b/sim/Statecraft.Sim.Tests/DeterminismTests.cs
index 02aaec5fb..69e9653db 100644
--- a/sim/Statecraft.Sim.Tests/DeterminismTests.cs
+++ b/sim/Statecraft.Sim.Tests/DeterminismTests.cs
@@ -19,7 +19,7 @@ public class DeterminismTests
/// <summary>Any change to this value is a deliberate rules or content change: update it here, in
/// GoldenRun.Hash1950 and in docs/determinism.md in the same commit.</summary>
- public const string GoldenHash = "a540b52e10474615b703a2b14b7c742adbde84611496074b93a5116ef654899c";
+ public const string GoldenHash = "d463d5b22c9f98cf6b604a2f31738f01ca4b72cd3b2fb3c58a6d31790f4ff26e";
/// <summary>Real content, seed 1948, Historical mode, HistoricalAI until the date reaches 1950-01-01.</summary>
public static GameState GoldenPlaythrough(Content c) => GoldenRun.Play(c, GoldenRun.Until1950);
@@ -34,7 +34,7 @@ public class DeterminismTests
}
/// <summary>Same rule for the second golden run, which covers the M3 content range (to 1952-01-01).</summary>
- public const string GoldenHash1952 = "16c16d7ec4f5419d31eb537d70924b8681b95ec94d7ed70874c0749292446e60";
+ public const string GoldenHash1952 = "f78fc68d91fce73bc12e31c3a6eade4e889fd7e6d3002c5b9821075e31ab1dfb";
[Fact]
public void GoldenPlaythroughTo1952HashIsFixed()
diff --git a/sim/Statecraft.Sim.Tests/EventRequiresTests.cs b/sim/Statecraft.Sim.Tests/EventRequiresTests.cs
new file mode 100644
index 000000000..a0c93608a
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/EventRequiresTests.cs
@@ -0,0 +1,116 @@
+using System;
+using System.Collections.Generic;
+using Statecraft.Sim;
+
+/// <summary>`requires: { event, option }`: an event is eligible only after another event was resolved with that option
+/// (fix round 1, I2: a follow-up card must not contradict the player's earlier choice).</summary>
+public class EventRequiresTests
+{
+ const string First = @"
+id: first
+title: First
+text: 'A choice.'
+from: 1948-06-01
+options:
+ - id: go
+ text: Go
+ historical: true
+ - id: stay
+ text: Stay
+";
+
+ const string Follow = @"
+id: follow
+title: Follow-up
+text: 'After going.'
+from: 1948-06-01
+requires: { event: first, option: go }
+options:
+ - id: ok
+ text: Fine
+";
+
+ static Content Load(string follow) =>
+ ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["first.yaml"] = First, ["follow.yaml"] = follow }));
+
+ static GameState State(Content c)
+ {
+ var s = new GameState { Date = new DateTime(1948, 6, 1), RngState = 3 };
+ EffectsSystem.Init(c, s); EconomySystem.Init(c, s);
+ return s;
+ }
+
+ [Fact]
+ public void TheRequirementIsReadFromYaml()
+ {
+ var e = Load(Follow).Events.Find(x => x.Id == "follow")!;
+ Assert.Equal("first", e.Requires!.Event);
+ Assert.Equal("go", e.Requires.Option);
+ Assert.Null(Load(Follow).Events.Find(x => x.Id == "first")!.Requires);
+ }
+
+ [Fact]
+ public void ItWaitsUntilTheRequiredOptionIsChosen()
+ {
+ var c = Load(Follow);
+ var s = State(c);
+ var why = new List<Cause>();
+ EventSystem.Trigger(c, s, why);
+ Assert.Equal(new[] { "first" }, s.PendingEvents); // the follow-up is not eligible yet
+ Assert.Null(EventSystem.Choose(c, s, "first", "go", why));
+ Assert.Equal("go", s.ChosenOptions["first"]);
+ EventSystem.Trigger(c, s, why);
+ Assert.Equal(new[] { "follow" }, s.PendingEvents);
+ }
+
+ [Fact]
+ public void AnotherOptionKeepsTheFollowUpAway()
+ {
+ var c = Load(Follow);
+ var s = State(c);
+ var why = new List<Cause>();
+ EventSystem.Trigger(c, s, why);
+ Assert.Null(EventSystem.Choose(c, s, "first", "stay", why));
+ EventSystem.Trigger(c, s, why);
+ Assert.Empty(s.PendingEvents);
+ Assert.DoesNotContain("follow", s.FiredEvents);
+ }
+
+ [Fact]
+ public void ADelegatedChoiceCountsToo()
+ {
+ var c = Load(Follow);
+ var s = State(c);
+ s.Mode = Mode.Historical;
+ var why = new List<Cause>();
+ EventSystem.Trigger(c, s, why);
+ EventSystem.Delegate(c, s, why);
+ Assert.True(s.ChosenOptions.ContainsKey("first"));
+ }
+
+ [Theory]
+ [InlineData("requires: { event: nope, option: go }", "events[follow].requires.event", "unknown event 'nope'")]
+ [InlineData("requires: { event: first, option: nope }", "events[follow].requires.option", "event 'first' has no option 'nope'")]
+ [InlineData("requires: { event: follow, option: ok }", "events[follow].requires.event", "an event cannot require itself")]
+ [InlineData("requires: { event: '', option: go }", "events[follow].requires.event", "unknown event ''")]
+ public void ABrokenRequirementIsALoadErrorNamingTheFile(string line, string field, string message)
+ {
+ var ex = Assert.Throws<ContentException>(() => Load(Follow.Replace("requires: { event: first, option: go }", line)));
+ Assert.Equal("events/follow.yaml", ex.File);
+ Assert.Equal(field, ex.Field);
+ Assert.Contains(message, ex.Message);
+ }
+
+ [Fact]
+ public void ChosenOptionsAreClonedAndHashed()
+ {
+ var a = new GameState { Date = new DateTime(1948, 6, 1) };
+ var b = a.Clone();
+ b.ChosenOptions["first"] = "go";
+ Assert.Empty(a.ChosenOptions);
+ Assert.NotEqual(a.Hash(), b.Hash());
+ var c = a.Clone();
+ c.ChosenOptions["first"] = "stay";
+ Assert.NotEqual(b.Hash(), c.Hash());
+ }
+}
diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.cs
index 0856bf8db..89829f3e9 100644
--- a/sim/Statecraft.Sim/Content.cs
+++ b/sim/Statecraft.Sim/Content.cs
@@ -175,6 +175,9 @@ namespace Statecraft.Sim
public sealed class EventCondition { public string Indicator { get; set; } = ""; public double? Min { get; set; } public double? Max { get; set; } }
+ /// <summary>An event's precondition: <see cref="Event"/> must have been resolved with <see cref="Option"/>.</summary>
+ public sealed class EventRequirement { public string Event { get; set; } = ""; public string Option { get; set; } = ""; }
+
public sealed class OptionEffect { public string Target { get; set; } = ""; public double Delta { get; set; } }
public sealed class EventOption
@@ -200,6 +203,8 @@ namespace Statecraft.Sim
public DateTime From { get; set; }
public DateTime? To { get; set; }
public List<EventCondition> Conditions { get; set; } = new List<EventCondition>();
+ /// <summary>Eligible only once this other event was resolved with this option (null: no requirement).</summary>
+ public EventRequirement? Requires { get; set; }
public List<EventOption> Options { get; set; } = new List<EventOption>();
public List<Source> Sources { get; set; } = new List<Source>();
public string Status { get; set; } = "draft";
diff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.cs
index 5c2916126..b9da60f11 100644
--- a/sim/Statecraft.Sim/EventRules.cs
+++ b/sim/Statecraft.Sim/EventRules.cs
@@ -33,6 +33,14 @@ namespace Statecraft.Sim
if (cond.Max.HasValue) { ContentLoader.Finite(e.File, F($"conditions.{cond.Indicator}.max"), cond.Max.Value); ContentLoader.Unit(e.File, F($"conditions.{cond.Indicator}.max"), cond.Max.Value); }
if (cond.Min > cond.Max) throw new ContentException(e.File, F($"conditions.{cond.Indicator}.min"), "must not exceed max");
}
+ if (e.Requires != null)
+ {
+ if (e.Requires.Event == e.Id) throw new ContentException(e.File, F("requires.event"), "an event cannot require itself");
+ var required = c.Events.FirstOrDefault(x => x.Id == e.Requires.Event)
+ ?? throw new ContentException(e.File, F("requires.event"), $"unknown event '{e.Requires.Event}'");
+ if (required.Options.All(o => o.Id != e.Requires.Option))
+ throw new ContentException(e.File, F("requires.option"), $"event '{required.Id}' has no option '{e.Requires.Option}'");
+ }
if (e.Options.Count == 0) throw new ContentException(e.File, F("options"), "at least one option required");
if (e.Options.Count(o => o.Historical) > 1) throw new ContentException(e.File, F("options"), "at most one option can be historical");
if (e.Anchor == "hard" && !e.Options.Any(o => o.Historical)) throw new ContentException(e.File, F("options"), "hard anchor needs a historical option");
diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.cs
index ac379b232..739b9d617 100644
--- a/sim/Statecraft.Sim/EventSystem.cs
+++ b/sim/Statecraft.Sim/EventSystem.cs
@@ -10,7 +10,7 @@ namespace Statecraft.Sim
static GameEvent Get(Content c, string id) => c.Events.First(e => e.Id == id);
- /// <summary>Fires every unfired event whose window [From, To) overlaps the turn just played,
+ /// <summary>Fires every unfired event whose `requires` (if any) is met, 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). Every eligible hard anchor fires; MaxSoftEventsPerTurn caps only the non-hard events per turn,
/// which fill the slots that remain (max(0, cap - hard count)) in the usual order (From, then id). The fired
@@ -23,6 +23,7 @@ namespace Statecraft.Sim
var start = turnStart ?? s.Date;
var eligible = c.Events
.Where(e => !s.FiredEvents.Contains(e.Id) && e.From <= s.Date && (e.To == null || start < e.To.Value))
+ .Where(e => e.Requires == null || (s.ChosenOptions.TryGetValue(e.Requires.Event, out var chosen) && chosen == e.Requires.Option))
.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)
@@ -55,6 +56,7 @@ namespace Statecraft.Sim
static void Resolve(Content c, GameState s, string eventId, EventOption o, string how, List<Cause> why)
{
s.PendingEvents.Remove(eventId);
+ s.ChosenOptions[eventId] = o.Id;
var fillDate = FillDate(Get(c, eventId), s.Date);
why.Add(new Cause("events", eventId, 0, $"{how}: {Cabinet.Fill(c, s, o.Text, fillDate)}"));
foreach (var fx in o.Effects)
diff --git a/sim/Statecraft.Sim/GameState.cs b/sim/Statecraft.Sim/GameState.cs
index 8cd48b1f1..391962a4d 100644
--- a/sim/Statecraft.Sim/GameState.cs
+++ b/sim/Statecraft.Sim/GameState.cs
@@ -34,6 +34,8 @@ namespace Statecraft.Sim
public double Attention;
public SortedSet<string> FiredEvents = new SortedSet<string>(StringComparer.Ordinal);
public List<string> PendingEvents = new List<string>();
+ /// <summary>The option each resolved event was resolved with (chosen or delegated), for events' `requires`.</summary>
+ public Dictionary<string, string> ChosenOptions = new Dictionary<string, string>();
/// <summary>Player appointments by office id; valid while the government in AppointedUnder is in office (Cabinet).</summary>
public Dictionary<string, Appointment> Appointments = new Dictionary<string, Appointment>();
public string AppointedUnder = "";
@@ -59,6 +61,7 @@ namespace Statecraft.Sim
c.Population = new Dictionary<string, double>(Population);
c.FiredEvents = new SortedSet<string>(FiredEvents, StringComparer.Ordinal);
c.PendingEvents = new List<string>(PendingEvents);
+ c.ChosenOptions = new Dictionary<string, string>(ChosenOptions);
c.Appointments = new Dictionary<string, Appointment>(Appointments);
c.Placements = new List<Placement>(Placements);
return c;
@@ -86,6 +89,9 @@ namespace Statecraft.Sim
Num("mob", Mobilization); Num("stab", CoalitionStability); Num("att", Attention);
sb.Append("fired[").Append(string.Join(",", FiredEvents)).Append(']');
sb.Append("pending[").Append(string.Join(",", PendingEvents)).Append(']');
+ sb.Append("chosen{");
+ foreach (var kv in ChosenOptions.OrderBy(k => k.Key, StringComparer.Ordinal)) sb.Append(kv.Key).Append('=').Append(kv.Value).Append(';');
+ sb.Append('}');
sb.Append("appt{");
foreach (var kv in Appointments.OrderBy(k => k.Key, StringComparer.Ordinal))
sb.Append(kv.Key).Append('=').Append(kv.Value.Person).Append('@').Append(kv.Value.From.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(';');
diff --git a/sim/Statecraft.Sim/GoldenRun.cs b/sim/Statecraft.Sim/GoldenRun.cs
index ef9247cfc..e422f2174 100644
--- a/sim/Statecraft.Sim/GoldenRun.cs
+++ b/sim/Statecraft.Sim/GoldenRun.cs
@@ -11,12 +11,12 @@ namespace Statecraft.Sim
/// <summary>Final hash of Play(content, Until1950). Changing it is a deliberate rules or content change:
/// update it, DeterminismTests and docs/determinism.md in the same commit and say why in the message.</summary>
- public const string Hash1950 = "a540b52e10474615b703a2b14b7c742adbde84611496074b93a5116ef654899c";
+ public const string Hash1950 = "d463d5b22c9f98cf6b604a2f31738f01ca4b72cd3b2fb3c58a6d31790f4ff26e";
public static readonly DateTime Until1950 = new DateTime(1950, 1, 1);
/// <summary>Final hash of Play(content, Until1952): the M3 content range (to 31 December 1951).</summary>
- public const string Hash1952 = "16c16d7ec4f5419d31eb537d70924b8681b95ec94d7ed70874c0749292446e60";
+ public const string Hash1952 = "f78fc68d91fce73bc12e31c3a6eade4e889fd7e6d3002c5b9821075e31ab1dfb";
public static readonly DateTime Until1952 = new DateTime(1952, 1, 1);