using System; using System.Collections.Generic; using System.Numerics; using RustToDust.Core; using RustToDust.Ymir; namespace RustToDust.Client; public readonly record struct FootstepPulse( bool IsContender, int EntityId, int LegIndex, string ChassisId, float MassKg, float LoadG, Vector3 WorldPos); /// /// Tracks per-walker leg GRF hysteresis (LoadG arming < 0.10, touchdown >= 0.24, /// 0.18 s cooldown) from interpolated s across both Solo and /// Multiplayer (). Zero Godot dependency so xUnit and headless /// Godot tests run the exact same edge detector. /// public sealed class WalkerFootstepTracker { public const float ArmLoadGThreshold = 0.10f; public const float TriggerLoadGThreshold = 0.24f; public const float LegCooldownSec = 0.18f; private struct LegHysteresis { public bool Armed; public float Cooldown; } private sealed class WalkerTrackState { public string ChassisId = string.Empty; public int LegCount; public readonly LegHysteresis[] Legs = new LegHysteresis[4]; } private readonly Dictionary<(bool IsContender, int Id), WalkerTrackState> _walkers = new(); public int TrackedWalkerCount => _walkers.Count; public void Clear() => _walkers.Clear(); public void StepSession( IClientArenaSession session, float dt, float alpha, List outPulses, Func? resolveLiveFootWorld = null) { var state = session.ViewState; var contenders = state.Contenders; for (int i = 0; i < contenders.Count; i++) { var c = contenders[i]; if (!c.Alive || !c.Grid.Spec.IsWalker || !session.IsContenderVisible(c.Id)) { _walkers.Remove((true, c.Id)); continue; } if (!session.TrySampleInterpolatedWalkerPose(true, c.Id, alpha, out var snap)) { continue; } StepActor( isContender: true, entityId: c.Id, chassisId: c.Grid.Spec.Id, massKg: c.Grid.Spec.MassKg, in snap, state.Terrain, dt, outPulses, resolveLiveFootWorld); } var specs = state.Spectators.Spectators; for (int i = 0; i < specs.Count; i++) { var s = specs[i]; if (!s.Alive || !s.Grid.Spec.IsWalker || !session.IsSpectatorVisible(s.Id)) { _walkers.Remove((false, s.Id)); continue; } if (!session.TrySampleInterpolatedWalkerPose(false, s.Id, alpha, out var snap)) { continue; } StepActor( isContender: false, entityId: s.Id, chassisId: s.Grid.Spec.Id, massKg: s.Grid.Spec.MassKg, in snap, state.Terrain, dt, outPulses, resolveLiveFootWorld); } } public void StepActor( bool isContender, int entityId, string chassisId, float massKg, in WalkerVisualSnapshot snap, TerrainField terrain, float dt, List outPulses, Func? resolveLiveFootWorld = null) { int legCount = Math.Clamp(snap.LegCount, 0, 4); if (legCount == 0) { return; } var key = (isContender, entityId); if (!_walkers.TryGetValue(key, out var track) || !string.Equals(track.ChassisId, chassisId, StringComparison.OrdinalIgnoreCase) || track.LegCount != legCount) { track = new WalkerTrackState { ChassisId = chassisId, LegCount = legCount }; // First sighting initializes Armed = false for zero spawn / swap pop _walkers[key] = track; return; } float stepDt = MathF.Max(0f, dt); WalkerGeometrySpec? geom = null; float geomScale = snap.GeomScale > 0.01f ? snap.GeomScale : 1f; Quaternion comRot = snap.ComRot.LengthSquared() > 1e-6f ? Quaternion.Normalize(snap.ComRot) : Quaternion.Identity; for (int i = 0; i < legCount; i++) { ref LegHysteresis leg = ref track.Legs[i]; if (leg.Cooldown > 0f) { leg.Cooldown = MathF.Max(0f, leg.Cooldown - stepDt); } float loadG = snap.LoadG[i]; if (loadG < ArmLoadGThreshold) { leg.Armed = true; continue; } if (leg.Armed && loadG >= TriggerLoadGThreshold && leg.Cooldown <= 0f) { leg.Armed = false; leg.Cooldown = LegCooldownSec; Vector3? liveFoot = resolveLiveFootWorld?.Invoke(isContender, entityId, i); Vector3 footWorld; if (liveFoot.HasValue) { float fy = terrain.SampleHeight(liveFoot.Value.X, liveFoot.Value.Z); footWorld = new Vector3(liveFoot.Value.X, fy, liveFoot.Value.Z); } else { geom ??= WalkerGeometrySpec.GetForChassis(chassisId); Vector2 restXZ = geom.Legs[i].RestFootXZ * geomScale; Vector3 offset = Vector3.Transform(new Vector3(restXZ.X, 0f, restXZ.Y), comRot); float fx = snap.ComPos.X + offset.X; float fz = snap.ComPos.Z + offset.Z; float fy = terrain.SampleHeight(fx, fz); footWorld = new Vector3(fx, fy, fz); } outPulses.Add(new FootstepPulse( isContender, entityId, i, chassisId, massKg, loadG, footWorld)); } } } }