Merge remote-tracking branch 'origin/nibu/rtd-prototype' into nibu/rtd-cut11b-walker

# Conflicts:
#	src/RustToDust.Net/RemoteArenaSession.cs
#	src/RustToDust.Ymir/ArenaSession.cs
#	src/RustToDust.Ymir/WalkerLocomotion.cs
#	tests/RustToDust.Tests/CultNetMultiplayerTests.cs
This commit is contained in:
Meta Vens 2026-12-06 06:04:25 +01:00
commit c61b0b6a3b
250 changed files with 29333 additions and 4118 deletions

View file

@ -957,6 +957,171 @@ namespace RustToDust.Tests
Assert.True(sawBlastClimb, "Idle local client must sync _predWalker smoothly to server upward blast without stale prediction lock");
}
[Fact]
public void Cut13a_OutOfPvsHistoricalCratersSyncViaPersistentScars_AndLiveCratersEnterWithin576mBubble()
{
using var host = new ServerMatchHost(IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Sion, maxHumans: 12, autoRestartSec: 0);
var ep = new IPEndPoint(IPAddress.Loopback, host.BoundPort);
const float histX = 700f;
const float histZ = 700f;
float baseHistH = host.Session.State.Terrain.SampleHeight(histX, histZ);
// 1. Historical out-of-PVS crater stamped before Client A joins
((IOrdnanceTargetSink)host.Session).StampTerrainImpact(histX, histZ, craterRadius: 6f, craterDepth: 2.0f, scorchRadius: 7f);
long nowMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
host.StepOnce(nowMs += 16);
float scarredHistH = host.Session.State.Terrain.SampleHeight(histX, histZ);
Assert.True(scarredHistH < baseHistH - 1.2f);
using var clientA = new RemoteArenaSession(ep, "Scout");
Assert.True(PumpUntil(host, new[] { clientA }, () => clientA.IsJoined, ref nowMs, 3000));
// Out-of-PVS historical crater must be present immediately via PersistentScars
Assert.Equal(scarredHistH, clientA.ViewState.Terrain.SampleHeight(histX, histZ), precision: 3);
Assert.InRange(clientA.PredTileSync.ResidentTileCount, 1, 9);
// 2. Live crater stamped far outside 576m bubble (-700, 100 vs open-sky ~700, 100 = ~1400m apart)
int slotA = clientA.LocalContenderId;
var sion = host.Session.State.SionMap!;
float liveX = 700f, liveZ = 100f;
for (float x = 640f; x <= 760f; x += 8f)
{
if (!sion.IsInsideAnyBuildingXz(x, liveZ, padding: 10f))
{
liveX = x;
break;
}
}
float baseLiveH = host.Session.State.Terrain.SampleHeight(liveX, liveZ);
host.Session.SetContenderPose(slotA, -700f, liveZ, 0f);
host.StepOnce(nowMs += 16);
((IOrdnanceTargetSink)host.Session).StampTerrainImpact(liveX, liveZ, craterRadius: 6f, craterDepth: 2.0f, scorchRadius: 7f);
for (int t = 0; t < 10; t++)
{
nowMs += 16;
host.StepOnce(nowMs);
clientA.PumpNetwork();
Thread.Sleep(1);
}
float hostLiveH = host.Session.State.Terrain.SampleHeight(liveX, liveZ);
Assert.True(hostLiveH < baseLiveH - 1.2f);
Assert.Equal(baseLiveH, clientA.ViewState.Terrain.SampleHeight(liveX, liveZ), precision: 3);
// 3. Client A enters 576m local geometry bubble (liveX - 450m -> dist = 450m <= 576m)
host.Session.SetContenderPose(slotA, liveX - 450f, liveZ, 0f);
Assert.True(PumpUntil(
host,
new[] { clientA },
() => MathF.Abs(clientA.ViewState.Terrain.SampleHeight(liveX, liveZ) - hostLiveH) < 0.05f,
ref nowMs,
3000),
"Client must receive live TerrainStampEvent upon entering 576m bubble before reaching _predWorld 3x3 window");
// 4. Client A steps into (liveX, liveZ) tile -> _predWorld 3x3 window matches server Box3D crater depth 1:1
host.Session.SetContenderPose(slotA, liveX - 20f, liveZ, 0f);
for (int t = 0; t < 10; t++)
{
nowMs += 16;
host.StepOnce(nowMs);
clientA.BeginRenderFrame(1.0 / 60.0, 1f);
Thread.Sleep(1);
}
Assert.InRange(clientA.PredTileSync.ResidentTileCount, 1, 9);
float rayStartY = sion.Metadata.MaxY + 25f;
float raySpanY = -(sion.Metadata.MaxY - sion.Metadata.MinY + 60f);
var hostHit = host.Session.CastWorldRayClosest(new Vector3(liveX, rayStartY, liveZ), new Vector3(0f, raySpanY, 0f));
var predHit = clientA.CastWorldRayClosest(new Vector3(liveX, rayStartY, liveZ), new Vector3(0f, raySpanY, 0f));
Assert.True(hostHit.Hit && predHit.Hit);
Assert.True(MathF.Abs(hostHit.Point.Y - predHit.Point.Y) < 0.05f,
$"Expected _predWorld crater impact Y ({predHit.Point.Y:F3}m) to match server ({hostHit.Point.Y:F3}m)");
}
[Fact]
public void Cut13a_OverlappingCraters_CoalesceIdenticallyAcrossLiveHostJoinerAndPostReset()
{
using var host = new ServerMatchHost(IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Sion, maxHumans: 12, autoRestartSec: 0);
var ep = new IPEndPoint(IPAddress.Loopback, host.BoundPort);
const float cx = -120f;
const float cz = 180f;
// Stamp 3 overlapping craters within < 0.75m (coalesce radius)
((IOrdnanceTargetSink)host.Session).StampTerrainImpact(cx, cz, craterRadius: 4.0f, craterDepth: 1.1f, scorchRadius: 4.5f);
((IOrdnanceTargetSink)host.Session).StampTerrainImpact(cx + 0.30f, cz - 0.20f, craterRadius: 5.8f, craterDepth: 2.2f, scorchRadius: 6.0f);
((IOrdnanceTargetSink)host.Session).StampTerrainImpact(cx - 0.15f, cz + 0.25f, craterRadius: 3.0f, craterDepth: 0.7f, scorchRadius: 3.5f);
long nowMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
host.StepOnce(nowMs += 16);
float liveHostH = host.Session.State.Terrain.SampleHeight(cx, cz);
float liveHostEdgeH = host.Session.State.Terrain.SampleHeight(cx + 2.5f, cz - 1.5f);
using var clientA = new RemoteArenaSession(ep, "Joiner");
Assert.True(PumpUntil(host, new[] { clientA }, () => clientA.IsJoined, ref nowMs, 3000));
float clientJoinH = clientA.ViewState.Terrain.SampleHeight(cx, cz);
float clientJoinEdgeH = clientA.ViewState.Terrain.SampleHeight(cx + 2.5f, cz - 1.5f);
Assert.True(MathF.Abs(liveHostH - clientJoinH) < 0.001f,
$"Expected liveHostH ({liveHostH:F4}) == clientJoinH ({clientJoinH:F4}) within 0.001m");
Assert.True(MathF.Abs(liveHostEdgeH - clientJoinEdgeH) < 0.001f,
$"Expected liveHostEdgeH ({liveHostEdgeH:F4}) == clientJoinEdgeH ({clientJoinEdgeH:F4}) within 0.001m");
host.ResetMatch(43, ArenaMapKind.Sion, startInLobby: false);
float postResetHostH = host.Session.State.Terrain.SampleHeight(cx, cz);
float postResetHostEdgeH = host.Session.State.Terrain.SampleHeight(cx + 2.5f, cz - 1.5f);
Assert.True(MathF.Abs(liveHostH - postResetHostH) < 0.001f,
$"Expected liveHostH ({liveHostH:F4}) == postResetHostH ({postResetHostH:F4}) within 0.001m");
Assert.True(MathF.Abs(liveHostEdgeH - postResetHostEdgeH) < 0.001f,
$"Expected liveHostEdgeH ({liveHostEdgeH:F4}) == postResetHostEdgeH ({postResetHostEdgeH:F4}) within 0.001m");
}
[Fact]
public void RemoteArenaSession_AppliesRemoteContenderCoreHpRatioOnPvsSnapshot()
{
using var host = new ServerMatchHost(IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Desert, maxHumans: 12, autoRestartSec: 0);
var ep = new IPEndPoint(IPAddress.Loopback, host.BoundPort);
using var client = new RemoteArenaSession(ep, "HpProbe");
long nowMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
Assert.True(PumpUntil(host, new[] { client }, () => client.IsJoined, ref nowMs, 3000));
int localSlot = client.LocalContenderId;
int rivalSlot = localSlot == 0 ? 1 : 0;
var serverLocal = host.Session.State.Contenders[localSlot];
var serverRival = host.Session.State.Contenders[rivalSlot];
host.Session.SetContenderPose(localSlot, 0f, 0f, 0f);
host.Session.SetContenderPose(rivalSlot, 0f, 40f, MathF.PI);
float targetHp = serverRival.Grid.MaxCoreHp * 0.42f;
serverRival.Grid.ApplyReplicatedDamage(0, 0, 0f, 20f, targetHp, serverRival.Grid.HitSequence + 1);
float expectedRatio = serverRival.Grid.CoreHp / serverRival.Grid.MaxCoreHp;
bool synced = PumpUntil(host, new[] { client }, () =>
{
if (!client.IsContenderVisible(rivalSlot)) return false;
var c = client.ViewState.Contenders[rivalSlot];
float r = c.Grid.MaxCoreHp > 0f ? c.Grid.CoreHp / c.Grid.MaxCoreHp : 1f;
return MathF.Abs(r - expectedRatio) < 0.02f;
}, ref nowMs, 2500);
var clientRival = client.ViewState.Contenders[rivalSlot];
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}");
// 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) =>
CultNetRudpPacketCodec.Encode(new CultNetRudpPacket
{