2026-12-05 10:14:54 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
using RustToDust.Core;
|
|
|
|
|
|
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Display-only post-anchor camera juice (recoil kick, quadratic trauma micro-shake, footstep thump,
|
|
|
|
|
/// and speed/VTOL FOV punch). Zero simulation authority and zero Godot dependency so unit tests and
|
|
|
|
|
/// headless smoke checks exercise the exact same state machine.
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Invariant: when <see cref="IsIdle"/> is <c>true</c>, <see cref="ComputeOffsets"/> outputs exact
|
|
|
|
|
/// <see cref="Vector3.Zero"/> and <c>0f</c> angles (strictly zero idle camera jitter).
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class CameraJuiceState
|
|
|
|
|
{
|
|
|
|
|
public const float BaseFovDeg = 58.0f;
|
|
|
|
|
private const float ZeroEpsilon = 1e-4f;
|
|
|
|
|
private const float TraumaDecayPerSec = 1.95f;
|
|
|
|
|
private const float RecoilDecayRate = 14.0f;
|
|
|
|
|
private const float FootstepDecayRate = 16.0f;
|
|
|
|
|
private const float FovBlendRate = 6.0f;
|
|
|
|
|
|
|
|
|
|
private float _time;
|
|
|
|
|
|
|
|
|
|
public float Trauma { get; private set; }
|
|
|
|
|
public float RecoilPitchRad { get; private set; }
|
|
|
|
|
public float RecoilBackMeters { get; private set; }
|
|
|
|
|
public float FootstepBumpY { get; private set; }
|
|
|
|
|
public float FovOffsetDeg { get; private set; }
|
|
|
|
|
|
|
|
|
|
public bool IsIdle =>
|
|
|
|
|
Trauma <= 0f &&
|
|
|
|
|
RecoilPitchRad == 0f &&
|
|
|
|
|
RecoilBackMeters == 0f &&
|
|
|
|
|
FootstepBumpY == 0f;
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
Trauma = 0f;
|
|
|
|
|
RecoilPitchRad = 0f;
|
|
|
|
|
RecoilBackMeters = 0f;
|
|
|
|
|
FootstepBumpY = 0f;
|
|
|
|
|
FovOffsetDeg = 0f;
|
|
|
|
|
_time = 0f;
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-05 10:48:54 +01:00
|
|
|
public void ConsumeEvents(
|
|
|
|
|
IReadOnlyList<ISimEvent> events,
|
|
|
|
|
int localContenderId,
|
|
|
|
|
Vector3 localPos,
|
|
|
|
|
IClientArenaSession? session = null)
|
|
|
|
|
{
|
|
|
|
|
ConsumeEvents(events, localContenderId, localPos, session is null ? null : (isContender, id) =>
|
|
|
|
|
{
|
|
|
|
|
if (isContender)
|
|
|
|
|
{
|
|
|
|
|
var contenders = session.ViewState.Contenders;
|
|
|
|
|
if (id >= 0 && id < contenders.Count && (id == session.LocalContenderId || session.IsContenderVisible(id)))
|
|
|
|
|
{
|
|
|
|
|
var c = contenders[id];
|
|
|
|
|
return (true, new Vector3(c.X, c.Y, c.Z));
|
|
|
|
|
}
|
|
|
|
|
return (false, Vector3.Zero);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (session.IsSpectatorVisible(id))
|
|
|
|
|
{
|
|
|
|
|
var specs = session.ViewState.Spectators.Spectators;
|
|
|
|
|
for (int s = 0; s < specs.Count; s++)
|
|
|
|
|
{
|
|
|
|
|
if (specs[s].Id == id)
|
|
|
|
|
{
|
|
|
|
|
var sp = specs[s];
|
|
|
|
|
return (true, new Vector3(sp.X, sp.Y, sp.Z));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int sIdx = id >= 1000 ? id - 1000 : id;
|
|
|
|
|
if (sIdx >= 0 && sIdx < specs.Count)
|
|
|
|
|
{
|
|
|
|
|
var sp = specs[sIdx];
|
|
|
|
|
return (true, new Vector3(sp.X, sp.Y, sp.Z));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (false, Vector3.Zero);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConsumeEvents(
|
|
|
|
|
IReadOnlyList<ISimEvent> events,
|
|
|
|
|
int localContenderId,
|
|
|
|
|
Vector3 localPos,
|
|
|
|
|
Func<bool, int, (bool Visible, Vector3 Pos)>? resolveActor)
|
2026-12-05 10:14:54 +01:00
|
|
|
{
|
|
|
|
|
for (int i = 0; i < events.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
switch (events[i])
|
|
|
|
|
{
|
|
|
|
|
case MatchResetEvent:
|
|
|
|
|
Reset();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ShotFiredEvent shot:
|
|
|
|
|
if (!shot.IsSpectatorShooter && shot.ShooterId == localContenderId)
|
|
|
|
|
{
|
|
|
|
|
ApplyWeaponRecoil(shot.WeaponId, (WeaponVisualClass)shot.WeaponClass);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case HitImpactEvent hit:
|
|
|
|
|
{
|
|
|
|
|
if (hit.TargetKind == (byte)HitTargetKind.Contender && hit.TargetId == localContenderId)
|
|
|
|
|
{
|
|
|
|
|
float incomingTrauma = (WeaponVisualClass)hit.WeaponClass switch
|
|
|
|
|
{
|
|
|
|
|
WeaponVisualClass.Explosive => 0.34f,
|
|
|
|
|
WeaponVisualClass.Autocannon => 0.22f,
|
|
|
|
|
WeaponVisualClass.ThermalBeam => 0.20f,
|
|
|
|
|
_ => 0.10f
|
|
|
|
|
};
|
|
|
|
|
AddTrauma(incomingTrauma);
|
|
|
|
|
}
|
|
|
|
|
else if ((WeaponVisualClass)hit.WeaponClass == WeaponVisualClass.Explosive)
|
|
|
|
|
{
|
|
|
|
|
float dx = hit.HitX - localPos.X;
|
|
|
|
|
float dy = hit.HitY - localPos.Y;
|
|
|
|
|
float dz = hit.HitZ - localPos.Z;
|
|
|
|
|
float distSq = dx * dx + dy * dy + dz * dz;
|
|
|
|
|
if (distSq < 36f * 36f)
|
|
|
|
|
{
|
|
|
|
|
float falloff = 1f - MathF.Sqrt(distSq) / 36f;
|
|
|
|
|
AddTrauma(0.24f * falloff);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-05 10:48:54 +01:00
|
|
|
case ContenderEliminatedEvent elim:
|
|
|
|
|
ApplyDeathTrauma(isContender: true, elim.VictimId, localPos, resolveActor);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SpectatorDestroyedEvent sdet:
|
|
|
|
|
ApplyDeathTrauma(isContender: false, sdet.SpectatorId, localPos, resolveActor);
|
2026-12-05 10:14:54 +01:00
|
|
|
break;
|
2026-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
|
case DropPodEvent dpe:
|
|
|
|
|
{
|
|
|
|
|
var kind = (DropPodEventKind)dpe.Kind;
|
|
|
|
|
if (dpe.ContenderId == localContenderId)
|
|
|
|
|
{
|
|
|
|
|
if (kind == DropPodEventKind.PodRetroBurnStart)
|
|
|
|
|
{
|
|
|
|
|
AddTrauma(0.18f);
|
|
|
|
|
}
|
|
|
|
|
else if (kind == DropPodEventKind.PodTouchdownBlowPetals)
|
|
|
|
|
{
|
|
|
|
|
AddTrauma(0.36f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (kind == DropPodEventKind.PodTouchdownBlowPetals)
|
|
|
|
|
{
|
|
|
|
|
float dx = dpe.X - localPos.X;
|
|
|
|
|
float dy = dpe.Y - localPos.Y;
|
|
|
|
|
float dz = dpe.Z - localPos.Z;
|
|
|
|
|
float distSq = dx * dx + dy * dy + dz * dz;
|
|
|
|
|
if (distSq < 48f * 48f)
|
|
|
|
|
{
|
|
|
|
|
float falloff = 1f - MathF.Sqrt(distSq) / 48f;
|
|
|
|
|
AddTrauma(0.18f * falloff);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ApplyOrbitalBurnRumble(float burnIntensity)
|
|
|
|
|
{
|
|
|
|
|
if (float.IsFinite(burnIntensity) && burnIntensity > 0f)
|
|
|
|
|
{
|
|
|
|
|
float target = Math.Clamp(burnIntensity, 0f, 1f) * 0.20f;
|
|
|
|
|
if (Trauma < target)
|
|
|
|
|
{
|
|
|
|
|
Trauma = target;
|
2026-12-05 10:14:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-05 10:48:54 +01:00
|
|
|
private void ApplyDeathTrauma(
|
|
|
|
|
bool isContender,
|
|
|
|
|
int entityId,
|
|
|
|
|
Vector3 localPos,
|
|
|
|
|
Func<bool, int, (bool Visible, Vector3 Pos)>? resolveActor)
|
|
|
|
|
{
|
|
|
|
|
if (resolveActor is null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var (visible, pos) = resolveActor(isContender, entityId);
|
|
|
|
|
if (!visible)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
float dist = Vector3.Distance(pos, localPos);
|
|
|
|
|
if (dist < 90f)
|
|
|
|
|
{
|
|
|
|
|
AddTrauma(0.14f * (1f - dist / 90f));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-05 10:14:54 +01:00
|
|
|
public void ConsumeFootsteps(IReadOnlyList<FootstepPulse> footsteps, int localContenderId)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < footsteps.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
FootstepPulse pulse = footsteps[i];
|
|
|
|
|
if (pulse.IsContender && pulse.EntityId == localContenderId)
|
|
|
|
|
{
|
|
|
|
|
float massFactor = Math.Clamp(pulse.MassKg / 3200f, 0.55f, 1.55f);
|
|
|
|
|
float loadFactor = Math.Clamp(pulse.LoadG, 0.5f, 1.8f);
|
|
|
|
|
float bump = -0.065f * massFactor * loadFactor;
|
|
|
|
|
FootstepBumpY = Math.Clamp(FootstepBumpY + bump, -0.18f, 0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddTrauma(float amount)
|
|
|
|
|
{
|
|
|
|
|
if (float.IsFinite(amount) && amount > 0f)
|
|
|
|
|
{
|
|
|
|
|
Trauma = Math.Clamp(Trauma + amount, 0f, 1.0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(float dt, float localSpeedMps, bool isVtol)
|
|
|
|
|
{
|
|
|
|
|
float stepDt = float.IsFinite(dt) && dt > 0f ? Math.Clamp(dt, 0f, 0.1f) : 0f;
|
|
|
|
|
if (stepDt > 0f)
|
|
|
|
|
{
|
|
|
|
|
_time += stepDt;
|
|
|
|
|
|
|
|
|
|
Trauma = MathF.Max(0f, Trauma - stepDt * TraumaDecayPerSec);
|
|
|
|
|
if (Trauma < ZeroEpsilon) Trauma = 0f;
|
|
|
|
|
|
|
|
|
|
float recoilDecay = MathF.Exp(-RecoilDecayRate * stepDt);
|
|
|
|
|
RecoilPitchRad *= recoilDecay;
|
|
|
|
|
RecoilBackMeters *= recoilDecay;
|
|
|
|
|
if (MathF.Abs(RecoilPitchRad) < ZeroEpsilon) RecoilPitchRad = 0f;
|
|
|
|
|
if (MathF.Abs(RecoilBackMeters) < ZeroEpsilon) RecoilBackMeters = 0f;
|
|
|
|
|
|
|
|
|
|
float stepDecay = MathF.Exp(-FootstepDecayRate * stepDt);
|
|
|
|
|
FootstepBumpY *= stepDecay;
|
|
|
|
|
if (MathF.Abs(FootstepBumpY) < ZeroEpsilon) FootstepBumpY = 0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float speed = float.IsFinite(localSpeedMps) ? MathF.Max(0f, localSpeedMps) : 0f;
|
|
|
|
|
float speedStart = isVtol ? 14f : 12f;
|
|
|
|
|
float speedFull = isVtol ? 46f : 36f;
|
|
|
|
|
float targetFovOffset = speed > speedStart
|
|
|
|
|
? Math.Clamp((speed - speedStart) / (speedFull - speedStart), 0f, 1f) * (isVtol ? 4.8f : 3.4f)
|
|
|
|
|
: 0f;
|
|
|
|
|
|
|
|
|
|
if (stepDt > 0f)
|
|
|
|
|
{
|
|
|
|
|
float blend = 1f - MathF.Exp(-FovBlendRate * stepDt);
|
|
|
|
|
FovOffsetDeg += (targetFovOffset - FovOffsetDeg) * blend;
|
|
|
|
|
if (MathF.Abs(FovOffsetDeg) < 0.002f && targetFovOffset == 0f)
|
|
|
|
|
{
|
|
|
|
|
FovOffsetDeg = 0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ComputeOffsets(
|
|
|
|
|
out Vector3 localOffset,
|
|
|
|
|
out float pitchOffsetRad,
|
|
|
|
|
out float yawOffsetRad,
|
|
|
|
|
out float rollOffsetRad)
|
|
|
|
|
{
|
|
|
|
|
if (IsIdle)
|
|
|
|
|
{
|
|
|
|
|
localOffset = Vector3.Zero;
|
|
|
|
|
pitchOffsetRad = 0f;
|
|
|
|
|
yawOffsetRad = 0f;
|
|
|
|
|
rollOffsetRad = 0f;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float shake = Trauma * Trauma;
|
|
|
|
|
float sx = MathF.Sin(_time * 43.0f) * 0.65f + MathF.Sin(_time * 71.0f) * 0.35f;
|
|
|
|
|
float sy = MathF.Cos(_time * 37.0f) * 0.65f + MathF.Sin(_time * 63.0f) * 0.35f;
|
|
|
|
|
float sr = MathF.Sin(_time * 29.0f + 1.1f);
|
|
|
|
|
|
|
|
|
|
localOffset = new Vector3(
|
|
|
|
|
sx * shake * 0.14f,
|
|
|
|
|
sy * shake * 0.11f + FootstepBumpY,
|
|
|
|
|
RecoilBackMeters);
|
|
|
|
|
|
|
|
|
|
pitchOffsetRad = RecoilPitchRad + sy * shake * 0.015f;
|
|
|
|
|
yawOffsetRad = sx * shake * 0.012f;
|
|
|
|
|
rollOffsetRad = sr * shake * 0.016f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ApplyWeaponRecoil(string weaponId, WeaponVisualClass weaponClass)
|
|
|
|
|
{
|
|
|
|
|
switch (weaponClass)
|
|
|
|
|
{
|
|
|
|
|
case WeaponVisualClass.Explosive:
|
|
|
|
|
if (string.Equals(weaponId, "recoilless", StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
RecoilPitchRad = MathF.Min(RecoilPitchRad + 0.022f, 0.042f);
|
|
|
|
|
RecoilBackMeters = MathF.Min(RecoilBackMeters + 0.34f, 0.60f);
|
|
|
|
|
AddTrauma(0.32f);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RecoilPitchRad = MathF.Min(RecoilPitchRad + 0.011f, 0.030f);
|
|
|
|
|
RecoilBackMeters = MathF.Min(RecoilBackMeters + 0.16f, 0.40f);
|
|
|
|
|
AddTrauma(0.18f);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WeaponVisualClass.Autocannon:
|
|
|
|
|
RecoilPitchRad = MathF.Min(RecoilPitchRad + 0.010f, 0.026f);
|
|
|
|
|
RecoilBackMeters = MathF.Min(RecoilBackMeters + 0.14f, 0.35f);
|
|
|
|
|
AddTrauma(0.16f);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WeaponVisualClass.ThermalBeam:
|
|
|
|
|
{
|
|
|
|
|
bool isLance = string.Equals(weaponId, "gilded_lance", StringComparison.Ordinal);
|
|
|
|
|
RecoilPitchRad = MathF.Min(RecoilPitchRad + (isLance ? 0.012f : 0.008f), 0.026f);
|
|
|
|
|
RecoilBackMeters = MathF.Min(RecoilBackMeters + (isLance ? 0.16f : 0.10f), 0.35f);
|
|
|
|
|
AddTrauma(isLance ? 0.18f : 0.12f);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: // KineticLight (mg_762, pd_flak)
|
|
|
|
|
RecoilPitchRad = MathF.Min(RecoilPitchRad + 0.0022f, 0.012f);
|
|
|
|
|
RecoilBackMeters = MathF.Min(RecoilBackMeters + 0.030f, 0.15f);
|
|
|
|
|
AddTrauma(0.035f);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|