using System; using System.Collections.Generic; using System.Numerics; using RustToDust.Client; using RustToDust.Core; using Xunit; namespace RustToDust.Tests; public sealed class CameraJuiceTests { [Fact] public void CameraJuiceState_ZeroIdleJitter_AndMonotonicDecayToExactZero() { var juice = new CameraJuiceState(); Assert.True(juice.IsIdle); for (int i = 0; i < 60; i++) { juice.Update(1f / 60f, localSpeedMps: 5f, isVtol: false); juice.ComputeOffsets(out Vector3 off, out float pitch, out float yaw, out float roll); Assert.True(juice.IsIdle); Assert.Equal(Vector3.Zero, off); Assert.Equal(0f, pitch); Assert.Equal(0f, yaw); Assert.Equal(0f, roll); Assert.Equal(0f, juice.FovOffsetDeg); } // Fire local recoilless shot var events = new List { new ShotFiredEvent { TracerId = 1, IsSpectatorShooter = false, ShooterId = 0, WeaponClass = (byte)WeaponVisualClass.Explosive, WeaponId = "recoilless" } }; juice.ConsumeEvents(events, localContenderId: 0, Vector3.Zero); Assert.False(juice.IsIdle); Assert.True(juice.RecoilPitchRad > 0f); Assert.True(juice.RecoilBackMeters > 0f); Assert.True(juice.Trauma > 0f); juice.ComputeOffsets(out Vector3 kickOff, out float kickPitch, out _, out _); Assert.True(kickOff.Z > 0f); Assert.True(kickPitch > 0f); // Decays monotonically back to exact zero float prevPitch = juice.RecoilPitchRad; for (int i = 0; i < 120; i++) { juice.Update(1f / 60f, localSpeedMps: 0f, isVtol: false); Assert.True(juice.RecoilPitchRad <= prevPitch); prevPitch = juice.RecoilPitchRad; } Assert.True(juice.IsIdle); juice.ComputeOffsets(out Vector3 settledOff, out float settledPitch, out float settledYaw, out float settledRoll); Assert.Equal(Vector3.Zero, settledOff); Assert.Equal(0f, settledPitch); Assert.Equal(0f, settledYaw); Assert.Equal(0f, settledRoll); } [Fact] public void CameraJuiceState_IgnoresRemoteShots_ClampsTrauma_AndResetsCleanly() { var juice = new CameraJuiceState(); // Remote contender or spectator shot must NOT kick local camera var remoteEvents = new List { new ShotFiredEvent { TracerId = 10, IsSpectatorShooter = false, ShooterId = 3, WeaponClass = (byte)WeaponVisualClass.Explosive, WeaponId = "recoilless" }, new ShotFiredEvent { TracerId = 11, IsSpectatorShooter = true, ShooterId = 0, WeaponClass = (byte)WeaponVisualClass.Explosive, WeaponId = "recoilless" } }; juice.ConsumeEvents(remoteEvents, localContenderId: 0, new Vector3(100f, 0f, 100f)); Assert.True(juice.IsIdle); // Clamp trauma to [0, 1] juice.AddTrauma(5.0f); Assert.Equal(1.0f, juice.Trauma); // Footstep bump triggers only for local contender var steps = new List { new(true, 2, 0, "bastion", 4600f, 1.2f, Vector3.Zero), new(true, 0, 0, "bastion", 4600f, 1.2f, Vector3.Zero) }; juice.ConsumeFootsteps(steps, localContenderId: 0); Assert.True(juice.FootstepBumpY < 0f); // Reset zeroes everything immediately juice.Reset(); Assert.True(juice.IsIdle); Assert.Equal(0f, juice.Trauma); Assert.Equal(0f, juice.FootstepBumpY); Assert.Equal(0f, juice.FovOffsetDeg); } [Fact] public void CameraJuiceState_DeathTrauma_RequiresVisibleAndWithin90m() { var juice = new CameraJuiceState(); var localPos = Vector3.Zero; // 1. Out-of-PVS (not visible) contender death even nearby -> 0.0f trauma var hiddenDeath = new ISimEvent[] { new ContenderEliminatedEvent { VictimId = 3, KillerId = 1, IsSpectatorKiller = false } }; juice.ConsumeEvents(hiddenDeath, localContenderId: 0, localPos, (_, _) => (false, new Vector3(15f, 0f, 0f))); Assert.Equal(0.0f, juice.Trauma); // 2. Visible but distant (>= 90m) contender death -> 0.0f trauma juice.ConsumeEvents(hiddenDeath, localContenderId: 0, localPos, (_, _) => (true, new Vector3(120f, 0f, 0f))); Assert.Equal(0.0f, juice.Trauma); // 3. No resolver/session -> 0.0f trauma (never global) juice.ConsumeEvents(hiddenDeath, localContenderId: 0, localPos); Assert.Equal(0.0f, juice.Trauma); // 4. Nearby visible death (20m) -> positive scaled trauma: 0.14 * (1 - 20 / 90) juice.ConsumeEvents(hiddenDeath, localContenderId: 0, localPos, (_, _) => (true, new Vector3(20f, 0f, 0f))); float expected = 0.14f * (1f - 20f / 90f); Assert.InRange(juice.Trauma, expected - 1e-4f, expected + 1e-4f); } [Fact] public void DropPodFov_WidensTo82DegAndEasesTo58DegOnTouchdown() { var juice = new CameraJuiceState(); const float dt = 1f / 60f; // 1. Freefall at max cross-zone glide speed (124 m/s) -> widens to 82..84 deg smoothly for (int i = 0; i < 120; i++) { juice.Update(dt, localSpeedMps: 124f, isVtol: false, isInDropPod: true, podPhase: OrbitalPodPhase.Freefall); } float freefallFov = CameraJuiceState.BaseFovDeg + juice.FovOffsetDeg; Assert.InRange(freefallFov, 80.0f, 85.0f); // 2. RetroBurn -> tightens to ~76 deg for (int i = 0; i < 90; i++) { juice.Update(dt, localSpeedMps: 15f, isVtol: false, isInDropPod: true, podPhase: OrbitalPodPhase.RetroBurn); } float retroFov = CameraJuiceState.BaseFovDeg + juice.FovOffsetDeg; Assert.InRange(retroFov, 74.5f, 77.5f); // 3. Touchdown ground release -> eases monotonically back toward 58 deg within ~1.5s float prevFov = retroFov; for (int i = 0; i < 90; i++) { juice.Update(dt, localSpeedMps: 0f, isVtol: false, isInDropPod: false); float curFov = CameraJuiceState.BaseFovDeg + juice.FovOffsetDeg; Assert.True(curFov <= prevFov + 1e-4f, $"Expected monotonic FOV release, jumped {prevFov:F3} -> {curFov:F3}"); Assert.True(prevFov - curFov < 2.0f, $"Expected smooth FOV release without discontinuity: delta={prevFov - curFov:F2}"); prevFov = curFov; } Assert.InRange(prevFov, 57.9f, 59.0f); } }