Cut 13d: Real Sion species trees (cosmo_tree_gen, rtd_flow_field wind, Box3D & destruction)

This commit is contained in:
Meta Vens 2026-12-06 03:52:02 +01:00
commit 9446c5bc7b
154 changed files with 10321 additions and 29 deletions

View file

@ -0,0 +1,301 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Numerics;
using System.Text.Json;
using RustToDust.Core;
using RustToDust.Net;
using RustToDust.Ymir;
using Xunit;
using Ymir.Box3D;
namespace RustToDust.Tests
{
public sealed class SionTreeTests
{
[Fact]
public void SionTreeCatalog_LoadsWithoutBeechExcludesBuildingsAndMatchesArbresRemarquables()
{
var sion = SionMapLoader.Load();
var catalog = sion.Trees;
Assert.InRange(catalog.Count, 500, SionTreeCatalog.MaxTrees);
// Check binary disk budget + tree assets directory <= 12.4 MiB
string treesBin = Path.Combine(sion.DirectoryPath, "trees_sion.bin");
string treesDir = Path.Combine(sion.DirectoryPath, "trees");
long totalBytes = new FileInfo(treesBin).Length;
foreach (var f in Directory.EnumerateFiles(treesDir, "*", SearchOption.AllDirectories))
{
totalBytes += new FileInfo(f).Length;
}
Assert.True(totalBytes <= 13_000_000L, $"Expected Sion tree assets + bin <= 12.4 MiB, got {totalBytes:N0} B");
bool hasSequoia = false;
bool hasCedrus = false;
bool hasMonumentalPlane = false;
// Planta d'en Haut Giant Sequoia: LV95 (2593636.1374, 1120075.402) -> (-513.8626, -25.402)
float seqWx = 2593636.1374f - 2594150.0f;
float seqWz = 1120050.0f - 1120075.402f;
// Pratifori / Cimetiere Lebanon Cedar: LV95 (2593522.9419, 1119968.4164) -> (-627.0581, 81.5836)
float cedWx = 2593522.9419f - 2594150.0f;
float cedWz = 1120050.0f - 1119968.4164f;
// Planta d'en Haut Monumental Avenue (Aesculus/PlatanusMonumental archetype #9): LV95 (2593619.7614, 1120373.0299)
float plaWx = 2593619.7614f - 2594150.0f;
float plaWz = 1120050.0f - 1120373.0299f;
for (int i = 0; i < catalog.Count; i++)
{
var t = catalog.Trees[i];
Assert.True((byte)t.Species <= (byte)SionTreeSpecies.PlatanusMonumental);
string spName = t.Species.ToString();
Assert.DoesNotContain("Fagus", spName, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("Beech", spName, StringComparison.OrdinalIgnoreCase);
Assert.False(sion.IsInsideAnyBuildingXz(t.X, t.Z, 0f),
$"Tree {t.TreeId} ({t.Species}) placed inside building at ({t.X:F2}, {t.Z:F2})");
Assert.InRange(t.HeightM, 4.0f, 38.0f);
Assert.InRange(t.TrunkRadiusM, 0.10f, 1.20f);
if (t.Species == SionTreeSpecies.Sequoiadendron &&
MathF.Sqrt((t.X - seqWx) * (t.X - seqWx) + (t.Z - seqWz) * (t.Z - seqWz)) < 0.25f)
{
hasSequoia = true;
}
if (t.Species == SionTreeSpecies.Cedrus &&
MathF.Sqrt((t.X - cedWx) * (t.X - cedWx) + (t.Z - cedWz) * (t.Z - cedWz)) < 0.25f)
{
hasCedrus = true;
}
if (t.Species == SionTreeSpecies.PlatanusMonumental &&
MathF.Sqrt((t.X - plaWx) * (t.X - plaWx) + (t.Z - plaWz) * (t.Z - plaWz)) < 0.25f)
{
hasMonumentalPlane = true;
}
}
Assert.True(hasSequoia, "Expected Planta d'en Haut Sequoiadendron at exact Sion coordinates");
Assert.True(hasCedrus, "Expected Lebanon Cedrus at exact Sion coordinates");
Assert.True(hasMonumentalPlane, "Expected En Ville PlatanusMonumental at exact Sion coordinates");
}
[Fact]
public void SionTreeDestruction_OrdnanceSnapsAndChars_UpdatesBox3DColliderPersistsAndHeals()
{
string tempDir = Path.Combine(Path.GetTempPath(), "rtd-cut13d-tree-scar-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempDir);
string scarPath = Path.Combine(tempDir, "sion_tree_scars.cultcache");
try
{
ushort targetId = 0;
SionTreePlacement targetTree = default;
bool foundTarget = false;
using (var host = new ServerMatchHost(
IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Sion,
autoRestartSec: 0, scarFilePath: scarPath, persistScars: true))
{
var trees = host.Session.State.SionTrees!;
var catalog = trees.Catalog;
// Pick an isolated solid tree (TrunkRadiusM >= 0.28m) with no nearby buildings/neighbors on the ray
for (int i = 0; i < catalog.Count; i++)
{
var t = catalog.Trees[i];
if (t.TrunkRadiusM < 0.28f || t.IsMonumental) continue;
if (host.Session.State.SionMap!.IsInsideAnyBuildingXz(t.X, t.Z, 12f)) continue;
targetTree = t;
targetId = t.TreeId;
foundTarget = true;
break;
}
Assert.True(foundTarget, "Expected a solid non-monumental tree outside buildings");
host.Session.TileSync.EnsureTreeTileResident(host.Session.World, trees, targetTree.Tx, targetTree.Tz);
ulong expectedStableId = Box3DPhysicsTileSync.TreeTileStableIdBase + (ulong)targetTree.TileIndex;
// Horizontal ray through the trunk must hit the Box3D tree tile mesh
var rayOrigin = new Vector3(targetTree.X - 4.0f, targetTree.Y + 1.4f, targetTree.Z);
var raySpan = new Vector3(8.0f, 0f, 0f);
var preHit = host.Session.CastWorldRayClosest(rayOrigin, raySpan);
Assert.True(preHit.Hit, "Expected horizontal ray to strike solid tree trunk collider");
Assert.Equal(expectedStableId, preHit.StableId);
// Artillery blast at trunk base snaps target tree; verify outer char radius on a second tree
var hitEvents = new List<TreeStateEvent>();
int changed = trees.ApplyBlast(new Vector3(targetTree.X, targetTree.Y + 0.5f, targetTree.Z), splashRadius: 10f, blastDamage: 240f, hitEvents);
Assert.True(changed >= 1);
Assert.Equal(SionTreeState.Snapped, trees.GetState(targetId));
foreach (var ev in hitEvents)
{
host.Session.State.ScarLedger.RecordTreeState(ev.TreeId, ev.State);
}
// Flush dirty tree tiles -> Box3D collider for snapped trunk must be gone
host.Session.TileSync.FlushDirtyTreeTiles(host.Session.World, trees, maxTilesPerTick: 4);
var postHit = host.Session.CastWorldRayClosest(rayOrigin, raySpan);
Assert.False(postHit.Hit && postHit.StableId == expectedStableId &&
MathF.Abs(postHit.Point.X - targetTree.X) <= targetTree.TrunkRadiusM * 1.5f,
"Expected snapped tree trunk collider to be removed from Box3D tile mesh");
// Also test outer thermal char state on a distant intact tree
ushort charId = 0;
for (int i = 0; i < catalog.Count; i++)
{
var t = catalog.Trees[i];
if (t.TreeId == targetId || trees.GetState(t.TreeId) != SionTreeState.Intact) continue;
if (t.TrunkRadiusM < 0.30f) continue;
var evs = new List<TreeStateEvent>();
// Blast 8.5m away (outside snapCore, inside charRadius = 1.35 * 9.0) with moderate heat/blast
trees.ApplyBlast(new Vector3(t.X + 8.5f, t.Y + 1.0f, t.Z), splashRadius: 9.0f, blastDamage: 60f, evs);
if (trees.GetState(t.TreeId) == SionTreeState.Charred)
{
charId = t.TreeId;
host.Session.State.ScarLedger.RecordTreeState(charId, (byte)SionTreeState.Charred);
break;
}
}
Assert.NotEqual(0, charId);
// ResetMatch must preserve both Snapped and Charred tree scars
host.ResetMatch(43, ArenaMapKind.Sion);
Assert.Equal(SionTreeState.Snapped, host.Session.State.SionTrees!.GetState(targetId));
Assert.Equal(SionTreeState.Charred, host.Session.State.SionTrees!.GetState(charId));
// RepairArenaScars restores all trees to Intact and restores Box3D trunk collider
host.RepairArenaScars();
Assert.Equal(SionTreeState.Intact, host.Session.State.SionTrees!.GetState(targetId));
Assert.Equal(SionTreeState.Intact, host.Session.State.SionTrees!.GetState(charId));
host.Session.TileSync.EnsureTreeTileResident(host.Session.World, host.Session.State.SionTrees, targetTree.Tx, targetTree.Tz);
var healedHit = host.Session.CastWorldRayClosest(rayOrigin, raySpan);
Assert.True(healedHit.Hit && healedHit.StableId == expectedStableId,
"Expected repaired tree trunk collider to be restored in Box3D");
}
}
finally
{
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, recursive: true);
}
}
}
[Fact]
public void SionTreeDestruction_HeavyWalkerTramplesThinTreesButNotThickOrMonumental()
{
var sion = SionMapLoader.Load();
var trees = new SionTreeDestructionState(sion.Trees);
SionTreePlacement thin = default;
SionTreePlacement thick = default;
bool foundThin = false, foundThick = false;
foreach (var t in sion.Trees.Trees)
{
if (!foundThin && t.TrunkRadiusM >= 0.14f && t.TrunkRadiusM < SionTreeDestructionState.WalkerTrampleMaxTrunkRadiusM)
{
thin = t;
foundThin = true;
}
if (!foundThick && t.TrunkRadiusM >= 0.45f)
{
thick = t;
foundThick = true;
}
if (foundThin && foundThick) break;
}
Assert.True(foundThin && foundThick);
// Light < 30t mech does not trample
trees.ApplyWalkerTrample(thin.X, thin.Z, 2.0f, 20_000f);
Assert.Equal(SionTreeState.Intact, trees.GetState(thin.TreeId));
// Heavy >= 30t walker tramples thin (< 0.30m) tree
trees.ApplyWalkerTrample(thin.X, thin.Z, 2.0f, 35_000f);
Assert.Equal(SionTreeState.Snapped, trees.GetState(thin.TreeId));
// Heavy walker does NOT trample thick (>= 0.30m) / monumental trees
trees.ApplyWalkerTrample(thick.X, thick.Z, 2.0f, 65_000f);
Assert.Equal(SionTreeState.Intact, trees.GetState(thick.TreeId));
}
[Fact]
public void CultNet_RemoteClientSyncsTreeScarsInPersistentSnapshotBubbleAndRepair()
{
using var host = new ServerMatchHost(
IPAddress.Loopback, 0, seed: 42, ArenaMapKind.Sion,
autoRestartSec: 0, persistScars: false);
var hostTrees = host.Session.State.SionTrees!;
// Snap a historical tree before client joins
var histTree = hostTrees.Catalog.Trees[0];
host.Session.State.ApplyTreeState(histTree.TreeId, SionTreeState.Snapped, recordInLedger: true);
using var client = new RemoteArenaSession(new IPEndPoint(IPAddress.Loopback, host.BoundPort), "TreeTester");
long nowMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
for (int i = 0; i < 120 && !client.IsJoined; i++)
{
nowMs += 16;
host.StepOnce(nowMs);
client.BeginRenderFrame(1f / 60f, 0f);
System.Threading.Thread.Sleep(5);
}
Assert.True(client.IsJoined, "Remote client failed to join Sion ServerMatchHost");
Assert.Equal(SionTreeState.Snapped, client.ViewState.SionTrees!.GetState(histTree.TreeId));
// Find a tree near the remote client's local contender (within 576m local geometry bubble)
var local = client.LocalContender;
SionTreePlacement liveTree = default;
bool foundLive = false;
foreach (var t in hostTrees.Catalog.Trees)
{
if (t.TreeId == histTree.TreeId || t.TrunkRadiusM < 0.25f) continue;
float dx = t.X - local.X;
float dz = t.Z - local.Z;
float dist = MathF.Sqrt(dx * dx + dz * dz);
if (dist <= 500f)
{
liveTree = t;
foundLive = true;
break;
}
}
Assert.True(foundLive, "Expected a solid tree within 500m of local contender");
// Snap it via Ordnance sink on host so TreeStateEvent flows through the 576m bubble
((IOrdnanceTargetSink)host.Session).ApplyTreeImpact(
Box3DPhysicsTileSync.TreeTileStableIdBase + (ulong)liveTree.TileIndex,
new Vector3(liveTree.X, liveTree.Y + 0.5f, liveTree.Z),
splashRadius: 6.0f,
damage: 250f);
for (int i = 0; i < 60 && client.ViewState.SionTrees!.GetState(liveTree.TreeId) != SionTreeState.Snapped; i++)
{
nowMs += 16;
host.StepOnce(nowMs);
client.BeginRenderFrame(1f / 60f, 0f);
System.Threading.Thread.Sleep(5);
}
Assert.Equal(SionTreeState.Snapped, client.ViewState.SionTrees!.GetState(liveTree.TreeId));
// RepairArenaScars propagates to remote client
host.RepairArenaScars();
for (int i = 0; i < 60 && client.ViewState.SionTrees!.GetState(liveTree.TreeId) != SionTreeState.Intact; i++)
{
nowMs += 16;
host.StepOnce(nowMs);
client.BeginRenderFrame(1f / 60f, 0f);
System.Threading.Thread.Sleep(5);
}
Assert.Equal(SionTreeState.Intact, client.ViewState.SionTrees!.GetState(histTree.TreeId));
Assert.Equal(SionTreeState.Intact, client.ViewState.SionTrees!.GetState(liveTree.TreeId));
}
}
}