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 <noreply@anthropic.com>
This commit is contained in:
soul 2026-12-01 06:24:51 +01:00
commit c77ef59953
7 changed files with 262 additions and 89 deletions

View file

@ -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<HaulOrderData> 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;

View file

@ -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> 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<HaulOrderData> Haul;
[Key(25)] public HaulSeen HaulSeen;
private int _price;

View file

@ -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<CultRecordKey> Refused(Ship ship)
{
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);
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<OrbitalEntity>().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<ICommodityStack>()
|| Goods(order).Where(good => !refused.Contains(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))
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<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. 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<ICommodityStack>().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<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));
}
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<CultRecordKey> 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<ItemData>(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;

View file

@ -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<HaulRefusal> Refusals = new List<HaulRefusal>();
// 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<ItemData> Good;
[JsonProperty("at"), Key(1)] public double At;
// Element identity: minted by the cache on write; "" means unset.
[Key(2)] [CultElementId] public string Id = "";
}