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.

Sim: the loader refuses a missing map override q/r and event delta (Fixes #2512) (#2546)

Berie9b0cb6c0fb5
diff --git a/changelog.d/2512-required-fields.md b/changelog.d/2512-required-fields.md
new file mode 100644
index 000000000..40d78fcdc
--- /dev/null
+++ b/changelog.d/2512-required-fields.md
@@ -0,0 +1,5 @@
+### Fixed
+
+- Content loading (Fixes #2512): a map override hex without `q` or `r` (`map/overrides.yaml`) and an event effect
+ without `delta` are now refused at load with an error naming the file and the field, instead of loading as
+ coordinate 0 or a change of 0. Shipped content already has both, so nothing in the game changes.
diff --git a/sim/Statecraft.Sim.Tests/EventTests.cs b/sim/Statecraft.Sim.Tests/EventTests.cs
index 6929b0ec3..a36c77eab 100644
--- a/sim/Statecraft.Sim.Tests/EventTests.cs
+++ b/sim/Statecraft.Sim.Tests/EventTests.cs
@@ -172,6 +172,18 @@ options:
Assert.Contains("unknown target", ex.Message);
}
+ /// <summary>#2512: the schema requires an effect's delta; a missing one is a load error naming file and field, not a
+ /// change of 0.</summary>
+ [Fact]
+ public void AMissingDeltaIsALoadError()
+ {
+ var ev = Shortage.Replace("target: fx_reserves\n delta: -10", "target: fx_reserves");
+ var ex = Assert.Throws<ContentException>(() => Load(new Dictionary<string, string> { ["shortage.yaml"] = ev }));
+ Assert.Equal("events/shortage.yaml", ex.File);
+ Assert.Equal("events[shortage].options[import].effects.fx_reserves.delta", ex.Field);
+ Assert.Contains("required", ex.Message);
+ }
+
[Fact]
public void EventIndicatorEffectPersistsThroughEffectsRebuilds()
{
diff --git a/sim/Statecraft.Sim.Tests/MapOverridesTests.cs b/sim/Statecraft.Sim.Tests/MapOverridesTests.cs
index e16d541c2..6d63cbd29 100644
--- a/sim/Statecraft.Sim.Tests/MapOverridesTests.cs
+++ b/sim/Statecraft.Sim.Tests/MapOverridesTests.cs
@@ -127,6 +127,22 @@ public class MapOverridesTests
Assert.Equal((1, 0), (s.Q, s.R));
}
+ // #2512: a hex left without q or r is refused, not read as coordinate 0.
+ [Theory]
+ [InlineData("terrain:\n - { hex: { r: 1 }, value: hills }\ncontrol: []\nlocations: []\n", "terrain[0].hex.q")]
+ [InlineData("terrain: []\ncontrol:\n - { hex: { q: 1 }, period: opening, value: I }\nlocations: []\n", "control[0].hex.r")]
+ [InlineData("terrain: []\ncontrol: []\nlocations:\n - { id: town_a, hex: { q: 2 } }\n", "locations[0].hex.r")]
+ public void TheLoaderRefusesAMissingCoordinate(string yaml, string field)
+ {
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string>
+ {
+ [MapOverrides.FileName] = yaml,
+ })));
+ Assert.Equal(MapOverrides.FileName, ex.File);
+ Assert.Equal(field, ex.Field);
+ Assert.Contains("required", ex.Message);
+ }
+
[Fact]
public void TheLoaderRejectsAnUnknownLocationNamingTheOverrideFile()
{
diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.cs
index 4e3a6c538..42e3bb842 100644
--- a/sim/Statecraft.Sim/Content.cs
+++ b/sim/Statecraft.Sim/Content.cs
@@ -1058,7 +1058,14 @@ namespace Statecraft.Sim
public List<Source> Sources { get; set; } = new List<Source>();
}
- public sealed class OptionEffect { public string Target { get; set; } = ""; public double Delta { get; set; } }
+ public sealed class OptionEffect
+ {
+ // Nullable behind the double so EventRules can refuse a delta the file left out instead of reading it as 0 (#2512).
+ double? delta;
+ public string Target { get; set; } = "";
+ public double Delta { get => delta ?? 0; set => delta = value; }
+ internal bool HasDelta => delta.HasValue;
+ }
public sealed class EventOption
{
diff --git a/sim/Statecraft.Sim/EventRules.cs b/sim/Statecraft.Sim/EventRules.cs
index 58247f651..93a8aac8b 100644
--- a/sim/Statecraft.Sim/EventRules.cs
+++ b/sim/Statecraft.Sim/EventRules.cs
@@ -95,6 +95,7 @@ namespace Statecraft.Sim
if (!indicators.Contains(fx.Target) && !SpecialTargets.Contains(fx.Target))
throw new ContentException(e.File, F($"options[{o.Id}].effects.{fx.Target}"), "unknown target");
Active(F($"options[{o.Id}].effects.{fx.Target}"), fx.Target);
+ if (!fx.HasDelta) throw new ContentException(e.File, F($"options[{o.Id}].effects.{fx.Target}.delta"), "required");
ContentLoader.Finite(e.File, F($"options[{o.Id}].effects.{fx.Target}.delta"), fx.Delta);
if ((fx.Target == Pools.FoodTarget || fx.Target == Pools.ArmsTarget) && Math.Abs(fx.Delta) > int.MaxValue)
throw new ContentException(e.File, F($"options[{o.Id}].effects.{fx.Target}"), $"{fx.Target} delta out of range");
diff --git a/sim/Statecraft.Sim/MapOverrides.cs b/sim/Statecraft.Sim/MapOverrides.cs
index cec192ca6..10244c90e 100644
--- a/sim/Statecraft.Sim/MapOverrides.cs
+++ b/sim/Statecraft.Sim/MapOverrides.cs
@@ -10,8 +10,12 @@ namespace Statecraft.Sim
{
public sealed class MapHexRef
{
- public int Q { get; set; }
- public int R { get; set; }
+ // Nullable behind the int so Parse can refuse a coordinate the file left out instead of reading it as 0 (#2512).
+ int? q, r;
+ public int Q { get => q ?? 0; set => q = value; }
+ public int R { get => r ?? 0; set => r = value; }
+ internal bool HasQ => q.HasValue;
+ internal bool HasR => r.HasValue;
}
public sealed class TerrainOverride
@@ -151,6 +155,8 @@ namespace Statecraft.Sim
static void Required(string file, string at, MapHexRef? hex)
{
if (hex == null) throw new ContentException(file, at + ".hex", "required");
+ if (!hex.HasQ) throw new ContentException(file, at + ".hex.q", "required");
+ if (!hex.HasR) throw new ContentException(file, at + ".hex.r", "required");
}
/// <summary>Checks terrain and location overrides against the map: a terrain override names a land hex of