From c77ef59953bf31fa3cb0b8837b5ebb6309656447 Mon Sep 17 00:00:00 2001 From: soul Date: Tue, 1 Dec 2026 06:24:51 +0100 Subject: [PATCH] A hauler acts on what it saw at its To door, saved with it; no zone's bays are read from afar Ruling hauler-remembers-what-it-saw. HaulRun.ToStation and its live read of the To station's bays are deleted. A hauler writes down at the To door which goods of its order were refused and when (Ship.HaulSeen, EntityPack Key 25); the leg is To while the hold holds a good it does not remember being refused, and a call at From returns what it remembers being refused and loads the rest. A refusal lives RefusalLife seconds, so a stale memory costs a real round trip. At the To door a call delivers what the station takes or learns what it does not (StationWants.Takable, derived once per call there and nowhere else). Tests: the learn-and-carry-home round trip, no leg reads another zone's bays, any-versus-all on a mixed hold and expiry, returns land in the output bays, the dive at every jump, the memory saved with the hauler, and wormhole transfers bounded against the memory-less mutant. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/Entity.cs | 2 + .../Scripts/ServerShared/EntitySerializer.cs | 5 +- Assets/Scripts/ServerShared/HaulRun.cs | 106 ++++++----- Assets/Scripts/ServerShared/StationWants.cs | 21 +++ tests/Aetheria.Shared.Tests/HaulRunTests.cs | 4 +- .../RegionLegsMemoryTests.cs | 170 ++++++++++++++++++ .../RegionLegsPaceTests.cs | 43 ----- 7 files changed, 262 insertions(+), 89 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 6b28af1cb..fe7200c93 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -26,6 +26,8 @@ public abstract class Entity // it at admission (HaulRun.TaskOf). Interim actor state (target r3, L-actors), deleted with EntityPack Key 24 when offices // issue haul acts (follow-up haul-run-to-offices). public CultRecordRef Haul; + // What this ship last saw at its To door (HaulRun.Saw): its own dated knowledge, null before its first visit. Saved with it. + public HaulSeen HaulSeen; public EquippableItem Hull; public EquippedItem EquippedHull; diff --git a/Assets/Scripts/ServerShared/EntitySerializer.cs b/Assets/Scripts/ServerShared/EntitySerializer.cs index 46b0794d9..5495dee97 100644 --- a/Assets/Scripts/ServerShared/EntitySerializer.cs +++ b/Assets/Scripts/ServerShared/EntitySerializer.cs @@ -38,6 +38,7 @@ public static class EntitySerializer pack.Settings = entity.Settings; pack.Boss = entity.IsBoss; pack.Haul = entity.Haul; + pack.HaulSeen = entity.HaulSeen?.Copy(); pack.Hull = entity.Hull; pack.Name = entity.Name; @@ -139,6 +140,7 @@ public static class EntitySerializer entity.Name = pack.Name; entity.IsBoss = pack.Boss; entity.Haul = pack.Haul; + entity.HaulSeen = pack.HaulSeen?.Copy(); entity.Faction = itemManager.ItemData.Get(pack.Faction); entity.Children = pack.Children.Select(c => { @@ -252,9 +254,10 @@ public abstract class EntityPack [Key(14)] public EntitySettings Settings; [Key(15)] public CultRecordRef Faction; [Key(16)] public int[][] WeaponGroups; - // Keys 17-20 belong to the subclasses; the base's next free key is 25 (Id is 23, Haul is 24, Boss is 21, the orbital pack's Kind is 22). + // Keys 17-20 belong to the subclasses; the base's next free key is 26 (Haul seen is 25, Id is 23, Haul is 24, Boss is 21, the orbital pack's Kind is 22). [Key(21)] public bool Boss; [Key(24)] public CultRecordRef Haul; + [Key(25)] public HaulSeen HaulSeen; private int _price; diff --git a/Assets/Scripts/ServerShared/HaulRun.cs b/Assets/Scripts/ServerShared/HaulRun.cs index d7833c84d..7eb39c840 100644 --- a/Assets/Scripts/ServerShared/HaulRun.cs +++ b/Assets/Scripts/ServerShared/HaulRun.cs @@ -22,80 +22,100 @@ 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; - // The orbit to steer for: the To station's while the hold holds a good of the order the To station would take now, the From - // station's otherwise (empty, or holding only what To refuses: ruling leftover-unloads-at-from). A To that is not live takes the - // order as written. + // 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; + + // 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 Refused(Ship ship) + { + var refused = new HashSet(); + 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); + return refused; + } + + // 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 taken = Taken(ToStation(ship, order), Goods(order)); - return carried.Any(stack => taken.Contains(((ItemInstance) stack).Data.Key)) ? order.To.Key : order.From.Key; - } - - // The order's To station wherever it stands in the region; null when no live zone holds it. - private static OrbitalEntity ToStation(Ship ship, HaulOrderData order) - { - var zone = ship.Zone; - if (zone == null) return null; - if (!zone.Orbits.ContainsKey(order.To.Key)) zone = ZoneOf(zone.Galaxy, order.To.Key)?.Contents; - return zone == null ? null : Station(zone, order.To.Key); + var refused = Refused(ship); + return carried.Any(stack => !refused.Contains(((ItemInstance) stack).Data.Key)) ? order.To.Key : order.From.Key; } public static OrbitalEntity Station(Zone zone, CultRecordKey orbit) => zone.Entities.OfType().FirstOrDefault(station => station.OrbitData.Equals(orbit)); - // Whether a call at this station would move a unit: read-only, stored nowhere. The bay TryDock would take (the first with no docked + // 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 the bays its outputs land in hold a good of the order the hold has room for; at the To station the station wants a carried - // good and has room for a unit of it. + // 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) { 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); if (orbit.Equals(order.From.Key)) - { - var to = ToStation(ship, order); - // A call here returns what the hold carries that To refuses, if the output bays have a place for a unit of it, and loads what To - // would take, if the hold has a place for a unit of it. - var taken = Taken(to, Goods(order)); - return Carried(ship, order).Any(stack => !taken.Contains(((ItemInstance) stack).Data.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)))) - || taken.Any(good => StationWants.OutputBays(station, good).SelectMany(bay => bay.Cargo.Keys).OfType() + || Goods(order).Where(good => !refused.Contains(good)).Any(good => StationWants.OutputBays(station, good).SelectMany(bay => bay.Cargo.Keys).OfType() .Any(stack => ((ItemInstance) stack).Data.Key.Equals(good) && ship.CargoBays.Any(bay => bay.TryFindSpace(stack.Split(1))))); - } if (orbit.Equals(order.To.Key)) - return Carried(ship, order).Any(stack => StationWants.Landing(station, ((ItemInstance) stack).Data.Key).Any(landing => landing.Room > 0 && landing.Bays.Any(bay => bay.TryFindSpace(stack.Split(1))))); + { + 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)); + } return false; } - // Which of the goods the To station would take now (all of them when it is not live): StationWants.Takable, which derives what the - // station wants once for all the goods, because this runs on every call, update and pass of every hauler. - private static HashSet Taken(OrbitalEntity to, IReadOnlyCollection goods) => - to == null ? goods.ToHashSet() : StationWants.Takable(to, goods); - // Docks at the station, loads at the From station or delivers at the To station, and undocks piloted: a ship that cannot - // move a unit does not dock. At the From station it first returns what To refuses, then loads what To takes. The pilot's Call verb and - // Fly are the callers. Returns the units moved. - public static int Call(Ship ship, Entity station) + // 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. + 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 0; - var moved = 0; + if (order == null || !(station is OrbitalEntity depot) || !CanCall(ship, depot, order) || depot.TryDock(ship) == null) return false; if (depot.OrbitData.Equals(order.From.Key)) { - var to = ToStation(ship, order); - var taken = Taken(to, Goods(order)); - moved = StationServices.Return(depot, Goods(order).Where(good => !taken.Contains(good)).ToArray(), ship) + StationServices.Load(depot, taken.ToArray(), ship); + var refused = Refused(ship); + StationServices.Return(depot, Goods(order).Where(refused.Contains).ToArray(), ship); + StationServices.Load(depot, Goods(order).Where(good => !refused.Contains(good)).ToArray(), ship); } else + { + var carried = Carried(ship, order).Select(stack => ((ItemInstance) stack).Data.Key).Distinct().ToArray(); + var taken = StationWants.Takable(depot, carried); foreach (var bay in ship.CargoBays.ToArray()) - foreach (var stack in bay.Cargo.Keys.OfType().Where(stack => Goods(order).Contains(((ItemInstance) stack).Data.Key)).ToArray()) - moved += StationServices.TryStorePart(bay, depot, stack).Sold; + foreach (var stack in bay.Cargo.Keys.OfType().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)); + } depot.TryUndock(ship, piloted: true); - return moved; + 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 refused) + { + 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.Refusals.RemoveAll(refusal => refusal.Good.Key.Equals(good)); + seen.Refusals.Add(new HaulRefusal { Good = new CultRecordRef(good), At = now }); + } + } // The GalaxyZone whose live contents hold the orbit; null when none does. public static GalaxyZone ZoneOf(Galaxy galaxy, CultRecordKey orbit) => @@ -176,7 +196,7 @@ public static class HaulRun { var station = Station(zone, orbit); if (station == null || !CanCall(ship, station, order)) return seconds; - if (Call(ship, station) == 0) return seconds; // a call that moved nothing would repeat until the step guard + if (!Call(ship, station)) return seconds; continue; } var origin = zone.GalaxyZone; diff --git a/Assets/Scripts/ServerShared/StationWants.cs b/Assets/Scripts/ServerShared/StationWants.cs index 010f12e89..f23cf3c57 100644 --- a/Assets/Scripts/ServerShared/StationWants.cs +++ b/Assets/Scripts/ServerShared/StationWants.cs @@ -140,3 +140,24 @@ 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. +[MessagePackObject, JsonObject(MemberSerialization.OptIn)] +public class HaulSeen +{ + [JsonProperty("refusals"), Key(0)] public List Refusals = new List(); + + // 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() }; +} + +[MessagePackObject, JsonObject(MemberSerialization.OptIn)] +public class HaulRefusal +{ + [JsonProperty("good"), Key(0)] public CultRecordRef Good; + [JsonProperty("at"), Key(1)] public double At; + // Element identity: minted by the cache on write; "" means unset. + [Key(2)] [CultElementId] public string Id = ""; +} diff --git a/tests/Aetheria.Shared.Tests/HaulRunTests.cs b/tests/Aetheria.Shared.Tests/HaulRunTests.cs index 6645a2624..bd0aca07a 100644 --- a/tests/Aetheria.Shared.Tests/HaulRunTests.cs +++ b/tests/Aetheria.Shared.Tests/HaulRunTests.cs @@ -162,7 +162,7 @@ public sealed partial class RunStartTests } // A hauler docks only when the call would move a unit: with the silo empty it never docks and keeps its pilot; with the refinery's - // bays full of something else, a loaded hauler never docks at the refinery, which refuses its cargo, and heads for Zenith with it + // bays full of something else, a loaded hauler docks once at the refinery, which refuses its cargo, writes the refusal down, and heads for Zenith with it // (ruling leftover-unloads-at-from): the 5 units are the hauler's or Zenith's silo's, none the refinery's. [Fact] public void AHaulerDocksOnlyToMoveSomething() @@ -184,7 +184,7 @@ public sealed partial class RunStartTests var atRefinery = 0; world.Hauler.Docked.Subscribe(station => { if (ReferenceEquals(station, world.Refinery)) atRefinery++; }); Fly(world.Arena, 60); - Assert.Equal(0, atRefinery); + Assert.Equal(1, atRefinery); // one visit to learn the refusal, then it goes home Assert.Equal(5, Units(world.Hauler.CargoBays, world.Good) + Units(new[] { world.Silo }, world.Good)); Assert.Equal(0, Units(world.Refinery.CargoBays, world.Good)); } diff --git a/tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs b/tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs new file mode 100644 index 000000000..953905403 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs @@ -0,0 +1,170 @@ +/* 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; + +// What a hauler remembers of its To station's door (aetheria-living-world, cut region-haul-legs fix batch; ruling hauler-remembers-what-it-saw, +// findings hauler-reads-to-bays-across-zones, return-and-mixed-hold-unpinned, pace-parity-is-one-crossing): a hauler acts on what it saw +// itself, dated and saved with it, and never reads another zone's bays. +public sealed partial class RunStartTests +{ + private static void FillStock(OrbitalEntity station, CultRecordKey filler) + { + foreach (var bay in station.CargoBays.Where(bay => !(bay.Data is SiloData))) + while (bay.TryStore(new SimpleCommodity { Data = new CultRecordRef(filler), Quantity = 1 })) { } + } + + private CultRecordKey FillerFor(OrbitalEntity station) => + _cache.GetAll().Select(data => _cache.RefOf(data).Key).First(key => !StationWants.Consumed(station).Contains(key)); + + private static HashSet AllZones(Galaxy galaxy) => new HashSet(galaxy.Zones.Select(zone => zone.Contents)); + + // A hauler learns at the To door, carries what it saw home and gives it back: with the To's stock bays full it flies to To, tries the + // door once (a visit that moves nothing and writes down the refusal), returns to From, and gives the load back to the output bays. + [Fact] + public void AHaulerLearnsAtTheDoorAndCarriesItHome() + { + var (galaxy, _, legs) = SeededRegion(1, quiet: true); + var leg = legs.First(candidate => candidate.Path.Length >= 2); + Stock(leg.From, leg.Good, 10); + var before = Held(new[] { leg.From }, leg.Good); + Assert.True(HaulRun.Call(leg.Hauler, leg.From)); + var carried = Held(new[] { leg.Hauler }, leg.Good); + Assert.True(carried > 0); + Assert.Equal(leg.To.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order)); + FillStock(leg.To, FillerFor(leg.To)); + var visits = 0; + leg.Hauler.Docked.Subscribe(station => { if (ReferenceEquals(station, leg.To)) visits++; }); + HaulRun.Fly(leg.Hauler, 6000, null, AllZones(galaxy)); + 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.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); + Assert.Equal(leg.From.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order)); + } + + // No decision of a hauler reads another zone's bays: with the hauler loaded at From, filling the To station's stock bays in another zone + // changes neither its leg nor its waypoint nor what a call at From would do. + [Fact] + public void NoLegReadsAnotherZonesBays() + { + var (_, _, legs) = SeededRegion(1, quiet: true); + var leg = legs.First(candidate => candidate.Path.Length >= 2); + Stock(leg.From, leg.Good, 10); + Assert.True(HaulRun.Call(leg.Hauler, leg.From)); + Stock(leg.From, leg.Good, 10); + var legBefore = HaulRun.Leg(leg.Hauler, leg.Order); + var wayBefore = HaulRun.Waypoint(leg.Hauler, leg.Order); + var canBefore = HaulRun.CanCall(leg.Hauler, leg.From, leg.Order); + Assert.Equal(leg.To.OrbitData, legBefore); + FillStock(leg.To, FillerFor(leg.To)); + Assert.Equal(legBefore, HaulRun.Leg(leg.Hauler, leg.Order)); + Assert.Equal(wayBefore, HaulRun.Waypoint(leg.Hauler, leg.Order)); + Assert.Equal(canBefore, HaulRun.CanCall(leg.Hauler, leg.From, leg.Order)); + } + + // The memory decides the leg: a hold with one refused good and one not heads for To (any), only refused goods head for From (all), and a + // refusal past its life is no refusal. + [Fact] + public void TheMemoryDecidesTheLegAndExpires() + { + var (_, _, legs) = SeededRegion(1, quiet: true); + var leg = legs.First(candidate => candidate.Order.Goods.Count >= 2 && candidate.Path.Length >= 2); + var (a, b) = (leg.Order.Goods[0].Key, leg.Order.Goods[1].Key); + var zone = leg.FromZone.Contents; + Assert.True(leg.Hauler.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef(a), Quantity = 1 })); + Assert.True(leg.Hauler.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef(b), Quantity = 1 })); + leg.Hauler.HaulSeen = new HaulSeen { Refusals = { new HaulRefusal { Good = new CultRecordRef(a), At = zone.ExactTime } } }; + 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().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)); + } + + // What a hauler gives back at From lands where the From's outputs land (OutputBays: its silo), never in a stock bay. + [Fact] + public void ReturnedGoodsLandInTheOutputBays() + { + var (_, _, legs) = SeededRegion(1, quiet: true); + var leg = legs.First(candidate => candidate.From.CargoBays.Any(bay => bay.Data is SiloData) && candidate.From.CargoBays.Any(bay => !(bay.Data is SiloData))); + var good = leg.Good; + Assert.True(leg.Hauler.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef(good), Quantity = 3 })); + leg.Hauler.HaulSeen = new HaulSeen { Refusals = { new HaulRefusal { Good = new CultRecordRef(good), At = leg.FromZone.Contents.ExactTime } } }; + var stock = leg.From.CargoBays.Where(bay => !(bay.Data is SiloData)).Sum(bay => bay.Count(good)); + var silo = leg.From.CargoBays.Where(bay => bay.Data is SiloData).Sum(bay => bay.Count(good)); + Assert.True(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order)); + Assert.True(HaulRun.Call(leg.Hauler, leg.From)); + Assert.Equal(0, Held(new[] { leg.Hauler }, good)); + Assert.Equal(silo + 3, leg.From.CargoBays.Where(bay => bay.Data is SiloData).Sum(bay => bay.Count(good))); + Assert.Equal(stock, leg.From.CargoBays.Where(bay => !(bay.Data is SiloData)).Sum(bay => bay.Count(good))); + } + + // The dive is charged at every jump: a crossing of two zones costs the distances over speed and two dives. + [Fact] + public void TheDiveIsChargedAtEveryJump() + { + var (galaxy, _, legs) = SeededRegion(1, quiet: true); + var leg = legs.First(candidate => candidate.Path.Length >= 3); + Stock(leg.From, leg.Good, 10); + var first = leg.Path[0].Contents; + var second = leg.Path[1].Contents; + var speed = Agent.TopSpeedOf(leg.Hauler); + var distance = math.length(first.WormholeTo(leg.Path[1]) - first.GetOrbitPosition(leg.From.OrbitData)) + + math.length(second.WormholeTo(leg.Path[2]) - second.WormholeTo(leg.Path[0])); + var advanced = new HashSet(galaxy.Zones.Select(zone => zone.Contents).Where(zone => zone != leg.Path[2].Contents)); + var spent = 100000 - HaulRun.Fly(leg.Hauler, 100000, null, advanced); + Assert.Same(leg.Path[2].Contents, leg.Hauler.Zone); + Assert.Equal(distance / speed + 2 * _items.GameplaySettings.WormholeAnimationDuration, spent, 1); + } + + // The memory is saved with the hauler: a pack and an unpacked ship carry the same dated refusals, as copies the ship's own visits do not change. + [Fact] + public void TheMemoryIsSavedWithTheHauler() + { + var (_, _, legs) = SeededRegion(1, quiet: true); + var leg = legs.First(); + leg.Hauler.HaulSeen = new HaulSeen { Refusals = { new HaulRefusal { Good = new CultRecordRef(leg.Good), At = 123.5 } } }; + var pack = EntitySerializer.Pack(leg.Hauler); + leg.Hauler.HaulSeen.Refusals[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)); + 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; + [Fact] + public void WormholeTransfersStayNearTheBase() + { + var (galaxy, _, legs) = SeededRegion(2); + var last = legs.ToDictionary(leg => leg.Hauler, leg => leg.FromZone.Contents); + var transfers = 0; + 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; } + }); + } + AdvanceBy(galaxy, null, 0, 24 * 3600); + Assert.InRange(transfers, 1, TransferBound); + } +} diff --git a/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs b/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs index 2911adab6..5b9d2563a 100644 --- a/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs +++ b/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs @@ -105,26 +105,6 @@ public sealed partial class RunStartTests Assert.Equal(1, galaxy.Zones.Count(candidate => candidate.Contents.Entities.Contains(leg.Hauler))); } - // CanCall at the To station reads the physical space: with the stock bays full of other goods a carried good that the station wants has - // no place to land and the call is refused (a call allowed here docks, stores nothing and undocks, once per step of Fly). - [Fact] - public void ACallAtAFullStockBayIsRefused() - { - var (_, _, legs) = SeededRegion(1, quiet: true); - var leg = legs.First(candidate => candidate.Path.Length >= 2); - Stock(leg.From, leg.Good, 10); - Assert.True(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order)); - Assert.True(HaulRun.Call(leg.Hauler, leg.From) > 0); - Assert.True(Held(new[] { leg.Hauler }, leg.Good) > 0); - Assert.NotEmpty(StationWants.Landing(leg.To, leg.Good)); - Assert.True(HaulRun.CanCall(leg.Hauler, leg.To, leg.Order)); - var filler = _cache.GetAll().First(data => !StationWants.Consumed(leg.To).Contains(_cache.RefOf(data).Key) && data.MaxStack > 0); - foreach (var bay in leg.To.CargoBays.Where(bay => !(bay.Data is SiloData))) - while (bay.TryStore(new SimpleCommodity { Data = new CultRecordRef(_cache.RefOf(filler).Key), Quantity = 1 })) { } - Assert.NotEmpty(StationWants.Landing(leg.To, leg.Good).Where(landing => landing.Room > 0)); - Assert.False(HaulRun.CanCall(leg.Hauler, leg.To, leg.Order)); - } - // An order carries only goods its To station's installed recipes consume, and a hauler holds nothing else: after 24 sim h on seed 1 every // hauler's cargo of its order's goods is something its To station draws. [Fact] @@ -178,27 +158,4 @@ public sealed partial class RunStartTests Assert.Empty(refused); } - // A call at From returns what the hold carries that To refuses to the output bays, then loads what To takes: a hauler holding only a - // good its To's bays cannot take is called at From, gives it back, and carries none of it away. - [Fact] - public void ACallAtFromReturnsWhatToRefuses() - { - var (_, _, legs) = SeededRegion(1, quiet: true); - var leg = legs.First(candidate => candidate.Path.Length >= 2 && !candidate.From.CargoBays.Any(bay => bay.Data is SiloData)); - Stock(leg.From, leg.Good, 10); - Assert.True(HaulRun.Call(leg.Hauler, leg.From) > 0); - var carried = Held(new[] { leg.Hauler }, leg.Good); - Assert.True(carried > 0); - Assert.Equal(leg.To.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order)); - var filler = _cache.GetAll().First(data => !StationWants.Consumed(leg.To).Contains(_cache.RefOf(data).Key) && data.MaxStack > 0); - foreach (var bay in leg.To.CargoBays.Where(bay => !(bay.Data is SiloData))) - while (bay.TryStore(new SimpleCommodity { Data = new CultRecordRef(_cache.RefOf(filler).Key), Quantity = 1 })) { } - Assert.Equal(leg.From.OrbitData, HaulRun.Leg(leg.Hauler, leg.Order)); - var fromBefore = Held(new[] { leg.From }, leg.Good); - Assert.True(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order)); - HaulRun.Call(leg.Hauler, leg.From); - Assert.Equal(0, Held(new[] { leg.Hauler }, leg.Good)); - Assert.Equal(fromBefore + carried, Held(new[] { leg.From }, leg.Good)); - Assert.False(HaulRun.CanCall(leg.Hauler, leg.From, leg.Order)); - } }