From c104ca834a99747cd4d251655b39d1b874e93283 Mon Sep 17 00:00:00 2001 From: soul Date: Sat, 28 Nov 2026 14:05:48 +0100 Subject: [PATCH] A good lands only in the bays that may hold it: StationWants.Bays, TryStorePart (the sale's gate and move without the price), Load (silo to ship); Zenith buys only into its silo Deletes TrySellPart's any-bay move (zenith-keeps-buying-past-its-silo). Co-Authored-By: Claude Sonnet 5.5 --- .../Scripts/ServerShared/StationServices.cs | 59 +++++++++++++++---- Assets/Scripts/ServerShared/StationWants.cs | 28 +++++++-- tests/Aetheria.Shared.Tests/HaulRunTests.cs | 53 +++++++++++++++++ 3 files changed, 123 insertions(+), 17 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/HaulRunTests.cs diff --git a/Assets/Scripts/ServerShared/StationServices.cs b/Assets/Scripts/ServerShared/StationServices.cs index 26945d364..3af42a483 100644 --- a/Assets/Scripts/ServerShared/StationServices.cs +++ b/Assets/Scripts/ServerShared/StationServices.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using GameCult.Caching; // What a station pays for goods and charges to mend gear. The one owner of the sell price, the repair cost, and the // two commits that move goods or restore durability against the run's credits; TradeMenu shows these numbers and @@ -114,32 +115,64 @@ public static class StationServices return true; } - // The sale of a commodity stack: only a good the station wants (StationWants; NotWanted moves nothing). The station's bays - // take what they can hold (aetheria-living-world:ruling:no-market-absorption), the station pays SellPrice for those units - // alone, and the rest stays in the seller's hold with the refusal NoRoom. Sold units are moved, never absorbed. + // The sale of a commodity stack: TryStorePart, and the station pays SellPrice for the units its bays took, added to + // `credits` (aetheria-living-world:ruling:no-market-absorption: no unit is paid for that no bay took). The rest stays in the + // seller's hold with the refusal NoRoom; a good the station does not want moves nothing (NotWanted). public static SaleResult TrySellPart(EquippedCargoBay from, Entity station, ICommodityStack stack, ref int credits) + { + var paid = 0; + var result = TryStorePart(from, station, stack, part => paid += SellPrice(station.ItemManager, part)); + credits += paid; + return result; + } + + // The station taking a commodity stack into the bays that may hold it (StationWants.Bays), as far as they hold, with no + // price: the gate and the move of every sale and of a hauler's delivery (ruling transit-depots-buy-for-onward-haul). + // `moved` is called with each part that went across. Sold units are moved, never absorbed. + public static SaleResult TryStorePart(EquippedCargoBay from, Entity station, ICommodityStack stack, Action moved = null) { var item = (ItemInstance) stack; var offered = stack.Quantity; if (!from.Cargo.ContainsKey(item)) return new SaleResult(0, offered, SaleRefusal.None); if (!StationWants.Of(station).Contains(item.Data.Key)) return new SaleResult(0, offered, SaleRefusal.NotWanted); - var sold = 0; - var remaining = offered; + var stored = MoveParts(from, StationWants.Bays(station, item.Data.Key).ToArray(), stack, moved); + 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. + 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); + return loaded; + } + + // 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) + { + var item = (ItemInstance) stack; + var total = 0; + var remaining = stack.Quantity; while (remaining > 0) { - var moved = 0; - for (var quantity = remaining; quantity >= 1 && moved == 0; quantity--) + var count = 0; + for (var quantity = remaining; quantity >= 1 && count == 0; quantity--) { var q = quantity; var part = stack.Split(q); - if (station.CargoBays.Any(bay => from.TryTransferItem(bay, item, q))) moved = q; - credits += moved > 0 ? SellPrice(station.ItemManager, part) : 0; + if (!into.Any(bay => from.TryTransferItem(bay, item, q))) continue; + count = q; + moved?.Invoke(part); } - if (moved == 0) break; - sold += moved; - remaining -= moved; + if (count == 0) break; + total += count; + remaining -= count; } - return new SaleResult(sold, offered, sold < offered ? SaleRefusal.NoRoom : SaleRefusal.None); + return total; } // The ore stacks of the bays are the stacks of any design a field kind yields, mined, bought or looted alike (ruling diff --git a/Assets/Scripts/ServerShared/StationWants.cs b/Assets/Scripts/ServerShared/StationWants.cs index 4c203387b..68eab133e 100644 --- a/Assets/Scripts/ServerShared/StationWants.cs +++ b/Assets/Scripts/ServerShared/StationWants.cs @@ -11,6 +11,22 @@ using Newtonsoft.Json; public static class StationWants { public static IReadOnlyCollection Of(Entity station) + { + var wants = Drawn(station); + wants.UnionWith(Ordered(station)); + return wants; + } + + // The bays a bought or delivered good may land in: every bay when an installed recipe facility draws it, only the silo bays + // when the station wants it only 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 Bays(Entity station, CultRecordKey good) + { + if (Drawn(station).Contains(good)) return station.CargoBays; + return Ordered(station).Contains(good) ? station.CargoBays.Where(bay => bay.Data is SiloData) : Enumerable.Empty(); + } + + private static HashSet Drawn(Entity station) { var catalog = station.ItemManager.ItemData; var wants = new HashSet(); @@ -22,12 +38,16 @@ public static class StationWants foreach (var draw in Production.Draws(catalog, recipe)) wants.Add(draw); } - if (station is OrbitalEntity { OrbitData: var orbit } && orbit.IsSet() && station.CargoBays.Any(bay => bay.Data is SiloData)) - foreach (var order in catalog.GetAll().Where(order => order.From.Key.Equals(orbit))) - foreach (var good in order.Goods) - wants.Add(good.Key); return wants; } + + private static IEnumerable Ordered(Entity station) + { + if (!(station is OrbitalEntity { OrbitData: var orbit }) || !orbit.IsSet() || !station.CargoBays.Any(bay => bay.Data is SiloData)) + return Enumerable.Empty(); + return station.ItemManager.ItemData.GetAll().Where(order => order.From.Key.Equals(orbit)) + .SelectMany(order => order.Goods.Select(good => good.Key)).ToArray(); + } } // A standing haul order: goods that move from one station's orbit to another's, keyed haul-order:. A run document: saved diff --git a/tests/Aetheria.Shared.Tests/HaulRunTests.cs b/tests/Aetheria.Shared.Tests/HaulRunTests.cs new file mode 100644 index 000000000..38f0048c0 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/HaulRunTests.cs @@ -0,0 +1,53 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System; +using System.Collections.Generic; +using System.Linq; +using GameCult.Caching; +using Xunit; + +// The demo's ore haul (aetheria-living-world, cut ore-haul-run; rulings transit-depots-buy-for-onward-haul, demo-zenith-ore-depot, +// no-market-absorption, comms-cells-are-zones, docking-is-enclosed-bays): a depot buys only into its silo, and a real hauler ship +// carries the silo's ore to the refinery under the standing haul order. +public sealed partial class RunStartTests +{ + // Finding zenith-keeps-buying-past-its-silo: with the silo full, a sale of order ore sells nothing and the stock bay does not + // take it; the refinery still takes drawn ore into its one bay. + [Fact] + public void ZenithBuysOnlyIntoItsSilo() + { + var (_, arena, staged, failures) = Launch(new DemoTerminus(), Inputs(() => 1)); + Assert.True(failures.Count == 0, string.Join("; ", failures)); + var zenith = StationOfKind(arena, "trade-hub"); + var refinery = StationOfKind(arena, "processing"); + var player = staged.Player; + var silo = zenith.CargoBays.Single(bay => bay.Data is SiloData); + var stock = zenith.CargoBays.Single(bay => !(bay.Data is SiloData)); + var good = StationWants.Of(zenith).OrderBy(key => key.Value, StringComparer.Ordinal).First(); + Assert.Equal(new[] { silo }, StationWants.Bays(zenith, good)); + Assert.Empty(StationWants.Bays(zenith, new CultRecordKey("commodity:not-wanted"))); + + while (silo.TryStore(new SimpleCommodity { Data = new CultRecordRef(good), Quantity = 10 })) { } + var before = stock.Cargo.Select(held => (held.Key, held.Value)).ToArray(); + var hold = player.CargoBays[0]; + var offered = new SimpleCommodity { Data = new CultRecordRef(good), Quantity = 5 }; + Assert.True(hold.TryStore(offered)); + var credits = 0; + var sale = StationServices.TrySellPart(hold, zenith, offered, ref credits); + Assert.Equal((0, 5, SaleRefusal.NoRoom), (sale.Sold, sale.Offered, sale.Refusal)); + Assert.Equal(0, credits); + Assert.Equal(5, hold.Count(good)); + Assert.Equal(before, stock.Cargo.Select(held => (held.Key, held.Value)).ToArray()); + Assert.Equal(0, stock.Count(good)); + + var drawn = StationWants.Of(refinery).First(key => FieldKinds.IsOre(_cache, _cache.Get(new CultRecordRef(key)))); + Assert.Equal(refinery.CargoBays, StationWants.Bays(refinery, drawn)); + var ore = new SimpleCommodity { Data = new CultRecordRef(drawn), Quantity = 5 }; + Assert.True(hold.TryStore(ore)); + var taken = StationServices.TrySellPart(hold, refinery, ore, ref credits); + Assert.Equal((5, SaleRefusal.None), (taken.Sold, taken.Refusal)); + Assert.True(credits > 0); + } +}