Compound stack sale moves only what the station holds (ruling no-market-absorption)
TrySellPart sells the units the station's bays take and pays for those alone; the rest stays in the seller's hold and the trade menu says how many sold. Simple goods keep their absorb path until the station-outflow cut. CommodityRecordsTests pins CompoundCommodityData's one key, MaxStack at 12. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
613ef9a185
commit
2a15c2f95c
4 changed files with 77 additions and 29 deletions
|
|
@ -100,26 +100,53 @@ public static class StationServices
|
|||
Math.Max(1, Math.Min(Math.Min(typed, credits / BuyPrice(items, stock)), stock is ICommodityStack stack ? stack.Quantity : 1));
|
||||
|
||||
// The sale: the item leaves the bay and the station pays SellPrice. A crafted item goes to one of the station's
|
||||
// cargo bays; false, with the item in its bay and the credits unchanged, when no bay can take it. A commodity stack,
|
||||
// simple or compound, always sells (aetheria-release:ruling:station-market-absorbs-sold-commodity, and
|
||||
// aetheria-living-world:question:compound-stack-sale-absorbs, option absorb): the bays take as much of the stack as
|
||||
// fits and the market absorbs the rest, at the full price.
|
||||
// cargo bays; false, with the item in its bay and the credits unchanged, when no bay can take it. A simple
|
||||
// commodity always sells (aetheria-release:ruling:station-market-absorbs-sold-commodity, retired for every good by
|
||||
// aetheria-living-world:ruling:no-market-absorption; a separate station-outflow cut removes this path): the bays take
|
||||
// as much of the stack as fits and the market absorbs the rest, at the full price. A compound stack sells by
|
||||
// TrySellPart: only what the bays take.
|
||||
public static bool TrySell(EquippedCargoBay from, Entity station, ItemInstance item, ref int credits)
|
||||
{
|
||||
if (item is CompoundCommodity compound) return TrySellPart(from, station, compound, ref credits) > 0;
|
||||
var price = SellPrice(station.ItemManager, item);
|
||||
if (item is ICommodityStack stack)
|
||||
if (item is SimpleCommodity commodity)
|
||||
{
|
||||
if (!from.Cargo.ContainsKey(item)) return false;
|
||||
var offered = stack.Quantity;
|
||||
var offered = commodity.Quantity;
|
||||
for (var quantity = offered; quantity >= 1; quantity--)
|
||||
if (station.CargoBays.Any(bay => from.TryTransferItem(bay, item, quantity))) break;
|
||||
if (from.Cargo.ContainsKey(item)) from.Remove(item, stack.Quantity);
|
||||
if (from.Cargo.ContainsKey(item)) from.Remove(item, commodity.Quantity);
|
||||
}
|
||||
else if (!station.CargoBays.Any(bay => from.TryTransferItem(bay, item, 1))) return false;
|
||||
credits += price;
|
||||
return true;
|
||||
}
|
||||
|
||||
// The sale of a compound stack (aetheria-living-world:ruling:no-market-absorption): the station's bays take what they
|
||||
// can hold, the station pays SellPrice for those units alone, and the rest stays in the seller's hold. Returns the
|
||||
// units sold; zero, with nothing moved and the credits unchanged, when no bay has room for one.
|
||||
public static int TrySellPart(EquippedCargoBay from, Entity station, CompoundCommodity stack, ref int credits)
|
||||
{
|
||||
if (!from.Cargo.ContainsKey(stack)) return 0;
|
||||
var sold = 0;
|
||||
var remaining = stack.Quantity;
|
||||
while (remaining > 0)
|
||||
{
|
||||
var moved = 0;
|
||||
for (var quantity = remaining; quantity >= 1 && moved == 0; quantity--)
|
||||
{
|
||||
var q = quantity;
|
||||
var part = stack.Split(q);
|
||||
if (station.CargoBays.Any(bay => from.TryTransferItem(bay, stack, q))) moved = q;
|
||||
credits += moved > 0 ? SellPrice(station.ItemManager, part) : 0;
|
||||
}
|
||||
if (moved == 0) break;
|
||||
sold += moved;
|
||||
remaining -= moved;
|
||||
}
|
||||
return sold;
|
||||
}
|
||||
|
||||
// The ore stacks of the bays are the stacks of any design a field kind yields, mined, bought or looted alike (ruling
|
||||
// simple-commodities-fungible); ammunition and other simple goods no belt yields are not ore.
|
||||
private static ItemInstance[] OreStacks(ItemManager items, IEnumerable<EquippedCargoBay> bays) =>
|
||||
|
|
|
|||
|
|
@ -296,6 +296,19 @@ public class TradeMenu : MonoBehaviour
|
|||
|
||||
private void Sell(ItemInstance item)
|
||||
{
|
||||
if (item is CompoundCommodity compound)
|
||||
{
|
||||
var offered = compound.Quantity;
|
||||
var sold = StationServices.TrySellPart(_targetCargo, GameManager.DockedEntity, compound, ref GameManager.Credits);
|
||||
UpdateCreditsLabel();
|
||||
Populate();
|
||||
if (sold == offered) return;
|
||||
Dialog.Clear();
|
||||
Dialog.Title.text = $"Sold {sold} of {offered}: Station Cargo Full!";
|
||||
Dialog.Show();
|
||||
Dialog.MoveToCursor();
|
||||
return;
|
||||
}
|
||||
if (StationServices.TrySell(_targetCargo, GameManager.DockedEntity, item, ref GameManager.Credits))
|
||||
{
|
||||
UpdateCreditsLabel();
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public sealed class CommodityRecordsTests
|
|||
int[] KeysOf(Type type) => type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
||||
.Select(f => f.GetCustomAttribute<KeyAttribute>()?.IntKey).Where(k => k.HasValue).Select(k => k.Value).ToArray();
|
||||
Assert.Equal(new[] { 9 }, KeysOf(typeof(SimpleCommodityData)));
|
||||
Assert.Empty(KeysOf(typeof(CompoundCommodityData)));
|
||||
Assert.Equal(new[] { 12 }, KeysOf(typeof(CompoundCommodityData))); // MaxStack, added by cut compound-commodity-stacks; 10 and 11 stay unused
|
||||
Assert.Null(typeof(SimpleCommodityData).GetField("Category"));
|
||||
Assert.Null(typeof(CompoundCommodityData).GetField("Category"));
|
||||
Assert.Null(typeof(CompoundCommodityData).GetField("DemandProfile"));
|
||||
|
|
|
|||
|
|
@ -214,42 +214,50 @@ public sealed class CompoundStackTests : IDisposable
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void AStackIsPricedPerUnitAndAlwaysSells()
|
||||
public void AStackIsPricedPerUnitAndSellsOnlyWhatTheStationCanHold()
|
||||
{
|
||||
var unit = Gem(_lotA, 1);
|
||||
var unitPrice = _items.GetPrice(unit);
|
||||
Assert.True(unitPrice > 0);
|
||||
Assert.Equal(unitPrice * 3, StationServices.BuyPrice(_items, unit, 3));
|
||||
var stack = Gem(_lotA, 3);
|
||||
var fraction = (double) _items.GameplaySettings.SellFraction;
|
||||
var expected = (int) Math.Floor(unitPrice * 3 * fraction);
|
||||
Assert.Equal(expected, StationServices.SellPrice(_items, stack));
|
||||
Assert.True(expected > 0);
|
||||
int Pays(int units) => (int) Math.Floor(unitPrice * units * fraction);
|
||||
Assert.Equal(Pays(3), StationServices.SellPrice(_items, Gem(_lotA, 3)));
|
||||
Assert.True(Pays(3) > 0);
|
||||
|
||||
// The station's one bay is full of guns, but for a stack of the same lot with one unit of room: the unit it takes,
|
||||
// the market absorbs the rest, and the full price is paid.
|
||||
// Empty bay: the whole stack goes and is paid in full.
|
||||
var from = ShipWith("Crate").CargoBays[0];
|
||||
var stack = Gem(_lotA, 3);
|
||||
Assert.True(from.TryStore(stack));
|
||||
var station = Station();
|
||||
var hold = station.CargoBays[0];
|
||||
var held = Gem(_lotA, 3);
|
||||
Assert.True(hold.TryStore(held));
|
||||
while (hold.TryStore(Mint("Gun"))) { }
|
||||
var credits = 100;
|
||||
Assert.True(StationServices.TrySell(from, station, stack, ref credits));
|
||||
Assert.Equal(100 + expected, credits);
|
||||
Assert.Equal(3, StationServices.TrySellPart(from, station, stack, ref credits));
|
||||
Assert.Equal(100 + Pays(3), credits);
|
||||
Assert.Empty(from.Cargo);
|
||||
Assert.Equal(4, held.Quantity);
|
||||
Assert.Equal(Weight(hold), hold.Mass, 3);
|
||||
Assert.Equal(3, station.CargoBays[0].Count(_cache.RefOf<ItemData>(_gem).Key));
|
||||
|
||||
// A hold with no room at all still pays and empties the stack.
|
||||
var second = Gem(_lotA, 2);
|
||||
// Part-full bay: the station's stack of the lot has room for one unit and every other cell holds a gun, so one unit
|
||||
// sells, is paid for alone, and two stay in the seller's hold.
|
||||
var second = Gem(_lotA, 3);
|
||||
Assert.True(from.TryStore(second));
|
||||
var hold = station.CargoBays[0];
|
||||
while (hold.TryStore(Mint("Gun"))) { }
|
||||
credits = 0;
|
||||
Assert.True(StationServices.TrySell(from, station, second, ref credits));
|
||||
Assert.Equal((int) Math.Floor(unitPrice * 2 * fraction), credits);
|
||||
Assert.Empty(from.Cargo);
|
||||
Assert.Equal(4, held.Quantity);
|
||||
Assert.Equal(1, StationServices.TrySellPart(from, station, second, ref credits));
|
||||
Assert.Equal(Pays(1), credits);
|
||||
Assert.Equal(2, second.Quantity);
|
||||
Assert.Contains(second, from.Cargo.Keys);
|
||||
Assert.Equal(4, hold.Count(_cache.RefOf<ItemData>(_gem).Key));
|
||||
Assert.Equal(Weight(hold), hold.Mass, 3);
|
||||
Assert.Equal(Weight(from), from.Mass, 3);
|
||||
|
||||
// Full bay: nothing moves and nothing is paid.
|
||||
credits = 0;
|
||||
Assert.Equal(0, StationServices.TrySellPart(from, station, second, ref credits));
|
||||
Assert.False(StationServices.TrySell(from, station, second, ref credits));
|
||||
Assert.Equal(0, credits);
|
||||
Assert.Equal(2, second.Quantity);
|
||||
Assert.Equal(4, hold.Count(_cache.RefOf<ItemData>(_gem).Key));
|
||||
}
|
||||
|
||||
private byte[] PreCutUnit(int lot, MessagePackSerializerOptions options)
|
||||
|
|
|
|||
Loading…
Reference in a new issue