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): validate settlement ids against settlements_1948.yaml; eager hex index; pin Jerusalem and Tel Aviv

eyalgolan455c43c7d09c
diff --git a/sim/Statecraft.Sim.Tests/Fixture.cs b/sim/Statecraft.Sim.Tests/Fixture.cs
index de5073f6f..20c94cd8b 100644
--- a/sim/Statecraft.Sim.Tests/Fixture.cs
+++ b/sim/Statecraft.Sim.Tests/Fixture.cs
@@ -149,6 +149,10 @@ anchors: []
["map/settlement_hexes.yaml"] = @"
settlements:
- { id: town_a, q: 1, r: 0 }
+",
+ ["map/settlements_1948.yaml"] = @"
+settlements:
+ - { id: town_a, name: Town A, lon: 34.29, lat: 33.35, type: town, founded: 1900, pre_modern: false }
",
["aliyah.yaml"] = @"
series:
diff --git a/sim/Statecraft.Sim.Tests/MapContentTests.cs b/sim/Statecraft.Sim.Tests/MapContentTests.cs
index 90f2f1363..47dece19c 100644
--- a/sim/Statecraft.Sim.Tests/MapContentTests.cs
+++ b/sim/Statecraft.Sim.Tests/MapContentTests.cs
@@ -51,6 +51,39 @@ public class MapContentTests
Assert.Equal("map/hexes.json", ex.File);
}
+ [Fact]
+ public void ASettlementHexRowForAnUnknownSettlementIsALoadError()
+ {
+ var bad = Fixture.Files["map/settlement_hexes.yaml"] + " - { id: town_b, q: 2, r: 0 }\n";
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["map/settlement_hexes.yaml"] = bad })));
+ Assert.Equal("map/settlement_hexes.yaml", ex.File);
+ Assert.Equal("settlements[town_b]", ex.Field);
+ Assert.Contains("is not in map/settlements_1948.yaml", ex.Message);
+ }
+
+ [Fact]
+ public void ASettlementWithoutAHexRowIsALoadError()
+ {
+ var bad = Fixture.Files["map/settlements_1948.yaml"] + " - { id: town_c, name: Town C, lon: 34.4, lat: 33.3, type: town, founded: 1900, pre_modern: false }\n";
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["map/settlements_1948.yaml"] = bad })));
+ Assert.Equal("map/settlements_1948.yaml", ex.File);
+ Assert.Equal("settlements[town_c]", ex.Field);
+ Assert.Contains("has no hex in map/settlement_hexes.yaml", ex.Message);
+ }
+
+ [Fact]
+ public void AMissingSettlementHexesFileIsALoadError()
+ {
+ var dir = Fixture.Write();
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(rel =>
+ {
+ var path = System.IO.Path.Combine(dir, rel);
+ return rel == "map/settlement_hexes.yaml" || !System.IO.File.Exists(path) ? null : System.IO.File.ReadAllText(path);
+ }, new string[0]));
+ Assert.Equal("map/settlement_hexes.yaml", ex.File);
+ Assert.Contains("missing file", ex.Message);
+ }
+
[Fact]
public void TheRealMapHas1102HexesAndEvery1948Settlement()
{
diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.cs
index 06ccb8259..bded155be 100644
--- a/sim/Statecraft.Sim/Content.cs
+++ b/sim/Statecraft.Sim/Content.cs
@@ -227,19 +227,26 @@ namespace Statecraft.Sim
public List<AliyahPeriod> Aliyah = new List<AliyahPeriod>();
public List<PopulationAnchor> PopulationAnchors = new List<PopulationAnchor>();
public List<GameEvent> Events = new List<GameEvent>();
- public List<MapHex> Hexes = new List<MapHex>();
- public List<SettlementHex> SettlementHexes = new List<SettlementHex>();
- Dictionary<(int, int), MapHex>? _hexIndex;
+ List<MapHex> _hexes = new List<MapHex>();
+ Dictionary<(int, int), MapHex> _hexIndex = new Dictionary<(int, int), MapHex>();
- /// <summary>The hex at axial (q, r), or null when the map has none there.</summary>
- public MapHex? HexAt(int q, int r)
+ /// <summary>The map. Assign it (the index behind <see cref="HexAt"/> is rebuilt on assignment, so it cannot go stale).</summary>
+ public List<MapHex> Hexes
{
- if (_hexIndex == null)
+ get => _hexes;
+ set
{
+ _hexes = value;
var index = new Dictionary<(int, int), MapHex>();
- foreach (var h in Hexes) index[(h.Q, h.R)] = h;
+ foreach (var h in value) index[(h.Q, h.R)] = h;
_hexIndex = index;
}
+ }
+ public List<SettlementHex> SettlementHexes = new List<SettlementHex>();
+
+ /// <summary>The hex at axial (q, r), or null when the map has none there.</summary>
+ public MapHex? HexAt(int q, int r)
+ {
return _hexIndex.TryGetValue((q, r), out var hex) ? hex : null;
}
}
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index 6fddc21fc..46da18380 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -42,6 +42,26 @@ namespace Statecraft.Sim
public const string HexesFile = "map/hexes.json";
public const string SettlementHexesFileName = "map/settlement_hexes.yaml";
+ public const string SettlementsFileName = "map/settlements_1948.yaml";
+
+ /// <summary>The ids of settlements_1948.yaml, which the tools own (its other keys are not the sim's business), so it is
+ /// read as plain maps rather than through the strict content types.</summary>
+ static List<string> ReadSettlementIds(Func<string, string?> readFile)
+ {
+ string? text;
+ try { text = readFile(SettlementsFileName); }
+ catch (IOException e) { throw new ContentException(SettlementsFileName, "(file)", e.Message); }
+ catch (UnauthorizedAccessException e) { throw new ContentException(SettlementsFileName, "(file)", e.Message); }
+ if (text == null) throw new ContentException(SettlementsFileName, "(file)", "missing file");
+ try
+ {
+ var file = new DeserializerBuilder().Build().Deserialize<Dictionary<string, List<Dictionary<string, object>>>>(text);
+ if (file == null || !file.TryGetValue("settlements", out var rows)) throw new ContentException(SettlementsFileName, "settlements", "required");
+ return rows.Select((r, i) => r.TryGetValue("id", out var id) && id is string sid && sid != ""
+ ? sid : throw new ContentException(SettlementsFileName, $"settlements[{i}].id", "required")).ToList();
+ }
+ catch (YamlException e) { throw new ContentException(SettlementsFileName, $"line {e.Start.Line}", e.InnerException?.Message ?? e.Message); }
+ }
static readonly IDeserializer Yaml = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
@@ -119,13 +139,14 @@ namespace Statecraft.Sim
c.PopulationAnchors = Read<PopulationFile>(readFile, "population.yaml").Anchors;
c.Hexes = Read<MapFile>(readFile, HexesFile).Hexes;
c.SettlementHexes = Read<SettlementHexesFile>(readFile, SettlementHexesFileName).Settlements;
+ var settlementIds = ReadSettlementIds(readFile);
foreach (var rel in eventFiles.OrderBy(p => p, StringComparer.Ordinal))
{
var e = Read<GameEvent>(readFile, rel);
e.File = rel;
c.Events.Add(e);
}
- Validate(c);
+ Validate(c, settlementIds);
return c;
}
@@ -203,7 +224,7 @@ namespace Statecraft.Sim
}
}
- static void Validate(Content c)
+ static void Validate(Content c, List<string> settlementIds)
{
var offices = new HashSet<string>(c.Offices.Select(o => o.Id));
var parties = new HashSet<string>(c.Parties.Select(p => p.Id));
@@ -380,11 +401,11 @@ namespace Statecraft.Sim
if (a.Date == default) throw new ContentException("population.yaml", path + ".date", "required");
Count("population.yaml", path + ".jews", a.Jews);
}
- ValidateMap(c);
+ ValidateMap(c, settlementIds);
EventRules.Validate(c, offices, indicators);
}
- static void ValidateMap(Content c)
+ static void ValidateMap(Content c, List<string> settlementIds)
{
if (c.Hexes.Count == 0) throw new ContentException(HexesFile, "hexes", "no hexes");
var seen = new HashSet<(int, int)>();
@@ -395,6 +416,15 @@ namespace Statecraft.Sim
if (h.Biome == "") throw new ContentException(HexesFile, at + ".biome", "required");
}
Unique(SettlementHexesFileName, "settlements", c.SettlementHexes.Select(s => s.Id));
+ Unique(SettlementsFileName, "settlements", settlementIds);
+ var known = new HashSet<string>(settlementIds);
+ var placed = new HashSet<string>(c.SettlementHexes.Select(s => s.Id));
+ foreach (var s in c.SettlementHexes)
+ if (!known.Contains(s.Id))
+ throw new ContentException(SettlementHexesFileName, $"settlements[{s.Id}]", $"is not in {SettlementsFileName}");
+ foreach (var id in settlementIds)
+ if (!placed.Contains(id))
+ throw new ContentException(SettlementsFileName, $"settlements[{id}]", $"has no hex in {SettlementHexesFileName}");
foreach (var s in c.SettlementHexes)
{
var hex = c.HexAt(s.Q, s.R);
diff --git a/tools/tests/test_settlement_hexes.py b/tools/tests/test_settlement_hexes.py
index ca9d98706..691cb0ca4 100644
--- a/tools/tests/test_settlement_hexes.py
+++ b/tools/tests/test_settlement_hexes.py
@@ -29,3 +29,11 @@ def test_every_settlement_is_on_its_rule_hex_and_that_hex_is_land():
def test_tiberias_is_moved_off_the_lake():
rows = {r["id"]: r for r in yaml.safe_load((MAP / "settlement_hexes.yaml").read_text())["settlements"]}
assert BY_QR[(rows["tiberias"]["q"], rows["tiberias"]["r"])]["biome"] != "lake"
+
+
+def test_two_known_settlements_are_pinned_literally():
+ rows = {r["id"]: (r["q"], r["r"]) for r in yaml.safe_load((MAP / "settlement_hexes.yaml").read_text())["settlements"]}
+ assert rows["jerusalem"] == (0, 23)
+ # jaffa and tel_aviv share this hex legitimately: Jaffa is the old port town beside Tel Aviv, inside one 5 km hex.
+ assert rows["tel_aviv"] == (-3, 19)
+ assert rows["jaffa"] == rows["tel_aviv"]