sion: 3072m Sion map loader, Ymir heightfield+trimesh physics, LOD1/LOD2 renderer, and tests
This commit is contained in:
parent
dccfbb95b5
commit
1864acecb7
14 changed files with 2242 additions and 183 deletions
228
tests/RustToDust.Tests/SionMapTests.cs
Normal file
228
tests/RustToDust.Tests/SionMapTests.cs
Normal file
|
|
@ -0,0 +1,228 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
using RustToDust.Core;
|
||||
using RustToDust.Ymir;
|
||||
using Xunit;
|
||||
using Ymir.Box3D;
|
||||
|
||||
namespace RustToDust.Tests
|
||||
{
|
||||
public sealed class SionMapTests
|
||||
{
|
||||
[Fact]
|
||||
public void SionMapLoader_LoadsMetadataHeightmapBuildingsAndRcolMeshesWithExactParity()
|
||||
{
|
||||
var sion = SionMapLoader.Load();
|
||||
var meta = sion.Metadata;
|
||||
|
||||
Assert.Equal(3072f, meta.SizeMeters);
|
||||
Assert.Equal(6, meta.TileGrid);
|
||||
Assert.Equal(512f, meta.TileSizeMeters);
|
||||
Assert.Equal(1025, meta.GridRes);
|
||||
Assert.Equal(3.0f, meta.CellSizeMeters);
|
||||
Assert.Contains("swisstopo", meta.Attribution, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
Assert.Equal(1025 * 1025, sion.Heights.Length);
|
||||
Assert.Equal(2319, sion.Buildings.Count);
|
||||
Assert.Equal(36, sion.CollisionMeshes.Count);
|
||||
|
||||
var terrain = TerrainField.LoadHeightmap(
|
||||
meta.SizeMeters, meta.GridRes, sion.Heights, meta.MinY, meta.MaxY);
|
||||
|
||||
// Verify Basilique de Valere and valley buildings align cleanly with heightmap.f32 (0.00m coordinate drift)
|
||||
bool foundValere = false;
|
||||
int flatAlignChecked = 0;
|
||||
int closeAlignCount = 0;
|
||||
for (int i = 0; i < sion.Buildings.Count; i++)
|
||||
{
|
||||
var b = sion.Buildings[i];
|
||||
if (b.Name.Contains("Val", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
foundValere = true;
|
||||
float hValere = terrain.SampleHeight(b.CenterX, b.CenterZ);
|
||||
Assert.InRange(hValere, 90f, 150f);
|
||||
}
|
||||
|
||||
if (b.Class == "Gebaeude Einzelhaus" &&
|
||||
b.HalfX < 10f && b.HalfZ < 10f &&
|
||||
terrain.SampleNormal(b.CenterX, b.CenterZ).Ny > 0.98f)
|
||||
{
|
||||
float hTerrain = terrain.SampleHeight(b.CenterX, b.CenterZ);
|
||||
// Buildings include a ~4m foundation skirt below ground level; sloped/stepped lots can vary slightly around center.
|
||||
Assert.InRange(hTerrain, b.GroundY - 5.0f, b.RoofY + 2.0f);
|
||||
if (MathF.Abs(hTerrain - (b.GroundY + 3.5f)) <= 2.5f)
|
||||
{
|
||||
closeAlignCount++;
|
||||
}
|
||||
flatAlignChecked++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(foundValere, "Expected Basilique de Valere in Sion buildings_manifest.json");
|
||||
Assert.True(flatAlignChecked > 200, $"Expected > 200 flat-ground buildings matching DEM, got {flatAlignChecked}");
|
||||
Assert.True(closeAlignCount > flatAlignChecked * 0.90f,
|
||||
$"Expected >90% of flat-lot buildings to sit within 2.5m of skirted DEM ({closeAlignCount}/{flatAlignChecked})");
|
||||
|
||||
// Verify RCOL collision mesh bounds match metadata tile bounds
|
||||
for (int i = 0; i < sion.CollisionMeshes.Count; i++)
|
||||
{
|
||||
var cm = sion.CollisionMeshes[i];
|
||||
var tm = meta.Tiles[i];
|
||||
Assert.Equal(tm.Tx, cm.Tx);
|
||||
Assert.Equal(tm.Tz, cm.Tz);
|
||||
Assert.True(cm.Vertices.Length >= 3);
|
||||
Assert.True(cm.Indices.Length >= 3 && cm.Indices.Length % 3 == 0);
|
||||
Assert.True(Vector3.Distance(tm.BoundsMin, cm.BoundsMin) < 0.05f);
|
||||
Assert.True(Vector3.Distance(tm.BoundsMax, cm.BoundsMax) < 0.05f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SionTerrainField_SupportsBilinearSamplingNormalsRaycastAndCraters()
|
||||
{
|
||||
var sion = SionMapLoader.Load();
|
||||
var meta = sion.Metadata;
|
||||
var terrain = TerrainField.LoadHeightmap(
|
||||
meta.SizeMeters, meta.GridRes, sion.Heights, meta.MinY, meta.MaxY);
|
||||
|
||||
Assert.True(terrain.IsHeightmapMode);
|
||||
Assert.Equal(3072f, terrain.MapSize);
|
||||
Assert.Equal(1536f, terrain.MapHalfExtent);
|
||||
|
||||
// Grid vertex exactness at (-1536, -1536) and (0, 0)
|
||||
Assert.Equal(sion.Heights[0], terrain.SampleHeight(-1536f, -1536f), precision: 4);
|
||||
Assert.Equal(sion.Heights[512 * 1025 + 512], terrain.SampleHeight(0f, 0f), precision: 4);
|
||||
|
||||
// Plaza spawn area is flat; Valere hill rises > 100m
|
||||
var (_, plazaNy, _) = terrain.SampleNormal(-60f, 360f);
|
||||
Assert.True(plazaNy >= 0.95f, $"Expected flat plaza normal.Y >= 0.95, got {plazaNy:F3}");
|
||||
float valerePeakH = terrain.SampleHeight(113f, -211f);
|
||||
Assert.True(valerePeakH > 100f, $"Expected Valere hill height > 100m, got {valerePeakH:F2}m");
|
||||
|
||||
// Raycast hits Valere hill at low elevation and clears above peak
|
||||
bool blocked = terrain.Raycast(113f, 35f, -350f, 0f, 0f, 1f, 280f, out _, out float hitY, out float hitZ, out _);
|
||||
Assert.True(blocked, "Expected terrain raycast through Valere ridge to hit");
|
||||
Assert.InRange(hitZ, -350f, -70f);
|
||||
Assert.True(hitY >= 34f);
|
||||
|
||||
bool clear = terrain.Raycast(113f, meta.MaxY + 10f, -350f, 0f, 0f, 1f, 280f, out _, out _, out _, out _);
|
||||
Assert.False(clear, "Expected high raycast above Valere peak to clear");
|
||||
|
||||
// Dynamic crater lowers height on top of DEM
|
||||
float before = terrain.SampleHeight(-60f, 360f);
|
||||
terrain.StampCrater(-60f, 360f, radius: 5f, depth: 1.5f);
|
||||
float after = terrain.SampleHeight(-60f, 360f);
|
||||
Assert.True(after < before - 1.0f, $"Expected crater to depress Sion DEM (before={before:F2}, after={after:F2})");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ArenaSession_SionLoadsHeightfieldAndMeshesSpawnsClearBlocksLosAndStepsCleanly()
|
||||
{
|
||||
using var session = new ArenaSession(seed: 42, ArenaMapKind.Sion);
|
||||
var state = session.State;
|
||||
var sion = state.SionMap!;
|
||||
|
||||
Assert.Equal(ArenaMapKind.Sion, state.MapKind);
|
||||
Assert.Empty(state.ArenaCoverBoxes);
|
||||
Assert.Equal(MatchState.SionInitialSafeRadius, state.Spectators.SafeRadius);
|
||||
|
||||
// Verify StaticHeightField (1) + StaticMesh (36) exist in Ymir
|
||||
int hfCount = 0;
|
||||
int meshCount = 0;
|
||||
var view = session.World.View;
|
||||
for (int lane = 0; lane < view.Count; lane++)
|
||||
{
|
||||
var kind = (Box3DSoaKind)view.Kind[lane];
|
||||
if (kind == Box3DSoaKind.StaticHeightField) hfCount++;
|
||||
else if (kind == Box3DSoaKind.StaticMesh) meshCount++;
|
||||
}
|
||||
Assert.Equal(1, hfCount);
|
||||
Assert.Equal(36, meshCount);
|
||||
|
||||
// Verify zero contenders, spectators, or drops spawned inside any building
|
||||
foreach (var c in state.Contenders)
|
||||
{
|
||||
float pad = MathF.Max(c.Grid.Spec.HalfWidth, c.Grid.Spec.HalfLength) + 1.5f;
|
||||
Assert.False(sion.IsInsideAnyBuildingXz(c.X, c.Z, pad),
|
||||
$"Contender {c.Id} ({c.Grid.Spec.Id}) spawned inside building at ({c.X:F2}, {c.Z:F2})");
|
||||
}
|
||||
foreach (var s in state.Spectators.Spectators)
|
||||
{
|
||||
Assert.False(sion.IsInsideAnyBuildingXz(s.X, s.Z, 1.5f),
|
||||
$"Spectator {s.Id} spawned inside building at ({s.X:F2}, {s.Z:F2})");
|
||||
}
|
||||
foreach (var d in state.GroundDrops)
|
||||
{
|
||||
Assert.False(sion.IsInsideAnyBuildingXz(d.X, d.Z, 1.5f),
|
||||
$"GroundDrop {d.Id} spawned inside building at ({d.X:F2}, {d.Z:F2})");
|
||||
}
|
||||
|
||||
// LOS blocked by Valere hill
|
||||
Assert.False(session.HasLineOfSight(113f, 35f, -350f, 113f, 35f, -70f),
|
||||
"Expected HasLineOfSight to be blocked by Valere hill");
|
||||
|
||||
// LOS blocked by a real Old Town solid building mesh (and clear over its roof)
|
||||
SionBuildingAabb? testBldg = null;
|
||||
for (int i = 0; i < sion.Buildings.Count; i++)
|
||||
{
|
||||
var b = sion.Buildings[i];
|
||||
if (b.Class == "Gebaeude Einzelhaus" &&
|
||||
b.HalfX >= 5f && b.HalfZ >= 5f &&
|
||||
(b.RoofY - b.GroundY) >= 8f &&
|
||||
MathF.Abs(b.CenterX) < 500f && MathF.Abs(b.CenterZ) < 500f)
|
||||
{
|
||||
testBldg = b;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Assert.NotNull(testBldg);
|
||||
var tb = testBldg!.Value;
|
||||
float midY = tb.GroundY + (tb.RoofY - tb.GroundY) * 0.45f;
|
||||
float zA = tb.CenterZ - tb.HalfZ - 4f;
|
||||
float zB = tb.CenterZ + tb.HalfZ + 4f;
|
||||
Assert.False(session.HasLineOfSight(tb.CenterX, midY, zA, tb.CenterX, midY, zB),
|
||||
$"Expected HasLineOfSight to be blocked by Sion building {tb.Id} at ({tb.CenterX:F1}, {midY:F1}, {tb.CenterZ:F1})");
|
||||
Assert.True(session.HasLineOfSight(tb.CenterX, tb.RoofY + 6f, zA, tb.CenterX, tb.RoofY + 6f, zB),
|
||||
$"Expected HasLineOfSight above Sion building {tb.Id} roof ({tb.RoofY:F1}m) to be clear");
|
||||
|
||||
// Step 180 ticks (3 seconds) driving & firing: zero NaNs, zero underground falls, player moves cleanly
|
||||
var player = state.Player;
|
||||
float pStartX = player.X;
|
||||
float pStartZ = player.Z;
|
||||
for (int tick = 0; tick < 180; tick++)
|
||||
{
|
||||
session.Step(new PlayerInput(
|
||||
Throttle: 1f,
|
||||
Steer: MathF.Sin(tick * 0.04f) * 0.25f,
|
||||
AimWorldX: player.X + MathF.Sin(player.Yaw) * 60f,
|
||||
AimWorldZ: player.Z + MathF.Cos(player.Yaw) * 60f,
|
||||
Fire: tick % 10 == 0,
|
||||
InteractPickup: false,
|
||||
Brake: false));
|
||||
|
||||
for (int cIdx = 0; cIdx < state.Contenders.Count; cIdx++)
|
||||
{
|
||||
var c = state.Contenders[cIdx];
|
||||
if (!c.Alive) continue;
|
||||
Assert.True(float.IsFinite(c.X) && float.IsFinite(c.Y) && float.IsFinite(c.Z),
|
||||
$"Non-finite contender {c.Id} pose at tick {tick}");
|
||||
float gh = state.Terrain.SampleHeight(c.X, c.Z);
|
||||
float bottomY = c.Y - c.Grid.Spec.HalfHeight;
|
||||
Assert.True(bottomY >= gh - 0.85f,
|
||||
$"Contender {c.Id} ({c.Grid.Spec.Id}) fell below Sion terrain at tick {tick}: bottom={bottomY:F2}m < ground={gh:F2}m");
|
||||
}
|
||||
|
||||
for (int sIdx = 0; sIdx < state.Spectators.Spectators.Count; sIdx++)
|
||||
{
|
||||
var s = state.Spectators.Spectators[sIdx];
|
||||
if (!s.Alive) continue;
|
||||
Assert.True(float.IsFinite(s.X) && float.IsFinite(s.Y) && float.IsFinite(s.Z),
|
||||
$"Non-finite spectator {s.Id} pose at tick {tick}");
|
||||
}
|
||||
}
|
||||
|
||||
float pDisp = MathF.Sqrt((player.X - pStartX) * (player.X - pStartX) + (player.Z - pStartZ) * (player.Z - pStartZ));
|
||||
Assert.True(pDisp > 5f, $"Expected player displacement > 5m on Sion after 180 ticks, got {pDisp:F2}m");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue