RustToDust Phase 1: Core simulation, Ymir Box3D arena, and behavioral tests

This commit is contained in:
Meta Vens 2026-12-04 13:47:43 +01:00
commit 1e6efd7b55
15 changed files with 3870 additions and 0 deletions

View file

@ -0,0 +1,91 @@
using System;
using RustToDust.Core;
using RustToDust.Ymir;
using Xunit;
namespace RustToDust.Tests
{
public sealed class ArenaSessionTests
{
[Fact]
public void DrivingAndFiring_MovesBuggyInYmirAndBuildsWeaponHeat()
{
using var session = new ArenaSession(seed: 42);
var player = session.State.Player;
float startX = player.X;
float startZ = player.Z;
float startYaw = player.Yaw;
for (int tick = 0; tick < 120; tick++)
{
session.Step(new PlayerInput(
Throttle: 1f,
Steer: 0.45f,
AimWorldX: player.X + 40f,
AimWorldZ: player.Z + 10f,
Fire: true,
InteractPickup: false,
Brake: false));
}
float dx = player.X - startX;
float dz = player.Z - startZ;
float displacement = MathF.Sqrt(dx * dx + dz * dz);
float yawDelta = MathF.Abs(MatchRules.NormalizeAngle(player.Yaw - startYaw));
Assert.True(displacement > 10f, $"Expected >10m displacement, got {displacement:F2}m");
Assert.True(yawDelta > 0.2f, $"Expected yaw change >0.2 rad, got {yawDelta:F2} rad");
var weapon = player.Grid.GetMountedOn("wpn_roof");
Assert.NotNull(weapon);
Assert.True(weapon!.CurrentTemp > ChassisGrid.AmbientTemp + 5f);
Assert.NotEmpty(session.State.Tracers);
}
[Fact]
public void ChassisSwap_ToManticoreAndRaptor_ReplacesBodyWithoutLeakingYmirLanes()
{
using var session = new ArenaSession(seed: 77);
var player = session.State.Player;
int initialFreeSlots = session.World.FreeBodySlots;
// Step once to settle world
session.Step(new PlayerInput(0f, 0f, 0f, 0f, false, false, false));
// Spawn Manticore (Dorsal) right beside player and swap
session.State.SpawnChassisDrop(player.X + 1f, player.Z + 1f, Catalog.Manticore);
Assert.True(session.TrySwapNearbyChassis());
Assert.Equal("manticore", player.Grid.Spec.Id);
Assert.Equal(GridPlane.Dorsal, player.Grid.Spec.Plane);
// Step so Ymir reclaims old tombstone lane
session.Step(new PlayerInput(0f, 0f, 0f, 0f, false, false, false));
Assert.Equal(initialFreeSlots, session.World.FreeBodySlots);
// Now spawn Raptor (Coronal) beside player and swap
session.State.SpawnChassisDrop(player.X + 1f, player.Z + 1f, Catalog.Raptor);
Assert.True(session.TrySwapNearbyChassis());
Assert.Equal("raptor", player.Grid.Spec.Id);
Assert.Equal(GridPlane.Coronal, player.Grid.Spec.Plane);
session.Step(new PlayerInput(0f, 0f, 0f, 0f, false, false, false));
Assert.Equal(initialFreeSlots, session.World.FreeBodySlots);
}
[Fact]
public void Raycast_RespectsStaticCoverWalls()
{
using var session = new ArenaSession(seed: 12);
// Cover block #0 sits at (28, 1.8, 16) with half-extents (4.5, 1.8, 1.4).
// A ray from one side (28, 1.0, 10) to the other side (28, 1.0, 22) must hit the static cover wall!
var blockedHit = session.World.CastRayClosest((28f, 1.0f, 10f), (0f, 0f, 15f));
Assert.True(blockedHit.Hit);
Assert.True(blockedHit.Point.Z < 16f, $"Expected cover hit before z=16, got z={blockedHit.Point.Z:F2}");
Assert.InRange(blockedHit.StableId, 10UL, 99UL); // Static cover StableId range
// Ray high above the cover block (y = 5.0 > 3.6 top) passes unobstructed
var clearHit = session.World.CastRayClosest((28f, 5.0f, 10f), (0f, 0f, 15f));
Assert.False(clearHit.Hit);
}
}
}

View file

