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.

fix(sim): keep event indicator effects as permanent shifts

eyalgolan0f4b958b32ea
diff --git a/sim/Statecraft.Sim.Tests/CoreTests.cs b/sim/Statecraft.Sim.Tests/CoreTests.cs
index 4418742d3..2587fd8af 100644
--- a/sim/Statecraft.Sim.Tests/CoreTests.cs
+++ b/sim/Statecraft.Sim.Tests/CoreTests.cs
@@ -63,4 +63,14 @@ public class CoreTests
Assert.NotEqual(a.Treasury, b.Treasury);
Assert.NotEqual(a.Hash(), b.Hash());
}
+ [Fact]
+ public void CloneIsolatesIndicatorShiftsAndHashSeesThem()
+ {
+ var a = new GameState { Date = new DateTime(1948, 5, 15) };
+ a.IndicatorShifts["morale"] = 0.0;
+ var b = a.Clone();
+ b.IndicatorShifts["morale"] = 0.1;
+ Assert.Equal(0.0, a.IndicatorShifts["morale"]);
+ Assert.NotEqual(a.Hash(), b.Hash());
+ }
}
diff --git a/sim/Statecraft.Sim.Tests/EventTests.cs b/sim/Statecraft.Sim.Tests/EventTests.cs
index d66c2cf73..a43ab7daf 100644
--- a/sim/Statecraft.Sim.Tests/EventTests.cs
+++ b/sim/Statecraft.Sim.Tests/EventTests.cs
@@ -75,6 +75,25 @@ options:
Assert.Equal("Event 'shortage' is not waiting for a decision", EventSystem.Choose(c, s, "shortage", "ration", why));
}
+ [Fact]
+ public void EventIndicatorEffectPersistsThroughEffectsRebuilds()
+ {
+ var ev = Shortage.Replace("target: food_supply\n delta: 0.2", "target: food_supply\n delta: 0.1");
+ var c = Load(new Dictionary<string, string> { ["shortage.yaml"] = ev });
+ var s = State(c, new DateTime(1948, 6, 1)); var why = new List<Cause>();
+ EventSystem.Trigger(c, s, why);
+ Assert.Null(EventSystem.Choose(c, s, "shortage", "ration", why));
+ Assert.Equal(0.5, s.Indicators["food_supply"], 9); // visible immediately: base 0.4 + 0.1
+ for (int i = 0; i < 3; i++)
+ {
+ var turn = new List<Cause>();
+ EffectsSystem.Apply(c, s, turn);
+ Assert.Equal(0.5, s.Indicators["food_supply"], 9);
+ Assert.DoesNotContain(turn, w => w.System == "effects" && w.Target == "food_supply"); // no fake reversal
+ }
+ Assert.Equal(0.1, s.IndicatorShifts["food_supply"], 9);
+ }
+
[Fact]
public void UnknownOptionIsRejected()
{
diff --git a/sim/Statecraft.Sim/EffectsSystem.cs b/sim/Statecraft.Sim/EffectsSystem.cs
index 8b58c5af6..f856e01b1 100644
--- a/sim/Statecraft.Sim/EffectsSystem.cs
+++ b/sim/Statecraft.Sim/EffectsSystem.cs
@@ -8,7 +8,7 @@ namespace Statecraft.Sim
{
public static void Init(Content c, GameState s)
{
- foreach (var i in c.Indicators) s.Indicators[i.Id] = i.Base;
+ foreach (var i in c.Indicators) { s.Indicators[i.Id] = i.Base; s.IndicatorShifts[i.Id] = 0.0; }
foreach (var p in c.Policies) s.Policies[p.Id] = p.Default;
foreach (var e in c.Effects) s.EffectContributions[e.Id] = 0;
}
@@ -44,11 +44,7 @@ namespace Statecraft.Sim
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));
- }
+ foreach (var i in c.Indicators) s.Indicators[i.Id] = Level(c, s, i);
// One cause per indicator that actually changed (a clamped indicator reports nothing).
foreach (var i in c.Indicators)
{
@@ -61,6 +57,13 @@ namespace Statecraft.Sim
UpdateSituations(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));
+ }
+
public static void UpdateSituations(Content c, GameState s, List<Cause> why)
{
foreach (var sit in c.Situations)
diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.cs
index b82fce3cb..10d22fbcb 100644
--- a/sim/Statecraft.Sim/EventSystem.cs
+++ b/sim/Statecraft.Sim/EventSystem.cs
@@ -44,7 +44,11 @@ namespace Statecraft.Sim
{
if (fx.Target == "treasury") s.Treasury += fx.Delta;
else if (fx.Target == "fx_reserves") s.FxReserves += fx.Delta;
- else s.Indicators[fx.Target] = Math.Max(0, Math.Min(1, s.Indicators[fx.Target] + fx.Delta));
+ else
+ {
+ s.IndicatorShifts[fx.Target] += fx.Delta;
+ s.Indicators[fx.Target] = EffectsSystem.Level(c, s, c.Indicators.First(i => i.Id == fx.Target));
+ }
why.Add(new Cause("events", fx.Target, fx.Delta, FormattableString.Invariant($"{fx.Target} {fx.Delta:+0.00;-0.00} ({eventId}/{o.Id})")));
}
if (o.SetRung.HasValue) SecuritySystem.SetRung(c, s, o.SetRung.Value, why);
diff --git a/sim/Statecraft.Sim/GameState.cs b/sim/Statecraft.Sim/GameState.cs
index 14f789847..32f62f16e 100644
--- a/sim/Statecraft.Sim/GameState.cs
+++ b/sim/Statecraft.Sim/GameState.cs
@@ -19,6 +19,8 @@ namespace Statecraft.Sim
public Dictionary<string, double> Indicators = new Dictionary<string, double>();
public Dictionary<string, double> Policies = new Dictionary<string, double>();
public Dictionary<string, double> EffectContributions = new Dictionary<string, double>();
+ /// <summary>Permanent per-indicator offsets from event choices, added on top of base and effects.</summary>
+ public Dictionary<string, double> IndicatorShifts = new Dictionary<string, double>();
public SortedSet<string> ActiveSituations = new SortedSet<string>(StringComparer.Ordinal);
public Dictionary<string, double> Budget = new Dictionary<string, double>();
public double Treasury;
@@ -42,6 +44,7 @@ namespace Statecraft.Sim
c.Indicators = new Dictionary<string, double>(Indicators);
c.Policies = new Dictionary<string, double>(Policies);
c.EffectContributions = new Dictionary<string, double>(EffectContributions);
+ c.IndicatorShifts = new Dictionary<string, double>(IndicatorShifts);
c.ActiveSituations = new SortedSet<string>(ActiveSituations, StringComparer.Ordinal);
c.Budget = new Dictionary<string, double>(Budget);
c.Population = new Dictionary<string, double>(Population);
@@ -64,7 +67,7 @@ namespace Statecraft.Sim
}
sb.Append(Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(';')
.Append(RngState).Append(';').Append(Mode).Append(';').Append(Turn).Append(';');
- Map("ind", Indicators); Map("pol", Policies); Map("eff", EffectContributions);
+ Map("ind", Indicators); Map("pol", Policies); Map("eff", EffectContributions); Map("shift", IndicatorShifts);
Map("bud", Budget); Map("pop", Population);
sb.Append("sit[").Append(string.Join(",", ActiveSituations)).Append(']');
Num("treasury", Treasury); Num("debt", Debt); Num("fx", FxReserves);