WIP pre-reboot checkpoint

This commit is contained in:
Meta Vens 2026-12-06 00:43:46 +01:00
commit dfdccc3bd3
5 changed files with 248 additions and 19 deletions

View file

@ -522,12 +522,23 @@ namespace RustToDust.Tests
session.Step();
}
int viewCountBeforeRepair = session.World.View.Count;
int freeBeforeRepair = session.World.FreeBodySlots;
Assert.True(freeBeforeRepair >= 20 + 32);
session.RepairArenaScars();
// Zero-step repair must NOT burst-allocate 20+ new Box3D heightfields in the same step
Assert.Equal(viewCountBeforeRepair, session.World.View.Count);
// When FreeBodySlots >= dirtyResidentCount + 32, RepairArenaScars flushes all dirty resident tiles immediately on tick 0
Assert.True(session.World.View.Count < ArenaSession.WorldBodyCapacity - 256,
$"Expected View.Count ({session.World.View.Count}) well below WorldBodyCapacity ({ArenaSession.WorldBodyCapacity}) after repair");
Assert.Equal(0, session.TileSync.CountDirtyResidentTiles(terrain));
// Step across ticks so incremental FlushDirtyHeightfields(maxTilesPerTick: 4) completes cleanly
var (spotX, spotZ, spotBaseH) = spots[0];
float rayStartY = sion.Metadata.MaxY + 25f;
float raySpanY = -(sion.Metadata.MaxY - sion.Metadata.MinY + 60f);
var hitTick0 = session.CastWorldRayClosest(new Vector3(spotX, rayStartY, spotZ), new Vector3(0f, raySpanY, 0f));
Assert.True(hitTick0.Hit);
Assert.True(MathF.Abs(hitTick0.Point.Y - spotBaseH) < 0.05f,
$"Expected tick-0 Box3D raycast height ({hitTick0.Point.Y:F3}) to match restored base ({spotBaseH:F3})");
// Step across ticks so recycled slots remain healthy without exhausting world capacity
for (int t = 0; t < 20; t++)
{
session.Step();
@ -648,5 +659,127 @@ namespace RustToDust.Tests
}
}
}
[Fact]
public void FlushDirtyHeightfields_RecycledFreeSlots_DoesNotWedgeAtHighViewCount()
{
using var session = new ArenaSession(seed: 42, ArenaMapKind.Sion, slot0Human: true, skipInitialDrop: true);
var terrain = session.State.Terrain;
var sion = session.State.SionMap!;
float cx = -96f, cz = -96f;
bool foundSpot = false;
for (float z = -176f; z <= -16f && !foundSpot; z += 8f)
{
for (float x = -176f; x <= -16f; x += 8f)
{
if (!sion.IsInsideAnyBuildingXz(x, z, padding: 10f))
{
cx = x;
cz = z;
foundSpot = true;
break;
}
}
}
Assert.True(foundSpot, "Expected open-sky spot inside Sion tile (7, 7)");
session.TileSync.EnsureTileRegion(session.World, terrain, cx, cz, halfTileRadius: 0);
// Push View.Count >= 1016, then remove those temporary bodies and step to recycle FreeBodySlots > 500
var tempHandles = new List<Box3DSoaBodyHandle>();
ulong nextId = 900_000UL;
while (session.World.View.Count < 1016 && session.World.FreeBodySlots > 0)
{
tempHandles.Add(session.World.AddStaticBox(nextId++, (0f, -500f, 0f), (0.5f, 0.5f, 0.5f)));
}
Assert.True(session.World.View.Count >= 1016, $"Expected View.Count >= 1016, got {session.World.View.Count}");
foreach (var h in tempHandles)
{
session.World.RemoveBody(h);
}
session.World.Step(1f / 60f);
Assert.True(session.World.View.Count >= 1016, $"Expected monotonic View.Count >= 1016, got {session.World.View.Count}");
Assert.True(session.World.FreeBodySlots > 500, $"Expected recycled FreeBodySlots > 500, got {session.World.FreeBodySlots}");
session.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = cx,
Z = cz,
Radius = 6f,
Depth = 2.5f
});
int rebuilt = session.TileSync.FlushDirtyHeightfields(session.World, terrain, maxTilesPerTick: 4);
Assert.Equal(1, rebuilt);
float cpuH = terrain.SampleHeight(cx, cz);
float rayStartY = sion.Metadata.MaxY + 25f;
float raySpanY = -(sion.Metadata.MaxY - sion.Metadata.MinY + 60f);
var hit = session.CastWorldRayClosest(new Vector3(cx, rayStartY, cz), new Vector3(0f, raySpanY, 0f));
Assert.True(hit.Hit);
Assert.True(MathF.Abs(hit.Point.Y - cpuH) < 0.05f,
$"Expected Box3D raycast height ({hit.Point.Y:F3}) to match CPU height ({cpuH:F3}) within 0.05 m");
}
[Fact]
public void ServerMatchHost_ResetMatch_RepairScarsOnCrossMapSwitch_ClearsTargetOnlyAndPreservesSource()
{
string tempDir = Path.Combine(Path.GetTempPath(), "rtd-cut13a-repairswitch-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempDir);
string explicitSionPath = Path.Combine(tempDir, "sion_scars_explicit.cultcache");
string desertPath = ServerMatchHost.ResolveScarFilePath(explicitSionPath, ArenaMapKind.Sion, ArenaMapKind.Desert);
try
{
using var host = new ServerMatchHost(
IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Sion,
autoRestartSec: 0, scarFilePath: explicitSionPath, persistScars: true);
string sionFp = host.Session.State.SionMap!.AssetFingerprint;
// Stamp Sion crater
host.Session.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = -120f,
Z = 180f,
Radius = 5.5f,
Depth = 1.8f
});
// Switch to Desert and stamp Desert crater
host.ResetMatch(42, ArenaMapKind.Desert, startInLobby: false);
host.Session.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = 0f,
Z = 0f,
Radius = 4.5f,
Depth = 1.4f
});
// Switch back to Sion, then ResetMatch(42, Desert, repairScars: true) FROM Sion
host.ResetMatch(42, ArenaMapKind.Sion, startInLobby: false);
Assert.Single(host.Session.State.ScarLedger.Terrain);
host.ResetMatch(42, ArenaMapKind.Desert, startInLobby: false, repairScars: true);
Assert.Empty(host.Session.State.ScarLedger.Terrain);
var sionDisk = ArenaScarLedgerStore.Load(explicitSionPath, sionFp);
var desertDisk = ArenaScarLedgerStore.Load(desertPath, "desert:42");
Assert.Single(sionDisk.Terrain);
Assert.Empty(desertDisk.Terrain);
}
finally
{
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, recursive: true);
}
}
}
}
}