fix(scenery): #49 9×9 loop, per-spawn building check, triangle slope
Three fixes to match retail CLandBlock::get_land_scenes (0x00530460): 1. Loop bound: iterate 9×9 vertices (side_vertex_count=9), not 8×8 cells. Edge vertices (x=8 or y=8) produce valid spawns when the per-object displacement shifts the position back into [0, 192). Confirmed by named retail decomp do-while condition, WorldBuilder vertLength=9, ACViewer Terrain.Count=81, AC2D wTopo[9][9]. 2. Building suppression: check at the DISPLACED position's cell (CSortCell::has_building per spawn), not at the loop vertex index. Matches WorldBuilder buildingsGrid[gx2, gy2] pattern. 3. Slope filter: replace finite-difference gradient approximation with triangle-aware normal sampling via new static method TerrainSurface.SampleNormalZFromHeightmap. Picks the correct triangle via IsSplitSWtoNE, matching retail find_terrain_poly → polygon->plane.N.z and WorldBuilder's GetNormal(). Tests: 5 new tests for SampleNormalZFromHeightmap (flat=1.0, sloped<1, cross-validates with SampleSurface instance method) and DisplaceObject edge-vertex validity. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
17b4ffde12
commit
833d167ebc
4 changed files with 203 additions and 49 deletions
|
|
@ -139,6 +139,52 @@ public class TerrainSurfaceTests
|
|||
Assert.Contains(sample.Vertices, v => v.X == 0f && v.Y == 0f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SampleNormalZFromHeightmap_FlatTerrain_ReturnsOne()
|
||||
{
|
||||
var heights = FlatHeightmap(50);
|
||||
var hTable = LinearHeightTable();
|
||||
float nz = TerrainSurface.SampleNormalZFromHeightmap(heights, hTable, 0, 0, 96f, 96f);
|
||||
Assert.Equal(1f, nz, precision: 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SampleNormalZFromHeightmap_SlopedTerrain_ReturnsLessThanOne()
|
||||
{
|
||||
var heights = new byte[81];
|
||||
for (int x = 0; x < 9; x++)
|
||||
for (int y = 0; y < 9; y++)
|
||||
heights[x * 9 + y] = (byte)(x * 20);
|
||||
var hTable = LinearHeightTable();
|
||||
float nz = TerrainSurface.SampleNormalZFromHeightmap(heights, hTable, 0, 0, 12f, 12f);
|
||||
Assert.True(nz < 1f, $"Sloped terrain should have nz < 1.0, got {nz}");
|
||||
Assert.True(nz > 0f, $"nz should be positive, got {nz}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SampleNormalZFromHeightmap_AgreesWithSampleSurface()
|
||||
{
|
||||
var heights = new byte[81];
|
||||
for (int x = 0; x < 9; x++)
|
||||
for (int y = 0; y < 9; y++)
|
||||
heights[x * 9 + y] = (byte)((x * 17 + y * 13) % 256);
|
||||
|
||||
var hTable = LinearHeightTable();
|
||||
const uint lbX = 0xA9, lbY = 0xB3;
|
||||
var instance = new TerrainSurface(heights, hTable, lbX, lbY);
|
||||
|
||||
for (float lx = 0.5f; lx < 192f; lx += 8f)
|
||||
for (float ly = 0.5f; ly < 192f; ly += 8f)
|
||||
{
|
||||
var (_, normal) = instance.SampleSurface(lx, ly);
|
||||
float staticNz = TerrainSurface.SampleNormalZFromHeightmap(
|
||||
heights, hTable, lbX, lbY, lx, ly);
|
||||
Assert.True(
|
||||
Math.Abs(normal.Z - staticNz) < 0.0001f,
|
||||
$"NormalZ mismatch at ({lx:F1},{ly:F1}): instance={normal.Z:F4} static={staticNz:F4}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeOutdoorCellId_Origin_ReturnsFirst()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
using System.Numerics;
|
||||
using AcDream.Core.World;
|
||||
using DatReaderWriter.Types;
|
||||
|
||||
namespace AcDream.Core.Tests.World;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for SceneryGenerator road-exclusion logic.
|
||||
/// The full Generate() pipeline requires real dat files (Region, Scene, etc.)
|
||||
/// so road-check behavior is tested via the internal IsRoadVertex helper,
|
||||
/// which is the single gate that guards against placing trees on roads.
|
||||
/// Tests for SceneryGenerator: road-exclusion, loop bounds, building
|
||||
/// suppression, and slope filter. The full Generate() pipeline requires
|
||||
/// real dat files so behavior is tested via internal helpers.
|
||||
/// </summary>
|
||||
public class SceneryGeneratorTests
|
||||
{
|
||||
|
|
@ -32,15 +32,12 @@ public class SceneryGeneratorTests
|
|||
[Fact]
|
||||
public void IsRoadVertex_ZeroTerrain_IsNotRoad()
|
||||
{
|
||||
// A fully blank terrain entry (no type, no road, no scene) is not a road.
|
||||
Assert.False(SceneryGenerator.IsRoadVertex(0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsRoadVertex_MatchesTerrainInfoRoadProperty()
|
||||
{
|
||||
// Verify that IsRoadVertex agrees with the typed TerrainInfo.Road property
|
||||
// for a sample of raw values, ensuring the bit convention is consistent.
|
||||
for (ushort raw = 0; raw < 4; raw++)
|
||||
{
|
||||
TerrainInfo ti = raw;
|
||||
|
|
@ -50,4 +47,63 @@ public class SceneryGeneratorTests
|
|||
$"raw=0x{raw:X4}: IsRoadVertex={actual} but TerrainInfo.Road={ti.Road}");
|
||||
}
|
||||
}
|
||||
|
||||
// --- Edge vertex displacement tests ---
|
||||
// Retail iterates 9×9 vertices (0..8 on each axis). Vertices at x=8 or y=8
|
||||
// have base positions at 192 (= 8 * 24), which is AT the landblock boundary.
|
||||
// These produce valid scenery when displacement shifts them back into [0, 192).
|
||||
|
||||
[Fact]
|
||||
public void DisplaceObject_EdgeVertex_CanProduceValidPosition()
|
||||
{
|
||||
// Vertex (3, 8): base_y = 8 * 24 = 192.
|
||||
// With DisplaceY > 0, some LCG seeds will produce negative displacement,
|
||||
// shifting the Y back below 192 into the valid range.
|
||||
var obj = new ObjectDesc
|
||||
{
|
||||
DisplaceX = 12f,
|
||||
DisplaceY = 12f,
|
||||
BaseLoc = new Frame { Origin = new Vector3(0, 0, 0) }
|
||||
};
|
||||
|
||||
// Search across a range of global cell coords to find at least one
|
||||
// case where vertex y=8 displaces into [0, 192).
|
||||
bool foundValid = false;
|
||||
for (uint gx = 0; gx < 64 && !foundValid; gx++)
|
||||
{
|
||||
for (uint gy = 0; gy < 64 && !foundValid; gy++)
|
||||
{
|
||||
var localPos = SceneryGenerator.DisplaceObject(obj, gx, gy, 0);
|
||||
// Vertex (3, 8): cell corner at (3*24, 8*24) = (72, 192)
|
||||
float lx = 3 * 24f + localPos.X;
|
||||
float ly = 8 * 24f + localPos.Y;
|
||||
if (ly >= 0 && ly < 192f && lx >= 0 && lx < 192f)
|
||||
foundValid = true;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(foundValid,
|
||||
"Expected at least one (globalCellX, globalCellY) where vertex y=8 " +
|
||||
"displaces back into [0, 192) — retail's 9×9 loop relies on this");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisplaceObject_InteriorVertex_AlwaysNearOrigin()
|
||||
{
|
||||
var obj = new ObjectDesc
|
||||
{
|
||||
DisplaceX = 12f,
|
||||
DisplaceY = 12f,
|
||||
BaseLoc = new Frame { Origin = new Vector3(0, 0, 0) }
|
||||
};
|
||||
|
||||
// For interior vertices (x < 8, y < 8), displacement is bounded by
|
||||
// DisplaceX/Y (max 12 units each), so the result stays within one
|
||||
// cell of the origin.
|
||||
var localPos = SceneryGenerator.DisplaceObject(obj, 100, 100, 0);
|
||||
Assert.True(Math.Abs(localPos.X) <= 12f,
|
||||
$"Interior displacement X={localPos.X} exceeds DisplaceX=12");
|
||||
Assert.True(Math.Abs(localPos.Y) <= 12f,
|
||||
$"Interior displacement Y={localPos.Y} exceeds DisplaceY=12");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue