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.

feat(ui,map): info panel with transit camps and housing, minimap, placement markers, map clicks

eyalgolanf2e9b250f83d
diff --git a/unity/Assets/Scripts/Map/GeoFrame.cs b/unity/Assets/Scripts/Map/GeoFrame.cs
index 906109e6c..ba5a4eb2b 100644
--- a/unity/Assets/Scripts/Map/GeoFrame.cs
+++ b/unity/Assets/Scripts/Map/GeoFrame.cs
@@ -10,5 +10,9 @@ namespace Statecraft.Game.Map
{ Lon0 = lon0; Lon1 = lon1; Lat0 = lat0; Lat1 = lat1; WidthM = widthM; LengthM = lengthM; }
public Vector3 ToWorld(double lon, double lat, float y) =>
new Vector3((float)((lon - Lon0) / (Lon1 - Lon0) * WidthM), y, (float)((lat - Lat0) / (Lat1 - Lat0) * LengthM));
+
+ /// <summary>The inverse of ToWorld on the ground plane (y is ignored).</summary>
+ public (double lon, double lat) ToLonLat(Vector3 world) =>
+ (Lon0 + world.x / WidthM * (Lon1 - Lon0), Lat0 + world.z / LengthM * (Lat1 - Lat0));
}
}
diff --git a/unity/Assets/Scripts/Map/HexPicker.cs b/unity/Assets/Scripts/Map/HexPicker.cs
new file mode 100644
index 000000000..33f34735d
--- /dev/null
+++ b/unity/Assets/Scripts/Map/HexPicker.cs
@@ -0,0 +1,29 @@
+#nullable enable
+using System;
+
+namespace Statecraft.Game.Map
+{
+ /// <summary>lon/lat -> axial hex, the same projection and rounding as tools/hexgrid.py (lonlat_to_km + point_to_hex):
+ /// x km east of lon0, y km south of lat1, pointy-top hexes, cube rounding. Used for map clicks and the minimap; the sim
+ /// never needs it (settlement hexes come precomputed from tools/settlement_hexes.py).</summary>
+ public static class HexPicker
+ {
+ public const double KmPerDegLat = 110.57, KmPerDegLonEq = 111.32; // tools/geo.py
+
+ public static double KmPerDegLon(double lat0, double lat1) => KmPerDegLonEq * Math.Cos((lat0 + lat1) / 2 * Math.PI / 180);
+
+ public static (int q, int r) FromLonLat(double lon, double lat, HexBounds b, double hexSizeKm)
+ {
+ var x = (lon - b.lon0) * KmPerDegLon(b.lat0, b.lat1);
+ var y = (b.lat1 - lat) * KmPerDegLat;
+ var qf = (Math.Sqrt(3) / 3 * x - y / 3) / hexSizeKm;
+ var rf = (2.0 / 3 * y) / hexSizeKm;
+ var sf = -qf - rf;
+ double q = Math.Round(qf, MidpointRounding.ToEven), r = Math.Round(rf, MidpointRounding.ToEven), s = Math.Round(sf, MidpointRounding.ToEven);
+ double dq = Math.Abs(q - qf), dr = Math.Abs(r - rf), ds = Math.Abs(s - sf);
+ if (dq > dr && dq > ds) q = -r - s;
+ else if (dr > ds) r = -q - s;
+ return ((int)q, (int)r);
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/Map/HexPicker.cs.meta b/unity/Assets/Scripts/Map/HexPicker.cs.meta
new file mode 100644
index 000000000..311dff6f4
--- /dev/null
+++ b/unity/Assets/Scripts/Map/HexPicker.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 99f8b292e659e4d4bbd39df38fd921d9
\ No newline at end of file
diff --git a/unity/Assets/Scripts/Map/MapBuilder.cs b/unity/Assets/Scripts/Map/MapBuilder.cs
index a7e0ecccb..86b562d3c 100644
--- a/unity/Assets/Scripts/Map/MapBuilder.cs
+++ b/unity/Assets/Scripts/Map/MapBuilder.cs
@@ -23,6 +23,9 @@ namespace Statecraft.Game.Map
/// capture, the 90 km flythrough) a coarse surface that rises above the draped hex outlines in valleys (K2).</summary>
public const float TerrainPixelError = 1f;
+ /// <summary>Biome colours of the terrain layers (the minimap uses the same ones).</summary>
+ public static IReadOnlyDictionary<string, Color> BiomeColours => Biome;
+
static readonly Dictionary<string, Color> Biome = new Dictionary<string, Color>
{
["sea"] = new Color(0.16f, 0.42f, 0.55f), ["lake"] = new Color(0.20f, 0.50f, 0.62f),
diff --git a/unity/Assets/Scripts/Map/MapPointer.cs b/unity/Assets/Scripts/Map/MapPointer.cs
new file mode 100644
index 000000000..28225b6dd
--- /dev/null
+++ b/unity/Assets/Scripts/Map/MapPointer.cs
@@ -0,0 +1,42 @@
+#nullable enable
+using System;
+using UnityEngine;
+using UnityEngine.InputSystem;
+
+namespace Statecraft.Game.Map
+{
+ /// <summary>Left click on the map (not on the UI) selects the hex under the pointer: a ray from the camera hits the
+ /// terrain's collider, the hit becomes lon/lat (GeoFrame) and then a hex (HexPicker).</summary>
+ public sealed class MapPointer : MonoBehaviour
+ {
+ public MapScene? Map;
+ public Func<Vector2, bool>? IsOverUi;
+ public event Action<int, int>? HexClicked;
+
+ void Update()
+ {
+ var mouse = Mouse.current;
+ if (mouse == null || !mouse.leftButton.wasPressedThisFrame) return;
+ HandleClick(mouse.position.ReadValue());
+ }
+
+ /// <summary>A left click at <paramref name="pos"/> (screen pixels): ignored over the UI (the same IsOverUi the camera
+ /// uses), otherwise the hex under the pointer is raised as HexClicked. Returns whether a hex was picked.</summary>
+ public bool HandleClick(Vector2 pos)
+ {
+ if (Map == null || Map.Terrain == null) return false;
+ if (IsOverUi != null && IsOverUi(pos)) return false;
+ var cam = Camera.main;
+ if (cam == null || !Physics.Raycast(cam.ScreenPointToRay(pos), out var hit, 1_000_000f)) return false;
+ var hex = Pick(Map, hit.point);
+ HexClicked?.Invoke(hex.q, hex.r);
+ return true;
+ }
+
+ public static (int q, int r) Pick(MapScene map, Vector3 world)
+ {
+ var (lon, lat) = map.Frame.ToLonLat(world);
+ return HexPicker.FromLonLat(lon, lat, map.Hexes.bounds, map.Hexes.hex_size_km);
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/Map/MapPointer.cs.meta b/unity/Assets/Scripts/Map/MapPointer.cs.meta
new file mode 100644
index 000000000..b78a3680f
--- /dev/null
+++ b/unity/Assets/Scripts/Map/MapPointer.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 8a2034f54e4284ead9e1f04deab25e8d
\ No newline at end of file
diff --git a/unity/Assets/Scripts/Map/MapScene.cs b/unity/Assets/Scripts/Map/MapScene.cs
index a583911f8..6c1dcf398 100644
--- a/unity/Assets/Scripts/Map/MapScene.cs
+++ b/unity/Assets/Scripts/Map/MapScene.cs
@@ -13,6 +13,8 @@ namespace Statecraft.Game.Map
[SerializeField] Material terrainMaterial, overlayMaterial, markerMaterial;
public GeoFrame Frame { get; private set; }
+ /// <summary>hexes.json as the last Build read it (bounds, hex size, biomes).</summary>
+ public HexFile Hexes { get; private set; }
public Terrain Terrain { get; private set; }
public Material MarkerMaterial => markerMaterial;
@@ -33,7 +35,8 @@ namespace Statecraft.Game.Map
Clear();
var (meta, heights) = HeightmapLoader.Load(inputs.HeightmapJson, inputs.HeightmapR16, MetaPath);
- var terrainGo = MapBuilder.Build(meta, heights, MapBuilder.LoadHexes(inputs.HexesJson, HexesPath), terrainMaterial, overlayMaterial);
+ Hexes = MapBuilder.LoadHexes(inputs.HexesJson, HexesPath);
+ var terrainGo = MapBuilder.Build(meta, heights, Hexes, terrainMaterial, overlayMaterial);
terrainGo.transform.SetParent(transform, true);
Terrain = terrainGo.GetComponent<Terrain>();
var size = Terrain.terrainData.size;
diff --git a/unity/Assets/Scripts/Map/PlacementLayer.cs b/unity/Assets/Scripts/Map/PlacementLayer.cs
new file mode 100644
index 000000000..75c79a663
--- /dev/null
+++ b/unity/Assets/Scripts/Map/PlacementLayer.cs
@@ -0,0 +1,46 @@
+#nullable enable
+using System.Collections.Generic;
+using Statecraft.Sim;
+using UnityEngine;
+
+namespace Statecraft.Game.Map
+{
+ /// <summary>Draws the player's transit camps and housing projects as marker boxes on their hex centres (placeholder art
+ /// like the M2 settlement markers; per-era prefabs are M4). Uses the editor-made marker material with a per-kind
+ /// colour through a MaterialPropertyBlock (no runtime Shader.Find). Render replaces what it drew before.</summary>
+ public sealed class PlacementLayer : MonoBehaviour
+ {
+ static readonly Dictionary<string, (Color colour, float size)> Look = new Dictionary<string, (Color, float)>
+ {
+ [MapActions.TransitCamp] = (new Color(0.62f, 0.58f, 0.50f), 700f),
+ [MapActions.Housing] = (new Color(0.95f, 0.94f, 0.90f), 900f),
+ };
+ static readonly int BaseColor = Shader.PropertyToID("_BaseColor");
+
+ public void Render(IEnumerable<Placement> placements, Content content, MapScene map)
+ {
+ for (int i = transform.childCount - 1; i >= 0; i--)
+ {
+ var old = transform.GetChild(i).gameObject;
+ if (Application.isPlaying) { old.transform.SetParent(null); Destroy(old); } else DestroyImmediate(old);
+ }
+ var cube = Resources.GetBuiltinResource<Mesh>("Cube.fbx");
+ var block = new MaterialPropertyBlock();
+ foreach (var p in placements)
+ {
+ var hex = content.HexAt(p.Q, p.R);
+ if (hex == null || !Look.TryGetValue(p.Action, out var look)) continue;
+ var at = map.Frame.ToWorld(hex.Lon, hex.Lat, 0);
+ at.y = map.Terrain.SampleHeight(at) + map.Terrain.transform.position.y;
+ var box = new GameObject($"{p.Action}_{p.Q}_{p.R}");
+ box.transform.SetParent(transform, false);
+ box.AddComponent<MeshFilter>().sharedMesh = cube;
+ var r = box.AddComponent<MeshRenderer>();
+ r.sharedMaterial = map.MarkerMaterial;
+ block.SetColor(BaseColor, look.colour); r.SetPropertyBlock(block);
+ box.transform.position = at + Vector3.up * look.size / 2;
+ box.transform.localScale = new Vector3(look.size * 1.6f, look.size, look.size);
+ }
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/Map/PlacementLayer.cs.meta b/unity/Assets/Scripts/Map/PlacementLayer.cs.meta
new file mode 100644
index 000000000..095595f37
--- /dev/null
+++ b/unity/Assets/Scripts/Map/PlacementLayer.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 694c57321f1ae42bfb78c05a3a7bfec4
\ No newline at end of file
diff --git a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
new file mode 100644
index 000000000..a24615ff4
--- /dev/null
+++ b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs
@@ -0,0 +1,73 @@
+#nullable enable
+using System.Collections.Generic;
+using System.Linq;
+using Statecraft.Sim;
+using UnityEngine.UIElements;
+
+namespace Statecraft.Game.UI
+{
+ /// <summary>The info panel (bottom-left): the selected hex's land, height and the settlements it owns, what the player
+ /// built there, and the two map actions (transit camp, housing project) with the reason when one is not possible
+ /// (MapActions.Check). Tile yields are deliberately not shown (owner decision, M3 rulings 2).</summary>
+ public sealed class InfoPanelPresenter : System.IDisposable
+ {
+ readonly VisualElement panel; readonly SimBridge bridge; readonly UiText text;
+ readonly Button campButton, housingButton; readonly System.Action onCamp, onHousing;
+ readonly IReadOnlyDictionary<string, string> settlementNames;
+
+ public InfoPanelPresenter(VisualElement root, SimBridge bridge, UiText text, IReadOnlyDictionary<string, string> settlementNames)
+ {
+ panel = root.Q<VisualElement>("info-panel"); this.bridge = bridge; this.text = text;
+ this.settlementNames = settlementNames;
+ campButton = panel.Q<Button>("build-camp"); housingButton = panel.Q<Button>("build-housing");
+ onCamp = () => Build(MapActions.TransitCamp); onHousing = () => Build(MapActions.Housing);
+ campButton.clicked += onCamp; housingButton.clicked += onHousing;
+ bridge.Changed += Render;
+ Render();
+ }
+
+ /// <summary>Unsubscribes from the bridge and the buttons (the owner calls this when disabled).</summary>
+ public void Dispose() { bridge.Changed -= Render; campButton.clicked -= onCamp; housingButton.clicked -= onHousing; }
+
+ public (int q, int r)? Selected { get; private set; }
+
+ public void Show(int q, int r) { Selected = (q, r); Render(); }
+
+ void Build(string action)
+ {
+ if (Selected == null) return;
+ // The bridge reports a refusal itself; the sim enforces legality and the per-turn cap when the turn runs.
+ bridge.Queue(new MapAction(action, Selected.Value.q, Selected.Value.r));
+ }
+
+ public void Render()
+ {
+ var c = bridge.Content; var s = bridge.State;
+ var camp = panel.Q<Button>("build-camp"); var housing = panel.Q<Button>("build-housing");
+ if (Selected == null)
+ {
+ panel.Q<Label>("info-title").text = text["info.hint"];
+ foreach (var name in new[] { "info-subtitle", "info-settlements", "info-placement", "info-reason" }) panel.Q<Label>(name).text = "";
+ camp.style.display = DisplayStyle.None; housing.style.display = DisplayStyle.None;
+ return;
+ }
+ var (q, r) = Selected.Value;
+ var hex = c.HexAt(q, r);
+ var owned = c.SettlementHexes.Where(x => x.Q == q && x.R == r).Select(x => settlementNames.TryGetValue(x.Id, out var n) ? n : x.Id).ToList();
+ panel.Q<Label>("info-title").text = owned.Count > 0 ? owned[0] : hex == null ? text["info.off_map"] : text["info.biome." + hex.Biome];
+ panel.Q<Label>("info-subtitle").text = hex == null ? "" : text.Format("info.hex", text["info.biome." + hex.Biome], q, r, hex.ElevM);
+ panel.Q<Label>("info-settlements").text = owned.Count > 0 ? text.Format("info.owned", string.Join(", ", owned)) : "";
+ var here = MapActions.At(s, q, r);
+ var queued = bridge.QueuedLike(new MapAction(MapActions.Housing, q, r)) as MapAction;
+ panel.Q<Label>("info-placement").text = queued != null ? text.Format("info.queued", text["info." + queued.Action])
+ : here != null ? text.Format("info.built", text["info." + here.Action], here.Date) : "";
+ var campWhy = MapActions.Check(c, s, MapActions.TransitCamp, q, r);
+ var housingWhy = MapActions.Check(c, s, MapActions.Housing, q, r);
+ camp.style.display = DisplayStyle.Flex; housing.style.display = DisplayStyle.Flex;
+ camp.SetEnabled(campWhy == null); housing.SetEnabled(housingWhy == null);
+ camp.text = text.Format("info.build_camp", c.Rules.MapActions.TryGetValue(MapActions.TransitCamp, out var cr) ? cr.Cost : 0);
+ housing.text = text.Format("info.build_housing", c.Rules.MapActions.TryGetValue(MapActions.Housing, out var hr) ? hr.Cost : 0);
+ panel.Q<Label>("info-reason").text = string.Join("\n", new[] { campWhy, housingWhy }.Where(x => x != null).Distinct()); // the sim's reasons, each once
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs.meta b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs.meta
new file mode 100644
index 000000000..7bdd37626
--- /dev/null
+++ b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 56350857069254130b8efff67d20268d
\ No newline at end of file
diff --git a/unity/Assets/Scripts/UI/MinimapPresenter.cs b/unity/Assets/Scripts/UI/MinimapPresenter.cs
new file mode 100644
index 000000000..9cd39b509
--- /dev/null
+++ b/unity/Assets/Scripts/UI/MinimapPresenter.cs
@@ -0,0 +1,87 @@
+#nullable enable
+using System;
+using Statecraft.Game.Map;
+using UnityEngine;
+using UnityEngine.UIElements;
+using Object = UnityEngine.Object;
+
+namespace Statecraft.Game.UI
+{
+ /// <summary>The minimap (bottom-right): every hex in its biome colour, north up, the camera's view as a frame, and a
+ /// click to move the camera there. The texture is made at runtime and destroyed by Dispose.</summary>
+ public sealed class MinimapPresenter : IDisposable
+ {
+ public const int Width = 128;
+ public static readonly Color Sea = MapBuilder.BiomeColours["sea"];
+ readonly VisualElement image, view; readonly HexFile hexes;
+ readonly double widthKm, lengthKm;
+
+ public Texture2D Texture { get; }
+ /// <summary>Raised with the lon/lat the player clicked.</summary>
+ public event Action<double, double>? Clicked;
+
+ public MinimapPresenter(VisualElement root, HexFile hexes)
+ {
+ this.hexes = hexes;
+ var b = hexes.bounds;
+ widthKm = (b.lon1 - b.lon0) * HexPicker.KmPerDegLon(b.lat0, b.lat1);
+ lengthKm = (b.lat1 - b.lat0) * HexPicker.KmPerDegLat;
+ Texture = Paint(hexes, Width, (int)Math.Round(Width * lengthKm / widthKm));
+ var box = root.Q<VisualElement>("minimap");
+ image = box.Q<VisualElement>("minimap-image");
+ view = box.Q<VisualElement>("minimap-view");
+ image.style.backgroundImage = new StyleBackground(Texture);
+ image.RegisterCallback<PointerDownEvent>(OnPointerDown);
+ }
+
+ void OnPointerDown(PointerDownEvent e)
+ {
+ var (lon, lat) = ToLonLat(e.localPosition.x / image.layout.width, e.localPosition.y / image.layout.height);
+ Clicked?.Invoke(lon, lat);
+ }
+
+ /// <summary>One pixel per ~1.3 km: each pixel takes the biome of the hex its centre falls in (sea off the grid).
+ /// Row 0 of a Texture2D is the bottom, so it is the south edge.</summary>
+ public static Texture2D Paint(HexFile hexes, int width, int height)
+ {
+ var biome = new System.Collections.Generic.Dictionary<(int, int), string>();
+ foreach (var h in hexes.hexes) biome[(h.q, h.r)] = h.biome;
+ var b = hexes.bounds;
+ var tex = new Texture2D(width, height, TextureFormat.RGB24, false) { name = "Minimap", filterMode = FilterMode.Point, wrapMode = TextureWrapMode.Clamp };
+ var pixels = new Color[width * height];
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ double lon = b.lon0 + (x + 0.5) / width * (b.lon1 - b.lon0), lat = b.lat0 + (y + 0.5) / height * (b.lat1 - b.lat0);
+ var hex = HexPicker.FromLonLat(lon, lat, b, hexes.hex_size_km);
+ pixels[y * width + x] = biome.TryGetValue(hex, out var name) ? MapBuilder.BiomeColours[name] : Sea;
+ }
+ tex.SetPixels(pixels); tex.Apply();
+ return tex;
+ }
+
+ /// <summary>(u, v) in the image, top-left origin, 0..1 -> lon/lat.</summary>
+ public (double lon, double lat) ToLonLat(float u, float v)
+ {
+ var b = hexes.bounds;
+ return (b.lon0 + Mathf.Clamp01(u) * (b.lon1 - b.lon0), b.lat1 - Mathf.Clamp01(v) * (b.lat1 - b.lat0));
+ }
+
+ /// <summary>Frames the area the camera looks at: about one and a half view distances wide, centred on lookAt.</summary>
+ public void SetView(GeoFrame frame, Vector3 lookAt, float distanceM)
+ {
+ var (lon, lat) = frame.ToLonLat(lookAt);
+ var b = hexes.bounds;
+ float u = (float)((lon - b.lon0) / (b.lon1 - b.lon0)), v = (float)((b.lat1 - lat) / (b.lat1 - b.lat0));
+ float w = (float)(1.5 * distanceM / 1000 / widthKm), h = (float)(1.0 * distanceM / 1000 / lengthKm);
+ view.style.left = Length.Percent((u - w / 2) * 100); view.style.top = Length.Percent((v - h / 2) * 100);
+ view.style.width = Length.Percent(w * 100); view.style.height = Length.Percent(h * 100);
+ }
+
+ public void Dispose()
+ {
+ image.UnregisterCallback<PointerDownEvent>(OnPointerDown);
+ if (Texture != null) Object.DestroyImmediate(Texture);
+ }
+ }
+}
diff --git a/unity/Assets/Scripts/UI/MinimapPresenter.cs.meta b/unity/Assets/Scripts/UI/MinimapPresenter.cs.meta
new file mode 100644
index 000000000..cafc6ad33
--- /dev/null
+++ b/unity/Assets/Scripts/UI/MinimapPresenter.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 9e50997f9bc4e4d4ba5d868e6902b6b9
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/MapActionUiTests.cs b/unity/Assets/Tests/EditMode/MapActionUiTests.cs
new file mode 100644
index 000000000..e7c290b03
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/MapActionUiTests.cs
@@ -0,0 +1,224 @@
+using System;
+using System.IO;
+using System.Linq;
+using NUnit.Framework;
+using Statecraft.Game;
+using Statecraft.Game.Map;
+using Statecraft.Game.UI;
+using Statecraft.Sim;
+using UnityEditor;
+using UnityEditor.SceneManagement;
+using UnityEngine;
+using UnityEngine.UIElements;
+
+public class MapActionUiTests
+{
+ static readonly Content C = ContentLoader.Load(Paths.Content);
+ static readonly HexFile Hexes = MapBuilder.LoadHexes(Path.Combine(Paths.Content, "map", "hexes.json"));
+ static readonly System.Collections.Generic.List<Settlement> Towns =
+ SettlementPlacer.Parse(File.ReadAllText(Path.Combine(Paths.Content, "map", SettlementPlacer.DefaultFileName)));
+ static UiText Text => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
+
+ [Test]
+ public void HexPickerAgreesWithTheToolsSettlementRule()
+ {
+ // tools/settlement_hexes.py moves a settlement off a water hex to its nearest land neighbour (Tiberias); every
+ // other settlement must land on exactly the hex the C# port computes.
+ foreach (var t in Towns)
+ {
+ var picked = HexPicker.FromLonLat(t.Lon, t.Lat, Hexes.bounds, Hexes.hex_size_km);
+ var owner = C.SettlementHexes.Single(s => s.Id == t.Id);
+ if (t.Id == "tiberias") { Assert.AreNotEqual((owner.Q, owner.R), picked); continue; }
+ Assert.AreEqual((owner.Q, owner.R), picked, t.Id);
+ }
+ }
+
+ [Test]
+ public void GeoFrameRoundTripsLonLat()
+ {
+ var f = new GeoFrame(34.2, 35.9, 29.45, 33.35, 161530f, 431220f);
+ var (lon, lat) = f.ToLonLat(f.ToWorld(34.78, 32.08, 0));
+ Assert.AreEqual(34.78, lon, 1e-5); Assert.AreEqual(32.08, lat, 1e-5);
+ }
+
+ static SimBridge At(DateTime date)
+ {
+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ while (g.State.Date < date) g.Step(HistoricalAI.Commands(C, g.State));
+ return new SimBridge(g, Text);
+ }
+
+ static InfoPanelPresenter Panel(UiTestKit ui, SimBridge bridge) =>
+ new InfoPanelPresenter(ui.Root, bridge, Text, Towns.ToDictionary(t => t.Id, t => t.Name));
+
+ [Test]
+ public void BeforeTheMaabarotTheButtonsSayWhy()
+ {
+ using var ui = new UiTestKit();
+ var info = Panel(ui, At(new DateTime(1949, 6, 1)));
+ Assert.AreEqual("Click the map to inspect a hex", ui.Q<Label>("info-title").text);
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ info.Show(tlv.Q, tlv.R);
+ Assert.AreEqual("Tel Aviv", ui.Q<Label>("info-title").text);
+ StringAssert.StartsWith("Coastal plain · hex (", ui.Q<Label>("info-subtitle").text);
+ Assert.IsFalse(ui.Q<Button>("build-camp").enabledSelf);
+ Assert.AreEqual("Transit camps and housing projects can be placed from 1950-01-01", ui.Q<Label>("info-reason").text);
+ }
+
+ [Test]
+ public void ATransitCampIsOrderedAndBuiltAtEndTurn()
+ {
+ using var ui = new UiTestKit();
+ var bridge = At(new DateTime(1950, 8, 1)); // after the ma'abarot decision (May 1950)
+ var info = Panel(ui, bridge);
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ info.Show(tlv.Q, tlv.R);
+ Assert.IsTrue(ui.Q<Button>("build-camp").enabledSelf, ui.Q<Label>("info-reason").text);
+ Assert.AreEqual("Place a transit camp (1.5m)", ui.Q<Button>("build-camp").text);
+ ui.Press("build-camp");
+ Assert.AreEqual("Ordered: A transit camp, at End Turn", ui.Q<Label>("info-placement").text);
+ bridge.EndTurn();
+ Assert.AreEqual(MapActions.TransitCamp, MapActions.At(bridge.State, tlv.Q, tlv.R)!.Action);
+ Assert.AreEqual("A transit camp built here in August 1950", ui.Q<Label>("info-placement").text);
+ }
+
+ [Test]
+ public void TheMinimapPaintsLandAndSeaNorthUp()
+ {
+ var tex = MinimapPresenter.Paint(Hexes, 128, 342);
+ try
+ {
+ // RGB24 stores 8 bits per channel, so compare within one step.
+ void Is(Color expected, double lon, double lat)
+ {
+ var c = tex.GetPixel((int)((lon - 34.2) / 1.7 * 128), (int)((lat - 29.45) / 3.9 * 342));
+ Assert.AreEqual(expected.r, c.r, 1f / 255); Assert.AreEqual(expected.g, c.g, 1f / 255); Assert.AreEqual(expected.b, c.b, 1f / 255);
+ }
+ Is(MapBuilder.BiomeColours["coastal_plain"], 34.80, 32.10); // just inland of Tel Aviv
+ Is(MinimapPresenter.Sea, 34.30, 32.60); // the Mediterranean
+ Is(MapBuilder.BiomeColours["desert"], 34.90, 30.50); // the Negev
+ }
+ finally { UnityEngine.Object.DestroyImmediate(tex); }
+ }
+
+ [Test]
+ public void PlacementsAreDrawnOnTheirHexes()
+ {
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ try
+ {
+ new GameObject("TestMainCamera") { tag = "MainCamera" }.AddComponent<Camera>();
+ var map = new GameObject("MapScene").AddComponent<MapScene>();
+ map.Configure(AssetDatabase.LoadAssetAtPath<Material>("Assets/Materials/Terrain.mat"),
+ AssetDatabase.LoadAssetAtPath<Material>("Assets/Materials/HexOverlay.mat"),
+ AssetDatabase.LoadAssetAtPath<Material>("Assets/Materials/SettlementMarker.mat"));
+ map.Build();
+ var layer = new GameObject("Placements").AddComponent<PlacementLayer>();
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ layer.Render(new[] { new Placement(MapActions.TransitCamp, tlv.Q, tlv.R, new DateTime(1950, 8, 26)) }, C, map);
+ layer.Render(new[] { new Placement(MapActions.Housing, tlv.Q, tlv.R, new DateTime(1950, 8, 26)) }, C, map);
+ Assert.AreEqual(1, layer.transform.childCount); // Render replaces what it drew
+ var box = layer.transform.GetChild(0);
+ Assert.AreEqual($"housing_{tlv.Q}_{tlv.R}", box.name);
+ var hex = C.HexAt(tlv.Q, tlv.R)!;
+ var expected = map.Frame.ToWorld(hex.Lon, hex.Lat, 0);
+ Assert.AreEqual(expected.x, box.position.x, 1f); Assert.AreEqual(expected.z, box.position.z, 1f);
+ Assert.AreEqual((tlv.Q, tlv.R), MapPointer.Pick(map, box.position));
+ }
+ finally { EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); }
+ }
+
+ [Test]
+ public void TheInfoPanelShowsNoTileYields()
+ {
+ using var ui = new UiTestKit();
+ var bridge = At(new DateTime(1950, 8, 1));
+ var info = Panel(ui, bridge);
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ info.Show(tlv.Q, tlv.R);
+ var shown = string.Join(" | ", new[] { "info-title", "info-subtitle", "info-settlements", "info-placement", "info-reason" }.Select(n => ui.Q<Label>(n).text)).ToLowerInvariant();
+ foreach (var word in new[] { "yield", "food", "production", "gold", "income" }) StringAssert.DoesNotContain(word, shown);
+ }
+
+ [Test]
+ public void ThePerTurnCapAndRefusalsComeFromTheSim()
+ {
+ using var ui = new UiTestKit();
+ var bridge = At(new DateTime(1950, 8, 1));
+ new ToastPresenter(ui.Root, bridge, Text);
+ var info = Panel(ui, bridge);
+ var hexes = C.SettlementHexes.Select(h => (h.Q, h.R)).Distinct().Take(3).ToList();
+ foreach (var (q, r) in hexes) { info.Show(q, r); ui.Press("build-housing"); }
+ Assert.AreEqual(3, bridge.Queued.OfType<MapAction>().Count()); // the sim, not the UI, enforces the cap at End Turn
+ bridge.EndTurn();
+ Assert.AreEqual(1, bridge.Session.Last!.Rejected.Count);
+ StringAssert.Contains($"No more than {C.Rules.MapActionsPerTurn} map actions in one turn", ui.Q<Label>("toast").text);
+ }
+
+ [Test]
+ public void ABuiltHexCannotBeBuiltOnAgainAndSaysWhy()
+ {
+ using var ui = new UiTestKit();
+ var bridge = At(new DateTime(1950, 8, 1));
+ var info = Panel(ui, bridge);
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ info.Show(tlv.Q, tlv.R);
+ ui.Press("build-housing");
+ bridge.EndTurn();
+ Assert.IsFalse(ui.Q<Button>("build-housing").enabledSelf);
+ Assert.AreEqual(MapActions.Check(C, bridge.State, MapActions.Housing, tlv.Q, tlv.R), ui.Q<Label>("info-reason").text);
+ }
+
+ [Test]
+ public void TheMinimapClicksAndFramesTheView()
+ {
+ using var ui = new UiTestKit();
+ using var mini = new MinimapPresenter(ui.Root, Hexes);
+ (double lon, double lat)? clicked = null;
+ mini.Clicked += (lon, lat) => clicked = (lon, lat);
+ var (lon0, lat0) = mini.ToLonLat(0, 0);
+ Assert.AreEqual(Hexes.bounds.lon0, lon0, 1e-9); Assert.AreEqual(Hexes.bounds.lat1, lat0, 1e-9); // top-left is north-west
+ var (lon1, lat1) = mini.ToLonLat(1, 1);
+ Assert.AreEqual(Hexes.bounds.lon1, lon1, 1e-9); Assert.AreEqual(Hexes.bounds.lat0, lat1, 1e-9);
+ var f = new GeoFrame(Hexes.bounds.lon0, Hexes.bounds.lon1, Hexes.bounds.lat0, Hexes.bounds.lat1, 161530f, 431220f);
+ mini.SetView(f, f.ToWorld(34.95, 31.95, 0), 20000f);
+ Assert.AreEqual(LengthUnit.Percent, ui.Q<VisualElement>("minimap-view").style.left.value.unit);
+ Assert.IsNull(clicked);
+ }
+
+ [Test]
+ public void ADisposedMinimapDestroysItsTextureAndADisposedPanelHearsNothing()
+ {
+ using var ui = new UiTestKit();
+ var bridge = At(new DateTime(1950, 8, 1));
+ var info = Panel(ui, bridge);
+ var tlv = C.SettlementHexes.Single(s => s.Id == "tel_aviv");
+ info.Show(tlv.Q, tlv.R);
+ info.Dispose();
+ ui.Q<Label>("info-title").text = "untouched";
+ bridge.EndTurn();
+ Assert.AreEqual("untouched", ui.Q<Label>("info-title").text);
+ var mini = new MinimapPresenter(ui.Root, Hexes);
+ var tex = mini.Texture;
+ mini.Dispose();
+ Assert.IsTrue(tex == null, "the texture is destroyed");
+ }
+
+ [Test]
+ public void AClickOverTheUiNeverReachesTheMap()
+ {
+ var go = new GameObject("PointerTest");
+ try
+ {
+ var pointer = go.AddComponent<MapPointer>();
+ int hexClicks = 0;
+ pointer.HexClicked += (q, r) => hexClicks++;
+ pointer.IsOverUi = _ => true;
+ Assert.IsFalse(pointer.HandleClick(new Vector2(10, 10)));
+ Assert.AreEqual(0, hexClicks);
+ pointer.IsOverUi = _ => false;
+ Assert.IsFalse(pointer.HandleClick(new Vector2(10, 10)), "no map, camera or terrain: nothing is picked");
+ }
+ finally { UnityEngine.Object.DestroyImmediate(go); }
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/MapActionUiTests.cs.meta b/unity/Assets/Tests/EditMode/MapActionUiTests.cs.meta
new file mode 100644
index 000000000..b074e3349
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/MapActionUiTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 3fa87107d492543b7b63d85a59cc2db0
\ No newline at end of file
diff --git a/unity/Assets/UI/Game.uxml b/unity/Assets/UI/Game.uxml
index e411fb182..4fd28fbce 100644
--- a/unity/Assets/UI/Game.uxml
+++ b/unity/Assets/UI/Game.uxml
@@ -54,8 +54,22 @@
<ui:VisualElement name="event-options" />
<ui:Label name="event-count" class="muted" />
</ui:VisualElement>
- <ui:VisualElement name="info-panel" class="panel info-panel" />
- <ui:VisualElement name="minimap" class="panel minimap" />
+ <ui:VisualElement name="info-panel" class="panel info-panel">
+ <ui:Label name="info-title" class="title" />
+ <ui:Label name="info-subtitle" class="muted" />
+ <ui:Label name="info-settlements" />
+ <ui:Label name="info-placement" class="muted" />
+ <ui:VisualElement class="row">
+ <ui:Button name="build-camp" />
+ <ui:Button name="build-housing" />
+ </ui:VisualElement>
+ <ui:Label name="info-reason" class="reason" />
+ </ui:VisualElement>
+ <ui:VisualElement name="minimap" class="panel minimap">
+ <ui:VisualElement name="minimap-image" class="minimap-image">
+ <ui:VisualElement name="minimap-view" class="minimap-view" picking-mode="Ignore" />
+ </ui:VisualElement>
+ </ui:VisualElement>
<ui:VisualElement name="screen-cabinet" class="panel screen">
<ui:VisualElement class="screen-header"><ui:Label name="cabinet-government" class="title" /><ui:Label name="cabinet-coalition" class="muted" /></ui:VisualElement>
<ui:VisualElement class="row grow">
diff --git a/unity/Assets/UI/strings.en.yaml b/unity/Assets/UI/strings.en.yaml
index 1782a8de9..9a074f730 100644
--- a/unity/Assets/UI/strings.en.yaml
+++ b/unity/Assets/UI/strings.en.yaml
@@ -65,3 +65,20 @@ cabinet.no_party: "no party"
cabinet.appointed: " (your appointee)"
cabinet.pick: "Appoint to {0}"
cabinet.queued: "Ordered: {0} to {1}, at End Turn"
+info.hint: "Click the map to inspect a hex"
+info.off_map: "Off the map"
+info.hex: "{0} · hex ({1}, {2}) · {3:N0} m"
+info.owned: "Settlements: {0}"
+info.built: "{0} built here in {1:MMMM yyyy}"
+info.queued: "Ordered: {0}, at End Turn"
+info.transit_camp: "A transit camp"
+info.housing: "A housing project"
+info.build_camp: "Place a transit camp ({0:0.0}m)"
+info.build_housing: "Build housing ({0:0.0}m)"
+info.biome.sea: "Sea"
+info.biome.lake: "Lake"
+info.biome.coastal_plain: "Coastal plain"
+info.biome.hills: "Hills"
+info.biome.mountains: "Mountains"
+info.biome.valley: "Valley"
+info.biome.desert: "Desert"