Cut 13a fix: occupancy-only Sion Box3D tiles, Terrain hit classification, top-96 TerrainRenderer LRU, incremental scar repair, per-map scar ledgers, and crater coalescing parity

This commit is contained in:
Meta Vens 2026-12-06 00:30:37 +01:00
commit 812956bd5a
13 changed files with 751 additions and 105 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Numerics;
@ -146,7 +147,7 @@ namespace RustToDust.Tests
if (kind == Box3DSoaKind.StaticHeightField) hfCount++;
else if (kind == Box3DSoaKind.StaticMesh) meshCount++;
}
Assert.InRange(hfCount, 1, 255);
Assert.InRange(hfCount, 1, 45);
Assert.Equal(hfCount, session.TileSync.ResidentTileCount);
Assert.Equal(36, meshCount);
@ -378,5 +379,274 @@ namespace RustToDust.Tests
}
}
}
[Fact]
public void ValereSlopeHits_ClassifyAsTerrainNeverStaticCover_ForHitscanAndProjectile()
{
using var session = new ArenaSession(seed: 42, ArenaMapKind.Sion, slot0Human: true, skipInitialDrop: true);
var terrain = session.State.Terrain;
var sion = session.State.SionMap!;
// Ensure Valere hill heightfield tiles are resident so Box3D heightfields are active on the slope ray
session.TileSync.EnsureTileRegion(session.World, terrain, 113f, -211f, halfTileRadius: 1);
// Find an open-sky steep Valere slope point and an unobstructed muzzle above it (zero buildings on ray)
Vector3 muzzle = default;
Vector3 dir = default;
bool foundSlope = false;
for (float sz = -290f; sz <= -175f && !foundSlope; sz += 6f)
{
for (float sx = 40f; sx <= 185f && !foundSlope; sx += 6f)
{
if (sion.IsInsideAnyBuildingXz(sx, sz, padding: 8.0f))
{
continue;
}
float sh = terrain.SampleHeight(sx, sz);
var (nx, ny, nz) = terrain.SampleNormal(sx, sz);
if (sh < 55f || ny >= 0.88f)
{
continue;
}
Vector3 slopePt = new(sx, sh, sz);
Vector3 nrm = Vector3.Normalize(new Vector3(nx, ny, nz));
Vector3 candMuzzle = slopePt + nrm * 32f + Vector3.UnitY * 8f;
Vector3 candDir = Vector3.Normalize(slopePt - candMuzzle);
float dist = Vector3.Distance(candMuzzle, slopePt) + 5f;
if (!sion.IntersectsAnyBuildingAabb(candMuzzle.X, candMuzzle.Y, candMuzzle.Z, candDir.X, candDir.Y, candDir.Z, dist) &&
terrain.Raycast(candMuzzle.X, candMuzzle.Y, candMuzzle.Z, candDir.X, candDir.Y, candDir.Z, dist, out float tx, out _, out float tz, out _) &&
!sion.IsInsideAnyBuildingXz(tx, tz, padding: 2.0f))
{
muzzle = candMuzzle;
dir = candDir;
foundSlope = true;
}
}
}
Assert.True(foundSlope, "Expected open-sky steep Valere slope ray unobstructed by buildings");
session.TileSync.EnsureTileRegion(session.World, terrain, muzzle.X, muzzle.Z, halfTileRadius: 1);
var events = new List<ISimEvent>();
session.DrainStepEvents(events);
// 1. Fire hitscan directly into Valere slope
var hitscanSpec = ProceduralWeaponGenerator.CreateDreamWeapon(WeaponArchetypeKind.RotaryAutocannon);
session.Ordnance.FireWeapon(new OrdnanceFireRequest(
Module: new MountedModule(hitscanSpec.Module, "hp.weapon.main", 0f),
ShooterStableId: ArenaSession.ContenderStableIdBase,
ShooterContender: session.State.Contenders[0],
ShooterLabel: "Tester",
IsSpectatorShooter: false,
ShooterEntityId: 0,
ShooterPos: muzzle,
MuzzlePos: muzzle,
AimDir: dir,
AimDist: 160f,
SpreadRad: 0f,
TargetEntityStableId: 0UL,
RootDef: hitscanSpec.RootNode,
WeaponKey: hitscanSpec.Key));
// 2. Fire ballistic projectile into Valere slope
var projSpec = ProceduralWeaponGenerator.CreateDreamWeapon(WeaponArchetypeKind.SabotSlugCannon);
session.Ordnance.FireWeapon(new OrdnanceFireRequest(
Module: new MountedModule(projSpec.Module, "hp.weapon.main", 0f),
ShooterStableId: ArenaSession.ContenderStableIdBase,
ShooterContender: session.State.Contenders[0],
ShooterLabel: "Tester",
IsSpectatorShooter: false,
ShooterEntityId: 0,
ShooterPos: muzzle,
MuzzlePos: muzzle,
AimDir: dir,
AimDist: 160f,
SpreadRad: 0f,
TargetEntityStableId: 0UL,
RootDef: projSpec.RootNode,
WeaponKey: projSpec.Key));
var impacts = new List<HitImpactEvent>();
for (int tick = 0; tick < 60; tick++)
{
session.Step();
events.Clear();
session.DrainStepEvents(events);
foreach (var ev in events)
{
if (ev is HitImpactEvent hit)
{
impacts.Add(hit);
}
}
if (impacts.Count >= 2)
{
break;
}
}
Assert.True(impacts.Count >= 2, $"Expected >= 2 slope impacts (hitscan + projectile), got {impacts.Count}");
Assert.All(impacts, h => Assert.Equal((byte)HitTargetKind.Terrain, h.TargetKind));
}
[Fact]
public void RepairArenaScars_ManyScarredTilesRebuildIncrementallyWithoutExhaustingWorldCapacity()
{
using var session = new ArenaSession(seed: 42, ArenaMapKind.Sion, slot0Human: true, skipInitialDrop: true);
var terrain = session.State.Terrain;
// Materialize 20 resident tiles in a 5x4 grid and crater all 20
var spots = new List<(float X, float Z, float BaseH)>();
for (int tz = 5; tz <= 8; tz++)
{
for (int tx = 5; tx <= 9; tx++)
{
float wx = -terrain.MapHalfExtent + (tx + 0.5f) * terrain.MapTileSize;
float wz = -terrain.MapHalfExtent + (tz + 0.5f) * terrain.MapTileSize;
session.TileSync.EnsureTileResident(session.World, terrain, tx, tz);
float baseH = terrain.SampleHeight(wx, wz);
spots.Add((wx, wz, baseH));
session.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = wx,
Z = wz,
Radius = 5f,
Depth = 1.6f
});
}
}
// Step a few ticks so all 20 craters flush into Box3D heightfields
for (int t = 0; t < 8; t++)
{
session.Step();
}
int viewCountBeforeRepair = session.World.View.Count;
session.RepairArenaScars();
// Zero-step repair must NOT burst-allocate 20+ new Box3D heightfields in the same step
Assert.Equal(viewCountBeforeRepair, session.World.View.Count);
// Step across ticks so incremental FlushDirtyHeightfields(maxTilesPerTick: 4) completes cleanly
for (int t = 0; t < 20; t++)
{
session.Step();
Assert.True(session.World.View.Count < ArenaSession.WorldBodyCapacity - 256,
$"Expected View.Count ({session.World.View.Count}) well below WorldBodyCapacity ({ArenaSession.WorldBodyCapacity}) at tick {t}");
}
foreach (var (wx, wz, baseH) in spots)
{
Assert.Equal(baseH, terrain.SampleHeight(wx, wz), precision: 3);
}
}
[Fact]
public void ServerMatchHost_SionDesertSion_PreservesSionScarsAndDoesNotClobberExplicitScarFile()
{
string tempDir = Path.Combine(Path.GetTempPath(), "rtd-cut13a-mapswitch-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempDir);
string explicitSionPath = Path.Combine(tempDir, "sion_scars_explicit.cultcache");
try
{
const float craterX = -120f;
const float craterZ = 180f;
float sionBaseH;
float sionScarredH;
using (var host = new ServerMatchHost(
IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Sion,
autoRestartSec: 0, scarFilePath: explicitSionPath, persistScars: true))
{
sionBaseH = host.Session.State.Terrain.SampleHeight(craterX, craterZ);
host.Session.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = craterX,
Z = craterZ,
Radius = 5.5f,
Depth = 1.8f
});
sionScarredH = host.Session.State.Terrain.SampleHeight(craterX, craterZ);
Assert.True(sionScarredH < sionBaseH - 1.2f);
// Switch Sion -> Desert: Desert must NOT inherit Sion craters, nor overwrite explicitSionPath
float desertBaseH;
host.ResetMatch(42, ArenaMapKind.Desert, startInLobby: false);
Assert.Empty(host.Session.State.ScarLedger.Terrain);
desertBaseH = host.Session.State.Terrain.SampleHeight(0f, 0f);
host.Session.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = 0f,
Z = 0f,
Radius = 4.5f,
Depth = 1.4f
});
float desertScarredH = host.Session.State.Terrain.SampleHeight(0f, 0f);
Assert.True(desertScarredH < desertBaseH - 0.8f);
// Switch Desert -> Sion: Sion craters must be restored cleanly!
host.ResetMatch(43, ArenaMapKind.Sion, startInLobby: false);
Assert.Equal(sionScarredH, host.Session.State.Terrain.SampleHeight(craterX, craterZ), precision: 3);
// Verify explicitSionPath on disk still holds Sion's AssetFingerprint & crater (never clobbered by Desert)
var diskSnap = ArenaScarLedgerStore.Load(explicitSionPath, host.Session.State.SionMap!.AssetFingerprint);
Assert.Single(diskSnap.Terrain);
Assert.Equal(craterX, diskSnap.Terrain[0].X);
Assert.Equal(craterZ, diskSnap.Terrain[0].Z);
}
// Also verify LocalArenaSession & ListenHostArenaSession carry scars across rounds and Sion -> Desert -> Sion
using (var local = new LocalArenaSession(seed: 42, mapKind: ArenaMapKind.Sion, skipInitialDrop: true))
{
local.Inner.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = craterX,
Z = craterZ,
Radius = 5.5f,
Depth = 1.8f
});
local.RequestNewMatch(43, ArenaMapKind.Sion);
Assert.Equal(sionScarredH, local.ViewState.Terrain.SampleHeight(craterX, craterZ), precision: 3);
local.RequestNewMatch(42, ArenaMapKind.Desert);
Assert.Empty(local.ViewState.ScarLedger.Terrain);
local.RequestNewMatch(44, ArenaMapKind.Sion);
Assert.Equal(sionScarredH, local.ViewState.Terrain.SampleHeight(craterX, craterZ), precision: 3);
local.RepairArenaScars();
local.RequestNewMatch(45, ArenaMapKind.Sion);
Assert.Equal(sionBaseH, local.ViewState.Terrain.SampleHeight(craterX, craterZ), precision: 3);
}
using (var listen = new ListenHostArenaSession(IPAddress.Loopback, 0, seed: 42, mapKind: ArenaMapKind.Sion))
{
listen.Host.Session.State.ApplyTerrainStamp(new TerrainStampEvent
{
Kind = (byte)TerrainStampKind.Crater,
X = craterX,
Z = craterZ,
Radius = 5.5f,
Depth = 1.8f
});
listen.RequestNewMatch(43, ArenaMapKind.Desert);
Assert.Empty(listen.ViewState.ScarLedger.Terrain);
listen.RequestNewMatch(44, ArenaMapKind.Sion);
Assert.Equal(sionScarredH, listen.ViewState.Terrain.SampleHeight(craterX, craterZ), precision: 3);
}
}
finally
{
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, recursive: true);
}
}
}
}
}