From 579a96cb9e8563e875a879955afe69ab456e8626 Mon Sep 17 00:00:00 2001 From: soul Date: Sat, 28 Nov 2026 14:09:40 +0100 Subject: [PATCH] The dock call: HaulRun (order, leg, CanCall, Call), PilotCommand.Call, and an undocked hauler re-admitted piloted with its haul task derived in CreateAgent Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/Entity.cs | 4 +- Assets/Scripts/ServerShared/HaulRun.cs | 61 ++++++++++++++++++++++++++ Assets/Scripts/ServerShared/Pilot.cs | 5 +++ Assets/Scripts/ServerShared/Zone.cs | 2 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 Assets/Scripts/ServerShared/HaulRun.cs diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 6f550af49..1da2d7ade 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -1765,7 +1765,7 @@ public abstract class Entity return bay; } - public bool TryUndock(Ship ship) + public bool TryUndock(Ship ship, bool piloted = false) { var bay = DockingBays.FirstOrDefault(x => x.DockedShip == ship); if (bay == null) @@ -1779,7 +1779,7 @@ public abstract class Entity bay.DockedShip = null; ship.RemoveParent(); - Zone.Admit(ship, piloted: false); + Zone.Admit(ship, piloted); return true; } diff --git a/Assets/Scripts/ServerShared/HaulRun.cs b/Assets/Scripts/ServerShared/HaulRun.cs new file mode 100644 index 000000000..16e95290a --- /dev/null +++ b/Assets/Scripts/ServerShared/HaulRun.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using System.Linq; +using GameCult.Caching; + +// The interim haul (aetheria-living-world:ruling:transit-depots-buy-for-onward-haul, demo-zenith-ore-depot): which ship carries for which +// standing order (Entity.Haul), where it flies (the leg, derived from its hold every update), and the one dock call that moves goods: +// the hauler docks (ruling docking-is-enclosed-bays), the From station's silo loads it or the To station takes its cargo through +// StationServices.TryStorePart, and it undocks piloted. No payment moves: both ends are in one comms cell and no station holds an account +// (ruling comms-cells-are-zones). The ship's task, orders and cargo are the ship's own; nothing here is stored. +public static class HaulRun +{ + public static HaulOrderData OrderOf(ItemManager items, CultRecordKey order) => + order.IsSet() ? items.ItemData.Get(order) : null; + + // The task a ship carrying a haul order starts on, derived at every admission; null for a ship with no order or whose order is gone. + 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; + + 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. At the From station the silo holds 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. Either needs a free docking bay. + public static bool CanCall(Ship ship, OrbitalEntity station, HaulOrderData order) + { + if (station.DockingBays.All(bay => bay.DockedShip != null)) return false; + var orbit = station.OrbitData; + if (orbit.Equals(order.From.Key)) + return station.CargoBays.Where(bay => bay.Data is SiloData).SelectMany(bay => bay.Cargo.Keys).OfType() + .Any(stack => Goods(order).Contains(((ItemInstance) stack).Data.Key) && ship.CargoBays.Any(bay => bay.TryFindSpace(stack.Split(1)))); + if (orbit.Equals(order.To.Key)) + return Carried(ship, order).Any(stack => StationWants.Bays(station, ((ItemInstance) stack).Data.Key).Any(bay => bay.TryFindSpace(stack.Split(1)))); + return false; + } + + // 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 is the only caller (PilotApplier). + public static void 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; + if (depot.OrbitData.Equals(order.From.Key)) StationServices.Load(depot, Goods(order), 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()) + StationServices.TryStorePart(bay, depot, stack); + depot.TryUndock(ship, piloted: true); + } + + private static IReadOnlyCollection Goods(HaulOrderData order) => order.Goods.Select(good => good.Key).ToArray(); + + private static IEnumerable Carried(Ship ship, HaulOrderData order) + { + var goods = Goods(order); + return ship.CargoBays.SelectMany(bay => bay.Cargo.Keys).OfType().Where(stack => goods.Contains(((ItemInstance) stack).Data.Key)).ToArray(); + } +} diff --git a/Assets/Scripts/ServerShared/Pilot.cs b/Assets/Scripts/ServerShared/Pilot.cs index e0f51078c..6600d95fd 100644 --- a/Assets/Scripts/ServerShared/Pilot.cs +++ b/Assets/Scripts/ServerShared/Pilot.cs @@ -58,12 +58,15 @@ public sealed class PilotCommand internal TargetVerb TargetVerb; // The step's give verb: a Give, or a stop when its Receiver is null. The last one in a step wins. internal (Entity Receiver, ItemInstance Item, int Quantity)? GiveVerb; + // The step's call on a station (HaulRun.Call): a one-shot a hauler's pilot commands. + internal Entity CallVerb; internal int AimCycles; internal int ShutdownOverrideToggles, HeatsinkToggles, ShieldToggles, Pings; public void Designate(TargetRef target) => Designations.Add(target); public void SelectItem(EquippedItem item) => Selections.Add(item); public void Hail(string message) => Hails.Add(message); + public void Call(Entity station) => CallVerb = station; public void Use(ConsumableItemData consumable) => Uses.Add(consumable); public void Pull(ActivatedBehavior behavior) => Triggers.Add((behavior, true)); public void Release(ActivatedBehavior behavior) => Triggers.Add((behavior, false)); @@ -120,6 +123,7 @@ public sealed class PilotCommand Iffs.Clear(); TargetVerb = TargetVerb.None; GiveVerb = null; + CallVerb = null; AimCycles = 0; ShutdownOverrideToggles = HeatsinkToggles = ShieldToggles = Pings = 0; } @@ -161,6 +165,7 @@ public static class PilotApplier if (give.Receiver == null) CargoTransfer.End(entity, GiveEnd.Stopped); else if (!ReferenceEquals(give.Receiver, entity)) CargoTransfer.Begin(entity, new GiveOrder(give.Receiver, give.Item, give.Quantity)); } + if (command.CallVerb is Entity station && entity is Ship caller) HaulRun.Call(caller, station); if (command.Aim is float3 aim) entity.Aim = aim; foreach (var (behavior, pull) in command.Triggers) if (pull) behavior.Activate(); diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index 13672376b..a60583d42 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -327,7 +327,7 @@ public class Zone _flights[key] = flight = new Flight(ship.Faction?.Doctrine, _itemManager.GameplaySettings); var id = NextAgentId(); var agent = new Minion(ship, flight.Combatant) { Id = id }; - flight.Join(agent, stated ?? PatrolCircuit(id)); + flight.Join(agent, stated ?? (AgentTask) HaulRun.TaskOf(ship) ?? PatrolCircuit(id)); return agent; }