From 4cc7d861a5b79204309b4799914670017860a519 Mon Sep 17 00:00:00 2001 From: soul Date: Tue, 1 Dec 2026 00:22:17 +0100 Subject: [PATCH] A hauler whose To takes nothing it holds returns to From, unloads there and loads only what To takes Ruling leftover-unloads-at-from. Leg is To while the hold holds a good of the order the To station would take now, From otherwise; a call at From first returns the goods To refuses to the output bays (StationServices.Return), then loads only what To takes (StationWants.Takable, one derivation of what the station wants for all the goods: a hauler asks on every call). A hauler never waits at a door holding what it refuses (Soul's wedge probe, NoHaulerWaitsAtItsToDoorHoldingWhatItRefuses); ACallAtFromReturnsWhatToRefuses pins the unload. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/HaulRun.cs | 48 ++++++++++++++++--- .../Scripts/ServerShared/StationServices.cs | 16 +++++++ Assets/Scripts/ServerShared/StationWants.cs | 19 ++++++-- .../RegionLegsPaceTests.cs | 46 ++++++++++++++++++ 4 files changed, 119 insertions(+), 10 deletions(-) diff --git a/Assets/Scripts/ServerShared/HaulRun.cs b/Assets/Scripts/ServerShared/HaulRun.cs index 6c4c68f4d..d7833c84d 100644 --- a/Assets/Scripts/ServerShared/HaulRun.cs +++ b/Assets/Scripts/ServerShared/HaulRun.cs @@ -22,9 +22,25 @@ 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 any good of the order, the From station's otherwise. - public static CultRecordKey Leg(Ship ship, HaulOrderData order) => - Carried(ship, order).Any() ? order.To.Key : order.From.Key; + // 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. + 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); + } public static OrbitalEntity Station(Zone zone, CultRecordKey orbit) => zone.Entities.OfType().FirstOrDefault(station => station.OrbitData.Equals(orbit)); @@ -39,21 +55,39 @@ public static class HaulRun if (dock == null || dock.Cargo.Any()) return false; var orbit = station.OrbitData; if (orbit.Equals(order.From.Key)) - return order.Goods.Select(good => good.Key).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))))); + { + 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) + && 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() + .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))))); 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. The pilot's Call verb and Fly are the callers. Returns the units moved. + // 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) { 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 (depot.OrbitData.Equals(order.From.Key)) moved = StationServices.Load(depot, Goods(order), ship); + 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); + } else 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()) diff --git a/Assets/Scripts/ServerShared/StationServices.cs b/Assets/Scripts/ServerShared/StationServices.cs index 9434f63ef..624189bba 100644 --- a/Assets/Scripts/ServerShared/StationServices.cs +++ b/Assets/Scripts/ServerShared/StationServices.cs @@ -155,6 +155,22 @@ public static class StationServices return loaded; } + // The ship giving back to the station's output bays the units of `goods` its hold carries, as far as those bays take them: the + // load a buyer refused, returned to where it was made (ruling leftover-unloads-at-from). The one writer of a hauler-to-station return; + // a hauler docks to call it. Returns the units moved. + public static int Return(Entity station, IReadOnlyCollection goods, Entity ship) + { + var returned = 0; + foreach (var good in goods) + { + var into = StationWants.OutputBays(station, good).ToArray(); + foreach (var bay in ship.CargoBays.ToArray()) + foreach (var stack in bay.Cargo.Keys.OfType().Where(stack => ((ItemInstance) stack).Data.Key.Equals(good)).ToArray()) + returned += MoveParts(bay, into, stack, null); + } + return returned; + } + // Moves the stack into the bays, the whole quantity first and then fewer, until nothing more fits: the units moved. private static int MoveParts(EquippedCargoBay from, IReadOnlyCollection into, ICommodityStack stack, Action moved, int limit = int.MaxValue) { diff --git a/Assets/Scripts/ServerShared/StationWants.cs b/Assets/Scripts/ServerShared/StationWants.cs index a091067b2..010f12e89 100644 --- a/Assets/Scripts/ServerShared/StationWants.cs +++ b/Assets/Scripts/ServerShared/StationWants.cs @@ -29,10 +29,23 @@ public static class StationWants // draws it (a drawn good is stock, never silo content); the silo bays, up to the good's share of the silo (Room), when the station // wants it through a haul order (a transit depot holds goods in its silo, never in its stock bay). The one owner of where a good // lands; none when the station does not want it. - public static IEnumerable<(IReadOnlyList Bays, int Room)> Landing(Entity station, CultRecordKey good) + public static IEnumerable<(IReadOnlyList Bays, int Room)> Landing(Entity station, CultRecordKey good) => + LandingOf(station, Drawn(station), Ordered(station).ToHashSet(), good); + + private static IEnumerable<(IReadOnlyList Bays, int Room)> LandingOf(Entity station, HashSet drawn, HashSet ordered, CultRecordKey good) { - if (Drawn(station).Contains(good)) yield return (station.CargoBays.Where(bay => !(bay.Data is SiloData)).ToArray(), int.MaxValue); - if (Ordered(station).Contains(good)) yield return (station.CargoBays.Where(bay => bay.Data is SiloData).ToArray(), Room(station, good)); + if (drawn.Contains(good)) yield return (station.CargoBays.Where(bay => !(bay.Data is SiloData)).ToArray(), int.MaxValue); + if (ordered.Contains(good)) yield return (station.CargoBays.Where(bay => bay.Data is SiloData).ToArray(), Room(station, good)); + } + + // Of the goods, those the station would take a unit of now: it wants the good (Landing), has room for it and a bay with a place for a + // unit. What a station wants is derived from its gear on every read, so this asks once for all the goods: a hauler asks on every call. + public static HashSet Takable(Entity station, IEnumerable goods) + { + var drawn = Drawn(station); + var ordered = Ordered(station).ToHashSet(); + return goods.Where(good => LandingOf(station, drawn, ordered, good).Any(landing => landing.Room > 0 + && landing.Bays.Any(bay => bay.TryFindSpace(new SimpleCommodity { Data = new CultRecordRef(good), Quantity = 1 })))).ToHashSet(); } public static IEnumerable Bays(Entity station, CultRecordKey good) => Landing(station, good).SelectMany(landing => landing.Bays); diff --git a/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs b/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs index 016f05f1c..2911adab6 100644 --- a/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs +++ b/tests/Aetheria.Shared.Tests/RegionLegsPaceTests.cs @@ -155,4 +155,50 @@ public sealed partial class RunStartTests foreach (var faction in legs.GroupBy(leg => leg.Hauler.Faction).Where(group => group.Count() >= 4)) Assert.True(faction.Select(leg => Fingerprint(leg.Hauler)).Distinct().Count() > 1, $"every hauler of {faction.Key?.Name} is the same ship"); } + + // Soul's wedge probe (finding legs-wedge-at-a-full-stock-bay): after 24 sim h of seed 1, no hauler waits at its To station's door holding + // goods the station refuses (9 of 37 did). A hauler whose To takes nothing it holds heads for From and returns the load there + // (ruling leftover-unloads-at-from), so what it holds at the door is something the station takes. + [Fact] + public void NoHaulerWaitsAtItsToDoorHoldingWhatItRefuses() + { + var (galaxy, _, legs) = SeededRegion(1); + AdvanceBy(galaxy, null, 0, 24 * 3600); + var refused = new List(); + foreach (var leg in legs) + { + var zone = leg.Hauler.Zone; + if (zone == null || zone != leg.ToZone.Contents || !zone.Entities.Contains(leg.Hauler)) continue; + if (math.length(leg.Hauler.Position.xz - zone.GetOrbitPosition(leg.To.OrbitData)) > 30) continue; + var carried = leg.Order.Goods.Select(good => good.Key).Where(good => Held(new[] { leg.Hauler }, good) > 0).ToArray(); + bool Takes(CultRecordKey good) => StationWants.Landing(leg.To, good).Any(landing => landing.Room > 0 + && landing.Bays.Any(bay => bay.TryFindSpace(new SimpleCommodity { Data = new CultRecordRef(good), Quantity = 1 }))); + if (carried.Length > 0 && !carried.Any(Takes)) refused.Add($"{leg.From.Name}>{leg.To.Name}"); + } + 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)); + } }