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.

Events: a city capacity or job delta saturates instead of wrapping (Refs #2725) (#2748)

Eyal Golancfc78e9e64d8
diff --git a/changelog.d/2725-city-effects-saturate.md b/changelog.d/2725-city-effects-saturate.md
new file mode 100644
index 000000000..c14825eac
--- /dev/null
+++ b/changelog.d/2725-city-effects-saturate.md
@@ -0,0 +1,4 @@
+### Fixed
+
+- An event that adds housing or job slots to a city near the largest possible number now stops at that number instead
+ of wrapping round and removing them (Refs #2725). No shipped event is affected; play and saves are unchanged.
diff --git a/sim/Statecraft.Sim.Tests/StructuredEffectTests.cs b/sim/Statecraft.Sim.Tests/StructuredEffectTests.cs
index 363dfedde..7fbb7d54f 100644
--- a/sim/Statecraft.Sim.Tests/StructuredEffectTests.cs
+++ b/sim/Statecraft.Sim.Tests/StructuredEffectTests.cs
@@ -226,6 +226,54 @@ sources: [{ url: https://example.org/event, checked: 2026-10-18 }]
Assert.Contains(why, w => w.Target == "town_a.housing_capacity" && w.Delta == -old);
}
+ // Red-first prediction at ae4bfdaab: +10 wraps to a cut; R-EFX-004 requires saturation with the applied gain of 5.
+ [Fact]
+ public void HousingCapacityGainSaturatesAtIntMaxValue()
+ {
+ var c = Load(Event(" city_effects:\n - { city: town_a, field: housing_capacity, delta: 10 }\n"));
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ var city = s.Cities["town_a"];
+ s.Cities["town_a"] = new City(city.Id, int.MaxValue - 5, city.JobSlots, city.Civilians, city.Employed,
+ city.Refugees, city.Recruits, city.Condition, city.Craftsmen, city.JobsDefault, city.Qualified);
+ var why = new List<Cause>();
+
+ Assert.Null(EventSystem.Choose(c, s, "arms", "accept", why));
+
+ Assert.Equal(int.MaxValue, s.Cities["town_a"].HousingCapacity);
+ Assert.Equal(5.0, Assert.Single(why, w => w.Target == "town_a.housing_capacity").Delta);
+ }
+
+ // Red-first prediction at ae4bfdaab: +10 wraps to employed; R-EFX-004 requires saturation with the applied gain of 5.
+ [Fact]
+ public void JobSlotGainSaturatesAtIntMaxValue()
+ {
+ var c = Load(Event(" city_effects:\n - { city: town_a, field: job_slots, delta: 10 }\n"));
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ var city = s.Cities["town_a"];
+ s.Cities["town_a"] = new City(city.Id, city.HousingCapacity, int.MaxValue - 5, city.Civilians, city.Employed,
+ city.Refugees, city.Recruits, city.Condition, city.Craftsmen, city.JobsDefault, city.Qualified);
+ var why = new List<Cause>();
+
+ Assert.Null(EventSystem.Choose(c, s, "arms", "accept", why));
+
+ Assert.Equal(int.MaxValue, s.Cities["town_a"].JobSlots);
+ Assert.Equal(5.0, Assert.Single(why, w => w.Target == "town_a.job_slots").Delta);
+ }
+
+ // Red-first prediction at ae4bfdaab: already passes; R-EFX-004 must keep the exact in-range result and cause unchanged.
+ [Fact]
+ public void InRangeHousingCapacityGainIsUnchanged()
+ {
+ var c = Load(Event(" city_effects:\n - { city: town_a, field: housing_capacity, delta: 10 }\n"));
+ var s = Simulation.NewGame(c, 1, Mode.Historical);
+ var why = new List<Cause>();
+
+ Assert.Null(EventSystem.Choose(c, s, "arms", "accept", why));
+
+ Assert.Equal(22, s.Cities["town_a"].HousingCapacity);
+ Assert.Equal(10.0, Assert.Single(why, w => w.Target == "town_a.housing_capacity").Delta);
+ }
+
// Structured effects act on GDD records, so the legacy loop (no cities, no units) refuses them at load.
[Fact]
public void TheLegacyLoopRefusesThem()
diff --git a/sim/Statecraft.Sim/EventSystem.cs b/sim/Statecraft.Sim/EventSystem.cs
index 5efe1f310..e48cef027 100644
--- a/sim/Statecraft.Sim/EventSystem.cs
+++ b/sim/Statecraft.Sim/EventSystem.cs
@@ -200,10 +200,14 @@ namespace Statecraft.Sim
continue;
}
var delta = (int)x.Delta;
- var housing = x.Field == HousingField ? Math.Max(0, city.HousingCapacity + delta) : city.HousingCapacity;
- var jobs = x.Field == JobsField ? Math.Max(city.Employed, city.JobSlots + delta) : city.JobSlots;
+ var housing = x.Field == HousingField
+ ? (int)Math.Max(0L, Math.Min((long)int.MaxValue, (long)city.HousingCapacity + delta))
+ : city.HousingCapacity;
+ var jobs = x.Field == JobsField
+ ? (int)Math.Max((long)city.Employed, Math.Min((long)int.MaxValue, (long)city.JobSlots + delta))
+ : city.JobSlots;
s.Cities[x.City] = new City(city.Id, housing, jobs, city.Civilians, city.Employed, city.Refugees, city.Recruits, city.Condition, city.Craftsmen, city.JobsDefault, city.Qualified);
- var applied = housing - city.HousingCapacity + jobs - city.JobSlots;
+ var applied = (long)housing - city.HousingCapacity + (long)jobs - city.JobSlots;
why.Add(new Cause("events", x.City + "." + x.Field, applied, $"{CityChange(c, x.City, x.Field, applied)} ({title})",
key: CauseKeys.EventCity, args: Context(title, localizedTitle, x.City, x.Field)));
}