A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Fix height-sized fallback canopy containment in opt-in hex tiles (#1160)
pixelrenderai-hasha970c39398e5
diff --git a/changelog.d/1160.md b/changelog.d/1160.mdnew file mode 100644index 000000000..589269053--- /dev/null+++ b/changelog.d/1160.md@@ -0,0 +1,3 @@+### Fixed+- Height-sized fallback trees on the opt-in hex map use their generated canopy bounds for footprint checks, including+ callers with narrower estimates, so accepted stand-in canopies stay inside the tile inset (refs #18, #1120).diff --git a/unity/Assets/Editor/HexTiles/Tests/HexHeightFootprintTests.cs b/unity/Assets/Editor/HexTiles/Tests/HexHeightFootprintTests.csnew file mode 100644index 000000000..cdcd54b63--- /dev/null+++ b/unity/Assets/Editor/HexTiles/Tests/HexHeightFootprintTests.cs@@ -0,0 +1,133 @@+#nullable enable+using System.Linq;+using NUnit.Framework;+using Statecraft.HexTiles;+using UnityEngine;++/// <summary>Check the mesh the fallback renderer draws, not just its placement origin.</summary>+public class HexHeightFootprintTests+{+ const string MissingModel = "tiles:__hex_boundary_regression__/absent";++ [TestCase(0.5f)]+ [TestCase(0.45f)] // PineForest's explicit broadleaf estimate must not shrink the fallback canopy.+ public void BroadleafCanopyCannotCrossTheInset(float footprintShare)+ {+ Assert.That(HexModels.Prefab(MissingModel), Is.Null, "The fixture must use the stand-in renderer");+ var ctx = new HexTileContext(0, 0, new FlatSurface());+ bool placed = ctx.PlaceByHeight(MissingModel, new Vector3(3600f, 0f, 0f), 0f, 1000f,+ StandIn.Tree, footprintShare: footprintShare);+ AssertVerticesInside(ctx);+ Assert.That(placed, Is.False, "This canopy is too wide for the remaining inset margin");+ Assert.That(ctx.Placements, Is.Empty);+ }++ [TestCase(StandIn.Tree, 0.45f, 0f)]+ [TestCase(StandIn.Tree, 0.45f, 30f)]+ [TestCase(StandIn.Tree, 0.45f, 90f)]+ [TestCase(StandIn.Pine, 0.3f, 30f)]+ [TestCase(StandIn.Palm, 0.5f, 30f)]+ public void HeightSizedStandInsStillFitAwayFromTheEdge(StandIn kind, float footprintShare, float yaw)+ {+ Assert.That(HexModels.Prefab(MissingModel), Is.Null);+ var ctx = new HexTileContext(2, -1, new FlatSurface());+ Assert.That(ctx.PlaceByHeight(MissingModel, ctx.Centre + new Vector3(3100f, 0f, 0f), yaw, 1000f,+ kind, footprintShare: footprintShare), Is.True);+ Assert.That(ctx.Placements.Count, Is.EqualTo(1));+ AssertVerticesInside(ctx);+ }++ [Test]+ public void DefaultFootprintCannotAdmitAnOverhangingCanopy()+ {+ var ctx = new HexTileContext(0, 0, new FlatSurface());+ bool placed = ctx.PlaceByHeight(MissingModel, new Vector3(3600f, 0f, 0f), 0f, 1000f, StandIn.Tree);+ AssertVerticesInside(ctx);+ Assert.That(placed, Is.False);+ Assert.That(ctx.Placements, Is.Empty);+ }++ [Test]+ public void RotatedCanopyCannotCrossASlantedEdge()+ {+ var ctx = new HexTileContext(0, 0, new FlatSurface());+ // 3,600 metres along the slanted edge's outward normal; yaw aligns the checked square to that edge.+ bool placed = ctx.PlaceByHeight(MissingModel, new Vector3(1800f, 0f, 3117.691f), 30f, 1000f,+ StandIn.Tree);+ AssertVerticesInside(ctx);+ Assert.That(placed, Is.False);+ Assert.That(ctx.Placements, Is.Empty);+ }++ [TestCase(StandIn.Pine, 0.1f, 3900f)]+ [TestCase(StandIn.Palm, 0.3f, 3700f)]+ public void NarrowEstimatesCannotAdmitOtherOverhangingStandIns(StandIn kind, float footprintShare, float x)+ {+ var ctx = new HexTileContext(0, 0, new FlatSurface());+ bool placed = ctx.PlaceByHeight(MissingModel, new Vector3(x, 0f, 0f), 0f, 1000f,+ kind, footprintShare: footprintShare);+ AssertVerticesInside(ctx);+ Assert.That(placed, Is.False);+ Assert.That(ctx.Placements, Is.Empty);+ }++ [Test]+ public void BroadleafCanopyStillFitsCloseToTheInset()+ {+ var ctx = new HexTileContext(0, 0, new FlatSurface());+ // The rendered canopy reaches x = 3,968.4, just inside the inset at x = 3,980.127.+ Assert.That(ctx.PlaceByHeight(MissingModel, new Vector3(3450f, 0f, 0f), 0f, 1000f,+ StandIn.Tree, footprintShare: 0.45f), Is.True);+ Assert.That(ctx.Placements.Count, Is.EqualTo(1));+ AssertVerticesInside(ctx);+ }++ [Test]+ public void LargerCallerFootprintStillControlsPlacement()+ {+ var ctx = new HexTileContext(0, 0, new FlatSurface());+ Assert.That(ctx.PlaceByHeight(MissingModel, new Vector3(3300f, 0f, 0f), 0f, 1000f,+ StandIn.Tree, footprintShare: 1.2f), Is.False);+ Assert.That(ctx.Placements, Is.Empty);+ }++ [TestCase("flat")]+ [TestCase("hills")]+ public void EnvironmentFallbacksKeepTheirWholeFootprintWithoutAnInset(string terrain)+ {+ var hexes = (from q in Enumerable.Range(-1, 3)+ from r in Enumerable.Range(-1, 3)+ select new BoardHex { q = q, r = r, env = true, terrain = terrain }).ToArray();+ var board = new HexBoard(new HexBoardFile { hexes = hexes });+ var surface = new FlatSurface();+ var placements = HexEnvironment.Dress(board, surface).ToArray();+ Assert.That(placements, Is.Not.Empty);+ CollectionAssert.AreEqual(placements.Select(p => p.Matrix),+ HexEnvironment.Dress(board, surface).Select(p => p.Matrix), "environment placement stays deterministic");+ TestContext.WriteLine($"Environment {terrain}: {placements.Length} placements, " ++ $"{placements.Count(p => p.StandIn == StandIn.Tree)} trees, {placements.Count(p => p.StandIn == StandIn.Pine)} pines");+ foreach (var placement in placements)+ {+ if (HexModels.Prefab(HexModels.Resolve(placement.Model, placement.Position)) != null) continue;+ var ctx = new HexTileContext(placement.Q, placement.R, surface, insetShare: 0f);+ AssertPlacementVerticesInside(ctx, placement);+ }+ }++ static void AssertVerticesInside(HexTileContext ctx)+ {+ foreach (var placement in ctx.Placements) AssertPlacementVerticesInside(ctx, placement);+ }++ static void AssertPlacementVerticesInside(HexTileContext ctx, HexPlacement placement)+ {+ foreach (var part in HexModels.StandInParts(placement.StandIn))+ foreach (var vertex in part.mesh.vertices)+ {+ // This is the same pair of matrices used by HexBoardRenderer for a missing model.+ var world = (placement.Matrix * part.local).MultiplyPoint3x4(vertex);+ Assert.That(ctx.Inside(world), Is.True,+ $"{placement.StandIn} vertex {world} crosses the inset (origin {placement.Position})");+ }+ }+}diff --git a/unity/Assets/Editor/HexTiles/Tests/HexHeightFootprintTests.cs.meta b/unity/Assets/Editor/HexTiles/Tests/HexHeightFootprintTests.cs.metanew file mode 100644index 000000000..033a8bebb--- /dev/null+++ b/unity/Assets/Editor/HexTiles/Tests/HexHeightFootprintTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 707dd6e5a1a74d0ca6ecf6e0c2de3bc2diff --git a/unity/Assets/Scripts/Map/HexTiles/Core/HexModels.cs b/unity/Assets/Scripts/Map/HexTiles/Core/HexModels.csindex c6d3abf74..859ad70c4 100644--- a/unity/Assets/Scripts/Map/HexTiles/Core/HexModels.cs+++ b/unity/Assets/Scripts/Map/HexTiles/Core/HexModels.cs@@ -124,6 +124,24 @@ namespace Statecraft.HexTiles};}+ /// <summary>The centred square enclosing the stand-in's parts in its unit frame, before placement scale/yaw.</summary>+ internal static float StandInFootprint(StandIn kind)+ {+ float width = 0f;+ foreach (var part in StandInParts(kind))+ {+ var b = part.mesh.bounds;+ for (int i = 0; i < 8; i++)+ {+ var corner = b.center + Vector3.Scale(b.extents,+ new Vector3((i & 1) == 0 ? -1f : 1f, (i & 2) == 0 ? -1f : 1f, (i & 4) == 0 ? -1f : 1f));+ var p = part.local.MultiplyPoint3x4(corner);+ width = Mathf.Max(width, 2f * Mathf.Max(Mathf.Abs(p.x), Mathf.Abs(p.z)));+ }+ }+ return width;+ }+public static int StandInTriangles(StandIn kind) => StandInParts(kind).Sum(p => IndexTriangles(p.mesh));/// <summary>Triangles from index counts, which a player can read on any mesh (mesh.triangles needs Read/Write).</summary>diff --git a/unity/Assets/Scripts/Map/HexTiles/Core/HexTileContext.cs b/unity/Assets/Scripts/Map/HexTiles/Core/HexTileContext.csindex 1ddcae46d..78586fb71 100644--- a/unity/Assets/Scripts/Map/HexTiles/Core/HexTileContext.cs+++ b/unity/Assets/Scripts/Map/HexTiles/Core/HexTileContext.cs@@ -123,12 +123,15 @@ namespace Statecraft.HexTilesbool PlaceByWidthExact(string model, Vector3 at, float yawDeg, float widthM, StandIn standIn, Color? tint, float maxSlopeDeg) =>Place(model, at, yawDeg, new Vector3(widthM, widthM, widthM), Fit.Width, standIn, tint, widthM, maxSlopeDeg);- /// <summary>A model sized by its height, <paramref name="heightM"/> (figures, trees); its footprint for the inside- /// test is taken as <paramref name="footprintShare"/> of the height.</summary>+ /// <summary>A model sized by its height, <paramref name="heightM"/> (figures, trees); its checked footprint is+ /// <paramref name="footprintShare"/> of the height, enlarged to contain the stand-in when the model is missing.</summary>public bool PlaceByHeight(string model, Vector3 at, float yawDeg, float heightM, StandIn standIn, Color? tint = null, float footprintShare = 0.5f, float maxSlopeDeg = 35f){heightM *= HexScale.PieceScale;- return Place(model, at, yawDeg, new Vector3(heightM, heightM, heightM), Fit.Height, standIn, tint, heightM * footprintShare, maxSlopeDeg);+ float footprintM = heightM * footprintShare;+ if (HexModels.Prefab(HexModels.Resolve(model, at)) == null)+ footprintM = Mathf.Max(footprintM, heightM * HexModels.StandInFootprint(standIn));+ return Place(model, at, yawDeg, new Vector3(heightM, heightM, heightM), Fit.Height, standIn, tint, footprintM, maxSlopeDeg);}/// <summary>A flat piece (a field, a road, a plaza) of width x depth, its top a few metres above the highest ground.</summary>