Cargo bay owns stack quantity: TryStore refuses below 1 and above MaxStack, TryBuyLots owns the stock limit, restore logs dropped stacks

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-10-22 22:01:42 +02:00
commit ddae55759d
3 changed files with 19 additions and 6 deletions

View file

@ -2530,9 +2530,12 @@ public class EquippedCargoBay : EquippedItem
// Try to store the given commodity at the given position
// If there's a stack at the given position it will be added to
// Returns true only when ALL of the items are successfully stored
// The bay decides what quantity may sit in it: a stack below one unit or above its design's MaxStack is refused with
// the bay as it was, so no caller, loot, pickup or restore has to check for it.
public bool TryStore(SimpleCommodity item, int2 cargoCoord)
{
var itemData = ItemManager.GetData(item);
if (item.Quantity < 1 || item.Quantity > itemData.MaxStack) return false;
if (ItemFits(item, cargoCoord))
{
foreach (var p in itemData.Shape.Coordinates)

View file

@ -47,6 +47,15 @@ public static class EntitySerializer
return pack;
}
// A saved stack goes back where it was. The bay refuses what cannot sit in it (a stack below one unit, as the typed
// -5 purchase of old saves left, or above MaxStack), and the refusal is logged, never silent.
private static void RestoreStack(ItemManager itemManager, EquippedCargoBay bay, int2 position, ItemInstance item)
{
if (bay.TryStore(item, position)) return;
var quantity = item is SimpleCommodity commodity ? $" x{commodity.Quantity}" : "";
itemManager.Log($"Dropped a saved item the bay refused at {position}: {itemManager.GetData(item).Name}{quantity}");
}
public static Entity Unpack(ItemManager itemManager, Zone zone, EntityPack pack)
{
return pack switch
@ -103,12 +112,12 @@ public static class EntitySerializer
for (var bayIndex = 0; bayIndex < pack.CargoContents.Length; bayIndex++)
if(entity.CargoBays.Count >= bayIndex + 1)
foreach (var (position, item) in pack.CargoContents[bayIndex])
entity.CargoBays[bayIndex].TryStore(item, position);
RestoreStack(itemManager, entity.CargoBays[bayIndex], position, item);
for (var bayIndex = 0; bayIndex < pack.DockingBayContents.Length; bayIndex++)
if(entity.DockingBays.Count >= bayIndex + 1)
foreach (var (position, item) in pack.DockingBayContents[bayIndex])
entity.DockingBays[bayIndex].TryStore(item, position);
RestoreStack(itemManager, entity.DockingBays[bayIndex], position, item);
entity.Temperature = pack.Temperature;

View file

@ -79,12 +79,13 @@ public static class StationServices
return BuyResult.Bought;
}
// The purchase of `quantity` units of a station stack, one lot of at most MaxStack at a time. `deliver` puts a lot of
// that many units where it goes, and each lot is charged for the units it delivered. The first lot the station
// cannot sell or the player cannot take ends the purchase, with the lots before it bought.
// The purchase of `quantity` units of a station stack, one lot of at most MaxStack at a time. The stock is the
// station's: a purchase of more than the stack holds, or of a stack that holds nothing, is refused before anything
// moves, so every lot is units the station has and `deliver` moves whole, and each lot is charged for the units it
// delivered. The first lot the player cannot take ends the purchase, with the lots before it bought.
public static BuyResult TryBuyLots(ItemManager items, SimpleCommodity stock, int quantity, ref int credits, Func<int, bool> deliver)
{
if (quantity < 1) return BuyResult.NoRoom;
if (quantity < 1 || quantity > stock.Quantity) return BuyResult.NoRoom;
var maxStack = items.GetData(stock).MaxStack;
for (var remaining = quantity; remaining > 0;)
{