RustToDust/tests/RustToDust.Tests/GridHeatTests.cs

128 lines
6 KiB
C#

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);
}
}
}