Merge nibu/opens-tests into nibu/opens-econ

This commit is contained in:
Nibu 2026-11-11 06:35:35 +00:00
commit 782aee4ae5
7 changed files with 82 additions and 22 deletions

View file

@ -109,7 +109,9 @@ public abstract class Weapon : ActivatedBehavior
public virtual bool HasMagazine => true;
// What the HUD's ammunition label shows: "magazine/reserve" for a magazine weapon, the reserve alone for one without a magazine, empty when
// the weapon draws no ammunition. The one rule the HUD reads, so the label cannot show a magazine a weapon does not have.
public string AmmoReadout => !_data.AmmoType.IsSet() ? "" : HasMagazine ? $"{Ammo}/{Reserve}" : Reserve.ToString();
public string AmmoReadout => !ShowsAmmoReadout ? "" : HasMagazine ? $"{Ammo}/{Reserve}" : Reserve.ToString();
// Whether the HUD shows the ammunition label at all (and not the infinite-ammunition icon): the weapon names an AmmoType.
public bool ShowsAmmoReadout => _data.AmmoType.IsSet();
public WeaponData WeaponData => _data;
public float RangeFactor(float at) => WeaponData.RangeFactor(MinRange, EffectiveRange, Range, _data.DamageAtMaxRange, at);

View file

@ -132,8 +132,8 @@ public class SchematicDisplay : MonoBehaviour
//x.ListElement.Label.text = x.Item.EquippableItem.Name;
if (!_enemy)
{
x.ListElement.InfiniteAmmoIcon.gameObject.SetActive(!x.Weapon.WeaponData.AmmoType.IsSet());
x.ListElement.AmmoLabel.gameObject.SetActive(x.Weapon.WeaponData.AmmoType.IsSet());
x.ListElement.InfiniteAmmoIcon.gameObject.SetActive(!x.Weapon.ShowsAmmoReadout);
x.ListElement.AmmoLabel.gameObject.SetActive(x.Weapon.ShowsAmmoReadout);
}
}
}
@ -233,7 +233,7 @@ public class SchematicDisplay : MonoBehaviour
if (x.Cooldown != null)
x.ListElement.CooldownFill.anchorMax = new Vector2(x.Cooldown.Progress, 1);
x.ListElement.DurabilityLabel.text = $"{(int)(x.Row.Item.EquippableItem.Durability / itemData.Durability * 100)}%";
if (x.Weapon.WeaponData.AmmoType.IsSet())
if (x.Weapon.ShowsAmmoReadout)
x.ListElement.AmmoLabel.text = x.Weapon.AmmoReadout;
}

View file

@ -193,5 +193,10 @@ public sealed partial class RunStartTests
Assert.Equal(reserve.ToString(), Label("Fed Beam"));
Assert.Equal($"{ship.Weapons.Single(w => _items.GetData(w.Item.EquippableItem).Name == "Fed Gun").Ammo}/{reserve}", Label("Fed Gun"));
Assert.Equal("", Label("Plain Gun"));
// The HUD shows the label (not the infinite-ammunition icon) exactly for the weapons that read something.
bool Shown(string name) => ship.Weapons.Single(weapon => _items.GetData(weapon.Item.EquippableItem).Name == name).ShowsAmmoReadout;
Assert.True(Shown("Fed Gun"));
Assert.True(Shown("Fed Beam"));
Assert.False(Shown("Plain Gun"));
}
}

View file

