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(map): TownLandmarks keeps the building layer instead of walking the map every frame (#2939)

Eyal Golan6a05a5457393
diff --git a/changelog.d/e21-layer-cache.md b/changelog.d/e21-layer-cache.md
new file mode 100644
index 000000000..6a32980f9
--- /dev/null
+++ b/changelog.d/e21-layer-cache.md
@@ -0,0 +1,4 @@
+### Fixed
+
+- The whole-town landmarks find the map's building layer once and keep it, instead of walking the whole map hierarchy
+ for it on every town swap, up to three times per camera per frame (frame cost finding, 7 November 2026).
diff --git a/unity/Assets/Scripts/Map/Collection/TownLandmarks.cs b/unity/Assets/Scripts/Map/Collection/TownLandmarks.cs
index f074b090e..645fb09f6 100644
--- a/unity/Assets/Scripts/Map/Collection/TownLandmarks.cs
+++ b/unity/Assets/Scripts/Map/Collection/TownLandmarks.cs
@@ -631,8 +631,20 @@ namespace Statecraft.Game.Map.Collection
/// road vehicle layers read it from here.</summary>
public static float ViewDistance(Camera cam) => Kits.TownModelLayer.ViewDistance(cam);
- /// <summary>#700's building layer of this map (null without kits).</summary>
- Kits.TownModelLayer? Layer() => map == null ? null : map.GetComponentInChildren<Kits.TownModelLayer>(true);
+ /// <summary>#700's building layer of this map (null without kits). Found once and kept: WholeReady asks for it for
+ /// every town swap, up to three times per camera per frame, and the walk of the whole map hierarchy grows below the
+ /// close switch (frame cost finding, 7 November 2026). A destroyed layer, or one no longer under this map, is looked
+ /// up again.</summary>
+ Kits.TownModelLayer? Layer()
+ {
+ if (map == null) return null;
+ if (buildingLayer != null && buildingLayer.transform.IsChildOf(map.transform)) return buildingLayer;
+ LayerLookups++;
+ return buildingLayer = map.GetComponentInChildren<Kits.TownModelLayer>(true);
+ }
+ Kits.TownModelLayer? buildingLayer;
+ /// <summary>How many times <see cref="Layer"/> has walked the map's hierarchy for the building layer (a test aid).</summary>
+ internal int LayerLookups { get; private set; }
void OnEnable()
{
diff --git a/unity/Assets/Tests/EditMode/TownLandmarksTests.cs b/unity/Assets/Tests/EditMode/TownLandmarksTests.cs
index 8cb71a4bf..eb27cc070 100644
--- a/unity/Assets/Tests/EditMode/TownLandmarksTests.cs
+++ b/unity/Assets/Tests/EditMode/TownLandmarksTests.cs
@@ -1120,4 +1120,34 @@ public class TownLandmarksTests
finally { enabled.SetValue(f.map.Residency.Towns, true); }
}
}
+
+ /// <summary>Frame cost finding of 7 November 2026 (the "none" switch set measured slower than "all" at Tel Aviv 39 km):
+ /// with the "kits" switch off the building layer's Draw is false, so no town is ReadyForTown and every town swap in
+ /// TownModelLayer.PrepareCameraCore asks TownLandmarks.WholeReady, which looked the building layer up with a
+ /// GetComponentInChildren walk of the whole map. The layer is found once and kept: frames of the swap update walk the
+ /// hierarchy no more, and a destroyed layer is looked up again.</summary>
+ [Test]
+ public void TheBuildingLayerIsLookedUpOnceNotOnEveryFrame()
+ {
+ var map = OpenMap(); var t = ShowAll(map);
+ Assert.IsNotEmpty(t.Seats, "precondition: seated whole towns, so the swap update asks WholeReady");
+ var layer = map.GetComponentInChildren<TownModelLayer>(true);
+ layer.Draw = false; // what the "kits" switch off does (KitLayer.ShowKits)
+ var inner = layer.WholeTownReady!; int asks = 0;
+ layer.WholeTownReady = (id, c) => { asks++; return inner(id, c); };
+ var cam = new GameObject("frame camera").AddComponent<Camera>();
+ var at = t.Seats.Keys.Select(k => t.Wanted.First(w => w.Key == k).At).First();
+ cam.transform.position = new Vector3(at.x, 27000, at.y - 27000); cam.transform.LookAt(new Vector3(at.x, 0, at.y));
+ t.Choose(default, cam); layer.PrepareCamera(cam); // the first frame may resolve the layer
+ int before = t.LayerLookups; asks = 0;
+ const int frames = 30;
+ for (int i = 0; i < frames; i++) { t.Choose(default, cam); layer.PrepareCamera(cam); } // ChooseCore and Submit
+ Assert.Greater(asks, 0, "the swap update asked WholeReady for the seated towns");
+ Assert.AreEqual(0, t.LayerLookups - before,
+ $"{frames} frames of the swap update ({asks} WholeReady asks) walked the map hierarchy for the building layer");
+ Object.DestroyImmediate(layer.gameObject);
+ t.Choose(default, cam);
+ Assert.Greater(t.LayerLookups, before, "a destroyed building layer is looked up again");
+ Object.DestroyImmediate(cam.gameObject);
+ }
}