RustToDust/tests/RustToDust.Tests/VehiclePackageTests.cs

138 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using RustToDust.Core;
using Xunit;
namespace RustToDust.Tests
{
public sealed class VehiclePackageTests
{
private static readonly string[] Expected15Ids =
{
"jackal", "mule", "ferret", "hornet", "mammoth",
"ironclad", "anvil", "condor", "breaker", "paladin",
"raptor", "manticore", "bastion", "spectre", "kestrel"
};
[Fact]
public void All15VehicleArchives_LoadFromCultCacheAndMatchManifests()
{
Assert.Equal(15, Catalog.AllChassis.Count);
Assert.Equal(15, Catalog.PackagesById.Count);
string vehiclesRoot = VehicleCatalogLoader.LocateVehiclesDirectory();
foreach (string id in Expected15Ids)
{
string ccPath = Path.Combine(vehiclesRoot, id, "vehicle.cc");
string manifestPath = Path.Combine(vehiclesRoot, id, "manifest.json");
Assert.True(File.Exists(ccPath), $"Missing vehicle.cc for {id}");
Assert.True(File.Exists(manifestPath), $"Missing manifest.json for {id}");
VehiclePackage pkg = VehiclePackageStore.Read(ccPath);
Assert.Equal(id, pkg.Id);
Assert.True(File.Exists(Path.Combine(vehiclesRoot, id, pkg.ModelAsset)), $"Missing GLB {pkg.ModelAsset}");
using var doc = JsonDocument.Parse(File.ReadAllText(manifestPath));
var root = doc.RootElement;
Assert.Equal(root.GetProperty("gridWidth").GetInt32(), pkg.GridWidth);
Assert.Equal(root.GetProperty("gridHeight").GetInt32(), pkg.GridHeight);
var manifestHps = root.GetProperty("hardpoints").EnumerateArray().ToArray();
Assert.Equal(manifestHps.Length, pkg.Hardpoints.Count);
for (int i = 0; i < manifestHps.Length; i++)
{
string expectedId = manifestHps[i].GetProperty("id").GetString()!;
string expectedAnchor = manifestHps[i].GetProperty("modelNodeId").GetString()!;
Assert.Equal(expectedId, pkg.Hardpoints[i].Id);
Assert.Equal(expectedAnchor, pkg.Hardpoints[i].AnchorId);
}
var manifestJoints = root.GetProperty("joints").EnumerateArray().ToArray();
Assert.Equal(manifestJoints.Length, pkg.Joints.Count);
for (int i = 0; i < manifestJoints.Length; i++)
{
Assert.Equal(manifestJoints[i].GetProperty("id").GetString(), pkg.Joints[i].Id);
}
var boundsSize = root.GetProperty("bounds").GetProperty("size").EnumerateArray().Select(e => e.GetSingle()).ToArray();
Assert.Equal(boundsSize[0] * 0.5f, pkg.HalfWidth, precision: 3);
Assert.Equal(boundsSize[1] * 0.5f, pkg.HalfHeight, precision: 3);
Assert.Equal(boundsSize[2] * 0.5f, pkg.HalfLength, precision: 3);
Assert.NotEmpty(pkg.PartCells);
var spec = pkg.ToChassisSpec();
Assert.All(pkg.PartCells, pc =>
{
Assert.False(string.IsNullOrWhiteSpace(pc.NodeName));
Assert.True(spec.IsActive(pc.CellX, pc.CellY), $"{id}: PartCell '{pc.NodeName}' mapped to inactive ({pc.CellX},{pc.CellY})");
});
}
}
[Fact]
public void BreakerArchive_RoundTripsFullMechanismHierarchy()
{
string vehiclesRoot = VehicleCatalogLoader.LocateVehiclesDirectory();
string manifestPath = Path.Combine(vehiclesRoot, "breaker", "manifest.json");
using var doc = JsonDocument.Parse(File.ReadAllText(manifestPath));
var mechJson = doc.RootElement.GetProperty("mechanisms");
var breaker = Catalog.PackagesById["breaker"];
Assert.NotNull(breaker.Mechanisms);
Assert.Equal(mechJson.GetProperty("dofs").GetArrayLength(), breaker.Mechanisms!.Dofs.Count);
Assert.Equal(35, breaker.Mechanisms.Dofs.Count);
Assert.Equal(mechJson.GetProperty("rams").GetArrayLength(), breaker.Mechanisms.Rams.Count);
Assert.Equal(4, breaker.Mechanisms.Rams.Count);
Assert.Equal(2, breaker.Mechanisms.Scissors.Count);
Assert.Equal(2, breaker.Mechanisms.Struts.Count);
Assert.Equal("LegL.Hip", breaker.Mechanisms.Dofs[0].Name);
Assert.Equal(
new[] { "LegL.RecessRam0", "LegL.RecessRam1", "LegR.RecessRam0", "LegR.RecessRam1" },
breaker.Mechanisms.Rams.Select(r => r.Name).ToArray());
Assert.Equal("LegL.Torque", breaker.Mechanisms.Scissors[0].Name);
Assert.Equal("LegL.Oleo", breaker.Mechanisms.Struts[0].Name);
Assert.Equal(0.2f, breaker.Mechanisms.Struts[0].Travel, precision: 4);
}
[Fact]
public void AliasesAndVtolBalance_MatchSpecification()
{
Assert.Same(Catalog.Jackal, Catalog.Buggy);
Assert.Same(Catalog.Mule, Catalog.Technical);
Assert.Same(Catalog.Anvil, Catalog.Tank);
// VTOL glass-cannon balance vs ground equivalents
Assert.Equal(MobilityKind.Vtol, Catalog.Hornet.Mobility);
Assert.Equal(MobilityKind.Vtol, Catalog.Condor.Mobility);
Assert.Equal(42f, Catalog.Hornet.HoverMaxAltitudeM);
Assert.Equal(36f, Catalog.Condor.HoverMaxAltitudeM);
Assert.Equal(0f, Catalog.Jackal.HoverMaxAltitudeM);
Assert.Equal(0f, Catalog.Anvil.HoverMaxAltitudeM);
Assert.True(Catalog.Hornet.BaseCellArmor < Catalog.Jackal.BaseCellArmor,
$"Hornet armor ({Catalog.Hornet.BaseCellArmor}) must be lighter than Jackal ({Catalog.Jackal.BaseCellArmor})");
Assert.True(Catalog.Hornet.BaseCellArmor < Catalog.Ferret.BaseCellArmor,
$"Hornet armor ({Catalog.Hornet.BaseCellArmor}) must be lighter than Ferret ({Catalog.Ferret.BaseCellArmor})");
Assert.True(Catalog.Condor.BaseCellArmor < Catalog.Ironclad.BaseCellArmor,
$"Condor armor ({Catalog.Condor.BaseCellArmor}) must be lighter than Ironclad ({Catalog.Ironclad.BaseCellArmor})");
Assert.True(Catalog.Condor.BaseCellArmor < Catalog.Anvil.BaseCellArmor,
$"Condor armor ({Catalog.Condor.BaseCellArmor}) must be lighter than Anvil ({Catalog.Anvil.BaseCellArmor})");
// All 14 non-starter chassis spawn as salvageable drops in MatchState
var state = new MatchState(seed: 42);
var droppedChassisIds = state.GroundDrops
.Where(d => d.Chassis is not null)
.Select(d => d.Chassis!.Id)
.ToHashSet(StringComparer.Ordinal);
foreach (string id in Expected15Ids.Where(x => x != "jackal"))
{
Assert.Contains(id, droppedChassisIds);
}
}
}
}