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.

feat(sim): effects web with inertia, competence and hysteresis

eyalgolan85bdc4217f86
diff --git a/sim/Statecraft.Sim.Tests/EffectsTests.cs b/sim/Statecraft.Sim.Tests/EffectsTests.cs
new file mode 100644
index 000000000..482945a54
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/EffectsTests.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Collections.Generic;
+using Statecraft.Sim;
+
+public class EffectsTests
+{
+ static readonly Content C = ContentLoader.Load(Fixture.Write());
+
+ static GameState Fresh(DateTime date)
+ {
+ var s = new GameState { Date = date };
+ EffectsSystem.Init(C, s);
+ return s;
+ }
+
+ [Fact]
+ public void InitSetsBasesAndPolicyDefaults()
+ {
+ var s = Fresh(new DateTime(1948, 6, 1));
+ Assert.Equal(0.5, s.Indicators["morale"]);
+ Assert.Equal(0.0, s.Policies["rationing"]);
+ }
+
+ [Fact]
+ public void EffectMovesTowardTargetOverInertiaAndScalesByMinisterCompetence()
+ {
+ // finance_minister on 1948-06-01 is def_two, competence 4 -> multiplier 0.5 + 4/20 = 0.7
+ var s = Fresh(new DateTime(1948, 6, 1));
+ s.Policies["rationing"] = 1.0;
+ var why = new List<Cause>();
+ EffectsSystem.Apply(C, s, why);
+ // target contribution 0.4 * 1.0 * 0.7 = 0.28, inertia 2 -> half way = 0.14
+ Assert.Equal(0.54, s.Indicators["food_supply"], 6);
+ EffectsSystem.Apply(C, s, why);
+ Assert.Equal(0.61, s.Indicators["food_supply"], 6);
+ Assert.Contains(why, w => w.Target == "food_supply" && w.Message.Contains("rationing_food"));
+ }
+
+ [Fact]
+ public void EffectWithoutOfficeHasNoMultiplierAndIndicatorsClampToUnitRange()
+ {
+ var s = Fresh(new DateTime(1948, 6, 1));
+ s.Policies["rationing"] = 5.0; // morale target 0.5 + (-0.2 * 5) = -0.5 -> clamped to 0
+ EffectsSystem.Apply(C, s, new List<Cause>());
+ Assert.Equal(0.0, s.Indicators["morale"]);
+ }
+
+ [Fact]
+ public void SituationUsesHysteresis()
+ {
+ var s = Fresh(new DateTime(1948, 6, 1));
+ var why = new List<Cause>();
+ s.Indicators["food_supply"] = 0.29; EffectsSystem.UpdateSituations(C, s, why);
+ Assert.Contains("hunger", s.ActiveSituations);
+ s.Indicators["food_supply"] = 0.40; EffectsSystem.UpdateSituations(C, s, why); // between thresholds: stays on
+ Assert.Contains("hunger", s.ActiveSituations);
+ s.Indicators["food_supply"] = 0.46; EffectsSystem.UpdateSituations(C, s, why);
+ Assert.DoesNotContain("hunger", s.ActiveSituations);
+ Assert.Contains(why, w => w.Target == "hunger");
+ }
+
+ [Fact]
+ public void ApprovalIsMeanOfClampedVoterGroupSupport()
+ {
+ var s = Fresh(new DateTime(1948, 6, 1));
+ Assert.Equal(0.5, EffectsSystem.Approval(C, s), 6);
+ }
+}
diff --git a/sim/Statecraft.Sim/Cause.cs b/sim/Statecraft.Sim/Cause.cs
new file mode 100644
index 000000000..b7267a950
--- /dev/null
+++ b/sim/Statecraft.Sim/Cause.cs
@@ -0,0 +1,14 @@
+namespace Statecraft.Sim
+{
+ /// <summary>One line of the "Why did this happen?" report.</summary>
+ public sealed class Cause
+ {
+ public string System { get; }
+ public string Target { get; }
+ public double Delta { get; }
+ public string Message { get; }
+ public Cause(string system, string target, double delta, string message)
+ { System = system; Target = target; Delta = delta; Message = message; }
+ public override string ToString() => $"[{System}] {Message}";
+ }
+}
diff --git a/sim/Statecraft.Sim/EffectsSystem.cs b/sim/Statecraft.Sim/EffectsSystem.cs
new file mode 100644
index 000000000..75688f17b
--- /dev/null
+++ b/sim/Statecraft.Sim/EffectsSystem.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Statecraft.Sim
+{
+ public static class EffectsSystem
+ {
+ public static void Init(Content c, GameState s)
+ {
+ foreach (var i in c.Indicators) s.Indicators[i.Id] = i.Base;
+ foreach (var p in c.Policies) s.Policies[p.Id] = p.Default;
+ foreach (var e in c.Effects) s.EffectContributions[e.Id] = 0;
+ }
+
+ /// <summary>0.5 + competence/20: a 10-competence minister delivers the full effect, a 0 delivers half.</summary>
+ public static double Multiplier(Content c, string office, DateTime date)
+ {
+ if (office == "") return 1.0;
+ var holder = Offices.Holder(c, office, date);
+ return holder == null ? 0.5 : 0.5 + holder.Competence / 20.0;
+ }
+
+ public static void Apply(Content c, GameState s, List<Cause> why)
+ {
+ // Read every source before writing anything, so effect order cannot change results.
+ var targets = c.Effects.ToDictionary(e => e.Id, e =>
+ e.K * (s.Policies.TryGetValue(e.Source, out var p) ? p : s.Indicators[e.Source]) * Multiplier(c, e.Office, s.Date));
+ foreach (var e in c.Effects)
+ {
+ var old = s.EffectContributions[e.Id];
+ var now = old + (targets[e.Id] - old) / e.Inertia;
+ s.EffectContributions[e.Id] = now;
+ if (Math.Abs(now - old) > 1e-9)
+ why.Add(new Cause("effects", e.Target, now - old, $"{e.Target} {now - old:+0.000;-0.000} ({e.Id})"));
+ }
+ foreach (var i in c.Indicators)
+ {
+ var sum = i.Base + c.Effects.Where(e => e.Target == i.Id).Sum(e => s.EffectContributions[e.Id]);
+ s.Indicators[i.Id] = Math.Max(0, Math.Min(1, sum));
+ }
+ UpdateSituations(c, s, why);
+ }
+
+ public static void UpdateSituations(Content c, GameState s, List<Cause> why)
+ {
+ foreach (var sit in c.Situations)
+ {
+ var v = s.Indicators[sit.Indicator];
+ bool on = s.ActiveSituations.Contains(sit.Id);
+ bool turnOn = sit.OnBelow.HasValue ? v < sit.OnBelow.Value : v > sit.OnAbove!.Value;
+ bool turnOff = sit.OffAbove.HasValue ? v > sit.OffAbove.Value : v < sit.OffBelow!.Value;
+ if (!on && turnOn) { s.ActiveSituations.Add(sit.Id); why.Add(new Cause("situations", sit.Id, 1, $"{sit.Id} began ({sit.Indicator} = {v:0.00})")); }
+ else if (on && turnOff) { s.ActiveSituations.Remove(sit.Id); why.Add(new Cause("situations", sit.Id, -1, $"{sit.Id} ended ({sit.Indicator} = {v:0.00})")); }
+ }
+ }
+
+ public static double Approval(Content c, GameState s) =>
+ c.VoterGroups.Count == 0 ? 0.5 :
+ c.VoterGroups.Average(g => Math.Max(0, Math.Min(1, g.Weights.Sum(w => w.Value * s.Indicators[w.Key]))));
+ }
+}