Tests for the seeded legs in flight
Seeding once and deterministically, the no-silo producer loading where its outputs land, off-screen hauling and jumping, cargo conserved across legs, jumps and a save, a hauler entering the player's zone as a ship, an on-screen hauler leaving by its wormhole, a hauler waiting for a free empty bay, and a killed hauler not replaced. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b76df4fd19
commit
bf139f9f06
1 changed files with 276 additions and 0 deletions
276
tests/Aetheria.Shared.Tests/RegionLegsFlightTests.cs
Normal file
276
tests/Aetheria.Shared.Tests/RegionLegsFlightTests.cs
Normal file
|
|
@ -0,0 +1,276 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CultMath;
|
||||
using GameCult.Caching;
|
||||
using UniRx;
|
||||
using Xunit;
|
||||
|
||||
// The seeded legs in flight (aetheria-living-world, cut region-haul-legs): off screen HaulRun.Fly under RegionClock.Advance, on screen
|
||||
// the pilot's own flight, and the one cargo move (HaulRun.Call) for both.
|
||||
public sealed partial class RunStartTests
|
||||
{
|
||||
private sealed class Leg
|
||||
{
|
||||
public HaulOrderData Order;
|
||||
public Ship Hauler;
|
||||
public OrbitalEntity From, To;
|
||||
public GalaxyZone FromZone, ToZone;
|
||||
public CultRecordKey Good;
|
||||
public GalaxyZone[] Path;
|
||||
}
|
||||
|
||||
// A demo region of the seed as a run starts it (legs seeded), and its seeded legs. quiet switches off every Facility and Habitation unit
|
||||
// of the region, so the only thing that moves goods is a hauler.
|
||||
private (Galaxy Galaxy, RunStart.Staged Staged, List<Leg> Legs) SeededRegion(uint seed, bool quiet = false)
|
||||
{
|
||||
var (galaxy, staged, failures) = Begun(new DemoTerminus(), seed);
|
||||
Assert.True(failures.Count == 0, string.Join("; ", failures));
|
||||
if (quiet)
|
||||
foreach (var unit in galaxy.Zones.SelectMany(zone => zone.Contents.Entities).SelectMany(entity => entity.Equipment).ToList())
|
||||
if (unit.Behaviors.Any(behavior => behavior is Facility || behavior is Habitation)) unit.Enabled.Value = false;
|
||||
var stations = RegionStations(galaxy).ToArray();
|
||||
var legs = new List<Leg>();
|
||||
foreach (var zone in galaxy.Zones)
|
||||
foreach (var hauler in zone.Contents.Entities.OfType<Ship>().Where(ship => ship.Haul.Key.IsSet()))
|
||||
{
|
||||
var order = HaulRun.OrderOf(_items, hauler.Haul.Key);
|
||||
if (order == null || order.Commitment != RegionLegs.Commitment) continue;
|
||||
var from = stations.Single(s => s.Station.OrbitData.Equals(order.From.Key));
|
||||
var to = stations.Single(s => s.Station.OrbitData.Equals(order.To.Key));
|
||||
// A leg is worth flying when its To station still wants one of its goods (a crew that is switched off eats nothing).
|
||||
var good = order.Goods.Select(g => g.Key).FirstOrDefault(g => StationWants.Of(to.Station).Contains(g));
|
||||
if (!good.IsSet()) continue;
|
||||
legs.Add(new Leg
|
||||
{
|
||||
Order = order, Hauler = hauler, From = from.Station, To = to.Station, FromZone = from.Zone, ToZone = to.Zone,
|
||||
Good = good, Path = galaxy.FindPath(from.Zone, to.Zone)
|
||||
});
|
||||
}
|
||||
return (galaxy, staged, legs);
|
||||
}
|
||||
|
||||
private static void Stock(OrbitalEntity station, CultRecordKey good, int units)
|
||||
{
|
||||
for (var left = units; left > 0; left -= 10)
|
||||
Assert.True(StationWants.OutputBays(station, good).Any(bay => bay.TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(good), Quantity = Math.Min(10, left) })));
|
||||
}
|
||||
|
||||
private static int Held(IEnumerable<Entity> entities, CultRecordKey good) => entities.Sum(entity => entity.CargoBays.Sum(bay => bay.Count(good)));
|
||||
|
||||
private static IEnumerable<Entity> EveryHolder(Galaxy galaxy) => galaxy.Zones.SelectMany(zone => zone.Contents.Entities);
|
||||
|
||||
private static void AdvanceBy(Galaxy galaxy, Zone current, double from, double to)
|
||||
{
|
||||
for (var time = from + RegionClock.Span; time <= to + 1e-9; time += RegionClock.Span) RegionClock.Advance(galaxy, current, time);
|
||||
}
|
||||
|
||||
// Seeding draws on its own stream (LegStep), writes orders as run documents and haulers as zone members: the same seed seeds the same
|
||||
// orders and hulls; a save and a Materialize of its zones add no order and no hauler; and every other entity of every zone is what a
|
||||
// run that seeds no legs draws.
|
||||
[Fact]
|
||||
public void SeedingIsOncePerRunAndDeterministic()
|
||||
{
|
||||
// Orbit records are minted per boot, so a leg is named by where its stations stand: zone index and entity order.
|
||||
string Place(GalaxyZone zone, OrbitalEntity station) => $"{Array.IndexOf(zone.Contents.Galaxy.Zones, zone)}.{zone.Contents.Entities.IndexOf(station)}";
|
||||
string[] Summary(List<Leg> legs) => legs.Select(leg => $"{Place(leg.FromZone, leg.From)}>{Place(leg.ToZone, leg.To)}:{string.Join(",", leg.Order.Goods.Select(g => g.Key.Value))}:{_items.GetData(leg.Hauler.EquippedHull.EquippableItem).Name}").ToArray();
|
||||
var (first, staged, legsOne) = SeededRegion(1);
|
||||
var one = Summary(legsOne);
|
||||
var (_, _, legsTwo) = SeededRegion(1);
|
||||
Assert.NotEmpty(one);
|
||||
Assert.Equal(one, Summary(legsTwo));
|
||||
|
||||
var (plain, _, _) = RegionRun(1);
|
||||
for (var i = 0; i < first.Zones.Length; i++)
|
||||
{
|
||||
var pack = first.Zones[i].Contents.PackZone();
|
||||
var lines = Drawn(first.Zones[i]).Zip(pack.Entities, (line, entity) => (line, entity))
|
||||
.Where(x => !(x.entity.Haul.Key.IsSet() && x.entity.Haul.Key.Value.StartsWith("haul-order:region:"))).Select(x => x.line).ToArray();
|
||||
Assert.Equal(Drawn(plain.Zones[i]), lines);
|
||||
}
|
||||
|
||||
var orders = _cache.GetAll<HaulOrderData>().Count();
|
||||
var haulers = EveryHolder(first).OfType<Ship>().Count(ship => ship.Haul.Key.IsSet());
|
||||
var (game, zones) = RunSave.Capture(_cache, first, first.Entrance.Contents, staged.Player, new SavedActionBarBinding[0], 0);
|
||||
RunSave.Commit(_cache, game, zones, _items.Lots);
|
||||
var restored = new Galaxy(_cache, _cache.GetGlobal<SavedGame>(), _ => { });
|
||||
RunStart.Materialize(_items, _zoneSettings, _planetSettings, restored);
|
||||
Assert.Equal(orders, _cache.GetAll<HaulOrderData>().Count());
|
||||
Assert.Equal(haulers, EveryHolder(restored).OfType<Ship>().Count(ship => ship.Haul.Key.IsSet()));
|
||||
}
|
||||
|
||||
// The bays a station's outputs land in are where its hauler loads: a station with no silo holds what its rig mines in whichever bay took
|
||||
// it, and the call takes it from there.
|
||||
[Fact]
|
||||
public void AProducerWithoutASiloLoadsWhereItsOutputsLand()
|
||||
{
|
||||
var (_, _, legs) = SeededRegion(1);
|
||||
var leg = legs.First(candidate => !candidate.From.CargoBays.Any(bay => bay.Data is SiloData));
|
||||
var bay = StationWants.OutputBays(leg.From, leg.Good).Last();
|
||||
Assert.True(bay.TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(leg.Good), Quantity = 5 }));
|
||||
var before = Held(new[] { leg.From }, leg.Good);
|
||||
Assert.True(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order));
|
||||
HaulRun.Call(leg.Hauler, leg.From);
|
||||
Assert.True(Held(new[] { leg.Hauler }, leg.Good) > 0);
|
||||
Assert.Equal(before, Held(new[] { leg.From }, leg.Good) + Held(new[] { leg.Hauler }, leg.Good));
|
||||
Assert.Contains(leg.Hauler, leg.FromZone.Contents.Entities);
|
||||
}
|
||||
|
||||
// Within the horizon a seeded leg of two or more jumps has its hauler a member of each zone of its path in turn, and delivers: the To
|
||||
// station's units of the leg's goods rise, and the hauler is a member of exactly one zone at every boundary.
|
||||
[Fact]
|
||||
public void OffScreenHaulersFlyCallAndJump()
|
||||
{
|
||||
var (galaxy, _, legs) = SeededRegion(1, quiet: true);
|
||||
var leg = legs.First(candidate => candidate.Path.Length >= 3);
|
||||
Stock(leg.From, leg.Good, 20);
|
||||
var visited = new List<GalaxyZone>();
|
||||
foreach (var zone in galaxy.Zones)
|
||||
{
|
||||
var member = zone;
|
||||
member.Contents.Entities.ObserveAdd().Subscribe(add => { if (add.Value == leg.Hauler && (visited.Count == 0 || visited[visited.Count - 1] != member)) visited.Add(member); });
|
||||
}
|
||||
var goods = leg.Order.Goods.Select(good => good.Key).ToArray();
|
||||
int AtTo() => goods.Sum(good => Held(new[] { leg.To }, good));
|
||||
var start = AtTo();
|
||||
for (var time = RegionClock.Span; time <= 6 * 3600 && AtTo() == start; time += RegionClock.Span)
|
||||
{
|
||||
RegionClock.Advance(galaxy, null, time);
|
||||
Assert.Equal(1, galaxy.Zones.Count(zone => zone.Contents.Entities.Contains(leg.Hauler)));
|
||||
}
|
||||
Assert.True(AtTo() > start, "the To station took none of the leg's goods in 6 sim h");
|
||||
Assert.Equal(leg.Path.Select(zone => zone.Name), visited.Take(leg.Path.Length).Select(zone => zone.Name));
|
||||
}
|
||||
|
||||
// With every unit of the region switched off, each good's units summed over every station and ship are the same before, after 6 sim h,
|
||||
// and after a save round trip mid-run, and a delivery happened.
|
||||
[Fact]
|
||||
public void CargoIsConservedAcrossLegsJumpsAndSaves()
|
||||
{
|
||||
foreach (var seed in RegionSeeds)
|
||||
{
|
||||
var (galaxy, staged, legs) = SeededRegion(seed, quiet: true);
|
||||
var goods = legs.SelectMany(leg => leg.Order.Goods.Select(good => good.Key)).Distinct().ToArray();
|
||||
var jumper = legs.First(leg => leg.Path.Length >= 2);
|
||||
Stock(jumper.From, jumper.Good, 20);
|
||||
Dictionary<CultRecordKey, int> Totals(IEnumerable<Entity> holders) => goods.ToDictionary(good => good, good => Held(holders, good));
|
||||
var before = Totals(EveryHolder(galaxy));
|
||||
var toBefore = legs.Select(leg => leg.To).Distinct().Sum(to => goods.Sum(good => Held(new[] { to }, good)));
|
||||
AdvanceBy(galaxy, null, 0, 3 * 3600);
|
||||
Assert.Equal(before, Totals(EveryHolder(galaxy)));
|
||||
var (game, zones) = RunSave.Capture(_cache, galaxy, galaxy.Entrance.Contents, staged.Player, new SavedActionBarBinding[0], 0);
|
||||
RunSave.Commit(_cache, game, zones, _items.Lots);
|
||||
var restored = new Galaxy(_cache, _cache.GetGlobal<SavedGame>(), _ => { });
|
||||
RunStart.Materialize(_items, _zoneSettings, _planetSettings, restored);
|
||||
Assert.Equal(before, Totals(EveryHolder(restored)));
|
||||
AdvanceBy(galaxy, null, 3 * 3600, 6 * 3600);
|
||||
Assert.Equal(before, Totals(EveryHolder(galaxy)));
|
||||
var toAfter = legs.Select(leg => leg.To).Distinct().Sum(to => goods.Sum(good => Held(new[] { to }, good)));
|
||||
Assert.True(toAfter > toBefore, $"seed {seed}: no leg delivered in 6 sim h");
|
||||
}
|
||||
}
|
||||
|
||||
// A hauler hopping into the player's zone is a ship there: in its Entities with an agent, at the wormhole back to its origin with
|
||||
// ExitWormhole playing, and it did not move during the Advance after the hop; stepped, it flies on.
|
||||
[Fact]
|
||||
public void AHaulerEnteringThePlayersZoneIsAShip()
|
||||
{
|
||||
var (galaxy, _, legs) = SeededRegion(1, quiet: true);
|
||||
var leg = legs.First(candidate => candidate.Path.Length >= 3);
|
||||
Stock(leg.From, leg.Good, 20);
|
||||
var current = leg.Path[1].Contents;
|
||||
var origin = leg.FromZone;
|
||||
var time = 0.0;
|
||||
while (!current.Entities.Contains(leg.Hauler) && time < 6 * 3600)
|
||||
{
|
||||
time += RegionClock.Span;
|
||||
RegionClock.Advance(galaxy, current, time);
|
||||
}
|
||||
Assert.Contains(leg.Hauler, current.Entities);
|
||||
Assert.Contains(current.Agents, agent => ReferenceEquals(agent.Ship, leg.Hauler));
|
||||
Assert.True(leg.Hauler.WormholeAnimationInProgress);
|
||||
var gate = current.WormholeTo(origin);
|
||||
Assert.Equal(gate, leg.Hauler.Position.xz);
|
||||
RegionClock.Advance(galaxy, current, time + RegionClock.Span);
|
||||
Assert.Equal(gate, leg.Hauler.Position.xz);
|
||||
for (var elapsed = 0f; elapsed < 30; elapsed += .02f) current.Update(.02f);
|
||||
Assert.True(math.length(leg.Hauler.Position.xz - gate) > _items.GameplaySettings.WormholeExitRadius);
|
||||
Assert.False(leg.Hauler.WormholeAnimationInProgress);
|
||||
}
|
||||
|
||||
// Stepping the current zone, a hauler bound out reaches its wormhole, commands the jump, and after the dive is a member of the hop zone
|
||||
// and not of the current one, with no agent left in the current zone.
|
||||
[Fact]
|
||||
public void AnOnScreenHaulerLeavesByItsWormhole()
|
||||
{
|
||||
var (galaxy, _, legs) = SeededRegion(1, quiet: true);
|
||||
var leg = legs.Where(candidate => candidate.Path.Length >= 2 && candidate.FromZone != galaxy.Entrance)
|
||||
.OrderBy(candidate => math.length(candidate.FromZone.Contents.WormholeTo(candidate.Path[1]) - candidate.FromZone.Contents.GetOrbitPosition(candidate.From.OrbitData))).First();
|
||||
Stock(leg.From, leg.Good, 10);
|
||||
var zone = leg.FromZone.Contents;
|
||||
var hop = leg.Path[1];
|
||||
for (var elapsed = 0f; elapsed < 3600 && zone.Entities.Contains(leg.Hauler); elapsed += .02f) RegionClock.Step(zone, .02f);
|
||||
Assert.DoesNotContain(leg.Hauler, zone.Entities);
|
||||
Assert.DoesNotContain(zone.Agents, agent => ReferenceEquals(agent.Ship, leg.Hauler));
|
||||
Assert.Contains(leg.Hauler, hop.Contents.Entities);
|
||||
Assert.Contains(hop.Contents.Agents, agent => ReferenceEquals(agent.Ship, leg.Hauler));
|
||||
Assert.Equal(1, galaxy.Zones.Count(candidate => candidate.Contents.Entities.Contains(leg.Hauler)));
|
||||
}
|
||||
|
||||
// The one bay a station has must be free and empty for a call: with the bay holding cargo, or holding a docked ship, a hauler waits
|
||||
// there (on and off screen) and is never lost; freed, it calls.
|
||||
[Fact]
|
||||
public void AHaulerWaitsForAFreeEmptyBay()
|
||||
{
|
||||
var (galaxy, _, legs) = SeededRegion(1, quiet: true);
|
||||
var leg = legs.First(candidate => candidate.Path.Length >= 2);
|
||||
Stock(leg.From, leg.Good, 20);
|
||||
var bay = leg.From.DockingBays.Single();
|
||||
var junk = new SimpleCommodity { Data = new CultRecordRef<ItemData>(leg.Good), Quantity = 1 };
|
||||
Assert.True(bay.TryStore(junk));
|
||||
Assert.False(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order));
|
||||
var units = Held(new[] { leg.From }, leg.Good);
|
||||
AdvanceBy(galaxy, null, 0, 1800);
|
||||
Assert.Contains(leg.Hauler, leg.FromZone.Contents.Entities);
|
||||
Assert.Equal(units, Held(new[] { leg.From }, leg.Good));
|
||||
Assert.Equal(0, Held(new[] { leg.Hauler }, leg.Good));
|
||||
|
||||
bay.Remove(junk);
|
||||
var other = legs.FirstOrDefault(candidate => candidate != leg && candidate.From == leg.From);
|
||||
if (other != null)
|
||||
{
|
||||
Assert.NotNull(leg.From.TryDock(other.Hauler));
|
||||
Assert.False(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order));
|
||||
AdvanceBy(galaxy, null, 1800, 3600);
|
||||
Assert.Contains(leg.Hauler, leg.FromZone.Contents.Entities);
|
||||
Assert.Equal(0, Held(new[] { leg.Hauler }, leg.Good));
|
||||
Assert.True(leg.From.TryUndock(other.Hauler, piloted: true));
|
||||
}
|
||||
Assert.True(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order));
|
||||
AdvanceBy(galaxy, null, other != null ? 3600 : 1800, other != null ? 7200 : 3600);
|
||||
Assert.True(Held(new[] { leg.From }, leg.Good) < units, "freed, the hauler took nothing");
|
||||
}
|
||||
|
||||
// A seeded hauler killed in the current zone drops its cargo as floating loot, and nothing replaces it: after 2 sim h no ship's Haul
|
||||
// names its order.
|
||||
[Fact]
|
||||
public void AKilledLegHaulerIsNotReplaced()
|
||||
{
|
||||
var (galaxy, _, legs) = SeededRegion(1, quiet: true);
|
||||
var leg = legs.First(candidate => candidate.Path.Length >= 2 && candidate.FromZone != galaxy.Entrance);
|
||||
Stock(leg.From, leg.Good, 20);
|
||||
var zone = leg.FromZone.Contents;
|
||||
HaulRun.Call(leg.Hauler, leg.From);
|
||||
var carried = Held(new[] { leg.Hauler }, leg.Good);
|
||||
Assert.True(carried > 0);
|
||||
leg.Hauler.HeatstrokeDeath.OnNext(Unit.Default);
|
||||
Assert.DoesNotContain(leg.Hauler, zone.Entities);
|
||||
Assert.Equal(carried, zone.FloatingItems.Where(floating => floating.Item is SimpleCommodity stack && stack.Data.Key.Equals(leg.Good)).Sum(floating => ((SimpleCommodity) floating.Item).Quantity));
|
||||
AdvanceBy(galaxy, zone, zone.ExactTime, zone.ExactTime + 7200);
|
||||
Assert.DoesNotContain(EveryHolder(galaxy).OfType<Ship>(), ship => ship.Haul.Key.Equals(leg.Hauler.Haul.Key));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue