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 <noreply@anthropic.com>
This commit is contained in:
soul 2026-12-01 00:22:17 +01:00
commit 4cc7d861a5
4 changed files with 119 additions and 10 deletions

View file

@ -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<OrbitalEntity>().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<ICommodityStack>()
.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<ICommodityStack>()
.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<CultRecordKey> Taken(OrbitalEntity to, IReadOnlyCollection<CultRecordKey> 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<ICommodityStack>().Where(stack => Goods(order).Contains(((ItemInstance) stack).Data.Key)).ToArray())

View file

@ -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<CultRecordKey> 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<ICommodityStack>().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<EquippedCargoBay> into, ICommodityStack stack, Action<ItemInstance> moved, int limit = int.MaxValue)
{

View file

@ -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<EquippedCargoBay> Bays, int Room)> Landing(Entity station, CultRecordKey good)
public static IEnumerable<(IReadOnlyList<EquippedCargoBay> Bays, int Room)> Landing(Entity station, CultRecordKey good) =>
LandingOf(station, Drawn(station), Ordered(station).ToHashSet(), good);
private static IEnumerable<(IReadOnlyList<EquippedCargoBay> Bays, int Room)> LandingOf(Entity station, HashSet<CultRecordKey> drawn, HashSet<CultRecordKey> 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<CultRecordKey> Takable(Entity station, IEnumerable<CultRecordKey> 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<ItemData>(good), Quantity = 1 })))).ToHashSet();
}
public static IEnumerable<EquippedCargoBay> Bays(Entity station, CultRecordKey good) => Landing(station, good).SelectMany(landing => landing.Bays);