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,content): event options can set policies; imposing austerity turns rationing on (M1)
eyalgolanc3f99ab26285
diff --git a/content/events/austerity_1949.yaml b/content/events/austerity_1949.yamlindex 9274f3599..a76192047 100644--- a/content/events/austerity_1949.yaml+++ b/content/events/austerity_1949.yaml@@ -10,6 +10,7 @@ options:text: Impose rationinghistorical: truerecommended: true+ set_policy: { rationing: 1.0 }effects:- { target: food_supply, delta: 0.15 }- { target: morale, delta: -0.05 }diff --git a/sim/Statecraft.Sim.Tests/RealContentTests.cs b/sim/Statecraft.Sim.Tests/RealContentTests.csindex 8f7ea4c4d..10709dd5f 100644--- a/sim/Statecraft.Sim.Tests/RealContentTests.cs+++ b/sim/Statecraft.Sim.Tests/RealContentTests.cs@@ -81,4 +81,12 @@ public class RealContentTestsAssert.Equal(239954, C.Aliyah[1].Origins.Values.Sum());Assert.Equal(13, C.Actors.Count);}++ [Fact]+ public void ImposingAusterityTurnsRationingOn()+ {+ var e = C.Events.Single(x => x.Id == "austerity_1949");+ Assert.Equal(1.0, e.Options.Single(o => o.Id == "impose").SetPolicy["rationing"]);+ Assert.Empty(e.Options.Single(o => o.Id == "decline").SetPolicy);+ }}diff --git a/sim/Statecraft.Sim.Tests/ValidationTests.cs b/sim/Statecraft.Sim.Tests/ValidationTests.csindex d32af2f19..4b1b6fc16 100644--- a/sim/Statecraft.Sim.Tests/ValidationTests.cs+++ b/sim/Statecraft.Sim.Tests/ValidationTests.cs@@ -193,4 +193,33 @@ status: draftAssert.Equal("people.yaml", ex.File);Assert.Equal("people[pm_one].living", ex.Field);}++ // M1: event options can set policies.+ internal static readonly string RationingEvent = Event.Replace(" historical: true\n", " historical: true\n set_policy: { rationing: 1.0 }\n");++ [Fact]+ public void ChoosingAnOptionSetsItsPolicies()+ {+ var c = ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["drought.yaml"] = RationingEvent }));+ var s = new GameState { Date = new DateTime(1948, 6, 1) };+ EffectsSystem.Init(c, s); EconomySystem.Init(c, s);+ var why = new List<Cause>();+ EventSystem.Trigger(c, s, why);+ Assert.Equal(0.0, s.Policies["rationing"]);+ Assert.Null(EventSystem.Choose(c, s, "drought", "ration", why));+ Assert.Equal(1.0, s.Policies["rationing"]);+ Assert.Contains(why, w => w.System == "events" && w.Target == "rationing" && w.Delta == 1.0);+ }++ [Theory]+ [InlineData("set_policy: { rationing: 1.0 }", "set_policy: { bread: 1.0 }", "events[drought].options[ration].set_policy.bread")]+ [InlineData("set_policy: { rationing: 1.0 }", "set_policy: { rationing: 1.5 }", "events[drought].options[ration].set_policy.rationing")]+ [InlineData("set_policy: { rationing: 1.0 }", "set_policy: { rationing: .nan }", "events[drought].options[ration].set_policy.rationing")]+ public void SetPolicyNeedsAKnownPolicyAndALevelFrom0To1(string from, string to, string field)+ {+ var bad = RationingEvent.Replace(from, to);+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(events: new Dictionary<string, string> { ["drought.yaml"] = bad })));+ Assert.Equal("events/drought.yaml", ex.File);+ Assert.Equal(field, ex.Field);+ }}diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.csindex 1560c56e5..8fbad5136 100644--- a/sim/Statecraft.Sim/Content.cs+++ b/sim/Statecraft.Sim/Content.cs@@ -151,6 +151,8 @@ namespace Statecraft.Simpublic bool Recommended { get; set; }public List<OptionEffect> Effects { get; set; } = new List<OptionEffect>();public int? SetRung { get; set; }+ /// <summary>Policies this option sets, by id, to a level in [0, 1].</summary>+ public Dictionary<string, double> SetPolicy { get; set; } = new Dictionary<string, double>();}public sealed class GameEventdiff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.csindex cbd194596..95a4a5162 100644--- a/sim/Statecraft.Sim/EventRules.cs+++ b/sim/Statecraft.Sim/EventRules.cs@@ -41,6 +41,11 @@ namespace Statecraft.Simif (!Offices.OfficeId.IsMatch(o.Id)) throw new ContentException(e.File, F($"options[{o.Id}].id"), ContentLoader.IdRule);if (!optionIds.Add(o.Id)) throw new ContentException(e.File, F($"options[{o.Id}].id"), "duplicate option id");CheckPlaceholders(c, e, F($"options[{o.Id}].text"), o.Text, offices);+ foreach (var kv in o.SetPolicy)+ {+ if (c.Policies.All(p => p.Id != kv.Key)) throw new ContentException(e.File, F($"options[{o.Id}].set_policy.{kv.Key}"), "unknown policy");+ ContentLoader.Unit(e.File, F($"options[{o.Id}].set_policy.{kv.Key}"), kv.Value);+ }if (o.SetRung.HasValue && (o.SetRung < 1 || o.SetRung > 5)) throw new ContentException(e.File, F($"options[{o.Id}].set_rung"), "must be 1-5");foreach (var fx in o.Effects){diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.csindex 2bc66e2dd..7b9a5f8d4 100644--- a/sim/Statecraft.Sim/EventSystem.cs+++ b/sim/Statecraft.Sim/EventSystem.cs@@ -61,6 +61,11 @@ namespace Statecraft.Sim}why.Add(new Cause("events", fx.Target, change, FormattableString.Invariant($"{fx.Target} {change:+0.00;-0.00} ({eventId}/{o.Id})")));}+ foreach (var kv in o.SetPolicy.OrderBy(k => k.Key, StringComparer.Ordinal))+ {+ s.Policies[kv.Key] = kv.Value;+ why.Add(new Cause("events", kv.Key, kv.Value, FormattableString.Invariant($"{kv.Key} set to {kv.Value:0.00} ({eventId}/{o.Id})")));+ }if (o.SetRung.HasValue) SecuritySystem.SetRung(c, s, o.SetRung.Value, why);}