Keep living remote contender alive when quantized CoreHpRatio rounds to 0

This commit is contained in:
Meta Vens 2026-12-05 23:53:04 +01:00
commit c4e67c951f
3 changed files with 16 additions and 3 deletions

View file

@ -890,10 +890,12 @@ namespace RustToDust.Core
UpdateMountedTelemetry(); UpdateMountedTelemetry();
} }
public void ApplyReplicatedCoreHpRatio(float coreHpRatio) public void ApplyReplicatedCoreHpRatio(float coreHpRatio, bool alive)
{ {
if (MaxCoreHp <= 0f) return; if (MaxCoreHp <= 0f) return;
CoreHp = Math.Clamp(coreHpRatio, 0f, 1f) * MaxCoreHp; CoreHp = alive
? MathF.Max(0.01f, Math.Clamp(coreHpRatio, 0f, 1f) * MaxCoreHp)
: 0f;
} }
public void ApplyReplicatedThermal(ReadOnlySpan<byte> quantizedCellTemps, ReadOnlySpan<float> mountedTemps, ReadOnlySpan<float> mountedCooldowns) public void ApplyReplicatedThermal(ReadOnlySpan<byte> quantizedCellTemps, ReadOnlySpan<float> mountedTemps, ReadOnlySpan<float> mountedCooldowns)

View file

@ -1433,7 +1433,7 @@ namespace RustToDust.Net
c.ThrottleCmd = nc.Throttle; c.ThrottleCmd = nc.Throttle;
c.SteerCmd = nc.Steer; c.SteerCmd = nc.Steer;
c.MaxThermalRatio = nc.MaxThermalRatio; c.MaxThermalRatio = nc.MaxThermalRatio;
c.Grid.ApplyReplicatedCoreHpRatio(nc.CoreHpRatio); c.Grid.ApplyReplicatedCoreHpRatio(nc.CoreHpRatio, nc.Alive);
c.X = nc.Pos.X; c.Y = nc.Pos.Y; c.Z = nc.Pos.Z; c.X = nc.Pos.X; c.Y = nc.Pos.Y; c.Z = nc.Pos.Z;
c.CurrRotX = nc.Rot.X; c.CurrRotY = nc.Rot.Y; c.CurrRotZ = nc.Rot.Z; c.CurrRotW = nc.Rot.W; c.CurrRotX = nc.Rot.X; c.CurrRotY = nc.Rot.Y; c.CurrRotZ = nc.Rot.Z; c.CurrRotW = nc.Rot.W;
c.VelX = nc.Vel.X; c.VelY = nc.Vel.Y; c.VelZ = nc.Vel.Z; c.VelX = nc.Vel.X; c.VelY = nc.Vel.Y; c.VelZ = nc.Vel.Z;

View file

@ -829,6 +829,17 @@ namespace RustToDust.Tests
var clientRival = client.ViewState.Contenders[rivalSlot]; var clientRival = client.ViewState.Contenders[rivalSlot];
float actualRatio = clientRival.Grid.MaxCoreHp > 0f ? clientRival.Grid.CoreHp / clientRival.Grid.MaxCoreHp : 1f; float actualRatio = clientRival.Grid.MaxCoreHp > 0f ? clientRival.Grid.CoreHp / clientRival.Grid.MaxCoreHp : 1f;
Assert.True(synced, $"Expected remote contender {rivalSlot} CoreHpRatio ~ {expectedRatio:F3}, got {actualRatio:F3}"); Assert.True(synced, $"Expected remote contender {rivalSlot} CoreHpRatio ~ {expectedRatio:F3}, got {actualRatio:F3}");
// Edge case: living rival at 0.30 HP rounds 8-bit CoreHpRatio to 0, yet must remain Alive on remote client
serverRival.Grid.ApplyReplicatedDamage(0, 0, 0f, 20f, 0.30f, serverRival.Grid.HitSequence + 1);
Assert.True(serverRival.Alive);
bool lowHpSynced = PumpUntil(host, new[] { client }, () =>
{
var c = client.ViewState.Contenders[rivalSlot];
return c.Alive && c.Grid.CoreHp > 0f && c.Grid.CoreHp <= 0.5f;
}, ref nowMs, 2500);
Assert.True(lowHpSynced && clientRival.Alive && !clientRival.Grid.IsDestroyed && clientRival.Grid.CoreHp > 0f,
$"Expected living 0.30 HP rival to stay Alive on remote client, got Alive={clientRival.Alive}, CoreHp={clientRival.Grid.CoreHp:F3}");
} }
private static byte[] EncodeRudpLatestDatagram(byte[] frameBytes, uint sequence) => private static byte[] EncodeRudpLatestDatagram(byte[] frameBytes, uint sequence) =>