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): report one truthful effects cause per changed indicator
eyalgolandd7f038ed3cd
diff --git a/sim/Statecraft.Sim.Tests/EffectsTests.cs b/sim/Statecraft.Sim.Tests/EffectsTests.csindex 482945a54..f8b24b240 100644--- a/sim/Statecraft.Sim.Tests/EffectsTests.cs+++ b/sim/Statecraft.Sim.Tests/EffectsTests.cs@@ -1,5 +1,6 @@using System;using System.Collections.Generic;+using System.Linq;using Statecraft.Sim;public class EffectsTests@@ -33,7 +34,45 @@ public class EffectsTestsAssert.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"));+ var food = why.Where(w => w.System == "effects" && w.Target == "food_supply").ToList();+ Assert.Equal(2, food.Count); // one truthful cause per turn the indicator moved+ Assert.Equal(0.14, food[0].Delta, 9);+ Assert.Equal(0.07, food[1].Delta, 9);+ Assert.All(food, w => Assert.Contains("rationing_food", w.Message));+ }++ [Fact]+ public void MovingIndicatorGetsExactlyOneCauseWithItsRealChange()+ {+ // Two effects on food_supply: one cause for the indicator, naming both, with the real change.+ var model = Fixture.Files["model.yaml"].Replace("situations:",+ " - id: rationing_food2\n source: rationing\n target: food_supply\n k: 0.1\n inertia: 1\nsituations:");+ var c = ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["model.yaml"] = model }));+ var s = new GameState { Date = new DateTime(1948, 6, 1) };+ EffectsSystem.Init(c, s);+ s.Policies["rationing"] = 1.0;+ var before = s.Indicators["food_supply"];+ var why = new List<Cause>();+ EffectsSystem.Apply(c, s, why);+ Assert.Equal(0.64, s.Indicators["food_supply"], 9); // 0.4 + 0.14 + 0.1+ var cause = Assert.Single(why, w => w.System == "effects" && w.Target == "food_supply");+ Assert.Equal(s.Indicators["food_supply"] - before, cause.Delta, 12);+ Assert.Equal("food_supply +0.240 (rationing_food, rationing_food2)", cause.Message);+ }++ [Fact]+ public void ClampedIndicatorWithMovingContributionHasNoCause()+ {+ var s = Fresh(new DateTime(1948, 6, 1));+ s.Policies["rationing"] = 5.0; // morale contribution -1.0: morale clamps to 0+ EffectsSystem.Apply(C, s, new List<Cause>());+ Assert.Equal(0.0, s.Indicators["morale"]);+ s.Policies["rationing"] = 6.0; // contribution moves to -1.2, morale stays at 0+ var why = new List<Cause>();+ EffectsSystem.Apply(C, s, why);+ Assert.Equal(0.0, s.Indicators["morale"]);+ Assert.Equal(-1.2, s.EffectContributions["rationing_morale"], 9);+ Assert.DoesNotContain(why, w => w.System == "effects" && w.Target == "morale");}[Fact]diff --git a/sim/Statecraft.Sim/EffectsSystem.cs b/sim/Statecraft.Sim/EffectsSystem.csindex 3065c95bc..17aaab47e 100644--- a/sim/Statecraft.Sim/EffectsSystem.cs+++ b/sim/Statecraft.Sim/EffectsSystem.cs@@ -35,19 +35,29 @@ namespace Statecraft.Sim// 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, s, e.Office));+ var moved = new HashSet<string>();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})"));+ if (Math.Abs(now - old) > 1e-9) moved.Add(e.Id);}+ var before = c.Indicators.ToDictionary(i => i.Id, i => s.Indicators[i.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));}+ // One cause per indicator that actually changed (a clamped indicator reports nothing).+ foreach (var i in c.Indicators)+ {+ var delta = s.Indicators[i.Id] - before[i.Id];+ if (Math.Abs(delta) <= 1e-9) continue;+ var ids = c.Effects.Where(e => e.Target == i.Id && moved.Contains(e.Id)).Select(e => e.Id).ToList();+ var from = ids.Count > 0 ? $" ({string.Join(", ", ids)})" : "";+ why.Add(new Cause("effects", i.Id, delta, FormattableString.Invariant($"{i.Id} {delta:+0.000;-0.000}{from}")));+ }UpdateSituations(c, s, why);}