From 9e5532466841e6b4a1069597d663d77f00cb2721 Mon Sep 17 00:00:00 2001 From: beezm Date: Fri, 7 Aug 2026 15:20:16 -0400 Subject: [PATCH] fix: clamp town slope-check sampling to map bounds (terrain-water task 05) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PlaceTownNodes read _heightMap[rx ± slopeRadius] unclamped; candidates spawn at [20, MapSize-20] but slopeRadius is 40 at 8K, so border candidates indexed out of bounds. Never fired only because border land stayed underwater and failed the above-sea test first — the redistribution curve's flat coastal toe arms exactly that path (D-033 consequence (b), recorded in sweep 01 and task 04). Behavior-preserving except where it would have crashed. Co-Authored-By: Claude Fable 5 --- Tools/Scripts/MapGenerator.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Tools/Scripts/MapGenerator.cs b/Tools/Scripts/MapGenerator.cs index a281436..7f721b9 100644 --- a/Tools/Scripts/MapGenerator.cs +++ b/Tools/Scripts/MapGenerator.cs @@ -1010,12 +1010,20 @@ public partial class MapGenerator : TextureRect if (t < minTemp || t > maxTemp) continue; float centerH = _heightMap[rx, ry]; - // Sample slope a bit wider (10px) due to higher resolution + // Sample slope a bit wider (10px) due to higher resolution. + // Samples are clamped to map bounds (task 05): candidates spawn at + // [20, MapSize-20] but slopeRadius is 40 at 8K, so the unclamped reads + // were out of bounds near the border — previously unreachable only + // because border land stayed underwater and failed the sea test first. int slopeRadius = (int)(MapSize * 0.005f); // Automatically scales! - if (Mathf.Abs(_heightMap[rx+slopeRadius, ry] - centerH) > maxSlope) continue; - if (Mathf.Abs(_heightMap[rx-slopeRadius, ry] - centerH) > maxSlope) continue; - if (Mathf.Abs(_heightMap[rx, ry+slopeRadius] - centerH) > maxSlope) continue; - if (Mathf.Abs(_heightMap[rx, ry-slopeRadius] - centerH) > maxSlope) continue; + int sxHi = Mathf.Clamp(rx + slopeRadius, 0, MapSize - 1); + int sxLo = Mathf.Clamp(rx - slopeRadius, 0, MapSize - 1); + int syHi = Mathf.Clamp(ry + slopeRadius, 0, MapSize - 1); + int syLo = Mathf.Clamp(ry - slopeRadius, 0, MapSize - 1); + if (Mathf.Abs(_heightMap[sxHi, ry] - centerH) > maxSlope) continue; + if (Mathf.Abs(_heightMap[sxLo, ry] - centerH) > maxSlope) continue; + if (Mathf.Abs(_heightMap[rx, syHi] - centerH) > maxSlope) continue; + if (Mathf.Abs(_heightMap[rx, syLo] - centerH) > maxSlope) continue; bool nearTrueOcean = false; bool nearAnyWater = false;