Fix Cut 1 findings: Polygon2D 256px terrain bake, spectator LOS memory leak, steep slope/biped physics, and HUD polish

This commit is contained in:
Meta Vens 2026-12-04 15:41:02 +01:00
commit af6604da80
9 changed files with 458 additions and 69 deletions

View file

@ -196,5 +196,117 @@ namespace RustToDust.Tests
}
}
}
[Fact]
public void OccludedSpectators_DoNotLeakLiveTargetAfterMemoryExpires_FiveSeconds()
{
using var session = new ArenaSession(seed: 42);
// Shrink safe circle so the tall mesa perimeter is outside SafeRadius
session.State.Time = 360f;
session.State.Spectators.UpdateSafeRadius(session.State.Time);
TerrainBrush mesa = default;
float bestArea = -1f;
for (int i = 0; i < session.State.Terrain.Brushes.Count; i++)
{
var b = session.State.Terrain.Brushes[i];
if (b.Kind == TerrainBrushKind.Mesa && b.HeightDelta >= 22f)
{
float area = b.RadiusX * b.RadiusZ;
if (area > bestArea)
{
bestArea = area;
mesa = b;
}
}
}
Assert.True(bestArea > 0f, "Expected at least one tall Mesa in generated terrain.");
var player = session.State.Player;
var tourists = session.State.Spectators.Spectators.Where(s => s.Tier == SpectatorTier.Tourist).ToList();
var provokedSpec = tourists[0];
var clearedSpec = tourists[1];
// Place player on the side of the mesa further from center (strictly outside SafeRadius)
float xNeg = mesa.X - mesa.RadiusX - 18f;
float xPos = mesa.X + mesa.RadiusX + 18f;
bool negIsFurther = (xNeg * xNeg + mesa.Z * mesa.Z) >= (xPos * xPos + mesa.Z * mesa.Z);
float occludedX = negIsFurther ? xNeg : xPos;
float specSideX = negIsFurther ? xPos : xNeg;
// Briefly establish visual contact for provokedSpec at initial spot, then move player behind the mesa
session.SetContenderPose(player.Id, specSideX - (negIsFurther ? 12f : -12f), mesa.Z);
session.SetSpectatorPose(provokedSpec.Id, specSideX, mesa.Z);
session.State.Spectators.OnSpectatorDamaged(provokedSpec, player.Id);
session.Step(new PlayerInput(0f, 0f, player.X, player.Z, false, false, true));
Assert.True(provokedSpec.HasTargetLos, "Expected initial LOS before player drops behind cover");
float provokedKnownX = provokedSpec.LastKnownTargetX;
float provokedKnownZ = provokedSpec.LastKnownTargetZ;
// Now hide player behind the mesa and place clearedSpec on the opposite side closest to the breach
session.SetContenderPose(player.Id, occludedX, mesa.Z - 14f);
session.SetSpectatorPose(provokedSpec.Id, specSideX, mesa.Z - 4f);
session.SetSpectatorPose(clearedSpec.Id, specSideX, mesa.Z + 4f);
// Verify clearedSpec gets Pass-2 Cleared assignment without LOS
session.Step(new PlayerInput(0f, 0f, player.X, player.Z, false, false, true));
Assert.False(provokedSpec.HasTargetLos);
Assert.Equal(SpectatorStance.Cleared, clearedSpec.Stance);
Assert.False(clearedSpec.HasTargetLos);
float clearedInitialKnownX = clearedSpec.LastKnownTargetX;
float clearedInitialKnownZ = clearedSpec.LastKnownTargetZ;
// Run 300 ticks (5.0s > 4.0s TargetMemoryDuration) while player moves laterally behind cover
for (int tick = 0; tick < 300; tick++)
{
float latZ = mesa.Z - 14f + (tick / 299f) * 34f;
if (tick % 10 == 0)
{
session.SetContenderPose(player.Id, occludedX, latZ);
}
session.Step(new PlayerInput(0f, 0f, player.X, player.Z, false, false, true));
Assert.False(provokedSpec.HasTargetLos, $"Provoked spectator regained unexpected LOS at tick {tick}");
Assert.False(clearedSpec.HasTargetLos, $"Cleared spectator regained unexpected LOS at tick {tick}");
if (tick == 270) // 4.5s > 4.0s memory window
{
Assert.True(provokedSpec.TimeSinceLastSeen >= 4.5f,
$"Provoked TimeSinceLastSeen reset unexpectedly: {provokedSpec.TimeSinceLastSeen:F2}s");
Assert.True(clearedSpec.TimeSinceLastSeen >= 4.5f,
$"Cleared TimeSinceLastSeen reset unexpectedly: {clearedSpec.TimeSinceLastSeen:F2}s");
}
}
// Neither spectator's LastKnownTarget or aim vector may track the player's new occluded position
float provokedDistToPlayer = MathF.Sqrt(
(provokedSpec.LastKnownTargetX - player.X) * (provokedSpec.LastKnownTargetX - player.X) +
(provokedSpec.LastKnownTargetZ - player.Z) * (provokedSpec.LastKnownTargetZ - player.Z));
Assert.Equal(provokedKnownX, provokedSpec.LastKnownTargetX, precision: 3);
Assert.Equal(provokedKnownZ, provokedSpec.LastKnownTargetZ, precision: 3);
Assert.True(provokedDistToPlayer > 25f,
$"Provoked LastKnownTarget leaked live player position (dist={provokedDistToPlayer:F2}m)");
float clearedDistToPlayer = MathF.Sqrt(
(clearedSpec.LastKnownTargetX - player.X) * (clearedSpec.LastKnownTargetX - player.X) +
(clearedSpec.LastKnownTargetZ - player.Z) * (clearedSpec.LastKnownTargetZ - player.Z));
Assert.Equal(clearedInitialKnownX, clearedSpec.LastKnownTargetX, precision: 3);
Assert.Equal(clearedInitialKnownZ, clearedSpec.LastKnownTargetZ, precision: 3);
Assert.True(clearedDistToPlayer > 20f,
$"Cleared LastKnownTarget leaked live player position (dist={clearedDistToPlayer:F2}m)");
MatchRules.ComputeSpectatorBehavior(provokedSpec, session.State, out _, out _, out float pAimX, out float pAimZ, out bool pFire);
Assert.False(pFire);
Assert.Equal(provokedKnownX, pAimX, precision: 3);
Assert.Equal(provokedKnownZ, pAimZ, precision: 3);
MatchRules.ComputeSpectatorBehavior(clearedSpec, session.State, out _, out _, out float cAimX, out float cAimZ, out bool cFire);
Assert.False(cFire);
Assert.Equal(clearedInitialKnownX, cAimX, precision: 3);
Assert.Equal(clearedInitialKnownZ, cAimZ, precision: 3);
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using RustToDust.Core;
using RustToDust.Ymir;
using Xunit;
@ -256,5 +257,90 @@ namespace RustToDust.Tests
$"Expected Road major axis to be tangential (dot ~ 0), got {radialDot:F4}");
}
}
[Theory]
[InlineData(42)]
[InlineData(101)]
public void SteepMesaAndCanyonTraversal_BuggyAndPaladin_NoTunnelingSkyLaunchOrOverturning(int seed)
{
RunSteepTerrainStress(seed, usePaladin: false);
RunSteepTerrainStress(seed, usePaladin: true);
}
private static void RunSteepTerrainStress(int seed, bool usePaladin)
{
using var session = new ArenaSession(seed);
var player = session.State.Player;
TerrainBrush mesa = default;
bool foundMesa = false;
for (int i = 0; i < session.State.Terrain.Brushes.Count; i++)
{
var b = session.State.Terrain.Brushes[i];
if (b.Kind == TerrainBrushKind.Mesa && b.HeightDelta >= 22f)
{
mesa = b;
foundMesa = true;
break;
}
}
Assert.True(foundMesa, $"Expected at least one tall mesa in seed {seed}");
float startX = mesa.X - mesa.RadiusX - 24f;
float startZ = mesa.Z;
float yawToMesa = MathF.Atan2(mesa.X - startX, mesa.Z - startZ);
session.SetContenderPose(player.Id, startX, startZ, yawToMesa);
if (usePaladin)
{
session.State.SpawnChassisDrop(player.X, player.Z, Catalog.Paladin);
Assert.True(session.TrySwapNearbyChassis());
Assert.Equal(GridPlane.Coronal, player.Grid.Spec.Plane);
}
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.2f;
session.Step(new PlayerInput(
Throttle: 1f,
Steer: steer,
AimWorldX: mesa.X,
AimWorldZ: mesa.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}, paladin={usePaladin})");
var body = session.GetContenderBody(player.Id);
Assert.True(body.HasValue, $"Expected live contender body at tick {tick} (seed={seed}, paladin={usePaladin})");
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;
minBottomRel = MathF.Min(minBottomRel, bottomRel);
maxAirGap = MathF.Max(maxAirGap, bottomRel);
minUpDot = MathF.Min(minUpDot, up.Y);
}
Assert.True(minBottomRel >= -1.2f,
$"Deep terrain tunneling detected (seed={seed}, paladin={usePaladin}): minBottomRel={minBottomRel:F2}m");
Assert.True(maxAirGap <= 10.0f,
$"Excessive sky launch detected (seed={seed}, paladin={usePaladin}): maxAirGap={maxAirGap:F2}m");
Assert.True(minUpDot > 0.25f,
$"Chassis overturned onto side/back (seed={seed}, paladin={usePaladin}): minUpDot={minUpDot:F3}");
}
}
}