diff --git a/Assets/Scripts/ServerShared/Agents/Agent.cs b/Assets/Scripts/ServerShared/Agents/Agent.cs index 03a76436a..f4660976b 100644 --- a/Assets/Scripts/ServerShared/Agents/Agent.cs +++ b/Assets/Scripts/ServerShared/Agents/Agent.cs @@ -34,7 +34,11 @@ public class Agent public GameplaySettings Settings { get; } // How this pilot fights: its faction's doctrine for its role (Zone.CreateAgent resolves it). public RoleDoctrine Doctrine { get; } - public float TopSpeed => _velocityLimit?.Limit ?? 100; + // How fast this hull flies: its VelocityLimit, or 100 without one. The pilot (the cached limit) and an off-screen hauler's flight + // (HaulRun.Fly, TopSpeedOf) read the one rule. + public float TopSpeed => SpeedOf(_velocityLimit); + public static float TopSpeedOf(Ship ship) => SpeedOf(ship.GetBehavior()); + private static float SpeedOf(VelocityLimit limit) => limit?.Limit ?? 100; public Agent(Ship ship, RoleDoctrine doctrine) { diff --git a/Assets/Scripts/ServerShared/HaulRun.cs b/Assets/Scripts/ServerShared/HaulRun.cs index 1240fa62e..8db9e6466 100644 --- a/Assets/Scripts/ServerShared/HaulRun.cs +++ b/Assets/Scripts/ServerShared/HaulRun.cs @@ -23,15 +23,18 @@ public static class HaulRun 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. + // Whether a call at this station would move a unit: 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. public static bool CanCall(Ship ship, OrbitalEntity station, HaulOrderData order) { - if (station.DockingBays.All(bay => bay.DockedShip != null)) return false; + var dock = station.DockingBays.FirstOrDefault(bay => bay.DockedShip == null); + if (dock == null || dock.Cargo.Any()) 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)))); + 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))))); 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; diff --git a/Assets/Scripts/ServerShared/StationServices.cs b/Assets/Scripts/ServerShared/StationServices.cs index 356989eec..9434f63ef 100644 --- a/Assets/Scripts/ServerShared/StationServices.cs +++ b/Assets/Scripts/ServerShared/StationServices.cs @@ -141,15 +141,17 @@ public static class StationServices return new SaleResult(stored, offered, stored < offered ? SaleRefusal.NoRoom : SaleRefusal.None); } - // The station's silo bays giving the ship the units of `goods` they hold, as far as the ship's bays take them. The one writer - // of a station-to-hauler move; a hauler docks to call it (ruling docking-is-enclosed-bays). Returns the units moved. + // The bays where the station's outputs land (StationWants.OutputBays: its silo, or every bay with no silo) giving the ship the units of + // `goods` they hold, as far as the ship's bays take them. The one writer of a station-to-hauler move; a hauler docks to call it (ruling + // docking-is-enclosed-bays). Returns the units moved. public static int Load(Entity station, IReadOnlyCollection goods, Entity ship) { var into = ship.CargoBays.ToArray(); var loaded = 0; - foreach (var silo in station.CargoBays.Where(bay => bay.Data is SiloData).ToArray()) - foreach (var stack in silo.Cargo.Keys.OfType().Where(stack => goods.Contains(((ItemInstance) stack).Data.Key)).ToArray()) - loaded += MoveParts(silo, into, stack, null); + foreach (var good in goods) + foreach (var bay in StationWants.OutputBays(station, good).ToArray()) + foreach (var stack in bay.Cargo.Keys.OfType().Where(stack => ((ItemInstance) stack).Data.Key.Equals(good)).ToArray()) + loaded += MoveParts(bay, into, stack, null); return loaded; } diff --git a/Assets/Scripts/ServerShared/StationWants.cs b/Assets/Scripts/ServerShared/StationWants.cs index 3d1b1a53a..3988bf737 100644 --- a/Assets/Scripts/ServerShared/StationWants.cs +++ b/Assets/Scripts/ServerShared/StationWants.cs @@ -50,8 +50,8 @@ public static class StationWants { var silos = station.CargoBays.Where(bay => bay.Data is SiloData).ToArray(); if (silos.Length == 0) return null; - var outputs = FacilityRecipes(station).Select(recipe => recipe.Output.Key).ToArray(); - if (outputs.Length == 0) return null; + var outputs = Makes(station); + if (outputs.Count == 0) return null; var slots = outputs.Concat(Ordered(station)).Distinct().Count(); var cells = silos.Sum(bay => bay.Data.InteriorShape.Coordinates.Length); var stack = station.ItemManager.ItemData.Get(good)?.MaxStack ?? 0; @@ -80,11 +80,20 @@ public static class StationWants } } - private static HashSet Drawn(Entity station) + private static HashSet Drawn(Entity station) => Draws(station, startableOnly: true); + + // Every good the station may draw: what its installed recipe facilities draw, startable or not (what a seeded haul leg may bring it, which + // does not wait for room), and what its crew eats. The one reader of what gear could use; Drawn is the startable part. + public static IReadOnlyCollection Drawable(Entity station) => Draws(station, startableOnly: false); + + // The goods its installed recipe facilities make: the set Allocation builds. + public static IReadOnlyCollection Makes(Entity station) => FacilityRecipes(station).Select(recipe => recipe.Output.Key).ToHashSet(); + + private static HashSet Draws(Entity station, bool startableOnly) { var catalog = station.ItemManager.ItemData; var wants = new HashSet(); - foreach (var recipe in FacilityRecipes(station).Where(recipe => CanStart(station, recipe))) + foreach (var recipe in FacilityRecipes(station).Where(recipe => !startableOnly || CanStart(station, recipe))) foreach (var draw in Production.Draws(catalog, recipe)) wants.Add(draw); // What its crew eats: the table of every habitation unit that runs (the module defines what and how much it consumes).