Cut 5b-2: Hybrid B centroidal GRF + 2nd-order joint mech locomotion across all 7 walkers

This commit is contained in:
Meta Vens 2026-12-05 06:37:26 +01:00
commit dee2e738d8
9 changed files with 3077 additions and 431 deletions

View file

@ -0,0 +1,714 @@
using System;
using System.Diagnostics;
using System.Numerics;
using RustToDust.Core;
using RustToDust.Ymir;
using Xunit;
using Xunit.Abstractions;
namespace RustToDust.Tests
{
public sealed class HybridBWalkerTests
{
private readonly ITestOutputHelper _output;
public HybridBWalkerTests(ITestOutputHelper output)
{
_output = output;
}
private static ChassisSpec MakeExemplarSpec() =>
Catalog.Raptor with
{
Id = "exemplar_frame",
Name = "Exemplar Frame",
BaseCellArmor = 22f,
MassKg = 1150f,
BaseThrust = 26000f,
BaseTurnRate = 6.2f,
MaxSpeed = 40f,
HalfWidth = 0.8f,
HalfHeight = 1.55f,
HalfLength = 0.8f
};
[Fact]
public void HybridB_All7Mechs_WalkTurnSlopes_SmoothLiftoff_ZeroTeleports()
{
var specs = new[]
{
Catalog.Breaker,
Catalog.Paladin,
Catalog.Raptor,
Catalog.Spectre,
Catalog.Kestrel,
Catalog.Manticore,
Catalog.Bastion,
MakeExemplarSpec()
};
// 1. Walk, turn, and climb slopes across all 7 mechs + exemplar_frame
foreach (var spec in specs)
{
using var session = new ArenaSession(seed: 42, mapKind: ArenaMapKind.Desert);
for (int i = 1; i < session.State.Contenders.Count; i++)
{
session.SetContenderPose(i, 500f + i * 16f, 500f);
}
// Phase A: start on flat/gentle ground near origin (60 ticks) to verify honest walk slip < 8cm
session.SetContenderPose(0, 0f, 0f, 0f);
session.State.SpawnChassisDrop(0f, 0f, spec);
Assert.True(session.TrySwapNearbyChassis(), $"Failed swap to {spec.Id}");
var player = session.State.Player;
Assert.Equal(spec.Id, player.Grid.Spec.Id);
var ws = session.GetWalkerState(true, player.Id);
Assert.NotNull(ws);
int nLegs = ws!.Geom.LegCount;
float geomScale = ws.GeomScale;
for (int t = 0; t < 60; t++)
{
session.Step(new PlayerInput(
Throttle: 0.45f,
Steer: 0f,
AimWorldX: player.X,
AimWorldZ: player.Z + 60f,
Fire: false,
InteractPickup: false,
Brake: false));
}
Assert.True(ws.MaxFootSlipM < 0.08f,
$"{spec.Id}: normal walk foot slip {ws.MaxFootSlipM:F4}m >= 0.08m (maxSpd={spec.MaxSpeed:F1})");
// Phase B: move to dune slope approach, turn & climb (180 ticks) checking liftoff teleport, C1 touchdown, omega continuity
float duneApproachX = 0f;
float duneApproachZ = 0f;
float bestScore = -1f;
foreach (var b in session.State.Terrain.Brushes)
{
if (b.Kind == TerrainBrushKind.DuneRidge && b.HeightDelta > 4f)
{
float d = MathF.Sqrt(b.X * b.X + b.Z * b.Z);
if (d > 40f && d < 280f && b.HeightDelta > bestScore)
{
bestScore = b.HeightDelta;
float dirX = -b.X / d;
float dirZ = -b.Z / d;
duneApproachX = b.X + dirX * MathF.Min(36f, b.RadiusX * 0.85f);
duneApproachZ = b.Z + dirZ * MathF.Min(36f, b.RadiusZ * 0.85f);
}
}
}
session.SetContenderPose(0, duneApproachX, duneApproachZ, MathF.Atan2(-duneApproachX, -duneApproachZ) + MathF.PI);
ws = session.GetWalkerState(true, player.Id)!;
var prevFoot = new Vector3[nLegs];
var prevPlanted = new bool[nLegs];
var prevOmegaHip = new float[nLegs];
for (int i = 0; i < nLegs; i++)
{
prevFoot[i] = ws.FootSole[i];
prevPlanted[i] = ws.Planted[i];
prevOmegaHip[i] = ws.Actuators[i].Omega.HipPitch;
}
float maxDeltaFootPerTick = 0f;
float maxLiftoffDelta = 0f;
float maxOmegaJumpAtSwap = 0f;
float maxTouchdownGroundErr = 0f;
float minUpDot = 1f;
int swapCount = 0;
for (int t = 0; t < 180; t++)
{
float steer = MathF.Sin(t * 0.06f) * 0.65f;
session.Step(new PlayerInput(
Throttle: 0.85f,
Steer: steer,
AimWorldX: player.X + 40f * MathF.Sin(player.Yaw),
AimWorldZ: player.Z + 40f * MathF.Cos(player.Yaw),
Fire: false,
InteractPickup: false,
Brake: false));
Assert.True(float.IsFinite(player.X) && float.IsFinite(player.Y) && float.IsFinite(player.Z),
$"{spec.Id}: non-finite pose at tick {t}");
var q = new Quaternion(player.CurrRotX, player.CurrRotY, player.CurrRotZ, player.CurrRotW);
var up = Vector3.Transform(Vector3.UnitY, q);
minUpDot = MathF.Min(minUpDot, up.Y);
for (int i = 0; i < nLegs; i++)
{
Vector3 curFoot = ws.FootSole[i];
float delta = (curFoot - prevFoot[i]).Length();
if (delta > maxDeltaFootPerTick)
{
maxDeltaFootPerTick = delta;
}
bool curPlanted = ws.Planted[i];
if (prevPlanted[i] && !curPlanted && !ws.IsAirborne)
{
if (delta > maxLiftoffDelta)
{
maxLiftoffDelta = delta;
}
}
if (!prevPlanted[i] && curPlanted)
{
swapCount++;
float groundY = session.State.Terrain.SampleHeight(curFoot.X, curFoot.Z);
float gErr = MathF.Abs(curFoot.Y - groundY);
if (gErr > maxTouchdownGroundErr)
{
maxTouchdownGroundErr = gErr;
}
float omegaDelta = MathF.Abs(ws.Actuators[i].Omega.HipPitch - prevOmegaHip[i]);
if (omegaDelta > maxOmegaJumpAtSwap)
{
maxOmegaJumpAtSwap = omegaDelta;
}
}
prevFoot[i] = curFoot;
prevPlanted[i] = curPlanted;
prevOmegaHip[i] = ws.Actuators[i].Omega.HipPitch;
}
}
float maxFootGroundSpeed = spec.MaxSpeed + MathF.Max(spec.HalfWidth, spec.HalfLength) * spec.BaseTurnRate * 0.5f;
float maxAllowedTickDelta = MathF.Max(0.55f, maxFootGroundSpeed * ArenaSession.FixedDt * 3.5f);
_output.WriteLine(
$"{spec.Id,-15} minUp={minUpDot:F3} slip={ws.MaxFootSlipM:F4}m maxFootDelta/tick={maxDeltaFootPerTick:F4}m " +
$"liftoffDelta={maxLiftoffDelta:F4}m tdGroundErr={maxTouchdownGroundErr:F5}m swapOmegaJump={maxOmegaJumpAtSwap:F3} swaps={swapCount}");
Assert.True(minUpDot >= 0.90f, $"{spec.Id}: unstable tilt minUpDot={minUpDot:F3}");
Assert.True(swapCount >= 4, $"{spec.Id}: expected >= 4 gait transitions, got {swapCount}");
Assert.True(maxLiftoffDelta <= 0.35f * MathF.Max(1f, geomScale),
$"{spec.Id}: Bug 1 liftoff teleport detected: liftoffDelta={maxLiftoffDelta:F4}m");
Assert.True(maxDeltaFootPerTick <= maxAllowedTickDelta,
$"{spec.Id}: 1-tick foot teleport detected: maxDelta={maxDeltaFootPerTick:F4}m > {maxAllowedTickDelta:F4}m");
Assert.True(maxTouchdownGroundErr <= 0.06f * MathF.Max(1f, geomScale),
$"{spec.Id}: non-C1 liftoff/touchdown height gap {maxTouchdownGroundErr:F4}m");
Assert.True(maxOmegaJumpAtSwap < 12.0f,
$"{spec.Id}: discontinuous joint omega jump across swap: {maxOmegaJumpAtSwap:F3} rad/s");
}
// 2. Benchmark 12 simultaneous mechs CPU < 1.5 ms/tick
{
using var benchSession = new ArenaSession(seed: 99, mapKind: ArenaMapKind.Desert);
for (int i = 0; i < benchSession.State.Contenders.Count; i++)
{
var spec = specs[i % 7];
float x = (i - 5.5f) * 28f;
float z = -80f + (i % 3) * 35f;
benchSession.SetContenderPose(i, x, z, 0f);
if (benchSession.State.Contenders[i].Grid.Spec.Id != spec.Id)
{
benchSession.State.Contenders[i].Grid.SwapChassis(spec);
benchSession.SetContenderPose(i, x, z, 0f);
}
}
var walkers = new (ContenderState C, WalkerState Ws, int Lane)[12];
for (int i = 0; i < 12; i++)
{
var c = benchSession.State.Contenders[i];
c.ThrottleCmd = 0.8f;
c.SteerCmd = (i % 2 == 0 ? 0.2f : -0.2f);
walkers[i] = (c, benchSession.GetWalkerState(true, c.Id)!, benchSession.GetContenderBody(c.Id)!.Value.Lane);
}
// Warmup JIT
for (int w = 0; w < 20; w++)
{
benchSession.Step(new PlayerInput(0.8f, 0.2f, 0f, 100f, false, false, false));
}
var view = benchSession.World.View;
const int benchTicks = 300;
long t0 = Stopwatch.GetTimestamp();
for (int t = 0; t < benchTicks; t++)
{
for (int i = 0; i < 12; i++)
{
var (c, ws, lane) = walkers[i];
var pos = new Vector3(view.PositionX[lane], view.PositionY[lane], view.PositionZ[lane]);
var q = new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]);
var vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
var angVel = new Vector3(view.AngularVelocityX[lane], view.AngularVelocityY[lane], view.AngularVelocityZ[lane]);
var fwd = Vector3.Transform(Vector3.UnitZ, q);
fwd.Y = 0f;
fwd = fwd.LengthSquared() < 1e-4f ? Vector3.UnitZ : Vector3.Normalize(fwd);
WalkerLocomotion.StepWalker(
ws, c.Grid, benchSession.State.Terrain, view, lane, view.Mass[lane],
pos, q, vel, angVel, fwd * (c.ThrottleCmd * c.Grid.Spec.MaxSpeed), 0.3f, false, ArenaSession.FixedDt);
}
}
long t1 = Stopwatch.GetTimestamp();
double msPerTick = (t1 - t0) * 1000.0 / Stopwatch.Frequency / benchTicks;
_output.WriteLine($"12-mech simultaneous WalkerLocomotion CPU: {msPerTick:F4} ms/tick");
Assert.True(msPerTick < 1.5, $"12 simultaneous mechs CPU {msPerTick:F4} ms/tick exceeded 1.5 ms/tick budget");
}
}
[Fact]
public void HybridB_CrippledLeg_ReducesGrfAndTorque_InducesLimp()
{
Span<float> legIntegrities = stackalloc float[4];
foreach (var spec in new[] { Catalog.Breaker, Catalog.Bastion })
{
using var session = new ArenaSession(seed: 17, mapKind: ArenaMapKind.Desert);
for (int i = 1; i < session.State.Contenders.Count; i++)
{
session.SetContenderPose(i, 450f + i * 16f, 450f);
}
session.SetContenderPose(0, 0f, 0f, 0f);
session.State.SpawnChassisDrop(0f, 0f, spec);
Assert.True(session.TrySwapNearbyChassis());
var player = session.State.Player;
var grid = player.Grid;
// Strip all left-leg cells (Breaker Coronal L: y>=H-2, x<W/2; Bastion Dorsal FL: y<H/2, x<W/2)
if (spec.Plane == GridPlane.Coronal)
{
for (int y = spec.Height - 2; y < spec.Height; y++)
{
for (int x = 0; x < spec.Width / 2; x++)
{
grid.Armor[x, y] = 0f;
}
}
}
else
{
for (int y = 0; y < spec.Height / 2; y++)
{
for (int x = 0; x < spec.Width / 2; x++)
{
grid.Armor[x, y] = 0f;
}
}
}
legIntegrities.Clear();
grid.GetLegIntegrities(legIntegrities);
Assert.Equal(0.25f, legIntegrities[0], precision: 3);
Assert.Equal(1.00f, legIntegrities[1], precision: 3);
Assert.True(grid.LegIntegrity < 1.0f, $"Expected {spec.Id} LegIntegrity < 1.0 after leg strip");
var ws = session.GetWalkerState(true, player.Id)!;
float peakLeftGrf = 0f;
float peakRightGrf = 0f;
float maxRollAbs = 0f;
float maxOmegaLeft = 0f;
float maxOmegaRight = 0f;
for (int t = 0; t < 180; t++)
{
session.Step(new PlayerInput(
Throttle: 0.9f,
Steer: 0f,
AimWorldX: player.X,
AimWorldZ: player.Z + 80f,
Fire: false,
InteractPickup: false,
Brake: false));
Assert.True(float.IsFinite(player.X) && float.IsFinite(player.Y) && float.IsFinite(player.Z));
peakLeftGrf = MathF.Max(peakLeftGrf, ws.LastGrfY[0]);
peakRightGrf = MathF.Max(peakRightGrf, ws.LastGrfY[1]);
maxOmegaLeft = MathF.Max(maxOmegaLeft, MathF.Abs(ws.Actuators[0].Omega.HipPitch));
maxOmegaRight = MathF.Max(maxOmegaRight, MathF.Abs(ws.Actuators[1].Omega.HipPitch));
var q = new Quaternion(player.CurrRotX, player.CurrRotY, player.CurrRotZ, player.CurrRotW);
var rightVec = Vector3.Transform(Vector3.UnitX, q);
maxRollAbs = MathF.Max(maxRollAbs, MathF.Abs(rightVec.Y));
}
_output.WriteLine(
$"{spec.Id} crippled left leg: peakLeftGrf={peakLeftGrf:F1}N peakRightGrf={peakRightGrf:F1}N " +
$"maxRollSin={maxRollAbs:F4} omegaL={maxOmegaLeft:F2} omegaR={maxOmegaRight:F2}");
Assert.True(peakLeftGrf < peakRightGrf * 0.55f,
$"{spec.Id}: expected crippled left GRF ({peakLeftGrf:F1}N) << healthy right GRF ({peakRightGrf:F1}N)");
Assert.True(maxRollAbs > 0.002f,
$"{spec.Id}: expected crippled leg asymmetry to induce measurable limp roll");
}
}
[Fact]
public void HybridB_AirborneMech_ZeroGrfForce()
{
foreach (var spec in new[] { Catalog.Breaker, Catalog.Manticore })
{
using var session = new ArenaSession(seed: 7, mapKind: ArenaMapKind.Desert);
session.SetContenderPose(0, 0f, 0f, 0f);
session.State.SpawnChassisDrop(0f, 0f, spec);
Assert.True(session.TrySwapNearbyChassis());
var player = session.State.Player;
var body = session.GetContenderBody(player.Id)!.Value;
var view = session.World.View;
int lane = body.Lane;
// 1. Direct StepWalker check with COM lifted 18m above terrain
float groundY = session.State.Terrain.SampleHeight(0f, 0f);
Vector3 liftedPos = new(view.PositionX[lane], groundY + spec.HalfHeight + 18f, view.PositionZ[lane]);
var ws = session.GetWalkerState(true, player.Id)!;
WalkerLocomotion.StepWalker(
ws,
player.Grid,
session.State.Terrain,
view,
lane,
view.Mass[lane],
liftedPos,
new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]),
Vector3.Zero,
Vector3.Zero,
new Vector3(0f, 0f, 10f),
0f,
false,
ArenaSession.FixedDt);
Assert.True(ws.IsAirborne, $"{spec.Id}: expected IsAirborne == true when lifted 18m above terrain");
Assert.Equal(Vector3.Zero, ws.LastTotalForce);
Assert.Equal(Vector3.Zero, ws.LastTotalTorque);
Assert.Equal(0f, view.ForceX[lane]);
Assert.Equal(0f, view.ForceY[lane]);
Assert.Equal(0f, view.ForceZ[lane]);
Assert.Equal(0f, view.TorqueX[lane]);
Assert.Equal(0f, view.TorqueY[lane]);
Assert.Equal(0f, view.TorqueZ[lane]);
// 2. End-to-end ArenaSession launch into air: confirm zero GRF while airborne in real Step()
session.World.ApplyImpulse(
lane,
(0f, view.Mass[lane] * 28f, 0f),
(view.PositionX[lane], view.PositionY[lane], view.PositionZ[lane]));
bool sawAirborneTick = false;
for (int t = 0; t < 20; t++)
{
session.Step(new PlayerInput(1f, 0f, 0f, 50f, false, false, false));
if (ws.IsAirborne)
{
sawAirborneTick = true;
Assert.Equal(Vector3.Zero, ws.LastTotalForce);
Assert.Equal(Vector3.Zero, ws.LastTotalTorque);
}
}
Assert.True(sawAirborneTick, $"{spec.Id}: expected vertical launch to trigger airborne zero-GRF ticks");
}
}
[Fact]
public void HybridB_All7Mechs_ObeyDirectionalInputAtMultipleYaws_NoGooseStepNoDrift()
{
var specs = new[]
{
Catalog.Breaker,
Catalog.Paladin,
Catalog.Raptor,
Catalog.Spectre,
Catalog.Kestrel,
Catalog.Manticore,
Catalog.Bastion
};
float[] yawsDeg = { 0f, 45f, 90f, 135f };
foreach (var spec in specs)
{
foreach (float yawDeg in yawsDeg)
{
float yawRad = yawDeg * MathF.PI / 180f;
using var session = new ArenaSession(seed: 42, mapKind: ArenaMapKind.Desert);
for (int i = 1; i < session.State.Contenders.Count; i++)
{
session.SetContenderPose(i, 500f + i * 16f, 500f);
}
session.SetContenderPose(0, 0f, 0f, yawRad);
session.State.SpawnChassisDrop(0f, 0f, spec);
Assert.True(session.TrySwapNearbyChassis(), $"Failed swap to {spec.Id}");
session.SetContenderPose(0, 0f, 0f, yawRad);
var player = session.State.Player;
// Lettle -> Wait 5 idle ticks to verify zero rest creep from dead stop
for (int t = 0; t < 5; t++)
{
session.Step(new PlayerInput(0f, 0f, player.X, player.Z + 50f, false, false, false));
}
Vector2 cmdFwd = new(MathF.Sin(yawRad), MathF.Cos(yawRad));
Vector2 cmdRight = new(-cmdFwd.Y, cmdFwd.X);
// 1. Hold W (Throttle = +1) for 2.0s (120 ticks) from rest
Vector2 wStart = new(player.X, player.Z);
for (int t = 0; t < 120; t++)
{
session.Step(new PlayerInput(
Throttle: 1f,
Steer: 0f,
AimWorldX: player.X + cmdFwd.X * 50f,
AimWorldZ: player.Z + cmdFwd.Y * 50f,
Fire: false,
InteractPickup: false,
Brake: false));
}
float endWSpeed = player.Speed;
Vector2 wDelta = new Vector2(player.X, player.Z) - wStart;
float fwdDisp = Vector2.Dot(wDelta, cmdFwd);
float latDrift = MathF.Abs(Vector2.Dot(wDelta, cmdRight));
Assert.True(fwdDisp >= 6.0f,
$"{spec.Id} @ {yawDeg}deg W 2.0s: fwdDisp={fwdDisp:F2}m < 6.0m (lat={latDrift:F2}m)");
Assert.True(latDrift < 2.0f,
$"{spec.Id} @ {yawDeg}deg W 2.0s: latDrift={latDrift:F2}m >= 2.0m (fwd={fwdDisp:F2}m)");
// 2. Release W (Throttle = 0) for 3.0s (180 ticks): stop < 0.30 m/s within physical Coulomb limit, final 1.0s (60 ticks) drift < 0.15m
float speedAt1_2s = 0f;
float speedAt2_0s = 0f;
float timeToStop = -1f;
Vector2 finalWindowStart = Vector2.Zero;
for (int t = 0; t < 180; t++)
{
session.Step(new PlayerInput(
Throttle: 0f,
Steer: 0f,
AimWorldX: player.X + cmdFwd.X * 50f,
AimWorldZ: player.Z + cmdFwd.Y * 50f,
Fire: false,
InteractPickup: false,
Brake: false));
if (timeToStop < 0f && player.Speed < 0.30f)
{
timeToStop = (t + 1) * ArenaSession.FixedDt;
}
if (t == 71) speedAt1_2s = player.Speed;
if (t == 119)
{
speedAt2_0s = player.Speed;
finalWindowStart = new Vector2(player.X, player.Z);
}
}
float finalDrift = (new Vector2(player.X, player.Z) - finalWindowStart).Length();
float maxPhysStopSec = MathF.Max(1.2f, endWSpeed / 16.0f + 0.35f);
if (yawDeg == 0f)
{
_output.WriteLine(
$"{spec.Id,-10} maxSpd={spec.MaxSpeed:F1} endWSpd={endWSpeed:F2} " +
$"tStop={timeToStop:F2}s (limit={maxPhysStopSec:F2}s) spd@1.2s={speedAt1_2s:F3} spd@2.0s={speedAt2_0s:F3} finalDrift={finalDrift:F4}m");
}
Assert.True(timeToStop > 0f && timeToStop <= maxPhysStopSec,
$"{spec.Id} @ {yawDeg}deg release W: tStop={timeToStop:F2}s > {maxPhysStopSec:F2}s (endWSpd={endWSpeed:F2}, @2.0s={speedAt2_0s:F3})");
Assert.True(finalDrift < 0.15f,
$"{spec.Id} @ {yawDeg}deg release W: final 1.0s drift={finalDrift:F4}m >= 0.15m");
// 3. Hold S (Throttle = -1) for 1.5s (90 ticks)
Vector2 sStart = new(player.X, player.Z);
Vector2 revDir = new(MathF.Sin(player.Yaw), MathF.Cos(player.Yaw));
for (int t = 0; t < 90; t++)
{
session.Step(new PlayerInput(
Throttle: -1f,
Steer: 0f,
AimWorldX: player.X + revDir.X * 50f,
AimWorldZ: player.Z + revDir.Y * 50f,
Fire: false,
InteractPickup: false,
Brake: false));
}
float revDisp = Vector2.Dot(new Vector2(player.X, player.Z) - sStart, -revDir);
Assert.True(revDisp >= 3.0f,
$"{spec.Id} @ {yawDeg}deg S 1.5s: revDisp={revDisp:F2}m < 3.0m");
// Settle until stopped before turning test
for (int t = 0; t < 180 && (t < 20 || player.Speed > 0.05f); t++)
{
session.Step(new PlayerInput(0f, 0f, player.X, player.Z + 50f, false, false, false));
}
// 4. Hold D (Steer = +1, Throttle = 0) for 1.5s (90 ticks)
Vector2 dStart = new(player.X, player.Z);
float prevYaw = player.Yaw;
float accumYaw = 0f;
for (int t = 0; t < 90; t++)
{
session.Step(new PlayerInput(
Throttle: 0f,
Steer: 1f,
AimWorldX: player.X,
AimWorldZ: player.Z + 50f,
Fire: false,
InteractPickup: false,
Brake: false));
accumYaw += MatchRules.NormalizeAngle(player.Yaw - prevYaw);
prevYaw = player.Yaw;
}
float dTrans = (new Vector2(player.X, player.Z) - dStart).Length();
// Steer = +1 turns right -> negative yaw change (-accumYaw >= 0.5 rad)
Assert.True(-accumYaw >= 0.5f,
$"{spec.Id} @ {yawDeg}deg D 1.5s: yawChange={accumYaw:F3} rad (expected <= -0.50 rad)");
Assert.True(dTrans < 1.0f,
$"{spec.Id} @ {yawDeg}deg D 1.5s: spurious translation={dTrans:F3}m >= 1.0m");
if (yawDeg == 0f || yawDeg == 135f)
{
_output.WriteLine(
$"{spec.Id,-10} yaw={yawDeg,3:F0}deg: fwd={fwdDisp:F2}m lat={latDrift:F2}m " +
$"stopSpd={speedAt1_2s:F3}m/s drift={finalDrift:F4}m rev={revDisp:F2}m turn={-accumYaw:F2}rad dTrans={dTrans:F3}m");
}
}
}
}
[Fact]
public void HybridB_BreakerAndBastion_SionSlopeControl_ObeysInput()
{
foreach (var spec in new[] { Catalog.Breaker, Catalog.Bastion })
{
using var session = new ArenaSession(seed: 42, mapKind: ArenaMapKind.Sion);
for (int i = 1; i < session.State.Contenders.Count; i++)
{
session.SetContenderPose(i, 900f + i * 16f, 900f);
}
var sion = session.State.SionMap!;
// Find a clear sloped hillside on Sion (slope ~7-18 deg, free of buildings and low wall meshes along a +/-7m corridor)
float slopeX = session.State.Player.X;
float slopeZ = session.State.Player.Z;
float slopeYaw = 0f;
bool foundSlope = false;
for (float r = 40f; r <= 560f && !foundSlope; r += 20f)
{
for (int a = 0; a < 36 && !foundSlope; a++)
{
float ang = a * (MathF.Tau / 36f);
float x = MathF.Cos(ang) * r;
float z = MathF.Sin(ang) * r;
if (sion.IsInsideAnyBuildingXz(x, z, 14f)) continue;
var (nx, ny, nz) = session.State.Terrain.SampleNormal(x, z);
if (ny <= 0.992f && ny >= 0.950f)
{
float yaw = MathF.Atan2(-nx, -nz); // uphill heading
float fx = MathF.Sin(yaw);
float fz = MathF.Cos(yaw);
float rx = -fz;
float rz = fx;
bool clear = true;
foreach (float lat in new[] { -7f, 0f, 7f })
{
float sx = x + rx * lat;
float sz = z + rz * lat;
for (float d = 0f; d < 28f; d += 4f)
{
float ax = sx + fx * d;
float az = sz + fz * d;
float bx = sx + fx * (d + 4f);
float bz = sz + fz * (d + 4f);
if (sion.IsInsideAnyBuildingXz(ax, az, 4f) ||
session.State.Terrain.SampleNormal(ax, az).Ny < 0.90f)
{
clear = false;
break;
}
float gyA = session.State.Terrain.SampleHeight(ax, az);
float gyB = session.State.Terrain.SampleHeight(bx, bz);
if (!session.HasLineOfSight(ax, gyA + 2.5f, az, bx, gyB + 2.5f, bz))
{
clear = false;
break;
}
}
if (!clear) break;
}
if (clear)
{
slopeX = x;
slopeZ = z;
slopeYaw = yaw;
foundSlope = true;
}
}
}
}
Assert.True(foundSlope, $"{spec.Id}: failed to locate clear Sion slope test site");
session.SetContenderPose(0, slopeX, slopeZ, slopeYaw);
session.State.SpawnChassisDrop(slopeX, slopeZ, spec);
Assert.True(session.TrySwapNearbyChassis(), $"Failed swap to {spec.Id} on Sion");
session.SetContenderPose(0, slopeX, slopeZ, slopeYaw);
var player = session.State.Player;
// 1. Drive forward up/along slope (2.0s)
Vector2 startPos = new(player.X, player.Z);
Vector2 fwdDir = new(MathF.Sin(slopeYaw), MathF.Cos(slopeYaw));
var ws = session.GetWalkerState(true, player.Id)!;
float gStart = session.State.Terrain.SampleHeight(player.X, player.Z);
float startY = player.Y;
for (int t = 0; t < 120; t++)
{
session.Step(new PlayerInput(
Throttle: 1f,
Steer: 0f,
AimWorldX: player.X + fwdDir.X * 50f,
AimWorldZ: player.Z + fwdDir.Y * 50f,
Fire: false,
InteractPickup: false,
Brake: false));
}
float gEnd = session.State.Terrain.SampleHeight(player.X, player.Z);
float fwdDisp = Vector2.Dot(new Vector2(player.X, player.Z) - startPos, fwdDir);
int pLane = session.GetContenderBody(player.Id)!.Value.Lane;
string touchesStr = "";
foreach (var tc in session.World.Facts.Touching)
{
if (tc.LaneA == pLane || tc.LaneB == pLane)
{
ulong other = tc.LaneA == pLane ? tc.StableIdB : tc.StableIdA;
touchesStr += $"{other},";
}
}
_output.WriteLine(
$"{spec.Id} Sion slope ({slopeX:F1},{slopeZ:F1}) gStart={gStart:F2} startY={startY:F2} (H={spec.HalfHeight:F2}) -> " +
$"({player.X:F1},{player.Y:F2},{player.Z:F1}) gEnd={gEnd:F2} pYaw={player.Yaw:F2} fwdDisp={fwdDisp:F2}m air={ws.IsAirborne} force={ws.LastTotalForce} touching=[{touchesStr}]");
Assert.True(fwdDisp >= 5.0f, $"{spec.Id} Sion slope W 2.0s: fwdDisp={fwdDisp:F2}m < 5.0m");
// 2. Turn cleanly on Sion slope (D for 1.5s)
float prevYaw = player.Yaw;
float accumYaw = 0f;
for (int t = 0; t < 90; t++)
{
session.Step(new PlayerInput(0f, 1f, player.X, player.Z + 50f, false, false, false));
accumYaw += MatchRules.NormalizeAngle(player.Yaw - prevYaw);
prevYaw = player.Yaw;
}
Assert.True(-accumYaw >= 0.35f, $"{spec.Id} Sion slope D 1.5s: yawChange={accumYaw:F3} rad");
// 3. Release controls on slope (3.0s): must stop cleanly and NOT slide away
float speedAt1_2s = 0f;
float timeToStop = -1f;
Vector2 finalWinStart = Vector2.Zero;
for (int t = 0; t < 180; t++)
{
session.Step(new PlayerInput(0f, 0f, player.X, player.Z + 50f, false, false, false));
if (timeToStop < 0f && player.Speed < 0.35f) timeToStop = (t + 1) * ArenaSession.FixedDt;
if (t == 71) speedAt1_2s = player.Speed;
if (t == 119) finalWinStart = new Vector2(player.X, player.Z);
}
float finalSlide = (new Vector2(player.X, player.Z) - finalWinStart).Length();
_output.WriteLine(
$"{spec.Id,-10} Sion slope ({slopeX:F1},{slopeZ:F1}): fwd={fwdDisp:F2}m turn={-accumYaw:F2}rad " +
$"tStop={timeToStop:F2}s spd@1.2s={speedAt1_2s:F3}m/s slopeSlide={finalSlide:F4}m");
Assert.True(timeToStop > 0f && timeToStop <= 2.0f, $"{spec.Id} Sion slope release: tStop={timeToStop:F2}s > 2.0s (spd@1.2s={speedAt1_2s:F3}m/s)");
Assert.True(finalSlide < 0.18f, $"{spec.Id} Sion slope release: 1.0s slide={finalSlide:F4}m >= 0.18m");
}
}
}
}