| 1 | import { |
| 2 | cloneStack, |
| 3 | insertStack, |
| 4 | stackQuantity, |
| 5 | takeFromSlot, |
| 6 | INVENTORY_SLOTS |
| 7 | } from "../items/inventory.js"; |
| 8 | import { itemDefinition } from "../definitions/items.js"; |
| 9 | import { attachOrigins, normalizeOrigins, splitOrigins } from "../items/origin.js"; |
| 10 | |
| 11 | /** |
| 12 | * How many additional empty bag slots a player would need after giving |
| 13 | * `giveLines` and receiving `receiveStacks`. 0 = swap fits. |
| 14 | * |
| 15 | * @param {Array} inventorySlots |
| 16 | * @param {Array<{ bagSlotIndex: number, quantity: number }>} giveLines |
| 17 | * @param {Array} receiveStacks stacks the partner is offering (item payloads) |
| 18 | */ |
| 19 | export function inventorySlotsShortAfterTrade (inventorySlots, giveLines, receiveStacks) |
| 20 | { |
| 21 | let next = (inventorySlots ?? []).map((slot) => cloneStack(slot)); |
| 22 | while (next.length < INVENTORY_SLOTS) |
| 23 | { next.push(null); } |
| 24 | |
| 25 | const gives = [...(giveLines ?? [])] |
| 26 | .map((line) => ({ |
| 27 | bagSlotIndex: line.bagSlotIndex | 0, |
| 28 | quantity: Math.max(1, line.quantity | 0 || 1) |
| 29 | })) |
| 30 | .sort((a, b) => b.bagSlotIndex - a.bagSlotIndex); |
| 31 | |
| 32 | for (const line of gives) |
| 33 | { |
| 34 | const taken = takeFromSlot(next, line.bagSlotIndex, line.quantity); |
| 35 | if (!taken.ok) |
| 36 | { return INVENTORY_SLOTS; } |
| 37 | next = taken.slots; |
| 38 | } |
| 39 | |
| 40 | for (const stack of receiveStacks ?? []) |
| 41 | { |
| 42 | if (!stack) |
| 43 | { continue; } |
| 44 | const def = itemDefinition(stack.itemId); |
| 45 | if (!def) |
| 46 | { return INVENTORY_SLOTS; } |
| 47 | const inserted = insertStack(next, cloneStack(stack), def); |
| 48 | if (!inserted.ok || (inserted.remainder | 0) > 0) |
| 49 | { |
| 50 | const empty = next.filter((slot) => !slot).length; |
| 51 | const needApprox = Math.max(1, Math.ceil( |
| 52 | stackQuantity(stack.quantity) / Math.max(1, def.maxStackSize | 0 || 1) |
| 53 | )); |
| 54 | return Math.max(1, needApprox - empty); |
| 55 | } |
| 56 | next = inserted.slots; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Snapshot offer lines as transferable stacks from a bag (or null if invalid). |
| 63 | * Origins are sliced to the offered quantity (same as takeFromSlot). |
| 64 | */ |
| 65 | export function resolveOfferStacks (inventorySlots, offerLines) |
| 66 | { |
| 67 | const stacks = []; |
| 68 | for (const line of offerLines ?? []) |
| 69 | { |
| 70 | const slot = inventorySlots?.[line.bagSlotIndex | 0]; |
| 71 | const qty = Math.max(1, line.quantity | 0 || 1); |
| 72 | if (!slot || stackQuantity(slot.quantity) < qty) |
| 73 | { return null; } |
| 74 | const split = splitOrigins(normalizeOrigins(slot), qty); |
| 75 | stacks.push(attachOrigins({ |
| 76 | itemId: slot.itemId, |
| 77 | quantity: qty, |
| 78 | instance: slot.instance ? structuredClone(slot.instance) : null, |
| 79 | origins: split.taken |
| 80 | })); |
| 81 | } |
| 82 | return stacks; |
| 83 | } |