396 lines
17 KiB
C#
396 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
using RustToDust.Core;
|
|
using RustToDust.Ymir;
|
|
using Xunit;
|
|
|
|
namespace RustToDust.Tests
|
|
{
|
|
public sealed class TerrainTests
|
|
{
|
|
[Fact]
|
|
public void Generate_ProducesDeterministicNonFlatReliefWithinBounds()
|
|
{
|
|
Assert.Equal(1536f, TerrainField.ArenaSize);
|
|
Assert.Equal(8, TerrainField.TileGridCount);
|
|
Assert.Equal(192f, TerrainField.TileSize);
|
|
Assert.Equal(-18f, TerrainField.MinWorldHeight);
|
|
Assert.Equal(52f, TerrainField.MaxWorldHeight);
|
|
|
|
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 = -700; gx <= 700; gx += 50)
|
|
{
|
|
for (int gz = -700; gz <= 700; gz += 50)
|
|
{
|
|
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 >= 25f, $"Expected dramatic mesa/dune/canyon relief >= 25m span, got [{minH:F2}, {maxH:F2}]");
|
|
Assert.InRange(minH, TerrainField.MinWorldHeight, -3.0f);
|
|
Assert.InRange(maxH, 18.0f, TerrainField.MaxWorldHeight);
|
|
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 (3, 3) spans X in [-192, 0), Z in [-192, 0).
|
|
// Point (-96, -96) with radius 6m is strictly inside tile (3, 3) and nowhere else.
|
|
const float cx = -96f;
|
|
const float cz = -96f;
|
|
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 (3, 3) 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 == 3 && tz == 3) ? 2 : 1;
|
|
Assert.Equal(expected, terrain.TileVersions[tx, tz]);
|
|
}
|
|
}
|
|
Assert.Equal(1UL << (3 * TerrainField.TileGridCount + 3), terrain.DirtyTileMask);
|
|
}
|
|
|
|
[Fact]
|
|
public void Raycast_DetectsBlockingMesaAndClearsAboveCrest()
|
|
{
|
|
var terrain = TerrainField.Generate(42UL, Array.Empty<ArenaBoxSpec>());
|
|
TerrainBrush? tallMesa = null;
|
|
for (int i = 0; i < terrain.Brushes.Count; i++)
|
|
{
|
|
var b = terrain.Brushes[i];
|
|
if (b.Kind == TerrainBrushKind.Mesa && b.HeightDelta >= 22f)
|
|
{
|
|
tallMesa = b;
|
|
break;
|
|
}
|
|
}
|
|
|
|
Assert.NotNull(tallMesa);
|
|
var mesa = tallMesa!.Value;
|
|
|
|
float startX = mesa.X - mesa.RadiusX - 25f;
|
|
float endX = mesa.X + mesa.RadiusX + 25f;
|
|
float span = endX - startX;
|
|
float lowY = terrain.SampleHeight(startX, mesa.Z) + 4f;
|
|
|
|
bool blocked = terrain.Raycast(
|
|
startX, lowY, mesa.Z,
|
|
1f, 0f, 0f,
|
|
span,
|
|
out float hx, out _, out _, out _);
|
|
|
|
Assert.True(blocked, "Expected low ray across tall mesa to be blocked by terrain");
|
|
Assert.InRange(hx, startX, endX);
|
|
|
|
float highY = terrain.SampleHeight(mesa.X, mesa.Z) + 6f;
|
|
bool clear = terrain.Raycast(
|
|
startX, highY, mesa.Z,
|
|
1f, 0f, 0f,
|
|
span,
|
|
out _, out _, out _, out _);
|
|
|
|
Assert.False(clear, "Expected ray above mesa plateau to pass unobstructed");
|
|
}
|
|
|
|
[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.Equal(36, state.ArenaCoverBoxes.Count);
|
|
Assert.True(maxCoverCornerDelta <= 0.6f,
|
|
$"Expected MaxCoverCornerDelta <= 0.6m 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(16, 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}");
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(42)]
|
|
[InlineData(101)]
|
|
public void SteepMesaAndDuneTraversal_AllSixChassis_NoTunnelingSkyLaunchOrOverturning(int seed)
|
|
{
|
|
foreach (var chassis in Catalog.AllChassis)
|
|
{
|
|
RunSteepTerrainStress(seed, chassis, testDune: false);
|
|
RunSteepTerrainStress(seed, chassis, testDune: true);
|
|
}
|
|
}
|
|
|
|
private static void RunSteepTerrainStress(int seed, ChassisSpec chassis, bool testDune)
|
|
{
|
|
using var session = new ArenaSession(seed);
|
|
var player = session.State.Player;
|
|
|
|
TerrainBrush targetBrush = default;
|
|
float startX = 0f;
|
|
float startZ = 0f;
|
|
float bestScore = -1f;
|
|
for (int i = 0; i < session.State.Terrain.Brushes.Count; i++)
|
|
{
|
|
var b = session.State.Terrain.Brushes[i];
|
|
bool matches = testDune
|
|
? b.Kind == TerrainBrushKind.DuneRidge && b.HeightDelta >= 14f
|
|
: b.Kind == TerrainBrushKind.Mesa && b.HeightDelta >= 22f;
|
|
if (!matches)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (MathF.Sqrt(b.X * b.X + b.Z * b.Z) > SpectatorRing.InitialRadius - 90f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float peakH = session.State.Terrain.SampleHeight(b.X, b.Z);
|
|
for (int k = 0; k < 8; k++)
|
|
{
|
|
float localAng = k * (MathF.PI * 0.25f);
|
|
float localX = MathF.Cos(localAng) * (b.RadiusX + 18f);
|
|
float localZ = MathF.Sin(localAng) * (b.RadiusZ + 18f);
|
|
float cosR = MathF.Cos(b.RotationRad);
|
|
float sinR = MathF.Sin(b.RotationRad);
|
|
float candX = b.X + localX * cosR - localZ * sinR;
|
|
float candZ = b.Z + localX * sinR + localZ * cosR;
|
|
if (MathF.Sqrt(candX * candX + candZ * candZ) > SpectatorRing.InitialRadius - 35f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float baseH = session.State.Terrain.SampleHeight(candX, candZ);
|
|
float relief = peakH - baseH;
|
|
if (relief > bestScore)
|
|
{
|
|
bestScore = relief;
|
|
targetBrush = b;
|
|
startX = candX;
|
|
startZ = candZ;
|
|
}
|
|
}
|
|
}
|
|
Assert.True(bestScore >= 10f, $"Expected a climbable {(testDune ? "dune" : "mesa")} approach inside safe circle in seed {seed}");
|
|
|
|
// Keep AI contenders on the opposite side of the safe zone so combat does not kill the vehicle mid-traversal
|
|
for (int i = 1; i < session.State.Contenders.Count; i++)
|
|
{
|
|
session.SetContenderPose(session.State.Contenders[i].Id, -targetBrush.X * 0.5f + (i - 6) * 14f, -targetBrush.Z * 0.5f);
|
|
}
|
|
|
|
float yawToTarget = MathF.Atan2(targetBrush.X - startX, targetBrush.Z - startZ);
|
|
session.SetContenderPose(player.Id, startX, startZ, yawToTarget);
|
|
|
|
if (chassis.Id != player.Grid.Spec.Id)
|
|
{
|
|
session.State.SpawnChassisDrop(player.X, player.Z, chassis);
|
|
Assert.True(session.TrySwapNearbyChassis(), $"Expected swap to {chassis.Id} to succeed");
|
|
Assert.Equal(chassis.Id, player.Grid.Spec.Id);
|
|
}
|
|
|
|
float startGroundY = session.State.Terrain.SampleHeight(player.X, player.Z);
|
|
float maxGroundY = startGroundY;
|
|
float minBottomRel = float.PositiveInfinity;
|
|
float maxAirGap = float.NegativeInfinity;
|
|
float minUpDot = float.PositiveInfinity;
|
|
|
|
for (int tick = 0; tick < 600; tick++)
|
|
{
|
|
float steer = MathF.Sin(tick * 0.03f) * 0.15f;
|
|
session.Step(new PlayerInput(
|
|
Throttle: 1f,
|
|
Steer: steer,
|
|
AimWorldX: targetBrush.X,
|
|
AimWorldZ: targetBrush.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} (seed={seed}, chassis={chassis.Id}, dune={testDune})");
|
|
|
|
var body = session.GetContenderBody(player.Id);
|
|
Assert.True(body.HasValue, $"Expected live contender body at tick {tick} (seed={seed}, chassis={chassis.Id})");
|
|
|
|
int lane = body!.Value.Lane;
|
|
var view = session.World.View;
|
|
var q = new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]);
|
|
var up = Vector3.Transform(Vector3.UnitY, q);
|
|
Assert.False(float.IsNaN(up.Y), $"NaN orientation at tick {tick}");
|
|
|
|
float gh = session.State.Terrain.SampleHeight(player.X, player.Z);
|
|
float bottomRel = (player.Y - player.Grid.Spec.HalfHeight) - gh;
|
|
|
|
maxGroundY = MathF.Max(maxGroundY, gh);
|
|
minBottomRel = MathF.Min(minBottomRel, bottomRel);
|
|
maxAirGap = MathF.Max(maxAirGap, bottomRel);
|
|
minUpDot = MathF.Min(minUpDot, up.Y);
|
|
}
|
|
|
|
float climbProgress = maxGroundY - startGroundY;
|
|
float minRequiredClimb = testDune ? 4.0f : 2.0f;
|
|
float minAllowedUpDot = chassis.IsWalker ? 0.927f : 0.50f;
|
|
|
|
Assert.True(climbProgress >= minRequiredClimb,
|
|
$"Insufficient uphill climb progress (seed={seed}, chassis={chassis.Id}, dune={testDune}): climb={climbProgress:F2}m start=({startX:F1},{startZ:F1},h={startGroundY:F2}) end=({player.X:F1},{player.Z:F1},maxH={maxGroundY:F2}) brush=({targetBrush.X:F1},{targetBrush.Z:F1},rx={targetBrush.RadiusX:F1},rz={targetBrush.RadiusZ:F1},rot={targetBrush.RotationRad:F2},dh={targetBrush.HeightDelta:F1})");
|
|
Assert.True(minBottomRel >= -0.35f,
|
|
$"Terrain tunneling detected (seed={seed}, chassis={chassis.Id}, dune={testDune}): minBottomRel={minBottomRel:F2}m");
|
|
Assert.True(maxAirGap <= 5.0f,
|
|
$"Excessive sky launch detected (seed={seed}, chassis={chassis.Id}, dune={testDune}): maxAirGap={maxAirGap:F2}m");
|
|
Assert.True(minUpDot >= minAllowedUpDot,
|
|
$"Excessive chassis tilt (seed={seed}, chassis={chassis.Id}, dune={testDune}): minUpDot={minUpDot:F3} (req>={minAllowedUpDot:F3})");
|
|
}
|
|
}
|
|
}
|