Trade: the purchase loop moves to StationServices.TryBuyLots, charging each lot it delivers; the Buy Quantity field clamps to at least one

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-10-22 21:10:07 +02:00
commit f0d7c12cbd
2 changed files with 25 additions and 12 deletions

View file

@ -79,6 +79,28 @@ 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.
public static BuyResult TryBuyLots(ItemManager items, SimpleCommodity stock, int quantity, ref int credits, Func<int, bool> deliver)
{
if (quantity < 1) return BuyResult.NoRoom;
var maxStack = items.GetData(stock).MaxStack;
for (var remaining = quantity; remaining > 0;)
{
var lot = Math.Min(remaining, maxStack);
var result = TryBuy(items, stock, lot, ref credits, () => deliver(lot));
if (result != BuyResult.Bought) return result;
remaining -= lot;
}
return BuyResult.Bought;
}
// The quantity a Buy Quantity field may hold: what was typed, at most what the credits cover and what the station
// has, and never below one unit (a price the credits do not cover is then refused as short of credits).
public static int BuyableQuantity(ItemManager items, SimpleCommodity stock, int typed, int credits) =>
Math.Max(1, Math.Min(Math.Min(typed, credits / BuyPrice(items, stock)), stock.Quantity));
// The sale: the item leaves the bay for one of the station's cargo bays and the station pays SellPrice. False,
// with the item in its bay and the credits unchanged, when no station bay can take it.
public static bool TrySell(EquippedCargoBay from, Entity station, ItemInstance item, ref int credits)

View file

@ -270,7 +270,7 @@ public class TradeMenu : MonoBehaviour
Dialog.Title.text = $"Buying {GameManager.ItemManager.Title(i.item)}";
Dialog.AddField("Quantity",
() => quantity,
q => quantity = min(min(q, GameManager.Credits / StationServices.BuyPrice(GameManager.ItemManager, s)), s.Quantity));
q => quantity = StationServices.BuyableQuantity(GameManager.ItemManager, s, q, GameManager.Credits));
Dialog.Show(() =>
{
Buy(s,quantity);
@ -343,17 +343,8 @@ public class TradeMenu : MonoBehaviour
private void Buy(SimpleCommodity simpleCommodity, int quantity)
{
var data = GameManager.ItemManager.GetData(simpleCommodity);
// Up-rounded integer division from https://stackoverflow.com/a/503201
int lots = (quantity - 1) / data.MaxStack + 1;
int remaining = quantity;
for (int i = 0; i < lots; i++)
{
int q = min(remaining, data.MaxStack);
if (!Bought(StationServices.TryBuy(GameManager.ItemManager, simpleCommodity, q, ref GameManager.Credits,
() => Inventory.TryTransferItem(_targetCargo, simpleCommodity, quantity)))) return;
remaining -= q;
}
Bought(StationServices.TryBuyLots(GameManager.ItemManager, simpleCommodity, quantity, ref GameManager.Credits,
lot => Inventory.TryTransferItem(_targetCargo, simpleCommodity, lot)));
}
// Refreshes the credits after a purchase, or tells the player why the station refused it.