159 lines
5.8 KiB
C#
159 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
|
|
namespace RustToDust.Client.Tests;
|
|
|
|
/// <summary>
|
|
/// Headless Godot smoke test: instantiates Main, drives 60 frames with throttle + fire + crater stamp,
|
|
/// verifies terrain tile baking and Ymir physics integration, and exits with code 0 on pass.
|
|
/// <c>godot --headless --path client res://tests/Smoke.tscn</c>
|
|
/// </summary>
|
|
public partial class Smoke : Node
|
|
{
|
|
private const double WatchdogSeconds = 60.0;
|
|
|
|
private readonly List<string> _failures = new();
|
|
private Main _main = null!;
|
|
private bool _done;
|
|
|
|
public override void _Ready() => _ = RunAsync();
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (!_done && Time.GetTicksMsec() > WatchdogSeconds * 1000.0)
|
|
{
|
|
Check(false, $"Smoke watchdog timed out after {WatchdogSeconds}s");
|
|
Finish();
|
|
}
|
|
}
|
|
|
|
private async Task RunAsync()
|
|
{
|
|
try
|
|
{
|
|
_main = GD.Load<PackedScene>("res://Main.tscn").Instantiate<Main>();
|
|
AddChild(_main);
|
|
|
|
if (_main.Failure is { } failure)
|
|
{
|
|
Check(false, "Main failed to initialize ArenaSession: " + failure);
|
|
}
|
|
else
|
|
{
|
|
await RunScenarioAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Check(false, ex.ToString());
|
|
}
|
|
|
|
Finish();
|
|
}
|
|
|
|
private async Task RunScenarioAsync()
|
|
{
|
|
Check(Engine.PhysicsTicksPerSecond == 60, $"Expected 60 physics ticks/sec, got {Engine.PhysicsTicksPerSecond}");
|
|
Check((string)ProjectSettings.GetSetting("physics/3d/physics_engine") == "Dummy", "Godot 3D physics_engine is not Dummy");
|
|
Check(!_main.GetWorld3D().Space.IsValid, "Godot should have no active 3D physics space");
|
|
Check(_main.Terrain.BakedTileCount == 16, $"Expected 16 baked terrain tiles, got {_main.Terrain.BakedTileCount}");
|
|
|
|
var player = _main.Session.State.Player;
|
|
float startX = player.X;
|
|
float startZ = player.Z;
|
|
|
|
_main.SetScriptedInput(new PlayerInput(
|
|
Throttle: 1f,
|
|
Steer: 0.25f,
|
|
AimWorldX: startX + 35f,
|
|
AimWorldZ: startZ + 10f,
|
|
Fire: true,
|
|
InteractPickup: false,
|
|
Brake: false));
|
|
|
|
for (int i = 0; i < 60; i++)
|
|
{
|
|
await ToSignal(GetTree(), SceneTree.SignalName.PhysicsFrame);
|
|
if (_main.Failure is { } f)
|
|
{
|
|
Check(false, "Session failure during physics loop: " + f);
|
|
return;
|
|
}
|
|
}
|
|
|
|
float dx = player.X - startX;
|
|
float dz = player.Z - startZ;
|
|
float displacement = MathF.Sqrt(dx * dx + dz * dz);
|
|
Check(displacement > 3f, $"Expected player displacement > 3m after 60 frames, got {displacement:F2}m");
|
|
Check(!float.IsNaN(player.X) && !float.IsNaN(player.Y) && !float.IsNaN(player.Z), "Player position became NaN");
|
|
|
|
// Pause physics during the isolated crater check so AI contender track stamps in other tiles do not race it.
|
|
_main.SetScriptedInput(null);
|
|
_main.SetPhysicsProcess(false);
|
|
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
|
|
|
bool isHeadless = DisplayServer.GetName() == "headless";
|
|
int rebakesBefore = _main.Terrain.DirtyRebakes;
|
|
float cpuBefore = _main.Session.State.Terrain.SampleHeight(-48f, -48f);
|
|
float gpuBefore = 0f;
|
|
if (!isHeadless)
|
|
{
|
|
bool hasGpuBefore = _main.Terrain.TrySampleBakedHeight(-48f, -48f, out gpuBefore);
|
|
Check(hasGpuBefore, "Expected GPU HeightViewport readback to succeed before crater");
|
|
if (hasGpuBefore)
|
|
{
|
|
Check(MathF.Abs(gpuBefore - cpuBefore) <= 0.25f,
|
|
$"Expected baked GPU height ({gpuBefore:F2}m) to match CPU height ({cpuBefore:F2}m) before crater");
|
|
}
|
|
}
|
|
|
|
_main.Session.State.Terrain.StampCrater(-48f, -48f, radius: 5f, depth: 1.4f);
|
|
_main.Terrain.Sync(_main.Session.State.Terrain);
|
|
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
|
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
|
|
|
float cpuAfter = _main.Session.State.Terrain.SampleHeight(-48f, -48f);
|
|
Check(cpuAfter < cpuBefore - 0.8f,
|
|
$"Expected CPU terrain height to drop after crater stamp (before {cpuBefore:F2}m, after {cpuAfter:F2}m)");
|
|
Check(_main.Terrain.DirtyRebakes == rebakesBefore + 1,
|
|
$"Expected 1 dirty tile rebake from isolated crater stamp, got {_main.Terrain.DirtyRebakes - rebakesBefore}");
|
|
|
|
if (!isHeadless)
|
|
{
|
|
bool hasGpuAfter = _main.Terrain.TrySampleBakedHeight(-48f, -48f, out float gpuAfter);
|
|
Check(hasGpuAfter, "Expected GPU HeightViewport readback to succeed after crater");
|
|
if (hasGpuAfter)
|
|
{
|
|
Check(MathF.Abs(gpuAfter - cpuAfter) <= 0.25f,
|
|
$"Expected baked GPU height ({gpuAfter:F2}m) to match CPU height ({cpuAfter:F2}m) after crater");
|
|
Check(gpuAfter < gpuBefore - 0.8f,
|
|
$"Expected baked GPU height to drop after crater (before {gpuBefore:F2}m, after {gpuAfter:F2}m)");
|
|
}
|
|
}
|
|
|
|
_main.SetPhysicsProcess(true);
|
|
}
|
|
|
|
private void Check(bool condition, string message)
|
|
{
|
|
if (!condition)
|
|
{
|
|
_failures.Add(message);
|
|
}
|
|
}
|
|
|
|
private void Finish()
|
|
{
|
|
if (_done) return;
|
|
_done = true;
|
|
foreach (var f in _failures)
|
|
{
|
|
GD.PrintErr("SMOKE FAIL: " + f);
|
|
}
|
|
GD.Print(_failures.Count == 0 ? "SMOKE PASS" : $"SMOKE FAILED ({_failures.Count})");
|
|
GetTree().Quit(_failures.Count == 0 ? 0 : 1);
|
|
}
|
|
}
|