sim: emit weapon HitFeedbackEvents and gate terrain craters to heavy explosives

This commit is contained in:
Meta Vens 2026-12-04 23:56:19 +01:00
commit 1319d85e08
3 changed files with 238 additions and 11 deletions

View file

@ -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)

View file

@ -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<GroundDrop> GroundDrops { get; } = new();
public List<TracerEvent> Tracers { get; } = new();
public List<HitFeedbackEvent> HitEvents { get; } = new();
public BroadcastFeed Broadcast { get; }
public IReadOnlyList<ArenaBoxSpec> ArenaCoverBoxes { get; }
public IReadOnlyList<ArenaBoxSpec> ArenaBoundaryBoxes { get; }
@ -169,6 +209,16 @@ namespace RustToDust.Core
public event Action<ContenderState>? 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)

View file

@ -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,