diff --git a/src/RustToDust.Core/MatchRules.cs b/src/RustToDust.Core/MatchRules.cs index 2981b61..6d982d7 100644 --- a/src/RustToDust.Core/MatchRules.cs +++ b/src/RustToDust.Core/MatchRules.cs @@ -427,6 +427,23 @@ namespace RustToDust.Core } } + public static void AdvanceHitEvents(MatchState state, float dt) + { + for (int i = state.HitEvents.Count - 1; i >= 0; i--) + { + var h = state.HitEvents[i]; + float nextTtl = h.Ttl - dt; + if (nextTtl <= 0f) + { + state.HitEvents.RemoveAt(i); + } + else + { + state.HitEvents[i] = h with { Ttl = nextTtl }; + } + } + } + public static void UpdateMatchOutcome(MatchState state) { if (state.Phase != MatchPhase.Active) diff --git a/src/RustToDust.Core/MatchState.cs b/src/RustToDust.Core/MatchState.cs index 1c2d8cd..38d345e 100644 --- a/src/RustToDust.Core/MatchState.cs +++ b/src/RustToDust.Core/MatchState.cs @@ -21,6 +21,22 @@ namespace RustToDust.Core float AimWorldY = float.NaN, float VerticalThrottle = 0f); + public enum WeaponVisualClass + { + KineticLight, + Autocannon, + Explosive, + ThermalBeam + } + + public enum HitTargetKind + { + Terrain, + StaticCover, + Contender, + Spectator + } + public readonly record struct TracerEvent( float StartX, float StartY, @@ -29,6 +45,28 @@ namespace RustToDust.Core float EndY, float EndZ, uint ColorRgba, + float Ttl, + WeaponVisualClass WeaponClass = WeaponVisualClass.KineticLight, + string WeaponId = ""); + + public readonly record struct HitFeedbackEvent( + long EventId, + float HitX, + float HitY, + float HitZ, + float NormalX, + float NormalY, + float NormalZ, + float SourceX, + float SourceY, + float SourceZ, + HitTargetKind TargetKind, + int TargetId, + int CellX, + int CellY, + WeaponVisualClass WeaponClass, + string WeaponId, + bool IsAirburst, float Ttl); public readonly record struct ArenaBoxSpec( @@ -126,6 +164,7 @@ namespace RustToDust.Core public const float TouristHorizonMaxRange = 420f; private int _nextDropId = 1; + private long _nextHitEventId = 1; public MatchState(int seed = 42) { @@ -153,6 +192,7 @@ namespace RustToDust.Core public SpectatorRing Spectators { get; } public List GroundDrops { get; } = new(); public List Tracers { get; } = new(); + public List HitEvents { get; } = new(); public BroadcastFeed Broadcast { get; } public IReadOnlyList ArenaCoverBoxes { get; } public IReadOnlyList ArenaBoundaryBoxes { get; } @@ -169,6 +209,16 @@ namespace RustToDust.Core public event Action? ChassisSwapped; + public long AllocateHitEventId() => _nextHitEventId++; + + public static WeaponVisualClass ClassifyWeaponVisual(string weaponId) => weaponId switch + { + "autocannon" => WeaponVisualClass.Autocannon, + "flak" or "recoilless" => WeaponVisualClass.Explosive, + "gilded_lance" or "touring_plasma" or "aegis_pd" => WeaponVisualClass.ThermalBeam, + _ => WeaponVisualClass.KineticLight + }; + public static bool IsContactOnRadar(PlayerContactMemory? mem) { if (mem is null) diff --git a/src/RustToDust.Ymir/ArenaSession.cs b/src/RustToDust.Ymir/ArenaSession.cs index db637cd..0da2b8a 100644 --- a/src/RustToDust.Ymir/ArenaSession.cs +++ b/src/RustToDust.Ymir/ArenaSession.cs @@ -203,6 +203,7 @@ namespace RustToDust.Ymir State.Time += FixedDt; MatchRules.AdvanceTracers(State, FixedDt); + MatchRules.AdvanceHitEvents(State, FixedDt); State.Spectators.UpdateSafeRadius(State.Time); State.Spectators.AdvancePerimeterOrbits(FixedDt); @@ -1567,9 +1568,26 @@ namespace RustToDust.Ymir int burst = Math.Max(1, mod.Spec.BurstCount); float totalSpread = mod.Spec.SpreadRad + extraSpread; float range = MathF.Max(5f, mod.Spec.WeaponRange); - uint tracerColor = mod.Spec.IsImperial - ? 0xFFD966FFu - : (mod.Spec.IsWideArc ? 0x66E6FFFFu : 0xFF8833FFu); + WeaponVisualClass weaponClass = MatchState.ClassifyWeaponVisual(mod.Spec.Id); + uint tracerColor = weaponClass switch + { + WeaponVisualClass.ThermalBeam => string.Equals(mod.Spec.Id, "gilded_lance", StringComparison.Ordinal) + ? 0xFFD966FFu + : 0x5CE6FFFFu, + WeaponVisualClass.Explosive => 0xFF6622FFu, + WeaponVisualClass.Autocannon => 0xFF9933FFu, + _ => string.Equals(mod.Spec.Id, "pd_flak", StringComparison.Ordinal) + ? 0xFF8833FFu + : 0xFFE888FFu + }; + float tracerTtl = weaponClass switch + { + WeaponVisualClass.ThermalBeam => 0.13f, + WeaponVisualClass.Explosive => 0.12f, + WeaponVisualClass.Autocannon => 0.10f, + _ => 0.09f + }; + bool isPdFlak = string.Equals(mod.Spec.Id, "pd_flak", StringComparison.Ordinal); for (int b = 0; b < burst; b++) { @@ -1596,6 +1614,26 @@ namespace RustToDust.Ymir { endPt = new Vector3(tx, ty, tz); StampTerrainWeaponImpact(mod.Spec.Id, tx, tz, burstIndex: b); + var (tnx, tny, tnz) = State.Terrain.SampleNormal(tx, tz); + State.HitEvents.Add(new HitFeedbackEvent( + EventId: State.AllocateHitEventId(), + HitX: tx, + HitY: ty, + HitZ: tz, + NormalX: tnx, + NormalY: tny, + NormalZ: tnz, + SourceX: shooterPos.X, + SourceY: shooterPos.Y, + SourceZ: shooterPos.Z, + TargetKind: HitTargetKind.Terrain, + TargetId: -1, + CellX: 0, + CellY: 0, + WeaponClass: weaponClass, + WeaponId: mod.Spec.Id, + IsAirburst: false, + Ttl: 0.35f)); } else { @@ -1608,6 +1646,11 @@ namespace RustToDust.Ymir ApplyHitToTarget( targetRef, hit.Point, + dir, + shooterPos, + weaponClass, + mod.Spec.Id, + isPdFlak, mod.Spec.WeaponDamage * mod.CurrentPerformance, mod.Spec.HeatOutput * 0.25f, shooterContender, @@ -1615,13 +1658,63 @@ namespace RustToDust.Ymir isSpectatorShooter, view); } + else if (hit.Hit && hit.StableId != shooterStableId) + { + // Static cover box hit + Vector3 rev = -dir; + State.HitEvents.Add(new HitFeedbackEvent( + EventId: State.AllocateHitEventId(), + HitX: endPt.X, + HitY: endPt.Y, + HitZ: endPt.Z, + NormalX: rev.X, + NormalY: rev.Y, + NormalZ: rev.Z, + SourceX: shooterPos.X, + SourceY: shooterPos.Y, + SourceZ: shooterPos.Z, + TargetKind: HitTargetKind.StaticCover, + TargetId: (int)hit.StableId, + CellX: 0, + CellY: 0, + WeaponClass: weaponClass, + WeaponId: mod.Spec.Id, + IsAirburst: isPdFlak, + Ttl: 0.35f)); + } + else if (isPdFlak && b == 0) + { + Vector3 airPt = muzzle + dir * MathF.Min(range, MathF.Max(20f, aimDelta.Length())); + Vector3 rev = -dir; + State.HitEvents.Add(new HitFeedbackEvent( + EventId: State.AllocateHitEventId(), + HitX: airPt.X, + HitY: airPt.Y, + HitZ: airPt.Z, + NormalX: rev.X, + NormalY: rev.Y, + NormalZ: rev.Z, + SourceX: shooterPos.X, + SourceY: shooterPos.Y, + SourceZ: shooterPos.Z, + TargetKind: HitTargetKind.StaticCover, + TargetId: -1, + CellX: 0, + CellY: 0, + WeaponClass: weaponClass, + WeaponId: mod.Spec.Id, + IsAirburst: true, + Ttl: 0.35f)); + } } State.Tracers.Add(new TracerEvent( muzzle.X, muzzle.Y, muzzle.Z, endPt.X, endPt.Y, endPt.Z, tracerColor, - 0.09f)); + tracerTtl, + weaponClass, + mod.Spec.Id)); } shooterGrid.InjectHardpointHeat(mod.HardpointId, mod.Spec.HeatOutput); @@ -1638,6 +1731,11 @@ namespace RustToDust.Ymir private void ApplyHitToTarget( (bool IsContender, int Index) targetRef, (float X, float Y, float Z) hitPoint, + Vector3 shotDir, + Vector3 shooterPos, + WeaponVisualClass weaponClass, + string weaponId, + bool isPdFlak, float damage, float heatTransfer, ContenderState? shooterContender, @@ -1654,7 +1752,28 @@ namespace RustToDust.Ymir } var (localRight, localForward, localHeightNorm) = ComputeLocalHitCoords(hitPoint, body.Lane, target.Grid.Spec, view); + Vector3 hitNormal = ComputeBoxFaceNormal(hitPoint, body.Lane, target.Grid.Spec, view, -shotDir); var result = target.Grid.ApplyHit(localRight, localForward, localHeightNorm, damage, heatTransfer); + State.HitEvents.Add(new HitFeedbackEvent( + EventId: State.AllocateHitEventId(), + HitX: hitPoint.X, + HitY: hitPoint.Y, + HitZ: hitPoint.Z, + NormalX: hitNormal.X, + NormalY: hitNormal.Y, + NormalZ: hitNormal.Z, + SourceX: shooterPos.X, + SourceY: shooterPos.Y, + SourceZ: shooterPos.Z, + TargetKind: HitTargetKind.Contender, + TargetId: target.Id, + CellX: result.CellX, + CellY: result.CellY, + WeaponClass: weaponClass, + WeaponId: weaponId, + IsAirburst: isPdFlak && target.Grid.Spec.IsVtol, + Ttl: 0.35f)); + if (result.CoreDestroyed) { HandleContenderEliminated(target, shooterContender, shooterLabel, isSpectatorShooter); @@ -1692,7 +1811,28 @@ namespace RustToDust.Ymir } var (localRight, localForward, localHeightNorm) = ComputeLocalHitCoords(hitPoint, body.Lane, spec.Grid.Spec, view); + Vector3 hitNormal = ComputeBoxFaceNormal(hitPoint, body.Lane, spec.Grid.Spec, view, -shotDir); var result = spec.Grid.ApplyHit(localRight, localForward, localHeightNorm, damage, heatTransfer); + State.HitEvents.Add(new HitFeedbackEvent( + EventId: State.AllocateHitEventId(), + HitX: hitPoint.X, + HitY: hitPoint.Y, + HitZ: hitPoint.Z, + NormalX: hitNormal.X, + NormalY: hitNormal.Y, + NormalZ: hitNormal.Z, + SourceX: shooterPos.X, + SourceY: shooterPos.Y, + SourceZ: shooterPos.Z, + TargetKind: HitTargetKind.Spectator, + TargetId: spec.Id, + CellX: result.CellX, + CellY: result.CellY, + WeaponClass: weaponClass, + WeaponId: weaponId, + IsAirburst: isPdFlak && spec.Grid.Spec.IsVtol, + Ttl: 0.35f)); + if (result.CoreDestroyed) { HandleSpectatorDestroyed(spec, shooterContender, shooterLabel); @@ -1760,22 +1900,42 @@ namespace RustToDust.Ymir State.Terrain.StampCrater(x, z, radius: 4.2f, depth: 1.05f); State.Terrain.StampScorch(x, z, radius: 4.5f); break; - case "gilded_lance": - State.Terrain.StampCrater(x, z, radius: 3.0f, depth: 0.70f); - State.Terrain.StampScorch(x, z, radius: 3.6f); - break; case "flak": if (burstIndex == 0) { State.Terrain.StampCrater(x, z, radius: 2.0f, depth: 0.35f); } break; - case "touring_plasma": - State.Terrain.StampScorch(x, z, radius: 1.3f); - break; } } + private static Vector3 ComputeBoxFaceNormal( + (float X, float Y, float Z) hitPoint, + int lane, + ChassisSpec spec, + Box3DSoaView view, + Vector3 fallback) + { + var bodyPos = new Vector3(view.PositionX[lane], view.PositionY[lane], view.PositionZ[lane]); + var bodyQuat = new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]); + var local = Vector3.Transform(new Vector3(hitPoint.X, hitPoint.Y, hitPoint.Z) - bodyPos, Quaternion.Conjugate(bodyQuat)); + + float nx = MathF.Abs(local.X) / MathF.Max(0.1f, spec.HalfWidth); + float ny = MathF.Abs(local.Y) / MathF.Max(0.1f, spec.HalfHeight); + float nz = MathF.Abs(local.Z) / MathF.Max(0.1f, spec.HalfLength); + + Vector3 localNormal; + if (nx >= ny && nx >= nz) + localNormal = new Vector3(local.X >= 0f ? 1f : -1f, 0f, 0f); + else if (ny >= nx && ny >= nz) + localNormal = new Vector3(0f, local.Y >= 0f ? 1f : -1f, 0f); + else + localNormal = new Vector3(0f, 0f, local.Z >= 0f ? 1f : -1f); + + Vector3 worldNormal = Vector3.Transform(localNormal, bodyQuat); + return worldNormal.LengthSquared() > 1e-6f ? Vector3.Normalize(worldNormal) : fallback; + } + private static (float LocalRight, float LocalForward, float LocalHeightNorm) ComputeLocalHitCoords( (float X, float Y, float Z) hitPoint, int lane,