@ -0,0 +1,48 @@
using System;
using CultMath;
using Xunit;
using static CultMath.math;
using float3 = CultMath.float3;
using Random = CultMath.Random;
// The consumer's side of aetheria-gravity:ruling:no-drag-below-top-speed: a ship launched below its hull's top speed and
// left alone keeps that speed, at the global thrust tuning the game ships with (GameplaySettings.ThrustScale 0.1) as well as
// at the fixtures' 1. Reads only the ship's velocity after the zone steps: not Drag, DragScale or any field of the fix.
// Kills: drag reintroduced below top speed, or any deceleration whose size depends on ThrustScale.
public sealed class CoastingTests
{
private const float PlayThrustScale = .1f; // GameplaySettings.ThrustScale's default, the value play runs at
private static float CoastedSpeed(float thrustScale, float launchSpeed, float seconds)
{
using var cache = RestoredHullsTests.OpenCatalog();
var hull = cache.GetByName<HullData>("Longinus");
var settings = RestoredHullsTests.Settings();
settings.ThrustScale = thrustScale;
var items = new ItemManager(1u, cache, new ProvenanceLedger(), settings, _ => { });
items.Random = new Random(1u);
var random = new Random(1);
var pack = new LoadoutGenerator(ref random, items, null, null, null, .5f).GenerateShipLoadout(candidate => candidate == hull);
Assert.NotNull(pack);
var zone = new Zone(items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Test", Owner = null }, null);
var ship = (Ship) EntitySerializer.Unpack(items, zone, pack);
zone.Admit(ship, piloted: false);
ship.Position = float3.zero;
zone.Update(0f);
ship.MovementDirection = float2(0, 0);
ship.Velocity = float2(0, launchSpeed);
for (var i = 0; i < (int) (seconds * 60); i++) zone.Update(1f / 60f);
return length(ship.Velocity);
}
[Theory]
[InlineData(PlayThrustScale)]
[InlineData(1f)]
public void AShipLaunchedBelowTopSpeedKeepsItsSpeed(float thrustScale)
{
const float launch = 50f; // Longinus's top speed is 100
var speed = CoastedSpeed(thrustScale, launch, 10f);
Assert.True(speed >= launch - .01f, $"ThrustScale {thrustScale}: launched at {launch}, coasting 10 s left it at {speed}");
}
}

View file

@ -13,15 +13,28 @@ internal static class ConsoleProbe
private static readonly object Gate = new object();
public static string WrittenBy(Action act)
{
Capture<int>(() => { act(); return 0; }, out var output, out _);
return output;
}
// The action's result with what it wrote to Console.Out and Console.Error on this thread.
public static T Capture<T>(Func<T> act, out string output, out string error)
{
lock (Gate)
{
var previous = Console.Out;
var probe = new ThreadWriter(Environment.CurrentManagedThreadId);
Console.SetOut(probe);
try { act(); }
finally { Console.SetOut(previous); }
return probe.Mine.ToString();
var (previousOut, previousError) = (Console.Out, Console.Error);
var (probeOut, probeError) = (new ThreadWriter(Environment.CurrentManagedThreadId), new ThreadWriter(Environment.CurrentManagedThreadId));
Console.SetOut(probeOut);
Console.SetError(probeError);
try { return act(); }
finally
{
Console.SetOut(previousOut);
Console.SetError(previousError);
output = probeOut.Mine.ToString();
error = probeError.Mine.ToString();
}
}
}

View file

@ -546,12 +546,8 @@ public sealed class ShipModCatalogTests : IDisposable
private static (int Code, string Output, string Error) Run(params string[] args)
{
var (output, error) = (new StringWriter(), new StringWriter());
var (previousOut, previousError) = (Console.Out, Console.Error);
Console.SetOut(output);
Console.SetError(error);
try { return (ShipAuthoringCommands.Run(args), output.ToString(), error.ToString()); }
finally { Console.SetOut(previousOut); Console.SetError(previousError); }
var code = ConsoleProbe.Capture(() => ShipAuthoringCommands.Run(args), out var output, out var error);
return (code, output, error);
}
[Fact]

View file

@ -247,12 +247,8 @@ public sealed class ShipProductTests : IDisposable
private static (int Code, string Output, string Error) Run(params string[] args)
{
var (output, error) = (new StringWriter(), new StringWriter());
var (previousOut, previousError) = (Console.Out, Console.Error);
Console.SetOut(output);
Console.SetError(error);
try { return (ShipAuthoringCommands.Run(args), output.ToString(), error.ToString()); }
finally { Console.SetOut(previousOut); Console.SetError(previousError); }
var code = ConsoleProbe.Capture(() => ShipAuthoringCommands.Run(args), out var output, out var error);
return (code, output, error);
}
[Fact]