diff --git a/src/RustToDust.Core/GridChassis.cs b/src/RustToDust.Core/GridChassis.cs index a48bea4..ee88072 100644 --- a/src/RustToDust.Core/GridChassis.cs +++ b/src/RustToDust.Core/GridChassis.cs @@ -104,6 +104,10 @@ namespace RustToDust.Core float HalfLength, int CargoCapacity) { + public bool IsWalker => + Plane == GridPlane.Coronal || + string.Equals(Id, "manticore", StringComparison.OrdinalIgnoreCase); + public bool IsActive(int x, int y) => x >= 0 && x < Width && y >= 0 && y < Height && Mask[y * Width + x]; } diff --git a/src/RustToDust.Ymir/ArenaSession.cs b/src/RustToDust.Ymir/ArenaSession.cs index 607e462..879694c 100644 --- a/src/RustToDust.Ymir/ArenaSession.cs +++ b/src/RustToDust.Ymir/ArenaSession.cs @@ -554,20 +554,21 @@ namespace RustToDust.Ymir Vector3 supportTorque, float terrainBlend) { - bool isTallRig = spec.Plane == GridPlane.Coronal || spec.Id == "manticore"; - float blend = isTallRig ? MathF.Min(terrainBlend, 0.45f) : terrainBlend; + bool isWalker = spec.IsWalker; + float blend = isWalker ? MathF.Min(terrainBlend, 0.30f) : terrainBlend; Vector3 desiredUp = Vector3.Normalize(Vector3.Lerp(Vector3.UnitY, terrainNormal, blend)); - if (isTallRig) + float maxDesiredTiltRad = isWalker + ? MathF.PI / 15f // 12 degrees target cap so dynamic overshoot stays <= 15-20 deg + : MathF.PI * 38f / 180f; // 38 degrees max slope alignment so cliff walls don't back-flip vehicles { - const float maxTiltRad = MathF.PI / 12f; // 15 degrees max tilt for bipeds/quadrupeds float dotY = Math.Clamp(desiredUp.Y, -1f, 1f); - if (MathF.Acos(dotY) > maxTiltRad) + if (MathF.Acos(dotY) > maxDesiredTiltRad) { Vector3 horiz = new Vector3(desiredUp.X, 0f, desiredUp.Z); float hLen = horiz.Length(); desiredUp = hLen > 1e-4f - ? Vector3.UnitY * MathF.Cos(maxTiltRad) + (horiz / hLen) * MathF.Sin(maxTiltRad) + ? Vector3.UnitY * MathF.Cos(maxDesiredTiltRad) + (horiz / hLen) * MathF.Sin(maxDesiredTiltRad) : Vector3.UnitY; } } @@ -587,8 +588,37 @@ namespace RustToDust.Ymir tiltAxis = recoveryDir * MathF.Max(axisLen, 1.0f); } - float rightingGain = isTallRig ? 28f : 16f; - return supportTorque + tiltAxis * (mass * rightingGain) - new Vector3(angVel.X, 0f, angVel.Z) * (mass * 6.5f); + float maxFoot = MathF.Max(spec.HalfWidth, spec.HalfLength); + float iTilt = MathF.Max(1f, (spec.HalfHeight * spec.HalfHeight + maxFoot * maxFoot) / 3f); + float rightingGain = (isWalker ? 48f : 28f) * iTilt; + float dampingGain = (isWalker ? 11f : 8.5f) * iTilt; + + float tiltLimitRad = isWalker ? MathF.PI / 12f : MathF.PI * 48f / 180f; + float worldTiltRad = MathF.Acos(Math.Clamp(up.Y, -1f, 1f)); + Vector3 arrestTorque = Vector3.Zero; + if (worldTiltRad > tiltLimitRad) + { + Vector3 toUpright = Vector3.Cross(up, Vector3.UnitY); + if (toUpright.LengthSquared() > 1e-6f) + { + Vector3 uprightDir = Vector3.Normalize(toUpright); + float supOut = Vector3.Dot(supportTorque, uprightDir); + if (supOut < 0f) + { + supportTorque -= uprightDir * supOut; + } + + float angOut = Vector3.Dot(new Vector3(angVel.X, 0f, angVel.Z), uprightDir); + if (angOut < 0f) + { + arrestTorque = uprightDir * (-angOut * mass * iTilt * (isWalker ? 28f : 18f)); + } + } + + rightingGain *= 1f + (worldTiltRad - tiltLimitRad) * (isWalker ? 6f : 4f); + } + + return supportTorque + arrestTorque + tiltAxis * (mass * rightingGain) - new Vector3(angVel.X, 0f, angVel.Z) * (mass * dampingGain); } private void ComputeTerrainSupport( @@ -621,7 +651,7 @@ namespace RustToDust.Ymir const float restTravel = 0.20f; float kSpring = mass * 85f; float cDamp = mass * 10.5f; - float torqueScale = spec.HalfHeight > spec.HalfLength ? 0.18f : 1.0f; + float torqueScale = spec.IsWalker ? 0.12f : 1.0f; float maxPen = 0f; Span corners = stackalloc Vector3[4] @@ -664,30 +694,22 @@ namespace RustToDust.Ymir supportForce += supportDir * fCenter; } - // Cap total spring + bump-stop vertical acceleration at <= 65 m/s^2 (and stop adding upward lift once vel.Y > 3.5 m/s) - float maxSpringY = vel.Y > 3.5f ? mass * 20f : mass * 65f; + // Base lift attenuation on normal separation velocity vDotN rather than world vel.Y so steep uphill + // climbs (vDotN <= 0) retain full support while separating crests (vDotN > 2.0) don't sky-launch. + float vDotN = Vector3.Dot(vel, terrainNormal); + float sepScale = vDotN > 2.0f ? Math.Clamp(1f - (vDotN - 2.0f) / 2.0f, 0f, 1f) : 1f; + float maxSpringY = vDotN > 2.0f ? mass * 20f * sepScale : mass * 75f; if (supportForce.Y > maxSpringY && supportForce.Y > 1e-4f) { supportForce *= maxSpringY / supportForce.Y; } - // Steep-slope velocity projection / deflection: when contacting terrain and moving into the surface, - // damp inward normal velocity cleanly while never converting horizontal speed into an upward sky launch. - if (maxPen > 0f) + // Steep-slope normal velocity projection: when contacting terrain and moving inward (vDotN < 0), + // arrest inward normal velocity up to vDotN = 0 regardless of upward world vel.Y. + if (maxPen > 0f && vDotN < 0f) { - float vn = Vector3.Dot(vel, terrainNormal); - if (vn < 0f) - { - float deflectMag = MathF.Min(-vn * mass * 42f, -vn * mass / FixedDt); - Vector3 deflectForce = terrainNormal * deflectMag; - // Allow vertical deflection above 65 m/s^2 ONLY to arrest actual downward velocity (vel.Y < 0). - float maxDeflectY = MathF.Max(0f, maxSpringY - supportForce.Y) + MathF.Max(0f, (-vel.Y * mass) / FixedDt); - if (deflectForce.Y > maxDeflectY) - { - deflectForce.Y = maxDeflectY; - } - supportForce += deflectForce; - } + float deflectMag = MathF.Min(-vDotN * mass * 42f, -vDotN * mass / FixedDt); + supportForce += terrainNormal * deflectMag; } } @@ -1146,7 +1168,13 @@ namespace RustToDust.Ymir if (shooterContender is not null) { - bool newlyProvoked = State.Spectators.OnSpectatorDamaged(spec, shooterContender.Id); + float inDx = shooterContender.X - hitPoint.X; + float inDz = shooterContender.Z - hitPoint.Z; + float inLen = MathF.Sqrt(inDx * inDx + inDz * inDz); + float? inDirX = inLen > 1e-3f ? inDx / inLen : null; + float? inDirZ = inLen > 1e-3f ? inDz / inLen : null; + + bool newlyProvoked = State.Spectators.OnSpectatorDamaged(spec, shooterContender.Id, inDirX, inDirZ); if (newlyProvoked) { spec.BroadcastWakeupEmitted = true; diff --git a/tests/RustToDust.Tests/TerrainTests.cs b/tests/RustToDust.Tests/TerrainTests.cs index 9cff564..0e6ec27 100644 --- a/tests/RustToDust.Tests/TerrainTests.cs +++ b/tests/RustToDust.Tests/TerrainTests.cs @@ -261,65 +261,108 @@ namespace RustToDust.Tests [Theory] [InlineData(42)] [InlineData(101)] - public void SteepMesaAndCanyonTraversal_BuggyAndPaladin_NoTunnelingSkyLaunchOrOverturning(int seed) + public void SteepMesaAndDuneTraversal_AllSixChassis_NoTunnelingSkyLaunchOrOverturning(int seed) { - RunSteepTerrainStress(seed, usePaladin: false); - RunSteepTerrainStress(seed, usePaladin: true); + foreach (var chassis in Catalog.AllChassis) + { + RunSteepTerrainStress(seed, chassis, testDune: false); + RunSteepTerrainStress(seed, chassis, testDune: true); + } } - private static void RunSteepTerrainStress(int seed, bool usePaladin) + private static void RunSteepTerrainStress(int seed, ChassisSpec chassis, bool testDune) { using var session = new ArenaSession(seed); var player = session.State.Player; - TerrainBrush mesa = default; - bool foundMesa = false; + 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]; - if (b.Kind == TerrainBrushKind.Mesa && b.HeightDelta >= 22f) + bool matches = testDune + ? b.Kind == TerrainBrushKind.DuneRidge && b.HeightDelta >= 14f + : b.Kind == TerrainBrushKind.Mesa && b.HeightDelta >= 22f; + if (!matches) { - mesa = b; - foundMesa = true; - break; + 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(foundMesa, $"Expected at least one tall mesa in seed {seed}"); + Assert.True(bestScore >= 10f, $"Expected a climbable {(testDune ? "dune" : "mesa")} approach inside safe circle 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) + // 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.State.SpawnChassisDrop(player.X, player.Z, Catalog.Paladin); - Assert.True(session.TrySwapNearbyChassis()); - Assert.Equal(GridPlane.Coronal, player.Grid.Spec.Plane); + 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.2f; + float steer = MathF.Sin(tick * 0.03f) * 0.15f; session.Step(new PlayerInput( Throttle: 1f, Steer: steer, - AimWorldX: mesa.X, - AimWorldZ: mesa.Z, + 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}, paladin={usePaladin})"); + $"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}, paladin={usePaladin})"); + 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; @@ -330,17 +373,24 @@ namespace RustToDust.Tests 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); } - 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}"); + 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})"); } } }