RustToDust/tests/RustToDust.Tests/TerrainTests.cs

211 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using RustToDust.Core;
using RustToDust.Ymir;
using Xunit;
namespace RustToDust.Tests
{
public sealed class TerrainTests
{
[Fact]
public void Generate_ProducesDeterministicNonFlatReliefWithinBounds()
{
var emptyCover = Array.Empty<ArenaBoxSpec>();
var a = TerrainField.Generate(42UL, emptyCover);
var b = TerrainField.Generate(42UL, emptyCover);
var c = TerrainField.Generate(999UL, emptyCover);
float minH = float.PositiveInfinity;
float maxH = float.NegativeInfinity;
bool anyDifferentSeed = false;
for (int gx = -160; gx <= 160; gx += 16)
{
for (int gz = -160; gz <= 160; gz += 16)
{
float ha = a.SampleHeight(gx, gz);
float hb = b.SampleHeight(gx, gz);
float hc = c.SampleHeight(gx, gz);
Assert.Equal(ha, hb);
if (MathF.Abs(ha - hc) > 0.05f)
{
anyDifferentSeed = true;
}
minH = MathF.Min(minH, ha);
maxH = MathF.Max(maxH, ha);
}
}
Assert.True(maxH - minH > 2.5f, $"Expected non-flat dune/mesa relief > 2.5m span, got [{minH:F2}, {maxH:F2}]");
Assert.InRange(minH, -4.5f, 1.0f);
Assert.InRange(maxH, 2.0f, 9.5f);
Assert.True(anyDifferentSeed, "Different seeds should generate distinct desert relief");
}
[Fact]
public void StampCrater_LowersCenterRaisesRimAndDirtyOnlyOverlappingTiles()
{
var terrain = TerrainField.Generate(42UL, Array.Empty<ArenaBoxSpec>());
terrain.ClearDirtyMask();
// Tile (1, 1) spans X in [-96, 0), Z in [-96, 0).
// Point (-48, -48) with radius 6m is strictly inside tile (1, 1) and nowhere else.
const float cx = -48f;
const float cz = -48f;
const float radius = 6f;
const float depth = 1.6f;
float beforeCenter = terrain.SampleHeight(cx, cz);
float beforeRim = terrain.SampleHeight(cx + radius * 0.775f, cz);
terrain.StampCrater(cx, cz, radius, depth);
float afterCenter = terrain.SampleHeight(cx, cz);
float afterRim = terrain.SampleHeight(cx + radius * 0.775f, cz);
Assert.True(afterCenter < beforeCenter - 1.0f,
$"Expected crater center to drop >1.0m (before {beforeCenter:F2}, after {afterCenter:F2})");
Assert.True(afterRim > beforeRim + 0.2f,
$"Expected crater ejecta rim to rise >0.2m (before {beforeRim:F2}, after {afterRim:F2})");
// Only tile (1, 1) should have incremented from version 1 to 2
for (int tz = 0; tz < TerrainField.TileGridCount; tz++)
{
for (int tx = 0; tx < TerrainField.TileGridCount; tx++)
{
int expected = (tx == 1 && tz == 1) ? 2 : 1;
Assert.Equal(expected, terrain.TileVersions[tx, tz]);
}
}
Assert.Equal(1u << (1 * TerrainField.TileGridCount + 1), terrain.DirtyTileMask);
}
[Fact]
public void StampTrack_BoundsDynamicDecalCount()
{
var terrain = TerrainField.Generate(7UL, Array.Empty<ArenaBoxSpec>());
int initialCount = terrain.Brushes.Count;
for (int i = 0; i < TerrainField.MaxDynamicDecals + 40; i++)
{
float x = -80f + (i % 40) * 4f;
float z = -80f + (i / 40) * 8f;
terrain.StampTrack(x, z, yaw: 0.4f, width: 1.6f, length: 2.5f);
}
Assert.Equal(initialCount + TerrainField.MaxDynamicDecals, terrain.Brushes.Count);
}
[Fact]
public void ArenaSession_BuggyDrivesAcrossDunesWithoutNanAndTracksElevation()
{
using var session = new ArenaSession(seed: 42);
var player = session.State.Player;
int initialBrushes = session.State.Terrain.Brushes.Count;
float minGround = float.PositiveInfinity;
float maxGround = float.NegativeInfinity;
for (int tick = 0; tick < 360; tick++)
{
float steer = MathF.Sin(tick * 0.025f) * 0.45f;
session.Step(new PlayerInput(
Throttle: 1f,
Steer: steer,
AimWorldX: player.X + 30f,
AimWorldZ: player.Z,
Fire: false,
InteractPickup: false,
Brake: false));
Assert.False(float.IsNaN(player.X) || float.IsNaN(player.Y) || float.IsNaN(player.Z),
$"NaN pose at tick {tick}");
float gh = session.State.Terrain.SampleHeight(player.X, player.Z);
minGround = MathF.Min(minGround, gh);
maxGround = MathF.Max(maxGround, gh);
// Chassis should remain supported near the terrain surface (never sink under -12m or rocket into orbit)
float bottomY = player.Y - player.Grid.Spec.HalfHeight;
Assert.InRange(bottomY - gh, -0.75f, 4.0f);
}
Assert.True(session.State.Terrain.Brushes.Count > initialBrushes,
"Driving across dust should stamp tire track brushes");
Assert.True(maxGround - minGround > 0.25f,
$"Expected buggy to traverse non-flat terrain (span {maxGround - minGround:F2}m)");
}
[Theory]
[InlineData(42)]
[InlineData(101)]
[InlineData(999)]
public void CoverBoxes_HaveFlatFootprintWithinHalfMeterAcrossAllCorners(int seed)
{
var state = new MatchState(seed);
float maxCoverCornerDelta = 0f;
foreach (var box in state.ArenaCoverBoxes)
{
float[] heights =
{
state.Terrain.SampleHeight(box.X, box.Z),
state.Terrain.SampleHeight(box.X - box.HalfX, box.Z - box.HalfZ),
state.Terrain.SampleHeight(box.X + box.HalfX, box.Z - box.HalfZ),
state.Terrain.SampleHeight(box.X - box.HalfX, box.Z + box.HalfZ),
state.Terrain.SampleHeight(box.X + box.HalfX, box.Z + box.HalfZ)
};
float minH = float.PositiveInfinity;
float maxH = float.NegativeInfinity;
for (int i = 0; i < heights.Length; i++)
{
minH = MathF.Min(minH, heights[i]);
maxH = MathF.Max(maxH, heights[i]);
}
maxCoverCornerDelta = MathF.Max(maxCoverCornerDelta, maxH - minH);
float boxBottom = box.Y - box.HalfY;
Assert.True(boxBottom <= minH + 0.01f,
$"Cover box at ({box.X}, {box.Z}) bottom {boxBottom:F2}m floats above corner height {minH:F2}m");
}
Assert.True(maxCoverCornerDelta <= 0.5f,
$"Expected MaxCoverCornerDelta <= 0.5m for seed {seed}, got {maxCoverCornerDelta:F3}m");
}
[Fact]
public void RoadBrushes_MajorAxisRunsTangentiallyToFormRingLoop()
{
var terrain = TerrainField.Generate(42UL, Array.Empty<ArenaBoxSpec>());
var roads = new List<TerrainBrush>();
for (int i = 0; i < terrain.Brushes.Count; i++)
{
if (terrain.Brushes[i].Kind == TerrainBrushKind.Road)
{
roads.Add(terrain.Brushes[i]);
}
}
Assert.Equal(8, roads.Count);
foreach (var road in roads)
{
Assert.True(road.RadiusX > road.RadiusZ,
$"Expected Road RadiusX ({road.RadiusX}) > RadiusZ ({road.RadiusZ}) so major axis follows tangential u");
float rLen = MathF.Sqrt(road.X * road.X + road.Z * road.Z);
float radX = road.X / rLen;
float radZ = road.Z / rLen;
float axisX = MathF.Cos(road.RotationRad);
float axisZ = MathF.Sin(road.RotationRad);
float radialDot = MathF.Abs(radX * axisX + radZ * axisZ);
Assert.True(radialDot < 1e-4f,
$"Expected Road major axis to be tangential (dot ~ 0), got {radialDot:F4}");
}
}
}
}