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.

Map: the settlements file parsed once at boot, the names from the map's parse, about 11 ms (refs #321) (#587)

Eyal Golan30f88557cb28
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 885998d08..faa93dc14 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -295,6 +295,8 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
+0.9 ms at the gameplay zoom), not yet on an idle one.
### Changed
+- The settlements file is parsed once at boot instead of twice: the info panel's settlement names come from the map's
+ own parse (refs #321). The names are unchanged. Presentation only.
- The units layer takes the unit spots the map placed instead of placing every unit again (refs #321), when it draws
the same units on the same hexes; otherwise it places them as before. Every token stands where it stood, and the
anchors with no clear spot are reported as before. Presentation only.
diff --git a/unity/Assets/Scripts/Map/MapScene.cs b/unity/Assets/Scripts/Map/MapScene.cs
index bf70e5e08..d877e27d9 100644
--- a/unity/Assets/Scripts/Map/MapScene.cs
+++ b/unity/Assets/Scripts/Map/MapScene.cs
@@ -25,8 +25,17 @@ namespace Statecraft.Game.Map
public HexFile Hexes { get; private set; }
public Terrain Terrain { get; private set; }
public Material MarkerMaterial => markerMaterial;
- /// <summary>The settlements the last Build placed (settlements_1948.yaml), for the UI's map labels.</summary>
+ /// <summary>The settlements the last Build placed (settlements_1948.yaml), for the UI's map labels and, through
+ /// SettlementsFor, the info panel's names. Read only: the markers, the kits, the labels and GameRoot share this one
+ /// list and its objects; nothing may write to them.</summary>
public IReadOnlyList<Settlement> Settlements { get; private set; } = new List<Settlement>();
+ string settlementsYamlRead; // null before a build
+
+ /// <summary>The settlements for <paramref name="yaml"/>: the list the last Build parsed when it read the same text
+ /// (issue #321: GameRoot parsed the file again at boot only for the names), else a parse of it.</summary>
+ public IReadOnlyList<Settlement> SettlementsFor(string yaml) =>
+ settlementsYamlRead != null && string.Equals(yaml, settlementsYamlRead, StringComparison.Ordinal)
+ ? Settlements : SettlementPlacer.Parse(yaml);
/// <summary>The 1948 localities the last Build drew with the kits (content/map/localities_1948.yaml; empty without the
/// kits), for the map labels: 1948 name, today's name. Presentation only, never in the sim.</summary>
public IReadOnlyList<Kits.Localities.Place> Localities { get; private set; } = new List<Kits.Localities.Place>();
@@ -219,7 +228,7 @@ namespace Statecraft.Game.Map
Hexes = MapBuilder.LoadHexes(inputs.HexesJson, HexesPath);
DiagLog.BootPhase("hexes parsed");
var list = SettlementPlacer.Parse(inputs.SettlementsYaml, (meta.lon0, meta.lon1, meta.lat0, meta.lat1), SettlementsPath);
- Settlements = list;
+ Settlements = list; settlementsYamlRead = inputs.SettlementsYaml;
bool kitsOn = kitMaterial != null && treeMaterial != null;
var localities = new Kits.Localities.Doc();
if (kitsOn)
@@ -441,6 +450,7 @@ namespace Statecraft.Game.Map
}
Terrain = null;
Hexes = null;
+ settlementsYamlRead = null;
UnitEntries = Array.Empty<Units.UnitEntry>(); unitsYamlRead = null;
UnitSpots = new Dictionary<Units.UnitAnchor, Units.AnchorSpot>(); UnitHexes = Array.Empty<Statecraft.Sim.MapHex>();
}
diff --git a/unity/Assets/Scripts/Map/SettlementPlacer.cs b/unity/Assets/Scripts/Map/SettlementPlacer.cs
index 355874fdb..20bcb567b 100644
--- a/unity/Assets/Scripts/Map/SettlementPlacer.cs
+++ b/unity/Assets/Scripts/Map/SettlementPlacer.cs
@@ -43,6 +43,10 @@ namespace Statecraft.Game.Map
static readonly Regex IdRule = new Regex(@"^[a-z][a-z0-9_]*\z");
+ /// <summary>How many times <see cref="Parse"/> has run in this domain (a test aid: SettlementsOnceTests counts the
+ /// parses of one boot, issue #321).</summary>
+ internal static int Parses { get; private set; }
+
/// <summary>Parses settlements_1948.yaml. Every malformed input throws InvalidDataException naming
/// <paramref name="file"/>, and for content errors the settlement id and the field: YAML syntax, duplicate keys,
/// unknown keys or bad scalars (with the id of the settlement they sit in); empty id or name; an id that is not
@@ -52,6 +56,7 @@ namespace Statecraft.Game.Map
public static List<Settlement> Parse(string yaml, (double lon0, double lon1, double lat0, double lat1)? bounds = null,
string file = DefaultFileName)
{
+ Parses++;
SettlementFile doc;
try { doc = new DeserializerBuilder().WithNamingConvention(UnderscoredNamingConvention.Instance).WithDuplicateKeyChecking().Build().Deserialize<SettlementFile>(yaml); }
catch (YamlException e)
diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.cs
index 75f5fd9b5..ade4e739c 100644
--- a/unity/Assets/Scripts/UI/GameRoot.cs
+++ b/unity/Assets/Scripts/UI/GameRoot.cs
@@ -208,7 +208,7 @@ namespace Statecraft.Game.UI
Own(new BudgetPresenter(root, bridge, text));
Own(new WhyPresenter(root, bridge, text));
Own(new CabinetPresenter(root, bridge, text, portraits));
- var names = SettlementPlacer.Parse(inputs.Map.SettlementsYaml).ToDictionary(s => s.Id, s => s.Name);
+ var names = map.SettlementsFor(inputs.Map.SettlementsYaml).ToDictionary(s => s.Id, s => s.Name); // the map's parse (issue #321)
// The 1948 sub-districts, outlined on the map, and each front's 1949 armistice line from its agreement's date
// where the line exists (display only: the sim never reads them; docs/visual-v1.md, owner rulings 2026-10-11).
// Since the owner's rulings of 2026-10-14 the areas are coloured by side from 11 June 1948 (control_hexes_1948_49.yaml),
diff --git a/unity/Assets/Tests/EditMode/SettlementsOnceTests.cs b/unity/Assets/Tests/EditMode/SettlementsOnceTests.cs
new file mode 100644
index 000000000..d28be1c35
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/SettlementsOnceTests.cs
@@ -0,0 +1,87 @@
+#nullable enable
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using NUnit.Framework;
+using Statecraft.Game;
+using Statecraft.Game.Boot;
+using Statecraft.Game.Map;
+using Statecraft.Game.UI;
+using UnityEditor.SceneManagement;
+using UnityEngine;
+using Object = UnityEngine.Object;
+
+/// <summary>Issue #321 (T57 (c)): settlements_1948.yaml was parsed at boot by the map (MapScene.Build, the markers, kits
+/// and footprints) and again by GameRoot, only for the settlements' names (the info panel's id-to-name). GameRoot now takes
+/// the map's list when the text is the one the map read.</summary>
+public class SettlementsOnceTests
+{
+ string saveDir = "";
+
+ [SetUp]
+ public void SetUp() => saveDir = Path.Combine(Path.GetTempPath(), "statecraft-settlonce-" + System.Guid.NewGuid().ToString("N"));
+
+ [TearDown]
+ public void TearDown()
+ {
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ VisualToggles.Clear();
+ if (Directory.Exists(saveDir)) Directory.Delete(saveDir, true);
+ }
+
+ static GameInputs Inputs() => GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);
+
+ /// <summary>One parse a boot: building the map and starting the game parses the file once.</summary>
+ [Test]
+ public void ABootParsesTheSettlementsOnce()
+ {
+ var inputs = Inputs();
+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ var map = Object.FindFirstObjectByType<MapScene>();
+ int before = SettlementPlacer.Parses;
+ map.Build(inputs.Map);
+ var root = Object.FindFirstObjectByType<GameRoot>();
+ root.Store = new SaveStore(saveDir);
+ root.Begin(inputs, 0);
+ Assert.AreEqual(1, SettlementPlacer.Parses - before, "settlements_1948.yaml parsed once a boot");
+ }
+
+ /// <summary>The names GameRoot gets are a fresh parse's, id for id; other text is parsed on its own.</summary>
+ [Test]
+ public void TheMapsSettlementsGiveTheSameNames()
+ {
+ var inputs = Inputs();
+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ var map = Object.FindFirstObjectByType<MapScene>();
+ map.Build(inputs.Map);
+ var shared = map.SettlementsFor(inputs.Map.SettlementsYaml);
+ Assert.AreSame(map.Settlements, shared);
+ Assert.AreSame(map.Settlements, map.SettlementsFor(new string(inputs.Map.SettlementsYaml.ToCharArray())), "equal text, another string");
+ var fresh = SettlementPlacer.Parse(inputs.Map.SettlementsYaml);
+ CollectionAssert.AreEqual(fresh.Select(s => (s.Id, s.Name)).ToList(), shared.Select(s => (s.Id, s.Name)).ToList());
+ var other = map.SettlementsFor(inputs.Map.SettlementsYaml + "\n# edited\n");
+ Assert.AreNotSame(map.Settlements, other);
+ CollectionAssert.AreEqual(fresh.Select(s => (s.Id, s.Name)).ToList(), other.Select(s => (s.Id, s.Name)).ToList());
+ // Review of #587, nit 4: text that really differs gives its own names, not the map's.
+ var first = fresh[0];
+ var renamed = map.SettlementsFor(inputs.Map.SettlementsYaml.Replace($"name: {first.Name}\n", "name: Renamed\n"));
+ Assert.AreEqual("Renamed", renamed.First(s => s.Id == first.Id).Name, "the edited text's own name");
+ Assert.AreEqual(first.Name, map.Settlements.First(s => s.Id == first.Id).Name, "the map's list is untouched");
+ }
+
+ /// <summary>Review of #587, nit 2: Clear forgets the text the map read. A rebuild that fails on its settlements file
+ /// (after Clear, before the new parse) must not hand the old list out as the parse of the old text.</summary>
+ [Test]
+ public void AFailedRebuildForgetsTheText()
+ {
+ var inputs = Inputs();
+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ var map = Object.FindFirstObjectByType<MapScene>();
+ map.Build(inputs.Map);
+ var read = inputs.Map.SettlementsYaml; var old = map.Settlements;
+ Assert.AreSame(old, map.SettlementsFor(read));
+ inputs.Map.SettlementsYaml = "settlements: []\n";
+ Assert.Throws<InvalidDataException>(() => map.Build(inputs.Map));
+ Assert.AreNotSame(old, map.SettlementsFor(read), "the old list is not the parse of the old text after Clear");
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/SettlementsOnceTests.cs.meta b/unity/Assets/Tests/EditMode/SettlementsOnceTests.cs.meta
new file mode 100644
index 000000000..191285243
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/SettlementsOnceTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 1a4a7154047e4d279c3315fb52ce96f4