@ -0,0 +1,128 @@
using System.Collections.Generic;
using RustToDust.Core;
using Xunit;
namespace RustToDust.Tests
{
public sealed class GridHeatTests
{
[Fact]
public void Conduction_SpreadsHeatFromHotHardpointToNeighbors()
{
var grid = Catalog.CreateStarterBuggy();
// wpn_roof is at (1, 0). Neighbor (0, 0) and (1, 1) start at ambient 20f.
grid.InjectHardpointHeat("wpn_roof", 600f);
float hotBefore = grid.Temperature[1, 0];
Assert.True(hotBefore > ChassisGrid.AmbientTemp + 20f);
Assert.Equal(ChassisGrid.AmbientTemp, grid.Temperature[0, 0]);
Assert.Equal(ChassisGrid.AmbientTemp, grid.Temperature[1, 1]);
for (int i = 0; i < 30; i++)
{
grid.StepHeat(1f / 60f);
}
Assert.True(grid.Temperature[0, 0] > ChassisGrid.AmbientTemp + 1f);
Assert.True(grid.Temperature[1, 1] > ChassisGrid.AmbientTemp + 1f);
Assert.True(grid.Temperature[1, 0] < hotBefore);
}
[Fact]
public void Radiation_CoolsBorderCellsWhileInteriorCellsDoNotRadiateDirectly()
{
var grid = new ChassisGrid(Catalog.Tank); // 4x5 full grid: interior cells exist at (1..2, 1..3)
for (int y = 0; y < grid.Spec.Height; y++)
{
for (int x = 0; x < grid.Spec.Width; x++)
{
grid.Temperature[x, y] = 120f;
}
}
// Single 60Hz substep: neighbors are all 120f so net conduction is 0.
// Only BorderCells lose heat to radiation; interior cell (1, 2) must stay at 120f.
grid.StepHeat(1f / 60f);
Assert.True(grid.BorderCells[0, 0]);
Assert.False(grid.BorderCells[1, 2]);
Assert.True(grid.Temperature[0, 0] < 120f);
Assert.Equal(120f, grid.Temperature[1, 2], precision: 5);
}
[Fact]
public void ModulePerformance_MatchesTrapezoidalPlateauCurve()
{
var mod = Catalog.Mg762; // Min=0, Optimal=65, Plateau=70 => [30, 100], Max=155
Assert.Equal(0f, mod.Performance(-5f));
Assert.Equal(0f, mod.Performance(0f));
Assert.Equal(0.5f, mod.Performance(15f), precision: 4);
Assert.Equal(1f, mod.Performance(30f), precision: 4);
Assert.Equal(1f, mod.Performance(65f), precision: 4);
Assert.Equal(1f, mod.Performance(100f), precision: 4);
Assert.Equal(0.5f, mod.Performance(127.5f), precision: 4);
Assert.Equal(0f, mod.Performance(155f));
Assert.Equal(0f, mod.Performance(210f));
}
[Fact]
public void DorsalHit_FrontStrikesFrontRowAndRearStrikesRearRow()
{
var grid = new ChassisGrid(Catalog.Tank);
var frontHit = grid.ApplyHit(localRight: 0f, localForward: 1f, localHeightNorm: 0.5f, damage: 15f, heatTransfer: 0f);
var rearHit = grid.ApplyHit(localRight: 0f, localForward: -1f, localHeightNorm: 0.5f, damage: 15f, heatTransfer: 0f);
var leftHit = grid.ApplyHit(localRight: -1f, localForward: 0f, localHeightNorm: 0.5f, damage: 15f, heatTransfer: 0f);
var rightHit = grid.ApplyHit(localRight: 1f, localForward: 0f, localHeightNorm: 0.5f, damage: 15f, heatTransfer: 0f);
Assert.Equal(0, frontHit.CellY);
Assert.Equal(grid.Spec.Height - 1, rearHit.CellY);
Assert.Equal(0, leftHit.CellX);
Assert.Equal(grid.Spec.Width - 1, rightHit.CellX);
}
[Fact]
public void CoronalHit_HighVsLowElevationStrikesUpperVsLowerBipedCells()
{
var grid = new ChassisGrid(Catalog.Raptor);
float initialLegs = grid.LegIntegrity;
Assert.Equal(1f, initialLegs);
var highHit = grid.ApplyHit(localRight: 0f, localForward: 1f, localHeightNorm: 0.95f, damage: 20f, heatTransfer: 0f);
var lowHit = grid.ApplyHit(localRight: -0.4f, localForward: 1f, localHeightNorm: 0.05f, damage: 35f, heatTransfer: 0f);
Assert.True(highHit.CellY <= 1);
Assert.True(lowHit.CellY >= grid.Spec.Height - 2);
Assert.True(grid.LegIntegrity < initialLegs);
}
[Fact]
public void LooseFitAndSwapChassis_PreservesCompatibleGearAndStowsOverflow()
{
// Tank has 2x2 turret hardpoint: smaller 1x1 weapon fits loosely; 2x2 weapon must NOT fit 1x1 buggy roof.
var tankGrid = new ChassisGrid(Catalog.Tank);
Assert.True(tankGrid.TryMount(Catalog.Recoilless, "wpn_turret")); // 2x2 into 2x2
Assert.True(tankGrid.TryMount(Catalog.Mg762, "wpn_coax")); // 1x1 into 1x1
Assert.True(tankGrid.TryMount(Catalog.TurboCore, "eng_rear")); // 1x2 into 2x2
Assert.True(tankGrid.TryMount(Catalog.GoldRadiator, "rad_deck")); // 1x1 into 1x1
Assert.True(tankGrid.TryMount(Catalog.WeldedPlate, "arm_glacis")); // 1x1 into 2x1
Assert.True(tankGrid.TryMount(Catalog.GildedPlate, "arm_skirt")); // 1x1 into 1x1
// Fill cargo near buggy capacity (4) so swapping down to Buggy overflows to ground!
Assert.True(tankGrid.TryAddCargo(Catalog.Flak));
Assert.True(tankGrid.TryAddCargo(Catalog.CopperRad));
List<ModuleSpec> dropped = tankGrid.SwapChassis(Catalog.Buggy);
// Buggy has 1x1 Weapon (mg_762 fits, recoilless 2x2 does not!), 1x1 Engine (turbo_core 1x2 does not fit!), 1x1 Radiator (gold_radiator fits!), 0 Armor hardpoints.
Assert.Equal("buggy", tankGrid.Spec.Id);
Assert.Contains(tankGrid.Mounted, m => m.Spec.Id == "mg_762");
Assert.Contains(tankGrid.Mounted, m => m.Spec.Id == "gold_radiator");
Assert.DoesNotContain(tankGrid.Mounted, m => m.Spec.Id == "recoilless");
// 6 mounted on Tank: 2 transferred (mg_762, gold_radiator), 4 leftover + 2 cargo = 6 items -> 4 in Buggy Cargo, 2 dropped!
Assert.Equal(Catalog.Buggy.CargoCapacity, tankGrid.Cargo.Count);
Assert.Equal(2, dropped.Count);
}
}
}

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\RustToDust.Core\RustToDust.Core.csproj" />
<ProjectReference Include="..\..\src\RustToDust.Ymir\RustToDust.Ymir.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,94 @@
using System;
using System.Linq;
using RustToDust.Core;
using Xunit;
namespace RustToDust.Tests
{
public sealed class SpectatorRingTests
{
[Fact]
public void Clearance_ZeroInsideSafeCircle_MonotonicOutside()
{
var ring = new SpectatorRing(seed: 101);
float r = ring.SafeRadius; // 150f
// Inside safe circle -> 0 cleared
ring.UpdateClearance(new[] { new RingContact(0, r - 20f, 0f, true) });
Assert.Equal(0, ring.CountClearedFor(0));
// Just outside safe circle (+5m) -> 1 cleared
ring.UpdateClearance(new[] { new RingContact(0, r + 5f, 0f, true) });
int clearedNear = ring.CountClearedFor(0);
Assert.Equal(1, clearedNear);
// Further outside (+30m) -> 1 + floor(30/12) = 3 cleared
ring.UpdateClearance(new[] { new RingContact(0, r + 30f, 0f, true) });
int clearedMid = ring.CountClearedFor(0);
Assert.True(clearedMid > clearedNear);
// Far outside (+100m) -> capped at 8 cleared
ring.UpdateClearance(new[] { new RingContact(0, r + 100f, 0f, true) });
int clearedFar = ring.CountClearedFor(0);
Assert.Equal(8, clearedFar);
// Stepping back inside safe circle revokes non-provoked clearance back to 0
ring.UpdateClearance(new[] { new RingContact(0, 0f, 0f, true) });
Assert.Equal(0, ring.CountClearedFor(0));
}
[Fact]
public void Provocation_AttackingNeutralSpectatorLocksOntoAttackerEvenInsideSafeCircle()
{
var ring = new SpectatorRing(seed: 202);
var targetSpec = ring.Spectators[0];
Assert.Equal(SpectatorStance.Neutral, targetSpec.Stance);
// Contender 0 is dead center (well inside SafeRadius)
var contenders = new[] { new RingContact(0, 0f, 0f, true) };
ring.UpdateClearance(contenders);
Assert.Equal(0, ring.CountClearedFor(0));
bool provoked = ring.OnSpectatorDamaged(targetSpec, attackerContenderId: 0);
Assert.True(provoked);
Assert.Equal(SpectatorStance.Provoked, targetSpec.Stance);
Assert.Equal(0, targetSpec.TargetContenderId);
// Subsequent clearance updates inside the safe circle must NOT clear Provoked lock
ring.UpdateClearance(contenders);
Assert.Equal(SpectatorStance.Provoked, targetSpec.Stance);
Assert.Equal(1, ring.CountProvokedFor(0));
// Once attacker dies, provocation reverts to Neutral
ring.UpdateClearance(new[] { new RingContact(0, 0f, 0f, false) });
Assert.Equal(SpectatorStance.Neutral, targetSpec.Stance);
Assert.Null(targetSpec.TargetContenderId);
}
[Fact]
public void SalvageDrops_TouristDropsImperialGearWhileExemplarDoesNot()
{
var ring = new SpectatorRing(seed: 303);
var rng = new Random(99);
var tourists = ring.Spectators.Where(s => s.Tier == SpectatorTier.Tourist).ToList();
var exemplars = ring.Spectators.Where(s => s.Tier == SpectatorTier.Exemplar).ToList();
Assert.NotEmpty(tourists);
Assert.NotEmpty(exemplars);
foreach (var tourist in tourists)
{
var drops = SpectatorRing.RollSalvageDrops(tourist, rng);
Assert.InRange(drops.Count, 1, 2);
Assert.All(drops, d => Assert.True(d.IsImperial));
}
foreach (var exemplar in exemplars)
{
var drops = SpectatorRing.RollSalvageDrops(exemplar, rng);
Assert.DoesNotContain(drops, d => d.IsImperial);
}
}
}
}