A refusal does not expire under its own cargo; a hauler loads toward what the door took; Takable once per visit

Soul s3. A sight (HaulSight: good, when, units taken, units refused) holds SightLife seconds, but a refusal of a good the hold still
carries holds as long as it is carried, and its clock restarts at the unload at From: the trip home outlasts 600 s and the hauler used to
turn round on its own expiry (5 to 16 percent of jumps). A call at From loads each good up to what the door took on the last visit (the
whole hold of a good it has no fresh sight of); a visit writes per good what was taken and refused. A visit derives what the station takes
once (Callable hands it to Call, Fly asks Call alone), and Fly ends on a second call in a row that left the hold as it was, so a mutant of
the memory fails fast instead of spinning the pass.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-12-01 09:29:18 +01:00
commit 14e9e11ecd
4 changed files with 167 additions and 62 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GameCult.Caching;
@ -22,29 +23,51 @@ public static class HaulRun
public static HaulingTask TaskOf(Ship ship) =>
ship.Haul.Key.IsSet() && OrderOf(ship.ItemManager, ship.Haul.Key) != null ? new HaulingTask { Order = ship.Haul.Key } : null;
// How long a refusal a hauler saw at its To station's door holds (sim seconds): one cycle of the usual recipe. After it the hauler
// tries the good again, a real round trip when the memory was stale (ruling hauler-remembers-what-it-saw).
public const double RefusalLife = 600;
// How long a sight a hauler took at its To station's door holds (sim seconds): one cycle of the usual recipe. After it the hauler loads
// and tries the good again, a real round trip when the memory was stale (ruling hauler-remembers-what-it-saw).
public const double SightLife = 600;
// The goods of the order the hauler remembers being refused at its To station's door, as of the zone it flies in now. What it knows
// is what it saw itself (Ship.HaulSeen, saved with it): no station's bays are read from afar (ruling comms-cells-are-zones).
public static HashSet<CultRecordKey> Refused(Ship ship)
private static bool Fresh(Ship ship, HaulSight sight) => (ship.Zone?.ExactTime ?? 0) - sight.At <= SightLife;
// The goods of the order the hauler remembers being refused at its To station's door: a sight that left units refused, while it is fresh
// or while the hold still carries the good (the trip home outlasts a sight's life, and a hauler must not turn round with its own
// cargo on its own clock). What it knows is what it saw itself (Ship.HaulSeen, saved with it): no station's bays are read from afar
// (ruling comms-cells-are-zones).
public static HashSet<CultRecordKey> Refused(Ship ship, HaulOrderData order)
{
var refused = new HashSet<CultRecordKey>();
var now = ship.Zone?.ExactTime ?? 0;
if (ship.HaulSeen != null)
foreach (var refusal in ship.HaulSeen.Refusals)
if (now - refusal.At <= RefusalLife) refused.Add(refusal.Good.Key);
if (ship.HaulSeen == null) return refused;
var carried = Carried(ship, order).Select(stack => ((ItemInstance) stack).Data.Key).ToHashSet();
foreach (var sight in ship.HaulSeen.Sights)
if (sight.Left > 0 && (Fresh(ship, sight) || carried.Contains(sight.Good.Key))) refused.Add(sight.Good.Key);
return refused;
}
// How many units of the good the hauler loads: what To took of it on its last visit while that sight is fresh, none of a good it saw
// refused outright, and the whole hold of a good it has no fresh sight of (it learns by trying).
private static int LoadLimit(Ship ship, CultRecordKey good)
{
var sight = ship.HaulSeen?.Sights.FirstOrDefault(candidate => candidate.Good.Key.Equals(good));
if (sight == null || !Fresh(ship, sight)) return int.MaxValue;
return sight.Took > 0 ? sight.Took : sight.Left > 0 ? 0 : int.MaxValue;
}
// The units of the good the hold may still take at From: its load limit less what it carries.
private static int Allowance(Ship ship, CultRecordKey good)
{
var limit = LoadLimit(ship, good);
return limit == int.MaxValue ? int.MaxValue : Math.Max(0, limit - Held(ship, good));
}
private static int Held(Ship ship, CultRecordKey good) => ship.CargoBays.Sum(bay => bay.Count(good));
// The orbit to steer for: the To station's while the hold holds a good of the order it does not remember being refused there, the
// From station's otherwise (empty, or holding only what it saw To refuse: ruling leftover-unloads-at-from).
public static CultRecordKey Leg(Ship ship, HaulOrderData order)
{
var carried = Carried(ship, order).ToArray();
if (carried.Length == 0) return order.From.Key;
var refused = Refused(ship);
var refused = Refused(ship, order);
return carried.Any(stack => !refused.Contains(((ItemInstance) stack).Data.Key)) ? order.To.Key : order.From.Key;
}
@ -54,66 +77,77 @@ public static class HaulRun
// Whether a call at this station would do something: read-only, stored nowhere. The bay TryDock would take (the first with no docked
// ship) must exist and hold no cargo, or TryUndock would refuse the ship its way out (finding call-ignores-undock-failure). At the From
// station a call returns what the hold carries that the hauler remembers To refusing, if the output bays have a place for a unit of it,
// and loads what it does not remember being refused, if the hold has a place for a unit of it. At the To station (read here, in its own
// zone) a call delivers a carried good the station takes, or learns: a carried good the hauler has no fresh memory of is tried at the door.
public static bool CanCall(Ship ship, OrbitalEntity station, HaulOrderData order)
// and loads a good up to its load limit, if the hold has a place for a unit of it. At the To station (read here, in its own zone) a
// call delivers a carried good the station takes, or learns: a carried good the hauler has no memory of refusing is tried at the door.
public static bool CanCall(Ship ship, OrbitalEntity station, HaulOrderData order) => Callable(ship, station, order, out _);
// CanCall, and for the To station what the station takes of the carried goods: derived once, so a visit's call reads it from here.
private static bool Callable(Ship ship, OrbitalEntity station, HaulOrderData order, out HashSet<CultRecordKey> taken)
{
taken = null;
var dock = station.DockingBays.FirstOrDefault(bay => bay.DockedShip == null);
if (dock == null || dock.Cargo.Any()) return false;
var orbit = station.OrbitData;
var refused = Refused(ship);
var refused = Refused(ship, order);
if (orbit.Equals(order.From.Key))
return Carried(ship, order).Any(stack => refused.Contains(((ItemInstance) stack).Data.Key)
&& StationWants.OutputBays(station, ((ItemInstance) stack).Data.Key).Any(bay => bay.TryFindSpace(stack.Split(1))))
|| Goods(order).Where(good => !refused.Contains(good)).Any(good => StationWants.OutputBays(station, good).SelectMany(bay => bay.Cargo.Keys).OfType<ICommodityStack>()
|| Goods(order).Where(good => Held(ship, good) < LoadLimit(ship, good)).Any(good => StationWants.OutputBays(station, good).SelectMany(bay => bay.Cargo.Keys).OfType<ICommodityStack>()
.Any(stack => ((ItemInstance) stack).Data.Key.Equals(good) && ship.CargoBays.Any(bay => bay.TryFindSpace(stack.Split(1)))));
if (orbit.Equals(order.To.Key))
{
var carried = Carried(ship, order).Select(stack => ((ItemInstance) stack).Data.Key).Distinct().ToArray();
var taken = StationWants.Takable(station, carried);
return carried.Any(good => taken.Contains(good) || !refused.Contains(good));
var takable = StationWants.Takable(station, carried);
taken = takable;
return carried.Any(good => takable.Contains(good) || !refused.Contains(good));
}
return false;
}
// Docks at the station, loads at the From station or delivers at the To station, and undocks piloted: a ship that has nothing to
// do there does not dock. At the From station it first returns what it remembers To refusing, then loads the rest. At the To station
// it delivers what the station takes and writes down what it saw refused, and when (Ship.HaulSeen). The pilot's Call verb and Fly are
// the callers. Returns whether it docked.
// do there does not dock. At the From station it first returns what it remembers To refusing, then loads each good up to its load
// limit. At the To station it delivers what the station takes and writes down, per good it carried, when, how many units were taken
// and how many refused (Ship.HaulSeen). The pilot's Call verb and Fly are the callers. Returns whether it docked.
public static bool Call(Ship ship, Entity station)
{
var order = OrderOf(ship.ItemManager, ship.Haul.Key);
if (order == null || !(station is OrbitalEntity depot) || !CanCall(ship, depot, order) || depot.TryDock(ship) == null) return false;
if (order == null || !(station is OrbitalEntity depot) || !Callable(ship, depot, order, out var taken) || depot.TryDock(ship) == null) return false;
var now = ship.Zone?.ExactTime ?? 0;
if (depot.OrbitData.Equals(order.From.Key))
{
var refused = Refused(ship);
var refused = Refused(ship, order);
StationServices.Return(depot, Goods(order).Where(refused.Contains).ToArray(), ship);
StationServices.Load(depot, Goods(order).Where(good => !refused.Contains(good)).ToArray(), ship);
// The sight's clock starts at the unload: a refusal governs the next load, not the trip home.
foreach (var sight in ship.HaulSeen?.Sights.Where(sight => refused.Contains(sight.Good.Key)) ?? Enumerable.Empty<HaulSight>())
{
sight.At = now;
if (sight.Took > 0) sight.Left = 0;
}
StationServices.Load(depot, Goods(order), ship, good => Allowance(ship, good));
}
else
{
var carried = Carried(ship, order).Select(stack => ((ItemInstance) stack).Data.Key).Distinct().ToArray();
var taken = StationWants.Takable(depot, carried);
var takes = taken;
var delivered = carried.ToDictionary(good => good, good => 0);
foreach (var bay in ship.CargoBays.ToArray())
foreach (var stack in bay.Cargo.Keys.OfType<ICommodityStack>().Where(stack => taken.Contains(((ItemInstance) stack).Data.Key)).ToArray())
StationServices.TryStorePart(bay, depot, stack);
Saw(ship, carried.Where(good => !taken.Contains(good)));
ship.HaulSeen?.Refusals.RemoveAll(refusal => taken.Contains(refusal.Good.Key));
foreach (var stack in bay.Cargo.Keys.OfType<ICommodityStack>().Where(stack => takes.Contains(((ItemInstance) stack).Data.Key)).ToArray())
delivered[((ItemInstance) stack).Data.Key] += StationServices.TryStorePart(bay, depot, stack).Sold;
Saw(ship, now, carried.Select(good => (good, delivered[good], Held(ship, good))));
}
depot.TryUndock(ship, piloted: true);
return true;
}
// Writes down at the To door that these goods were refused now; entries past their life are dropped.
private static void Saw(Ship ship, IEnumerable<CultRecordKey> refused)
// Writes down at the To door, for each good the hold carried there: now, the units taken, the units refused. A good is one sight.
private static void Saw(Ship ship, double now, IEnumerable<(CultRecordKey Good, int Took, int Left)> goods)
{
var now = ship.Zone?.ExactTime ?? 0;
var seen = ship.HaulSeen ?? (ship.HaulSeen = new HaulSeen());
seen.Refusals.RemoveAll(refusal => now - refusal.At > RefusalLife);
foreach (var good in refused)
seen.Sights.RemoveAll(sight => now - sight.At > SightLife);
foreach (var (good, took, left) in goods)
{
seen.Refusals.RemoveAll(refusal => refusal.Good.Key.Equals(good));
seen.Refusals.Add(new HaulRefusal { Good = new CultRecordRef<ItemData>(good), At = now });
seen.Sights.RemoveAll(sight => sight.Good.Key.Equals(good));
seen.Sights.Add(new HaulSight { Good = new CultRecordRef<ItemData>(good), At = now, Took = took, Left = left });
}
}
@ -173,6 +207,7 @@ public static class HaulRun
{
var order = OrderOf(ship.ItemManager, ship.Haul.Key);
var speed = Agent.TopSpeedOf(ship);
var idle = 0;
for (var steps = 0; steps < 256 && order != null && seconds > 0 && ship.Zone != null && ship.Parent == null; steps++)
{
var zone = ship.Zone;
@ -195,8 +230,12 @@ public static class HaulRun
if (hop == null)
{
var station = Station(zone, orbit);
if (station == null || !CanCall(ship, station, order)) return seconds;
if (station == null) return seconds;
var before = Goods(order).Select(good => Held(ship, good)).ToArray();
if (!Call(ship, station)) return seconds;
// A call that left the hold as it was (a learning visit) twice running is a loop: the hauler waits for the next pass.
if (!Goods(order).Select(good => Held(ship, good)).SequenceEqual(before)) idle = 0;
else if (++idle > 1) return seconds;
continue;
}
var origin = zone.GalaxyZone;

View file

@ -144,14 +144,22 @@ public static class StationServices
// The bays where the station's outputs land (StationWants.OutputBays: its silo, or every bay with no silo) giving the ship the units of
// `goods` they hold, as far as the ship's bays take them. The one writer of a station-to-hauler move; a hauler docks to call it (ruling
// docking-is-enclosed-bays). Returns the units moved.
public static int Load(Entity station, IReadOnlyCollection<CultRecordKey> goods, Entity ship)
public static int Load(Entity station, IReadOnlyCollection<CultRecordKey> goods, Entity ship, Func<CultRecordKey, int> allowance = null)
{
var into = ship.CargoBays.ToArray();
var loaded = 0;
foreach (var good in goods)
{
var left = allowance?.Invoke(good) ?? int.MaxValue;
foreach (var bay in StationWants.OutputBays(station, good).ToArray())
foreach (var stack in bay.Cargo.Keys.OfType<ICommodityStack>().Where(stack => ((ItemInstance) stack).Data.Key.Equals(good)).ToArray())
loaded += MoveParts(bay, into, stack, null);
{
if (left <= 0) break;
var moved = MoveParts(bay, into, stack, null, left);
loaded += moved;
left -= moved;
}
}
return loaded;
}

View file

@ -141,23 +141,25 @@ public class HaulOrderData
public static CultRecordKey KeyOf(string id) => new CultRecordKey("haul-order:" + id);
}
// What a hauler last saw at its To station's door (ruling hauler-remembers-what-it-saw): the goods of its order that were refused there and
// when (zone time of the visit). The hauler's own knowledge, set at the door and read to choose its leg and its load; saved with it
// (EntityPack Key 25). A refusal lives HaulRun.RefusalLife seconds.
// What a hauler last saw at its To station's door (ruling hauler-remembers-what-it-saw): for each good of its order that it carried there,
// when (zone time of the visit), how many units the station took (Took) and how many it refused (Left). The hauler's own knowledge, set
// at the door and read to choose its leg and its load; saved with it (EntityPack Key 25). A sight lives HaulRun.SightLife seconds.
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class HaulSeen
{
[JsonProperty("refusals"), Key(0)] public List<HaulRefusal> Refusals = new List<HaulRefusal>();
[JsonProperty("sights"), Key(0)] public List<HaulSight> Sights = new List<HaulSight>();
// A copy no later visit changes: what a pack holds and what the ship holds are never one list.
public HaulSeen Copy() => new HaulSeen { Refusals = Refusals.Select(refusal => new HaulRefusal { Good = refusal.Good, At = refusal.At }).ToList() };
public HaulSeen Copy() => new HaulSeen { Sights = Sights.Select(sight => new HaulSight { Good = sight.Good, At = sight.At, Took = sight.Took, Left = sight.Left }).ToList() };
}
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class HaulRefusal
public class HaulSight
{
[JsonProperty("good"), Key(0)] public CultRecordRef<ItemData> Good;
[JsonProperty("at"), Key(1)] public double At;
[JsonProperty("took"), Key(3)] public int Took;
[JsonProperty("left"), Key(4)] public int Left;
// Element identity: minted by the cache on write; "" means unset.
[Key(2)] [CultElementId] public string Id = "";
}

View file

@ -46,7 +46,7 @@ public sealed partial class RunStartTests
Assert.Equal(1, visits);
Assert.Equal(0, Held(new[] { leg.To }, leg.Good));
Assert.NotNull(leg.Hauler.HaulSeen);
Assert.Contains(leg.Hauler.HaulSeen.Refusals, refusal => refusal.Good.Key.Equals(leg.Good));
Assert.Contains(leg.Hauler.HaulSeen.Sights, refusal => refusal.Good.Key.Equals(leg.Good));
Assert.Equal(0, Held(new[] { leg.Hauler }, leg.Good));
Assert.Equal(before, Held(new[] { leg.From }, leg.Good));
Assert.Same(leg.FromZone.Contents, leg.Hauler.Zone);
@ -84,14 +84,25 @@ public sealed partial class RunStartTests
var zone = leg.FromZone.Contents;
Assert.True(leg.Hauler.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(a), Quantity = 1 }));
Assert.True(leg.Hauler.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(b), Quantity = 1 }));
leg.Hauler.HaulSeen = new HaulSeen { Refusals = { new HaulRefusal { Good = new CultRecordRef<ItemData>(a), At = zone.ExactTime } } };
leg.Hauler.HaulSeen = new HaulSeen { Sights = { new HaulSight { Good = new CultRecordRef<ItemData>(a), At = zone.ExactTime, Left = 1 } } };
Assert.Equal(leg.To.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order));
foreach (var bay in leg.Hauler.CargoBays)
foreach (var stack in bay.Cargo.Keys.OfType<ICommodityStack>().Where(stack => ((ItemInstance) stack).Data.Key.Equals(b)).ToArray())
bay.Remove((ItemInstance) stack);
Assert.Equal(leg.From.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order));
leg.Hauler.HaulSeen.Refusals[0].At = zone.ExactTime - HaulRun.RefusalLife - 1;
Assert.Equal(leg.To.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order));
// A sight does not expire under its own cargo: the trip home outlasts its life and the hauler goes on home with what it carries.
leg.Hauler.HaulSeen.Sights[0].At = zone.ExactTime - 601;
Assert.Equal(leg.From.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order));
Assert.Equal(600, HaulRun.SightLife);
// At From the unload gives it back and starts the clock; a sight older than 600 s that the hold no longer carries is no refusal.
leg.Hauler.HaulSeen.Sights[0].At = zone.ExactTime - 601;
Stock(leg.From, a, 1);
Assert.True(HaulRun.Call(leg.Hauler, leg.From));
Assert.Equal(0, Held(new[] { leg.Hauler }, a));
Assert.Equal(zone.ExactTime, leg.Hauler.HaulSeen.Sights[0].At);
Assert.Contains(a, HaulRun.Refused(leg.Hauler, leg.Order));
leg.Hauler.HaulSeen.Sights[0].At = zone.ExactTime - 601;
Assert.DoesNotContain(a, HaulRun.Refused(leg.Hauler, leg.Order));
}
// What a hauler gives back at From lands where the From's outputs land (OutputBays: its silo) and nowhere else: with the silo full but
@ -110,7 +121,7 @@ public sealed partial class RunStartTests
silos.First(bay => bay.Cargo.ContainsKey((ItemInstance) freed)).Remove((ItemInstance) freed);
Assert.True(leg.Hauler.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(good), Quantity = max }));
Assert.True(leg.Hauler.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(good), Quantity = 2 }));
leg.Hauler.HaulSeen = new HaulSeen { Refusals = { new HaulRefusal { Good = new CultRecordRef<ItemData>(good), At = leg.FromZone.Contents.ExactTime } } };
leg.Hauler.HaulSeen = new HaulSeen { Sights = { new HaulSight { Good = new CultRecordRef<ItemData>(good), At = leg.FromZone.Contents.ExactTime, Left = 1 } } };
var stock = leg.From.CargoBays.Where(bay => !(bay.Data is SiloData)).Sum(bay => bay.Count(good));
var silo = silos.Sum(bay => bay.Count(good));
Assert.True(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order));
@ -144,35 +155,80 @@ public sealed partial class RunStartTests
{
var (_, _, legs) = SeededRegion(1, quiet: true);
var leg = legs.First();
leg.Hauler.HaulSeen = new HaulSeen { Refusals = { new HaulRefusal { Good = new CultRecordRef<ItemData>(leg.Good), At = 123.5 } } };
leg.Hauler.HaulSeen = new HaulSeen { Sights = { new HaulSight { Good = new CultRecordRef<ItemData>(leg.Good), At = 123.5, Took = 4, Left = 2 } } };
var pack = EntitySerializer.Pack(leg.Hauler);
leg.Hauler.HaulSeen.Refusals[0].At = 999;
leg.Hauler.HaulSeen.Sights[0].At = 999;
var restored = (Ship) EntitySerializer.Unpack(_items, leg.FromZone.Contents, pack);
var refusal = Assert.Single(restored.HaulSeen.Refusals);
Assert.Equal((leg.Good, 123.5), (refusal.Good.Key, refusal.At));
var refusal = Assert.Single(restored.HaulSeen.Sights);
Assert.Equal((leg.Good, 123.5, 4, 2), (refusal.Good.Key, refusal.At, refusal.Took, refusal.Left));
Assert.NotSame(pack.HaulSeen, restored.HaulSeen);
}
// No ping-pong: over 24 sim h on seed 2 the haulers cross wormholes 4262 times shipped (Starfire, Debug; the live read of h3 gave 1558: a
// hauler learning at the door spends a round trip every refusal life), not by the
// hundred thousand a memory-less own-zone read gave (131398, Soul's mutant r5). Bound by the margin rule: sqrt(4262 * 131398) = 23665, 5.5
// from each. Counted as admissions of a hauler into a zone it was not in.
private const int TransferBound = 23665;
// The visit writes down what the door took and refused, and when: with the To bays free the hauler delivers, the sight reads the units it
// gave and none refused, dated by the To zone's clock, and a delivered good is no refusal.
[Fact]
public void WormholeTransfersStayNearTheBase()
public void AVisitWritesWhatTheDoorTookAndWhen()
{
var (galaxy, _, legs) = SeededRegion(1, quiet: true);
var leg = legs.First(candidate => candidate.Path.Length >= 2);
RegionClock.Advance(galaxy, null, 1000);
Stock(leg.From, leg.Good, 10);
var before = Held(new[] { leg.To }, leg.Good);
Assert.True(HaulRun.Call(leg.Hauler, leg.From));
var carried = Held(new[] { leg.Hauler }, leg.Good);
HaulRun.Fly(leg.Hauler, 6000, null, AllZones(galaxy));
var delivered = Held(new[] { leg.To }, leg.Good) - before;
Assert.True(delivered > 0);
var sight = leg.Hauler.HaulSeen.Sights.First(candidate => candidate.Good.Key.Equals(leg.Good));
Assert.Equal((delivered, 0), (sight.Took, sight.Left));
Assert.Equal(leg.ToZone.Contents.ExactTime, sight.At);
Assert.Equal(carried, delivered + Held(new[] { leg.Hauler }, leg.Good));
Assert.DoesNotContain(leg.Good, HaulRun.Refused(leg.Hauler, leg.Order));
}
// A hauler loads toward what the door took on its last visit, not a whole hold: with a fresh sight of 3 units taken, a call at a From that
// holds twenty loads three; with no sight it loads what the hold takes.
[Fact]
public void ALoadFollowsWhatTheDoorTook()
{
var (_, _, legs) = SeededRegion(1, quiet: true);
var leg = legs.First(candidate => candidate.Path.Length >= 2);
Stock(leg.From, leg.Good, 20);
leg.Hauler.HaulSeen = new HaulSeen { Sights = { new HaulSight { Good = new CultRecordRef<ItemData>(leg.Good), At = leg.FromZone.Contents.ExactTime, Took = 3 } } };
Assert.True(HaulRun.Call(leg.Hauler, leg.From));
Assert.Equal(3, Held(new[] { leg.Hauler }, leg.Good));
Assert.False(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order) && Held(new[] { leg.Hauler }, leg.Good) >= 3 && HaulRun.Leg(leg.Hauler, leg.Order).Equals(leg.From.OrbitData));
}
// No ping-pong: over 24 sim h on seed 2 a hauler never turns straight back through the wormhole it came by without a call between (the
// refusal expiring under its own cargo made it do so 238 times). Counted per hauler from the admissions into zones and its docks.
// Bound (margin rule): 1518 wormhole transfers shipped on seed 2 over 24 sim h; the refusal expiring under its cargo gave 4262.
// sqrt(1518 * 4262) = 2543, 1.7 from each.
private const int TransferBound = 2543;
[Fact]
public void NoHaulerTurnsBackWithoutACall()
{
var (galaxy, _, legs) = SeededRegion(2);
var last = legs.ToDictionary(leg => leg.Hauler, leg => leg.FromZone.Contents);
var path = legs.ToDictionary(leg => leg.Hauler, leg => new List<Zone> { leg.FromZone.Contents });
var called = legs.ToDictionary(leg => leg.Hauler, leg => false);
var turns = 0;
var transfers = 0;
foreach (var leg in legs) leg.Hauler.Docked.Subscribe(_ => called[leg.Hauler] = true);
foreach (var zone in galaxy.Zones.Select(candidate => candidate.Contents))
{
var member = zone;
member.Entities.ObserveAdd().Subscribe(add =>
{
if (add.Value is Ship ship && last.TryGetValue(ship, out var was) && was != member) { transfers++; last[ship] = member; }
if (!(add.Value is Ship ship) || !path.TryGetValue(ship, out var visited) || visited[visited.Count - 1] == member) return;
transfers++;
if (visited.Count >= 2 && visited[visited.Count - 2] == member && !called[ship]) turns++;
visited.Add(member);
called[ship] = false;
});
}
AdvanceBy(galaxy, null, 0, 24 * 3600);
Assert.Equal(0, turns);
Assert.InRange(transfers, 1, TransferBound);
}
}