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): layer event shifts on the clamped effects level
eyalgolan0576cf67a4a4
diff --git a/sim/Statecraft.Sim.Tests/EventTests.cs b/sim/Statecraft.Sim.Tests/EventTests.csindex e7e3d2f4e..bc4226619 100644--- a/sim/Statecraft.Sim.Tests/EventTests.cs+++ b/sim/Statecraft.Sim.Tests/EventTests.cs@@ -162,6 +162,56 @@ options:Assert.Equal(-0.2, r[1].causeDelta, 9);}+ // Morale: base 0.5, effect rationing_morale k -0.2, inertia 1, no office, so Base + Σ = 0.5 - 0.2 * rationing.+ static void SetInner(Content c, GameState s, double rationing) { s.Policies["rationing"] = rationing; EffectsSystem.Apply(c, s, new List<Cause>()); }++ static double ChooseSwing(Content c, GameState s, string option)+ {+ s.PendingEvents.Add("swing"); var why = new List<Cause>();+ Assert.Null(EventSystem.Choose(c, s, "swing", option, why));+ return why.Single(w => w.Target == "morale").Delta;+ }++ [Fact]+ public void EventBelowAnOverBoundEffectSumIsVisibleAndSurvivesEffectChanges()+ {+ var c = Load(new Dictionary<string, string> { ["swing.yaml"] = Swing });+ var s = State(c, new DateTime(1948, 6, 1));+ SetInner(c, s, -4.0); // Base + Σ = 1.3+ Assert.Equal(1.0, s.Indicators["morale"], 9);+ Assert.Equal(-0.2, ChooseSwing(c, s, "down"), 9); // cause = visible change+ Assert.Equal(0.8, s.Indicators["morale"], 9); // visible immediately+ SetInner(c, s, -4.0); // a rebuild with the same effects keeps it+ Assert.Equal(0.8, s.Indicators["morale"], 9);+ SetInner(c, s, -2.5); // Base + Σ = 1.0+ Assert.Equal(0.8, s.Indicators["morale"], 9);+ SetInner(c, s, -1.0); // Base + Σ = 0.7+ Assert.Equal(0.5, s.Indicators["morale"], 9);+ }++ [Fact]+ public void OvershootingEventLeavesOnlyTheVisibleShift()+ {+ var c = Load(new Dictionary<string, string> { ["swing.yaml"] = Swing });+ var s = State(c, new DateTime(1948, 6, 1));+ SetInner(c, s, -2.0); // inner 0.9+ Assert.Equal(0.1, ChooseSwing(c, s, "up"), 9); // +0.3 only moves it to 1.0+ Assert.Equal(1.0, s.Indicators["morale"], 9);+ Assert.Equal(0.1, s.IndicatorShifts["morale"], 9);+ Assert.Equal(-0.2, ChooseSwing(c, s, "down"), 9);+ Assert.Equal(0.8, s.Indicators["morale"], 9);+ }++ [Fact]+ public void EventWithNoVisibleChangeReportsZero()+ {+ var c = Load(new Dictionary<string, string> { ["swing.yaml"] = Swing });+ var s = State(c, new DateTime(1948, 6, 1));+ SetInner(c, s, -4.0); // inner 1.0 (Base + Σ = 1.3)+ Assert.Equal(0.0, ChooseSwing(c, s, "rise"), 9); // already at 1.0+ Assert.Equal(1.0, s.Indicators["morale"], 9);+ }+[Fact]public void UnknownOptionIsRejected(){diff --git a/sim/Statecraft.Sim/EffectsSystem.cs b/sim/Statecraft.Sim/EffectsSystem.csindex f856e01b1..7994731ea 100644--- a/sim/Statecraft.Sim/EffectsSystem.cs+++ b/sim/Statecraft.Sim/EffectsSystem.cs@@ -57,12 +57,13 @@ namespace Statecraft.SimUpdateSituations(c, s, why);}- /// <summary>clamp(base + event shift + effect contributions) to [0, 1].</summary>- internal static double Level(Content c, GameState s, Indicator i)- {- var sum = i.Base + s.IndicatorShifts[i.Id] + c.Effects.Where(e => e.Target == i.Id).Sum(e => s.EffectContributions[e.Id]);- return Math.Max(0, Math.Min(1, sum));- }+ /// <summary>clamp(base + effect contributions) to [0, 1], before event shifts.</summary>+ internal static double Inner(Content c, GameState s, Indicator i) =>+ Math.Max(0, Math.Min(1, i.Base + c.Effects.Where(e => e.Target == i.Id).Sum(e => s.EffectContributions[e.Id])));++ /// <summary>clamp(inner + event shift) to [0, 1].</summary>+ internal static double Level(Content c, GameState s, Indicator i) =>+ Math.Max(0, Math.Min(1, Inner(c, s, i) + s.IndicatorShifts[i.Id]));public static void UpdateSituations(Content c, GameState s, List<Cause> why){diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.csindex 67ee1cdac..f9f2824d6 100644--- a/sim/Statecraft.Sim/EventSystem.cs+++ b/sim/Statecraft.Sim/EventSystem.cs@@ -51,14 +51,13 @@ namespace Statecraft.Simelse if (fx.Target == "fx_reserves") s.FxReserves += fx.Delta;else{- // Store only the effective change, so a shift never builds up beyond the 0..1 clamp.+ // The shift is set so the level lands exactly on the visible result; it never exceeds the clamp.var ind = c.Indicators.First(i => i.Id == fx.Target);- var before = EffectsSystem.Level(c, s, ind);- var contributions = c.Effects.Where(e => e.Target == ind.Id).Sum(e => s.EffectContributions[e.Id]);- var target = Math.Max(0, Math.Min(1, ind.Base + s.IndicatorShifts[ind.Id] + contributions + fx.Delta));- s.IndicatorShifts[ind.Id] = target - ind.Base - contributions;- s.Indicators[ind.Id] = target;- change = target - before;+ var level = EffectsSystem.Level(c, s, ind);+ var newLevel = Math.Max(0, Math.Min(1, level + fx.Delta));+ s.IndicatorShifts[ind.Id] = newLevel - EffectsSystem.Inner(c, s, ind);+ s.Indicators[ind.Id] = newLevel;+ change = newLevel - level;}why.Add(new Cause("events", fx.Target, change, FormattableString.Invariant($"{fx.Target} {change:+0.00;-0.00} ({eventId}/{o.Id})")));}