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.

Built field and drained swamp sites: no kit, no pill (Refs #2812) (#2872)

Eyal Golaned96a9172788
diff --git a/changelog.d/2812.md b/changelog.d/2812.md
new file mode 100644
index 000000000..fbd6da07e
--- /dev/null
+++ b/changelog.d/2812.md
@@ -0,0 +1,4 @@
+### Changed
+
+- A plowed field site or the drained Hula swamp no longer gets a kibbutz kit and a name pill of its own on the map.
+ Only a built yishuv or camp site does, so Beit Kama and its fields show one kibbutz and one pill on their hex.
diff --git a/unity/Assets/Scripts/Map/SiteLayer.cs b/unity/Assets/Scripts/Map/SiteLayer.cs
index 891ec2779..f9bbfa79c 100644
--- a/unity/Assets/Scripts/Map/SiteLayer.cs
+++ b/unity/Assets/Scripts/Map/SiteLayer.cs
@@ -10,10 +10,11 @@ namespace Statecraft.Game.Map
{
/// <summary>GDD Phase 1 PR 8 (#381): the designated sites the player builds on (sites.yaml, State.Sites).
/// <list type="bullet">
- /// <item>A built site is drawn as a rural kit on its hex (SettlementKits, as a kibbutz: the one archetype a new yishuv has
+ /// <item>A built yishuv site is drawn as a rural kit on its hex (SettlementKits, as a kibbutz: the one archetype a new yishuv has
/// in Phase 1), or a built camp as a camp kit (SettlementKits.Camp.cs, #792; <see cref="KitOf"/>), named
/// <see cref="Prefix"/> + its id. With the map's "kits" toggle off, it is the kibbutz marker box, as the settlements are
/// (KitLayer).</item>
+ /// <item>A built field or drained swamp site draws nothing and has no pill (#2812, "Ground change only").</item>
/// <item>A site being built is a scaffold marker: an open frame of raw timber on its hex, named Prefix + id +
/// <see cref="ScaffoldSuffix"/>.</item>
/// <item>An available site, with no record, draws nothing.</item>
@@ -74,6 +75,9 @@ namespace Statecraft.Game.Map
var at = map.Frame.ToWorld(hex.Lon, hex.Lat, 0);
at.y = map.Terrain.SampleHeight(at) + map.Terrain.transform.position.y;
if (w.BuiltOn == null) { Scaffold(Prefix + w.Site + ScaffoldSuffix, at, cube, map, block); continue; }
+ // #2812 ("Ground change only"): a built field or drained swamp is no place, so no kit, box or pill; it is left to
+ // the hex's ground. Only a yishuv or a camp site becomes a place.
+ if (!Projects.IsYishuvSite(site) && !Projects.IsCamp(site)) continue;
built.Add(new Settlement { Id = Prefix + w.Site, Name = site.Name, NameHe = site.NameHe, Type = "kibbutz", Lon = hex.Lon, Lat = hex.Lat });
kitPlaces.Add(KitOf(site, built[built.Count - 1]));
}
diff --git a/unity/Assets/Tests/EditMode/SiteFieldShots.cs b/unity/Assets/Tests/EditMode/SiteFieldShots.cs
new file mode 100644
index 000000000..1ee1cb76f
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/SiteFieldShots.cs
@@ -0,0 +1,147 @@
+#nullable enable
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.IO;
+using NUnit.Framework;
+using Statecraft.Game;
+using Statecraft.Game.Map;
+using Statecraft.Game.UI;
+using Statecraft.Sim;
+using UnityEditor.SceneManagement;
+using UnityEngine;
+using UnityEngine.TestTools;
+using UnityEngine.UIElements;
+using Object = UnityEngine.Object;
+
+/// <summary>Writes the before and after stills for #2812 ("Ground change only", PR #2872) to `.superpowers/shots-2812/` at the
+/// repository root (local, not committed), as `&lt;state&gt;-&lt;lang&gt;.png` in English (en) and Hebrew (he):
+/// <list type="bullet">
+/// <item>`both-built`, the main shot: Beit Kama (beit_kama, [yishuv]) and Beit Kama fields (beit_kama_field, [field]) both
+/// built, both at hex (-9, 32). Before: two kits or boxes and two pills on the hex. After: one of each, the yishuv's.</item>
+/// <item>`yishuv-only`, for context: Beit Kama built alone, the same camera.</item>
+/// <item>`hula-drained`: the Hula swamp (hula, [swamp]) built (drained), at its hex (16, 3). Before: a kit or box and a
+/// pill. After: neither.</item>
+/// </list>
+/// The sites are put in the game's own state (State.Sites, SiteWork.Built, as SiteLayerTests and SiteLabelTests build it)
+/// and drawn by GameRoot itself: its RenderSitesIfChanged (SiteLayer.Render, then the labels' SetSites) runs on the
+/// bridge's Changed, raised here with SimBridge.Skip on an id no unit has. The camera looks at the hex from 15 km with yaw 0
+/// and the game's own top inset (so the game's tilt-shift). Each still is the map camera's frame with the UI drawn over it
+/// (CivMovementShots.Boot, Aim's ground point, and its Shot's alpha composite). It uses only calls main has, so the same
+/// file gives the "before" stills on main and the "after" ones on the branch. Run (Unity, batch mode, the unity/ project):
+/// <code>-runTests -testPlatform EditMode -testFilter SiteFieldShots</code>
+/// at the PR's head for AFTER, and with this file (and its .meta) copied onto main for BEFORE. Explicit, so it does not run
+/// with the checks.</summary>
+[Explicit("writes stills for a pull request, not a check")]
+public class SiteFieldShots
+{
+ const float Distance = 15000f;
+ static string ShotDir => Path.GetFullPath(Path.Combine(Application.dataPath, "..", "..", ".superpowers", "shots-2812"));
+ static readonly string[] Ours = { "beit_kama", "beit_kama_field", "hula" };
+ string saveDir = "";
+ RenderTexture? rt; PanelSettings? ps;
+ VisualElement ui = null!;
+ GameRoot root = null!;
+ int poke = -2812000; // ids no unit has, for SimBridge.Skip to raise Changed (each id only once)
+
+ [SetUp]
+ public void TempSaves()
+ {
+ saveDir = Path.Combine(Path.GetTempPath(), "statecraft-site-shots-" + Guid.NewGuid().ToString("N"));
+ Directory.CreateDirectory(ShotDir);
+ }
+
+ [TearDown]
+ public void DiscardScene()
+ {
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ if (ps != null) Object.DestroyImmediate(ps);
+ if (rt != null) { rt.Release(); Object.DestroyImmediate(rt); }
+ if (Directory.Exists(saveDir)) Directory.Delete(saveDir, true);
+ }
+
+ static IEnumerator Frames(int n) { for (int i = 0; i < n; i++) yield return null; }
+
+ /// <summary>Only <paramref name="built"/> of our three sites are built (others' records are left alone), in the state the
+ /// map shows and the turn's start (the same object at a turn's start), then GameRoot redraws the sites.</summary>
+ void Build(params string[] built)
+ {
+ var bridge = root.Bridge!;
+ foreach (var s in new HashSet<GameState> { bridge.State, bridge.TurnStart })
+ {
+ foreach (var id in Ours) s.Sites.Remove(id);
+ foreach (var id in built) s.Sites[id] = SiteWork.Built(id, s.Date);
+ }
+ bridge.Skip(poke--);
+ }
+
+ /// <summary>CivMovementShots.Aim's ground point, with yaw 0; the top inset is left as the game set it.</summary>
+ static void Aim(int q, int r)
+ {
+ var map = Object.FindFirstObjectByType<MapScene>();
+ var hex = MapFocusTests.Inputs.Content.HexAt(q, r)!;
+ var ground = map.Frame.ToWorld(hex.Lon, hex.Lat, 0);
+ ground.y = map.Terrain.SampleHeight(ground) + map.Terrain.transform.position.y;
+ var rig = Camera.main.GetComponent<CameraRig>();
+ rig.LookAt = ground; rig.Distance = Distance; rig.Yaw = 0f;
+ rig.Apply();
+ }
+
+ static Color[] Read(RenderTexture from)
+ {
+ var prev = RenderTexture.active;
+ RenderTexture.active = from;
+ var tex = new Texture2D(from.width, from.height, TextureFormat.RGBA32, false);
+ tex.ReadPixels(new Rect(0, 0, from.width, from.height), 0, 0);
+ tex.Apply();
+ RenderTexture.active = prev;
+ var px = tex.GetPixels();
+ Object.DestroyImmediate(tex);
+ return px;
+ }
+
+ /// <summary>CivMovementShots.Shot into this still's folder: the map camera's frame with the UI texture over it (the UI's
+ /// pixels are premultiplied by their alpha).</summary>
+ static void Shot(RenderTexture uiTexture, string name)
+ {
+ int w = CivMovementShots.Width, h = CivMovementShots.Height;
+ var cam = Camera.main;
+ var frame = new RenderTexture(w, h, 24, RenderTextureFormat.ARGB32);
+ var before = cam.targetTexture;
+ cam.targetTexture = frame; cam.Render(); cam.targetTexture = before;
+ var map = Read(frame); var over = Read(uiTexture);
+ frame.Release(); Object.DestroyImmediate(frame);
+ var outPx = new Color[map.Length];
+ for (int i = 0; i < map.Length; i++)
+ {
+ var u = over[i]; var m = map[i];
+ outPx[i] = new Color(u.r + m.r * (1f - u.a), u.g + m.g * (1f - u.a), u.b + m.b * (1f - u.a), 1f);
+ }
+ var tex = new Texture2D(w, h, TextureFormat.RGB24, false);
+ tex.SetPixels(outPx); tex.Apply();
+ File.WriteAllBytes(Path.Combine(ShotDir, name + ".png"), tex.EncodeToPNG());
+ Object.DestroyImmediate(tex);
+ Debug.Log("SHOT " + name);
+ }
+
+ IEnumerator Take(string name, int q, int r, params string[] built)
+ {
+ Build(built);
+ yield return Frames(3);
+ Aim(q, r);
+ yield return Frames(8); // the kits stream in and the labels place and declutter
+ Shot(rt!, name);
+ }
+
+ IEnumerator Shots(bool hebrew)
+ {
+ var lang = hebrew ? "he" : "en";
+ yield return CivMovementShots.Boot(hebrew, saveDir, (t, p, u, g) => { rt = t; ps = p; ui = u; root = g; });
+ yield return Take($"both-built-{lang}", -9, 32, "beit_kama", "beit_kama_field");
+ yield return Take($"yishuv-only-{lang}", -9, 32, "beit_kama");
+ yield return Take($"hula-drained-{lang}", 16, 3, "hula");
+ }
+
+ [UnityTest] public IEnumerator English() { yield return Shots(false); }
+ [UnityTest] public IEnumerator Hebrew() { yield return Shots(true); }
+}
diff --git a/unity/Assets/Tests/EditMode/SiteFieldShots.cs.meta b/unity/Assets/Tests/EditMode/SiteFieldShots.cs.meta
new file mode 100644
index 000000000..4cd978b8a
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/SiteFieldShots.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: deaeade0faa84682960ec5d46a62efb2
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/SiteLabelTests.cs b/unity/Assets/Tests/EditMode/SiteLabelTests.cs
index c46101415..dd90ac74d 100644
--- a/unity/Assets/Tests/EditMode/SiteLabelTests.cs
+++ b/unity/Assets/Tests/EditMode/SiteLabelTests.cs
@@ -60,6 +60,20 @@ public class SiteLabelTests
Assert.IsNull(ui.Root.Q<VisualElement>("slot-" + site.Id), "no pill once the site is not built");
}
+ /// <summary>Issue #2812 ("Ground change only"): the pills GameRoot gives the sites are SiteLayer.Places. Built Beit Kama and
+ /// built Beit Kama fields, both at (-9, 32), give one pill on that hex: the yishuv's.</summary>
+ [Test]
+ public void ABuiltYishuvAndItsBuiltFieldShowOnePillOnTheirHex()
+ {
+ var layer = new GameObject("Sites").AddComponent<SiteLayer>();
+ layer.Render(new[] { SiteWork.Built("beit_kama", new System.DateTime(1949, 4, 18)), SiteWork.Built("beit_kama_field", new System.DateTime(1949, 6, 30)) }, C, map);
+ using var ui = new UiTestKit();
+ using var labels = new MapLabelsPresenter(ui.Root, map, cam);
+ labels.SetSites(layer.Places.Select(p => (p, layer.Columns.TryGetValue(p.Id, out var c) ? c : (Bounds[]?)null)).ToList());
+ Assert.AreEqual(new[] { "Beit Kama" }, labels.SiteLabels.Select(l => l.text).ToArray(), "one pill, the yishuv's");
+ Assert.IsNull(ui.Root.Q<VisualElement>("slot-" + SiteLayer.Prefix + "beit_kama_field"), "no pill for the field");
+ }
+
/// <summary>Issue #1219 item 1: in the Hebrew UI a built site's pill reads the row's name_he, in English its name.</summary>
[Test]
public void ABuiltSitesPillReadsItsHebrewNameInHebrewAndItsNameInEnglish()
diff --git a/unity/Assets/Tests/EditMode/SiteLayerTests.cs b/unity/Assets/Tests/EditMode/SiteLayerTests.cs
index 86267f840..6588181d2 100644
--- a/unity/Assets/Tests/EditMode/SiteLayerTests.cs
+++ b/unity/Assets/Tests/EditMode/SiteLayerTests.cs
@@ -4,6 +4,7 @@ using System.Linq;
using NUnit.Framework;
using Statecraft.Game;
using Statecraft.Game.Map;
+using Statecraft.Game.Map.Kits;
using Statecraft.Sim;
using UnityEditor;
using UnityEditor.SceneManagement;
@@ -68,4 +69,72 @@ public class SiteLayerTests
}
finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); }
}
+
+ /// <summary>Issue #2812 ("Ground change only"): a built field site in a yishuv's hex is no place of its own. Built Beit
+ /// Kama and built Beit Kama fields, both at (-9, 32) in sites.yaml, give one place (one pill) and one kit or box there.</summary>
+ [Test]
+ public void ABuiltYishuvAndItsBuiltFieldGiveOnePlaceAndOneKitOnTheirHex()
+ {
+ var yishuv = C.Sites.Single(x => x.Id == "beit_kama"); var field = C.Sites.Single(x => x.Id == "beit_kama_field");
+ Assert.AreEqual((-9, 32), (yishuv.Q, yishuv.R)); Assert.AreEqual((yishuv.Q, yishuv.R), (field.Q, field.R), "one hex");
+ Assert.Contains(Projects.YishuvKind, yishuv.Kinds); Assert.Contains(Projects.FieldKind, field.Kinds);
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ try
+ {
+ var map = Map();
+ var layer = new GameObject("Sites").AddComponent<SiteLayer>();
+ layer.Render(new[] { SiteWork.Built(yishuv.Id, new DateTime(1949, 4, 18)), SiteWork.Built(field.Id, new DateTime(1949, 6, 30)) }, C, map);
+ Assert.AreEqual(new[] { SiteLayer.Prefix + yishuv.Id }, layer.Places.Select(p => p.Id).ToArray(), "the yishuv is the hex's one place");
+ Assert.AreEqual(1, layer.transform.childCount, "one kit or box on the hex");
+ Assert.AreEqual(SiteLayer.Prefix + yishuv.Id, layer.transform.GetChild(0).name);
+ Assert.IsFalse(layer.Columns.ContainsKey(SiteLayer.Prefix + field.Id), "no kit for the field");
+ }
+ finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); }
+ }
+
+ /// <summary>Issue #2812 ("Ground change only"): a drained swamp (the built hula site, kinds [swamp]) is no place: no
+ /// pill, no kit and no box.</summary>
+ [Test]
+ public void ADrainedSwampGivesNoPlaceAndNoKit()
+ {
+ var swamp = C.Sites.Single(x => x.Id == "hula");
+ Assert.Contains(Projects.SwampKind, swamp.Kinds);
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ try
+ {
+ var map = Map();
+ var layer = new GameObject("Sites").AddComponent<SiteLayer>();
+ layer.Render(new[] { SiteWork.Built(swamp.Id, new DateTime(1951, 6, 30)) }, C, map);
+ Assert.IsEmpty(layer.Places, "no place, so no pill");
+ Assert.AreEqual(0, layer.transform.childCount, "no kit and no box");
+ Assert.IsEmpty(layer.Columns);
+ }
+ finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); }
+ }
+
+ /// <summary>Issue #2812: a built camp site (tzrifin, kinds [military_camp]) is still a place with its own camp kit.</summary>
+ [Test]
+ public void ABuiltCampStillGetsItsOwnKit()
+ {
+ var camp = C.Sites.Single(x => x.Id == "tzrifin");
+ Assert.IsTrue(Projects.IsCamp(camp));
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ try
+ {
+ var map = Map();
+ var layer = new GameObject("Sites").AddComponent<SiteLayer>();
+ layer.Render(new[] { SiteWork.Built(camp.Id, new DateTime(1949, 6, 30)) }, C, map);
+ Assert.AreEqual(new[] { SiteLayer.Prefix + camp.Id }, layer.Places.Select(p => p.Id).ToArray(), "the camp is a place");
+ Assert.AreEqual(1, layer.transform.childCount);
+ Assert.AreEqual(SettlementKits.CampType, SiteLayer.KitOf(camp, layer.Places[0]).Type, "its kit is the camp's");
+ var mesh = layer.transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh;
+ if (map.KitsShown)
+ {
+ Assert.AreNotEqual("Cube", mesh.name, "a camp kit, not the marker box");
+ Assert.IsTrue(layer.Columns.ContainsKey(SiteLayer.Prefix + camp.Id));
+ }
+ else Assert.AreEqual("Cube", mesh.name, "with the kits off, the marker box");
+ }
+ finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); }
+ }
}