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 units layer takes the map's unit spots instead of placing again, about 24 ms (refs #321) (#563)
Eyal Golana8ae6d355d43
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8ccfab32f..35aee37af 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -279,6 +279,9 @@ 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 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.- Decision videos (refs #434): the clips no longer carry the burned-in "AI-generated · נוצר בבינה מלאכותית" caption.Owner ruling of 14 October 2026, relayed by the coordinator: "the 3 ilustration clips are great but no need to saythere were created by ai". All 14 clips were re-encoded from their promo shots (each checked by SHA-256) with the samediff --git a/unity/Assets/Scripts/Map/MapScene.cs b/unity/Assets/Scripts/Map/MapScene.csindex eb02cf3ef..bf70e5e08 100644--- a/unity/Assets/Scripts/Map/MapScene.cs+++ b/unity/Assets/Scripts/Map/MapScene.cs@@ -142,6 +142,32 @@ namespace Statecraft.Game.Mappublic IReadOnlyList<Units.UnitEntry> UnitEntries { get; private set; } = Array.Empty<Units.UnitEntry>();string unitsYamlRead; // null before a build+ /// <summary>The unit spots the last Build placed (UnitGround.Place over <see cref="UnitEntries"/>, keyed by their+ /// anchors; an anchor it could not place has none) and the hexes it placed them on. The units layer takes them instead+ /// of placing again when it draws the same units on the same hexes (issue #321; UnitSpotsFor).</summary>+ public IReadOnlyDictionary<Units.UnitAnchor, Units.AnchorSpot> UnitSpots { get; private set; } = new Dictionary<Units.UnitAnchor, Units.AnchorSpot>();+ /// <remarks>Kept for the map's life, not released once a layer has taken the spots: GameRoot.Begin may run again on+ /// the same map (it shuts the old layer down and makes a new one; the editor tests do), and each layer checks its+ /// hexes against these. 1,102 small objects; nothing writes to them after Build.</remarks>+ public IReadOnlyList<Statecraft.Sim.MapHex> UnitHexes { get; private set; } = Array.Empty<Statecraft.Sim.MapHex>();++ /// <summary>The map's unit spots when <paramref name="units"/> is the list the map+ /// placed and <paramref name="hexes"/> match the hexes it placed them on (q, r, lon, lat and land, in order): the same+ /// ground, so the same spots. Null otherwise: the caller places them itself.</summary>+ public IReadOnlyDictionary<Units.UnitAnchor, Units.AnchorSpot> UnitSpotsFor(+ IReadOnlyList<Units.UnitEntry> units, IReadOnlyList<Statecraft.Sim.MapHex> hexes)+ {+ if (unitsYamlRead == null || !ReferenceEquals(units, UnitEntries) || hexes == null || hexes.Count != UnitHexes.Count) return null;+ for (int i = 0; i < hexes.Count; i++)+ {+ var (a, b) = (hexes[i], UnitHexes[i]);+ if (a.Q != b.Q || a.R != b.R || a.Land != b.Land+ || BitConverter.DoubleToInt64Bits(a.Lon) != BitConverter.DoubleToInt64Bits(b.Lon)+ || BitConverter.DoubleToInt64Bits(a.Lat) != BitConverter.DoubleToInt64Bits(b.Lat)) return null;+ }+ return UnitSpots;+ }+/// <summary>The units for <paramref name="yaml"/>: the list the last Build parsed when it read the same text (issue/// #321: the file was parsed twice at boot, here and for the units layer), else a parse of it (UnitData.ReadOrNone,/// warning through <paramref name="warn"/>).</summary>@@ -272,6 +298,7 @@ namespace Statecraft.Game.MapUnitEntries = unitData; unitsYamlRead = inputs.UnitsYaml ?? "";DiagLog.BootPhase("unit data read");var unitHexes = Hexes.hexes.Select(h => new Statecraft.Sim.MapHex { Q = h.q, R = h.r, Lon = h.lon, Lat = h.lat, ElevM = h.elev_m, Land = h.land, Biome = h.biome }).ToList();+ UnitHexes = unitHexes;var unitGround = new Units.UnitGround(SettlementFootprints, RouteCorridors, unitHexes,(lon, lat) => { var w = Frame.ToWorld(lon, lat, 0); return new Vector2(w.x, w.z); }, hexSizeM);var clearings = new List<(Vector2 centre, float radius)>();@@ -279,6 +306,7 @@ namespace Statecraft.Game.Mapif (unitData.Count > 0){var spots = unitGround.Place(unitData, (lon, lat) => { var w = Frame.ToWorld(lon, lat, 0); return new Vector2(w.x, w.z); }, null);+ UnitSpots = spots;foreach (var kv in spots.OrderBy(k => k.Value.X).ThenBy(k => k.Value.Z)){var c = new Vector2((float)kv.Value.X, (float)kv.Value.Z);@@ -414,6 +442,7 @@ namespace Statecraft.Game.MapTerrain = null;Hexes = 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/Units/UnitLayer.cs b/unity/Assets/Scripts/Map/Units/UnitLayer.csindex b3c3a578d..175aadcbf 100644--- a/unity/Assets/Scripts/Map/Units/UnitLayer.cs+++ b/unity/Assets/Scripts/Map/Units/UnitLayer.cs@@ -29,7 +29,7 @@ namespace Statecraft.Game.Map.Units/// <summary>The unit data the layer draws from (read only; MapScene.UnitEntries when it read the same file).</summary>public IReadOnlyList<UnitEntry> Units => units;UnitGround ground = null!;- Dictionary<UnitAnchor, AnchorSpot> spots = new Dictionary<UnitAnchor, AnchorSpot>();+ IReadOnlyDictionary<UnitAnchor, AnchorSpot> spots = new Dictionary<UnitAnchor, AnchorSpot>();MapRuntimeAssets assets = null!;Material paint = null!, ringTemplate = null!, badgePlate = null!, badgeGlyph = null!;readonly Dictionary<string, Mesh> meshes = new Dictionary<string, Mesh>();@@ -81,7 +81,14 @@ namespace Statecraft.Game.Map.Unitslayer.map = map; layer.units = units; layer.still = still;// The same ground and the same placement as MapScene's (which keeps the kits off a token in a town).layer.ground = Ground(map, hexes);- layer.spots = layer.ground.Place(units, XZ(map), Debug.LogWarning);+ // The map's own spots when it placed these units on these hexes (issue #321: the same ground, so the same spots;+ // UnitSpotsOnceTests), with its skipped anchors' warnings; else placed here.+ if (map.UnitSpotsFor(units, hexes) is { } mapSpots)+ {+ layer.spots = mapSpots;+ foreach (var skip in UnitPlacer.SkipMessages(units, mapSpots)) Debug.LogWarning(skip);+ }+ else layer.spots = layer.ground.Place(units, XZ(map), Debug.LogWarning);layer.assets = root.AddComponent<MapRuntimeAssets>();var palette = layer.assets.Own(UnitPalette.MakeTexture(readable: false));layer.paint = layer.assets.Own(new Material(template) { name = "UnitMiniature (palette)" });diff --git a/unity/Assets/Scripts/Map/Units/UnitPlacer.cs b/unity/Assets/Scripts/Map/Units/UnitPlacer.csindex 94e27fa4d..48a642960 100644--- a/unity/Assets/Scripts/Map/Units/UnitPlacer.cs+++ b/unity/Assets/Scripts/Map/Units/UnitPlacer.cs@@ -112,7 +112,7 @@ namespace Statecraft.Game.Map.Unitsif (best != null) break;if (sea) break; // a ship's sea hex is all its test; rounds 2 and 3 add nothing}- if (best == null) { skipped?.Invoke($"UNITS no clear spot within one hex of {a.Place} for {u.Id}; not drawn"); continue; }+ if (best == null) { skipped?.Invoke(SkipMessage(u, a)); continue; }held.Add((best.Value.Q, best.Value.R, from, to));result[a] = best.Value;}@@ -120,6 +120,22 @@ namespace Statecraft.Game.Map.Unitsreturn result;}+ /// <summary>The message for an anchor PlaceAll found no clear spot for.</summary>+ public static string SkipMessage(UnitEntry u, UnitAnchor a) => $"UNITS no clear spot within one hex of {a.Place} for {u.Id}; not drawn";++ /// <summary>PlaceAll's skip messages for a placement it made of <paramref name="units"/>: one for every anchor of a+ /// drawn unit with no spot, in its order (issue #321: the units layer reports the map's skips when it takes its spots).</summary>+ public static List<string> SkipMessages(IReadOnlyList<UnitEntry> units, IReadOnlyDictionary<UnitAnchor, AnchorSpot> spots)+ {+ var list = new List<string>();+ foreach (var u in units)+ {+ if (u.Hidden != null) continue;+ foreach (var a in u.Anchors) if (!spots.ContainsKey(a)) list.Add(SkipMessage(u, a));+ }+ return list;+ }+/// <summary>Within the one-hex limit around a `toward` point (every round): rings every/// <see cref="TowardStepM"/> out to <see cref="TowardReachM"/>, twelve points each, so a spot can sit between a/// town's roads near the sourced place, not only on the hex grid's points.</summary>diff --git a/unity/Assets/Tests/EditMode/UnitSpotsOnceTests.cs b/unity/Assets/Tests/EditMode/UnitSpotsOnceTests.csnew file mode 100644index 000000000..ae5c23403--- /dev/null+++ b/unity/Assets/Tests/EditMode/UnitSpotsOnceTests.cs@@ -0,0 +1,150 @@+#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.Map.Units;+using Statecraft.Game.UI;+using Statecraft.Sim;+using UnityEditor.SceneManagement;+using UnityEngine;+using UnityEngine.TestTools;+using Object = UnityEngine.Object;++/// <summary>Issue #321 (T57): the units layer placed every anchor again (UnitGround.Place, about 40 ms at cpu 4) on the+/// ground the map had just placed them on. It now takes the map's spots, but only when its ground is the map's: the same+/// units (the map's own list, #560), the same footprints and route corridors (the map's), and the same hexes, q, r, lon,+/// lat and land in order. These tests first show the ground is the same on the real map, then that the spots the layer+/// takes are exactly a fresh placement on its own ground (every spot and every skip), and that a rebuilt map never hands+/// on spots for other units.</summary>+public class UnitSpotsOnceTests+{+ string saveDir = "";++ [SetUp]+ public void SetUp() => saveDir = Path.Combine(Path.GetTempPath(), "statecraft-spotsonce-" + 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);++ static (MapScene map, GameRoot root) Boot(GameInputs inputs, string saveDir)+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var map = Object.FindFirstObjectByType<MapScene>();+ map.Build(inputs.Map);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ root.Begin(inputs, 0);+ return (map, root);+ }++ /// <summary>The ground the layer places on (UnitLayer.Ground: the map's footprints and corridors, the sim content's+ /// hexes) is the ground the map placed on (its own hexes from hexes.json): the same hexes, bit for bit, in order.</summary>+ [Test]+ public void TheLayersGroundIsTheMapsGround()+ {+ var inputs = Inputs();+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var map = Object.FindFirstObjectByType<MapScene>();+ map.Build(inputs.Map);+ var mine = inputs.Content.Hexes; var theirs = map.UnitHexes;+ Assert.AreEqual(theirs.Count, mine.Count);+ for (int i = 0; i < mine.Count; i++)+ {+ Assert.AreEqual((theirs[i].Q, theirs[i].R, theirs[i].Land), (mine[i].Q, mine[i].R, mine[i].Land), $"hex {i}");+ Assert.AreEqual(System.BitConverter.DoubleToInt64Bits(theirs[i].Lon), System.BitConverter.DoubleToInt64Bits(mine[i].Lon), $"hex {i} lon");+ Assert.AreEqual(System.BitConverter.DoubleToInt64Bits(theirs[i].Lat), System.BitConverter.DoubleToInt64Bits(mine[i].Lat), $"hex {i} lat");+ }+ Assert.IsNotNull(map.UnitSpotsFor(map.UnitEntries, mine), "the map hands its spots to the layer's ground");+ }++ /// <summary>The booted layer takes the map's spots (no second placement) and they are a fresh placement on the layer's+ /// own ground, spot for spot and skip for skip.</summary>+ [Test]+ public void TheLayerTakesTheMapsSpotsWhichAreAFreshPlacement()+ {+ var inputs = Inputs();+ var (map, root) = Boot(inputs, saveDir);+ var layer = root.Units!;+ Assert.IsNotEmpty(map.UnitSpots);+ Assert.AreSame(map.UnitSpots, layer.Spots, "the layer took the map's spots instead of placing again");+ var skipped = new List<string>();+ var fresh = UnitLayer.Ground(map, inputs.Content.Hexes)+ .Place(layer.Units, (lon, lat) => { var w = map.Frame.ToWorld(lon, lat, 0); return new Vector2(w.x, w.z); }, skipped.Add);+ Assert.AreEqual(fresh.Count, layer.Spots.Count);+ foreach (var kv in fresh)+ {+ Assert.IsTrue(layer.Spots.TryGetValue(kv.Key, out var spot), kv.Key.Place);+ Assert.AreEqual(kv.Value, spot, kv.Key.Place);+ }+ CollectionAssert.AreEqual(skipped, UnitPlacer.SkipMessages(layer.Units, layer.Spots), "the same anchors skipped, with the same messages");+ }++ /// <summary>Review of #563, nit 3: an anchor with no clear spot is still reported when the layer takes the map's spots+ /// (it logs the map's skips as its own placement did). Golani's first anchor is moved out to sea, where no army token+ /// can stand within one hex.</summary>+ [Test]+ public void TheLayerReportsTheMapsSkippedAnchors()+ {+ var inputs = Inputs();+ const string yavneel = " lon: 35.49944\n lat: 32.70944\n";+ StringAssert.Contains(yavneel, inputs.Map.UnitsYaml, "Golani's first anchor, Yavne'el");+ inputs.Map.UnitsYaml = inputs.Map.UnitsYaml.Replace(yavneel, " lon: 34.30000\n lat: 31.90000\n");+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var map = Object.FindFirstObjectByType<MapScene>();+ map.Build(inputs.Map);+ var skips = UnitPlacer.SkipMessages(map.UnitEntries, map.UnitSpots);+ Assert.IsNotEmpty(skips, "the map could not place the anchor at sea");+ foreach (var skip in skips) LogAssert.Expect(LogType.Warning, skip);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ root.Begin(inputs, 0);+ Assert.AreSame(map.UnitSpots, root.Units!.Spots, "taken from the map, so the warnings are the re-logged skips");+ }++ /// <summary>Other hexes, other units or a rebuilt map: no spots handed on, so the layer places its own.</summary>+ [Test]+ public void SpotsAreHandedOnOnlyForTheSameUnitsAndHexes()+ {+ var inputs = Inputs();+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var map = Object.FindFirstObjectByType<MapScene>();+ map.Build(inputs.Map);+ var units = map.UnitEntries; var hexes = inputs.Content.Hexes;+ Assert.IsNotNull(map.UnitSpotsFor(units, hexes));+ Assert.IsNull(map.UnitSpotsFor(UnitData.Parse(inputs.Map.UnitsYaml), hexes), "a list the map did not place");+ // Each field the guard compares, changed on one hex alone (review of #563: lat, land, q, r, and the last hex, so a+ // guard that dropped a field or stopped a hex short would hand the spots on).+ List<MapHex> With(int i, System.Func<MapHex, MapHex> change)+ {+ var copy = hexes.Select(h => new MapHex { Q = h.Q, R = h.R, Lon = h.Lon, Lat = h.Lat, ElevM = h.ElevM, Land = h.Land, Biome = h.Biome }).ToList();+ copy[i] = change(copy[i]);+ return copy;+ }+ MapHex Copy(MapHex h) => new MapHex { Q = h.Q, R = h.R, Lon = h.Lon, Lat = h.Lat, ElevM = h.ElevM, Land = h.Land, Biome = h.Biome };+ int last = hexes.Count - 1;+ Assert.IsNotNull(map.UnitSpotsFor(units, With(100, h => { var c = Copy(h); c.ElevM += 10f; return c; })), "elevation is not the placer's input");+ Assert.IsNull(map.UnitSpotsFor(units, With(100, h => { var c = Copy(h); c.Lon += 1e-9; return c; })), "a hex's lon moved");+ Assert.IsNull(map.UnitSpotsFor(units, With(100, h => { var c = Copy(h); c.Lat += 1e-9; return c; })), "a hex's lat moved");+ Assert.IsNull(map.UnitSpotsFor(units, With(100, h => { var c = Copy(h); c.Land = !c.Land; return c; })), "a hex's land flipped");+ Assert.IsNull(map.UnitSpotsFor(units, With(100, h => { var c = Copy(h); c.Q += 1; return c; })), "a hex's q changed");+ Assert.IsNull(map.UnitSpotsFor(units, With(100, h => { var c = Copy(h); c.R += 1; return c; })), "a hex's r changed");+ Assert.IsNull(map.UnitSpotsFor(units, With(last, h => { var c = Copy(h); c.Lat += 1e-9; return c; })), "the last hex moved");+ Assert.IsNull(map.UnitSpotsFor(units, hexes.Take(hexes.Count - 1).ToList()), "a hex missing");+ // A rebuild from other unit text: the old list is no longer the map's, and the new spots are for the new units.+ inputs.Map.UnitsYaml = "";+ map.Build(inputs.Map);+ Assert.IsNull(map.UnitSpotsFor(units, hexes), "stale units after a rebuild");+ CollectionAssert.IsEmpty(map.UnitSpots);+ }+}diff --git a/unity/Assets/Tests/EditMode/UnitSpotsOnceTests.cs.meta b/unity/Assets/Tests/EditMode/UnitSpotsOnceTests.cs.metanew file mode 100644index 000000000..316a746a2--- /dev/null+++ b/unity/Assets/Tests/EditMode/UnitSpotsOnceTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: bceafa7db3d148519c2f92a0b6526f33