803 lines
38 KiB
C#
803 lines
38 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Numerics;
|
|
using RustToDust.Core;
|
|
using RustToDust.Net;
|
|
using RustToDust.Ymir;
|
|
using Xunit;
|
|
using Ymir.Box3D;
|
|
|
|
namespace RustToDust.Tests
|
|
{
|
|
public sealed class SionMapTests
|
|
{
|
|
[Fact]
|
|
public void SionMapLoader_LoadsMetadataHeightmapBuildingsAndRcolMeshesWithExactParity()
|
|
{
|
|
var sion = SionMapLoader.Load();
|
|
var meta = sion.Metadata;
|
|
|
|
Assert.Equal(3072f, meta.SizeMeters);
|
|
Assert.Equal(16, meta.TileGrid);
|
|
Assert.Equal(192f, meta.TileSizeMeters);
|
|
Assert.Equal(6145, meta.GridRes);
|
|
Assert.Equal(0.5f, meta.CellSizeMeters);
|
|
Assert.Equal("heightmap_05m.u16z", meta.HeightmapFile);
|
|
Assert.False(string.IsNullOrWhiteSpace(meta.DemAssetHash));
|
|
Assert.Contains("swisstopo", meta.Attribution, StringComparison.OrdinalIgnoreCase);
|
|
|
|
Assert.Equal(6145 * 6145, sion.BaseDem.Length);
|
|
Assert.Equal(2319, sion.Buildings.Count);
|
|
Assert.Equal(36, sion.CollisionMeshes.Count);
|
|
|
|
var terrain = TerrainField.LoadSharedHeightmap(
|
|
meta.SizeMeters, meta.GridRes, meta.TileGrid,
|
|
sion.BaseDem, sion.BaseDemMinY, sion.BaseDemScale,
|
|
meta.MinY, meta.MaxY);
|
|
|
|
// Verify Basilique de Valere and valley buildings align cleanly with heightmap_05m.u16z (0.00m coordinate drift)
|
|
bool foundValere = false;
|
|
int flatAlignChecked = 0;
|
|
int closeAlignCount = 0;
|
|
for (int i = 0; i < sion.Buildings.Count; i++)
|
|
{
|
|
var b = sion.Buildings[i];
|
|
if (b.Name.Contains("Val", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
foundValere = true;
|
|
float hValere = terrain.SampleHeight(b.CenterX, b.CenterZ);
|
|
Assert.InRange(hValere, 90f, 150f);
|
|
}
|
|
|
|
if (b.Class == "Gebaeude Einzelhaus" &&
|
|
b.HalfX < 10f && b.HalfZ < 10f &&
|
|
terrain.SampleNormal(b.CenterX, b.CenterZ).Ny > 0.98f)
|
|
{
|
|
float hTerrain = terrain.SampleHeight(b.CenterX, b.CenterZ);
|
|
// Buildings include a ~4m foundation skirt below ground level; sloped/stepped lots can vary slightly around center.
|
|
Assert.InRange(hTerrain, b.GroundY - 5.0f, b.RoofY + 2.0f);
|
|
if (MathF.Abs(hTerrain - (b.GroundY + 3.5f)) <= 2.5f)
|
|
{
|
|
closeAlignCount++;
|
|
}
|
|
flatAlignChecked++;
|
|
}
|
|
}
|
|
|
|
Assert.True(foundValere, "Expected Basilique de Valere in Sion buildings_manifest.json");
|
|
Assert.True(flatAlignChecked > 200, $"Expected > 200 flat-ground buildings matching DEM, got {flatAlignChecked}");
|
|
Assert.True(closeAlignCount > flatAlignChecked * 0.90f,
|
|
$"Expected >90% of flat-lot buildings to sit within 2.5m of skirted DEM ({closeAlignCount}/{flatAlignChecked})");
|
|
|
|
// Verify RCOL collision mesh bounds match metadata tile bounds
|
|
for (int i = 0; i < sion.CollisionMeshes.Count; i++)
|
|
{
|
|
var cm = sion.CollisionMeshes[i];
|
|
var tm = meta.Tiles[i];
|
|
Assert.Equal(tm.Tx, cm.Tx);
|
|
Assert.Equal(tm.Tz, cm.Tz);
|
|
Assert.True(cm.Vertices.Length >= 3);
|
|
Assert.True(cm.Indices.Length >= 3 && cm.Indices.Length % 3 == 0);
|
|
Assert.True(Vector3.Distance(tm.BoundsMin, cm.BoundsMin) < 0.05f);
|
|
Assert.True(Vector3.Distance(tm.BoundsMax, cm.BoundsMax) < 0.05f);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void SionTerrainField_SupportsBilinearSamplingNormalsRaycastAndCraters()
|
|
{
|
|
var sion = SionMapLoader.Load();
|
|
var meta = sion.Metadata;
|
|
var terrain = TerrainField.LoadSharedHeightmap(
|
|
meta.SizeMeters, meta.GridRes, meta.TileGrid,
|
|
sion.BaseDem, sion.BaseDemMinY, sion.BaseDemScale,
|
|
meta.MinY, meta.MaxY);
|
|
|
|
Assert.True(terrain.IsHeightmapMode);
|
|
Assert.Equal(3072f, terrain.MapSize);
|
|
Assert.Equal(1536f, terrain.MapHalfExtent);
|
|
Assert.Equal(16, terrain.TileGridCount);
|
|
|
|
// Grid vertex exactness at (-1536, -1536) and (0, 0)
|
|
Assert.Equal(sion.SampleBaseDemAtIndex(0), terrain.SampleHeight(-1536f, -1536f), precision: 4);
|
|
Assert.Equal(sion.SampleBaseDemAtIndex(3072 * 6145 + 3072), terrain.SampleHeight(0f, 0f), precision: 4);
|
|
|
|
// Plaza spawn area is flat; Valere hill rises > 100m
|
|
var (_, plazaNy, _) = terrain.SampleNormal(-60f, 360f);
|
|
Assert.True(plazaNy >= 0.95f, $"Expected flat plaza normal.Y >= 0.95, got {plazaNy:F3}");
|
|
float valerePeakH = terrain.SampleHeight(113f, -211f);
|
|
Assert.True(valerePeakH > 100f, $"Expected Valere hill height > 100m, got {valerePeakH:F2}m");
|
|
|
|
// Raycast hits Valere hill at low elevation and clears above peak
|
|
bool blocked = terrain.Raycast(113f, 35f, -350f, 0f, 0f, 1f, 280f, out _, out float hitY, out float hitZ, out _);
|
|
Assert.True(blocked, "Expected terrain raycast through Valere ridge to hit");
|
|
Assert.InRange(hitZ, -350f, -70f);
|
|
Assert.True(hitY >= 34f);
|
|
|
|
bool clear = terrain.Raycast(113f, meta.MaxY + 10f, -350f, 0f, 0f, 1f, 280f, out _, out _, out _, out _);
|
|
Assert.False(clear, "Expected high raycast above Valere peak to clear");
|
|
|
|
// Dynamic crater lowers height on top of DEM
|
|
float before = terrain.SampleHeight(-60f, 360f);
|
|
terrain.StampCrater(-60f, 360f, radius: 5f, depth: 1.5f);
|
|
float after = terrain.SampleHeight(-60f, 360f);
|
|
Assert.True(after < before - 1.0f, $"Expected crater to depress Sion DEM (before={before:F2}, after={after:F2})");
|
|
}
|
|
|
|
[Fact]
|
|
public void ArenaSession_SionLoadsHeightfieldAndMeshesSpawnsClearBlocksLosAndStepsCleanly()
|
|
{
|
|
using var session = new ArenaSession(seed: 42, ArenaMapKind.Sion, slot0Human: true, skipInitialDrop: true);
|
|
var state = session.State;
|
|
var sion = state.SionMap!;
|
|
|
|
Assert.Equal(ArenaMapKind.Sion, state.MapKind);
|
|
Assert.Empty(state.ArenaCoverBoxes);
|
|
Assert.Equal(MatchState.SionInitialSafeRadius, state.Spectators.SafeRadius);
|
|
|
|
// Verify lazy tiled StaticHeightField (192m tiles) + StaticMesh (36) exist in Ymir
|
|
int hfCount = 0;
|
|
int meshCount = 0;
|
|
var view = session.World.View;
|
|
for (int lane = 0; lane < view.Count; lane++)
|
|
{
|
|
var kind = (Box3DSoaKind)view.Kind[lane];
|
|
if (kind == Box3DSoaKind.StaticHeightField) hfCount++;
|
|
else if (kind == Box3DSoaKind.StaticMesh) meshCount++;
|
|
}
|
|
Assert.InRange(hfCount, 1, 45);
|
|
Assert.Equal(hfCount, session.TileSync.ResidentTileCount);
|
|
Assert.Equal(36, meshCount);
|
|
|
|
// Verify zero contenders, spectators, or drops spawned inside any building
|
|
foreach (var c in state.Contenders)
|
|
{
|
|
float pad = MathF.Max(c.Grid.Spec.HalfWidth, c.Grid.Spec.HalfLength) + 1.5f;
|
|
Assert.False(sion.IsInsideAnyBuildingXz(c.X, c.Z, pad),
|
|
$"Contender {c.Id} ({c.Grid.Spec.Id}) spawned inside building at ({c.X:F2}, {c.Z:F2})");
|
|
}
|
|
foreach (var s in state.Spectators.Spectators)
|
|
{
|
|
Assert.False(sion.IsInsideAnyBuildingXz(s.X, s.Z, 1.5f),
|
|
$"Spectator {s.Id} spawned inside building at ({s.X:F2}, {s.Z:F2})");
|
|
}
|
|
foreach (var d in state.GroundDrops)
|
|
{
|
|
Assert.False(sion.IsInsideAnyBuildingXz(d.X, d.Z, 1.5f),
|
|
$"GroundDrop {d.Id} spawned inside building at ({d.X:F2}, {d.Z:F2})");
|
|
}
|
|
|
|
// LOS blocked by Valere hill
|
|
Assert.False(session.HasLineOfSight(113f, 35f, -350f, 113f, 35f, -70f),
|
|
"Expected HasLineOfSight to be blocked by Valere hill");
|
|
|
|
// LOS blocked by a real Old Town solid building mesh (and clear over its roof)
|
|
SionBuildingAabb? testBldg = null;
|
|
for (int i = 0; i < sion.Buildings.Count; i++)
|
|
{
|
|
var b = sion.Buildings[i];
|
|
if (b.Class == "Gebaeude Einzelhaus" &&
|
|
b.HalfX >= 5f && b.HalfZ >= 5f &&
|
|
(b.RoofY - b.GroundY) >= 8f &&
|
|
MathF.Abs(b.CenterX) < 500f && MathF.Abs(b.CenterZ) < 500f)
|
|
{
|
|
testBldg = b;
|
|
break;
|
|
}
|
|
}
|
|
Assert.NotNull(testBldg);
|
|
var tb = testBldg!.Value;
|
|
float midY = tb.GroundY + (tb.RoofY - tb.GroundY) * 0.45f;
|
|
float zA = tb.CenterZ - tb.HalfZ - 4f;
|
|
float zB = tb.CenterZ + tb.HalfZ + 4f;
|
|
Assert.False(session.HasLineOfSight(tb.CenterX, midY, zA, tb.CenterX, midY, zB),
|
|
$"Expected HasLineOfSight to be blocked by Sion building {tb.Id} at ({tb.CenterX:F1}, {midY:F1}, {tb.CenterZ:F1})");
|
|
Assert.True(session.HasLineOfSight(tb.CenterX, tb.RoofY + 6f, zA, tb.CenterX, tb.RoofY + 6f, zB),
|
|
$"Expected HasLineOfSight above Sion building {tb.Id} roof ({tb.RoofY:F1}m) to be clear");
|
|
|
|
// Step 180 ticks (3 seconds) driving & firing: zero NaNs, zero underground falls, player moves cleanly
|
|
var player = state.Contenders[0];
|
|
float pStartX = player.X;
|
|
float pStartZ = player.Z;
|
|
for (int tick = 0; tick < 180; tick++)
|
|
{
|
|
session.SetHumanInput(0, new PlayerInput(
|
|
Throttle: 1f,
|
|
Steer: MathF.Sin(tick * 0.04f) * 0.25f,
|
|
AimWorldX: player.X + MathF.Sin(player.Yaw) * 60f,
|
|
AimWorldZ: player.Z + MathF.Cos(player.Yaw) * 60f,
|
|
Fire: tick % 10 == 0,
|
|
InteractPickup: false,
|
|
Brake: false));
|
|
session.Step();
|
|
|
|
for (int cIdx = 0; cIdx < state.Contenders.Count; cIdx++)
|
|
{
|
|
var c = state.Contenders[cIdx];
|
|
if (!c.Alive) continue;
|
|
Assert.True(float.IsFinite(c.X) && float.IsFinite(c.Y) && float.IsFinite(c.Z),
|
|
$"Non-finite contender {c.Id} pose at tick {tick}");
|
|
float gh = state.Terrain.SampleHeight(c.X, c.Z);
|
|
float bottomY = c.Y - c.Grid.Spec.HalfHeight;
|
|
Assert.True(bottomY >= gh - 0.85f,
|
|
$"Contender {c.Id} ({c.Grid.Spec.Id}) fell below Sion terrain at tick {tick}: bottom={bottomY:F2}m < ground={gh:F2}m");
|
|
}
|
|
|
|
for (int sIdx = 0; sIdx < state.Spectators.Spectators.Count; sIdx++)
|
|
{
|
|
var s = state.Spectators.Spectators[sIdx];
|
|
if (!s.Alive) continue;
|
|
Assert.True(float.IsFinite(s.X) && float.IsFinite(s.Y) && float.IsFinite(s.Z),
|
|
$"Non-finite spectator {s.Id} pose at tick {tick}");
|
|
}
|
|
}
|
|
|
|
float pDisp = MathF.Sqrt((player.X - pStartX) * (player.X - pStartX) + (player.Z - pStartZ) * (player.Z - pStartZ));
|
|
Assert.True(pDisp > 5f, $"Expected player displacement > 5m on Sion after 180 ticks, got {pDisp:F2}m");
|
|
}
|
|
|
|
[Fact]
|
|
public void SionBox3DTile_CraterRebuildsLocalTileOnlyAndLeavesNeighborsIntact()
|
|
{
|
|
using var session = new ArenaSession(seed: 42, ArenaMapKind.Sion, slot0Human: true, skipInitialDrop: true);
|
|
var terrain = session.State.Terrain;
|
|
|
|
// Tile (7, 7) spans [-192, 0) x [-192, 0); neighbor tile (8, 7) spans [0, 192) x [-192, 0).
|
|
// Pick an open-sky coordinate strictly inside tile (7, 7) away from any building footprint.
|
|
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: 1);
|
|
|
|
Assert.True(session.TileSync.TryGetResidentHandle(7, 7, terrain.TileGridCount, out var handleTargetBefore));
|
|
Assert.True(session.TileSync.TryGetResidentHandle(8, 7, terrain.TileGridCount, out var handleNeighborBefore));
|
|
|
|
float rayStartY = sion.Metadata.MaxY + 25f;
|
|
float raySpanY = -(sion.Metadata.MaxY - sion.Metadata.MinY + 60f);
|
|
var hitBefore = session.CastWorldRayClosest(new Vector3(cx, rayStartY, cz), new Vector3(0f, raySpanY, 0f));
|
|
Assert.True(hitBefore.Hit);
|
|
Assert.Equal(Box3DPhysicsTileSync.HeightfieldTileStableIdBase + (ulong)(7 * 16 + 7), hitBefore.StableId);
|
|
|
|
// Stamp crater strictly inside tile (7, 7)
|
|
session.State.ApplyTerrainStamp(new TerrainStampEvent
|
|
{
|
|
Kind = (byte)TerrainStampKind.Crater,
|
|
X = cx,
|
|
Z = cz,
|
|
Radius = 6f,
|
|
Depth = 2.0f
|
|
});
|
|
int rebuilt = session.TileSync.FlushDirtyHeightfields(session.World, terrain, maxTilesPerTick: 4);
|
|
Assert.Equal(1, rebuilt);
|
|
|
|
Assert.True(session.TileSync.TryGetResidentHandle(7, 7, terrain.TileGridCount, out var handleTargetAfter));
|
|
Assert.True(session.TileSync.TryGetResidentHandle(8, 7, terrain.TileGridCount, out var handleNeighborAfter));
|
|
|
|
Assert.NotEqual(handleTargetBefore, handleTargetAfter);
|
|
Assert.Equal(handleNeighborBefore, handleNeighborAfter);
|
|
|
|
var hitAfter = session.CastWorldRayClosest(new Vector3(cx, rayStartY, cz), new Vector3(0f, raySpanY, 0f));
|
|
Assert.True(hitAfter.Hit);
|
|
Assert.True(hitAfter.Point.Y < hitBefore.Point.Y - 1.2f,
|
|
$"Expected Box3D raycast impact height to drop in crater (before={hitBefore.Point.Y:F3}, after={hitAfter.Point.Y:F3})");
|
|
}
|
|
|
|
[Fact]
|
|
public void ArenaScarLedgerStore_PersistsCratersAndRubbleAcrossResetAndRepairsToLayer0()
|
|
{
|
|
string tempDir = Path.Combine(Path.GetTempPath(), "rtd-cut13a-scar-test-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(tempDir);
|
|
string scarPath = Path.Combine(tempDir, "sion_scars_test.cultcache");
|
|
|
|
try
|
|
{
|
|
const float craterX = -120f;
|
|
const float craterZ = 180f;
|
|
const float rubbleX = 140f;
|
|
const float rubbleZ = 180f;
|
|
float baseCraterH;
|
|
float baseRubbleH;
|
|
float scarredCraterH;
|
|
float scarredRubbleH;
|
|
|
|
using (var host1 = new ServerMatchHost(
|
|
IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Sion,
|
|
autoRestartSec: 0, scarFilePath: scarPath, persistScars: true))
|
|
{
|
|
var state1 = host1.Session.State;
|
|
baseCraterH = state1.Terrain.SampleHeight(craterX, craterZ);
|
|
baseRubbleH = state1.Terrain.SampleHeight(rubbleX, rubbleZ);
|
|
|
|
state1.ApplyTerrainStamp(new TerrainStampEvent
|
|
{
|
|
Kind = (byte)TerrainStampKind.Crater,
|
|
X = craterX,
|
|
Z = craterZ,
|
|
Radius = 5.5f,
|
|
Depth = 1.8f
|
|
});
|
|
state1.ApplyTerrainStamp(new TerrainStampEvent
|
|
{
|
|
Kind = (byte)TerrainStampKind.Rubble,
|
|
X = rubbleX,
|
|
Z = rubbleZ,
|
|
Radius = 6.0f,
|
|
Depth = 1.2f,
|
|
Yaw = 0.4f
|
|
});
|
|
|
|
scarredCraterH = state1.Terrain.SampleHeight(craterX, craterZ);
|
|
scarredRubbleH = state1.Terrain.SampleHeight(rubbleX, rubbleZ);
|
|
Assert.True(scarredCraterH < baseCraterH - 1.2f);
|
|
Assert.True(scarredRubbleH > baseRubbleH + 0.6f);
|
|
|
|
// ResetMatch preserves scars in RAM & flushes to CultCache
|
|
host1.ResetMatch(43, ArenaMapKind.Sion, startInLobby: false);
|
|
Assert.Equal(scarredCraterH, host1.Session.State.Terrain.SampleHeight(craterX, craterZ), precision: 3);
|
|
Assert.Equal(scarredRubbleH, host1.Session.State.Terrain.SampleHeight(rubbleX, rubbleZ), precision: 3);
|
|
}
|
|
|
|
Assert.True(File.Exists(scarPath), "Expected CultCache scar file on disk after host dispose");
|
|
|
|
using (var host2 = new ServerMatchHost(
|
|
IPAddress.Loopback, 0, seed: 44, ArenaMapKind.Sion,
|
|
autoRestartSec: 0, scarFilePath: scarPath, persistScars: true))
|
|
{
|
|
var state2 = host2.Session.State;
|
|
Assert.Equal(scarredCraterH, state2.Terrain.SampleHeight(craterX, craterZ), precision: 3);
|
|
Assert.Equal(scarredRubbleH, state2.Terrain.SampleHeight(rubbleX, rubbleZ), precision: 3);
|
|
|
|
// RepairArenaScars zeroes back to Layer 0
|
|
host2.RepairArenaScars();
|
|
Assert.Equal(baseCraterH, state2.Terrain.SampleHeight(craterX, craterZ), precision: 3);
|
|
Assert.Equal(baseRubbleH, state2.Terrain.SampleHeight(rubbleX, rubbleZ), precision: 3);
|
|
Assert.Empty(state2.ScarLedger.Terrain);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (Directory.Exists(tempDir))
|
|
{
|
|
Directory.Delete(tempDir, recursive: true);
|
|
}
|
|
}
|
|
}
|
|
|
|
[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
|
|
});
|
|
}
|
|
}
|
|
|
|
// Flush all 20 cratered tiles into Box3D heightfields without evicting non-actor tiles
|
|
int flushedBeforeRepair = session.TileSync.FlushDirtyHeightfields(session.World, terrain, maxTilesPerTick: 20);
|
|
Assert.Equal(20, flushedBeforeRepair);
|
|
session.World.Step(1f / 60f);
|
|
|
|
var sion = session.State.SionMap!;
|
|
var sionMeta = sion.Metadata;
|
|
(float X, float Z, float BaseH) openSpot = default;
|
|
bool foundOpenSpot = false;
|
|
foreach (var s in spots)
|
|
{
|
|
if (!sion.IsInsideAnyBuildingXz(s.X, s.Z, padding: 6f))
|
|
{
|
|
openSpot = s;
|
|
foundOpenSpot = true;
|
|
break;
|
|
}
|
|
}
|
|
Assert.True(foundOpenSpot, "Expected at least one open-sky spot among the 20 cratered tiles");
|
|
|
|
float rayStartY = sionMeta.MaxY + 25f;
|
|
float raySpanY = -(sionMeta.MaxY - sionMeta.MinY + 60f);
|
|
var hitCratered = session.CastWorldRayClosest(new Vector3(openSpot.X, rayStartY, openSpot.Z), new Vector3(0f, raySpanY, 0f));
|
|
Assert.True(hitCratered.Hit);
|
|
Assert.True(hitCratered.Point.Y < openSpot.BaseH - 1.0f,
|
|
$"Expected cratered Box3D raycast height ({hitCratered.Point.Y:F3}) well below base ({openSpot.BaseH:F3})");
|
|
|
|
int freeBeforeRepair = session.World.FreeBodySlots;
|
|
Assert.True(freeBeforeRepair >= 20 + 32);
|
|
session.RepairArenaScars();
|
|
// 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));
|
|
|
|
var hitTick0 = session.CastWorldRayClosest(new Vector3(openSpot.X, rayStartY, openSpot.Z), new Vector3(0f, raySpanY, 0f));
|
|
Assert.True(hitTick0.Hit);
|
|
Assert.True(MathF.Abs(hitTick0.Point.Y - openSpot.BaseH) < 0.05f,
|
|
$"Expected tick-0 Box3D raycast height ({hitTick0.Point.Y:F3}) to match restored base ({openSpot.BaseH:F3})");
|
|
|
|
// Step across ticks so recycled slots remain healthy without exhausting world capacity
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|