| 1 | export const INVENTORY_SLOTS = 32; |
| 2 | export const EQUIPMENT_SLOTS = Object.freeze(["head", "chest", "legs", "boots", "weapon", "offhand"]); |
| 3 | |
| 4 | import { attachOrigins, mergeOriginLists, normalizeOrigins, splitOrigins } from "./origin.js"; |
| 5 | import { stackRulesFor } from "./stackRules.js"; |
| 6 | import { BLANK_CHEQUE_CARRY_CAP, ItemId, itemDefinition } from "../definitions/items.js"; |
| 7 | |
| 8 | /** Stack counts can exceed 32-bit range (coin stacks up to 999B). */ |
| 9 | export function stackQuantity (value) |
| 10 | { |
| 11 | const n = Number(value); |
| 12 | if (!Number.isFinite(n) || n <= 0) |
| 13 | { return 0; } |
| 14 | const q = Math.floor(n); |
| 15 | return Number.isSafeInteger(q) ? q : 0; |
| 16 | } |
| 17 | |
| 18 | export function emptySlots (count = INVENTORY_SLOTS) |
| 19 | { |
| 20 | return Array.from({ length: count }, () => null); |
| 21 | } |
| 22 | |
| 23 | export function emptyEquipment () |
| 24 | { |
| 25 | const slots = {}; |
| 26 | for (const slot of EQUIPMENT_SLOTS) |
| 27 | { slots[slot] = null; } |
| 28 | return { slots }; |
| 29 | } |
| 30 | |
| 31 | export function cloneStack (stack) |
| 32 | { |
| 33 | return stack ? structuredClone(stack) : null; |
| 34 | } |
| 35 | |
| 36 | /** One item from a stack, preserving a single origin slice (for equip). */ |
| 37 | export function singleItemFromStack (stack) |
| 38 | { |
| 39 | if (!stack) |
| 40 | { return null; } |
| 41 | const split = splitOrigins(normalizeOrigins(stack), 1); |
| 42 | return { |
| 43 | itemId: stack.itemId, |
| 44 | quantity: 1, |
| 45 | instance: stack.instance ? cloneStack(stack.instance) : null, |
| 46 | origins: split.taken |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | export function potionStackIdentity (instance) |
| 51 | { |
| 52 | if (!instance || instance.kind !== "potion") |
| 53 | { return null; } |
| 54 | const effectKey = instance.effectKey |
| 55 | ?? (instance.effect ? JSON.stringify(instance.effect) : ""); |
| 56 | return `${instance.potionKey ?? ""}|${effectKey}`; |
| 57 | } |
| 58 | |
| 59 | export function stacksMatch (left, right, definition, { inVault = false } = {}) |
| 60 | { |
| 61 | if (!left || !right || left.itemId !== right.itemId) |
| 62 | { return false; } |
| 63 | const rules = stackRulesFor(definition, { inVault }); |
| 64 | if (rules.uniqueInstance) |
| 65 | { |
| 66 | return JSON.stringify(left.instance ?? null) === JSON.stringify(right.instance ?? null); |
| 67 | } |
| 68 | if (definition?.potion) |
| 69 | { |
| 70 | const a = potionStackIdentity(left.instance); |
| 71 | const b = potionStackIdentity(right.instance); |
| 72 | return a != null && a === b; |
| 73 | } |
| 74 | if (left.instance != null || right.instance != null) |
| 75 | { return JSON.stringify(left.instance ?? null) === JSON.stringify(right.instance ?? null); } |
| 76 | return rules.stackable; |
| 77 | } |
| 78 | |
| 79 | function stackPayload (stack, quantity, origins) |
| 80 | { |
| 81 | return { |
| 82 | itemId: stack.itemId, |
| 83 | quantity, |
| 84 | instance: stack.instance ?? null, |
| 85 | origins: origins.map((entry) => ({ |
| 86 | origin: entry.origin ?? null, |
| 87 | quantity: stackQuantity(entry.quantity) |
| 88 | })).filter((entry) => entry.quantity > 0) |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | export function insertStack (slots, stack, definition, options = {}) |
| 93 | { |
| 94 | if (!stack || stack.quantity <= 0) |
| 95 | { return { ok: true, slots: slots.map(cloneStack), remainder: 0 }; } |
| 96 | const rules = stackRulesFor(definition, options); |
| 97 | const next = slots.map(cloneStack); |
| 98 | let remaining = stackQuantity(stack.quantity); |
| 99 | let pendingOrigins = normalizeOrigins(stack); |
| 100 | |
| 101 | if (rules.stackable && !rules.uniqueInstance) |
| 102 | { |
| 103 | for (const slot of next) |
| 104 | { |
| 105 | if (!slot || !stacksMatch(slot, stack, definition, options)) |
| 106 | { continue; } |
| 107 | const space = rules.maxStackSize - stackQuantity(slot.quantity); |
| 108 | if (space <= 0) |
| 109 | { continue; } |
| 110 | const add = Math.min(space, remaining); |
| 111 | const split = splitOrigins(pendingOrigins, add); |
| 112 | slot.origins = mergeOriginLists(normalizeOrigins(slot), split.taken); |
| 113 | slot.quantity = stackQuantity(slot.quantity) + add; |
| 114 | if (stack.instance != null && slot.instance == null) |
| 115 | { slot.instance = cloneStack(stack.instance); } |
| 116 | delete slot.origin; |
| 117 | pendingOrigins = split.remain; |
| 118 | remaining -= add; |
| 119 | if (remaining === 0) |
| 120 | { return { ok: true, slots: next, remainder: 0 }; } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | for (let i = 0; i < next.length; i++) |
| 125 | { |
| 126 | if (next[i]) |
| 127 | { continue; } |
| 128 | const cap = rules.maxStackSize === Number.POSITIVE_INFINITY ? remaining : rules.maxStackSize; |
| 129 | const amount = rules.stackable && !rules.uniqueInstance |
| 130 | ? Math.min(remaining, cap) |
| 131 | : 1; |
| 132 | const split = splitOrigins(pendingOrigins, amount); |
| 133 | next[i] = stackPayload(stack, amount, split.taken); |
| 134 | pendingOrigins = split.remain; |
| 135 | remaining -= amount; |
| 136 | if (remaining === 0) |
| 137 | { return { ok: true, slots: next, remainder: 0 }; } |
| 138 | if (!rules.stackable || rules.uniqueInstance) |
| 139 | { break; } |
| 140 | } |
| 141 | return { ok: false, slots, remainder: remaining }; |
| 142 | } |
| 143 | |
| 144 | export function takeFromSlot (slots, slotIndex, quantity = null) |
| 145 | { |
| 146 | const next = slots.map(cloneStack); |
| 147 | const slot = next[slotIndex]; |
| 148 | if (!slot) |
| 149 | { return { ok: false, slots, taken: null }; } |
| 150 | const maxQty = stackQuantity(slot.quantity); |
| 151 | const amount = quantity == null |
| 152 | ? maxQty |
| 153 | : Math.min(maxQty, Math.max(1, stackQuantity(quantity))); |
| 154 | if (amount <= 0) |
| 155 | { return { ok: false, slots, taken: null }; } |
| 156 | const split = splitOrigins(normalizeOrigins(slot), amount); |
| 157 | const taken = stackPayload(slot, amount, split.taken); |
| 158 | if (amount >= maxQty) |
| 159 | { next[slotIndex] = null; } |
| 160 | else |
| 161 | { |
| 162 | slot.quantity = maxQty - amount; |
| 163 | slot.origins = split.remain; |
| 164 | delete slot.origin; |
| 165 | } |
| 166 | return { ok: true, slots: next, taken }; |
| 167 | } |
| 168 | |
| 169 | /** Like insertStack, but grows the slot array when capacity runs out (vault). */ |
| 170 | export function insertStackGrowable (slots, stack, definition) |
| 171 | { |
| 172 | if (!stack || stack.quantity <= 0) |
| 173 | { return { ok: true, slots: (slots ?? []).map(cloneStack), remainder: 0 }; } |
| 174 | let next = (slots ?? []).map(cloneStack); |
| 175 | let remaining = stackQuantity(stack.quantity); |
| 176 | let pendingOrigins = normalizeOrigins(stack); |
| 177 | const rules = stackRulesFor(definition, { inVault: true }); |
| 178 | |
| 179 | if (rules.stackable && !rules.uniqueInstance) |
| 180 | { |
| 181 | for (const slot of next) |
| 182 | { |
| 183 | if (!slot || !stacksMatch(slot, stack, definition, { inVault: true })) |
| 184 | { continue; } |
| 185 | const space = rules.maxStackSize - stackQuantity(slot.quantity); |
| 186 | if (space <= 0) |
| 187 | { continue; } |
| 188 | const add = Math.min(space, remaining); |
| 189 | const split = splitOrigins(pendingOrigins, add); |
| 190 | slot.origins = mergeOriginLists(normalizeOrigins(slot), split.taken); |
| 191 | slot.quantity = stackQuantity(slot.quantity) + add; |
| 192 | if (stack.instance != null && slot.instance == null) |
| 193 | { slot.instance = cloneStack(stack.instance); } |
| 194 | delete slot.origin; |
| 195 | pendingOrigins = split.remain; |
| 196 | remaining -= add; |
| 197 | if (remaining === 0) |
| 198 | { return { ok: true, slots: next, remainder: 0 }; } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | while (remaining > 0) |
| 203 | { |
| 204 | const cap = rules.maxStackSize === Number.POSITIVE_INFINITY ? remaining : rules.maxStackSize; |
| 205 | const amount = rules.stackable && !rules.uniqueInstance |
| 206 | ? Math.min(remaining, cap) |
| 207 | : 1; |
| 208 | const split = splitOrigins(pendingOrigins, amount); |
| 209 | next.push(stackPayload(stack, amount, split.taken)); |
| 210 | pendingOrigins = split.remain; |
| 211 | remaining -= amount; |
| 212 | if (!rules.stackable || rules.uniqueInstance) |
| 213 | { break; } |
| 214 | } |
| 215 | return { ok: remaining === 0, slots: next, remainder: remaining }; |
| 216 | } |
| 217 | |
| 218 | /** Take `quantity` of matching goods from slots (any slots). Vault may compact; inventory should pass `{ compact: false }`. */ |
| 219 | export function takeMatchingFromSlots (slots, { itemId, quantity, instance = null, compact = true }) |
| 220 | { |
| 221 | let remaining = Math.max(0, stackQuantity(quantity)); |
| 222 | if (!remaining) |
| 223 | { return { ok: false, slots, taken: null }; } |
| 224 | const next = (slots ?? []).map(cloneStack); |
| 225 | const takenOrigins = []; |
| 226 | for (let i = 0; i < next.length && remaining > 0; i++) |
| 227 | { |
| 228 | const slot = next[i]; |
| 229 | if (!slot || slot.itemId !== itemId) |
| 230 | { continue; } |
| 231 | if (instance != null) |
| 232 | { |
| 233 | if (JSON.stringify(slot.instance ?? null) !== JSON.stringify(instance)) |
| 234 | { continue; } |
| 235 | } |
| 236 | const amount = Math.min(stackQuantity(slot.quantity), remaining); |
| 237 | const split = splitOrigins(normalizeOrigins(slot), amount); |
| 238 | takenOrigins.push(...split.taken); |
| 239 | if (amount >= stackQuantity(slot.quantity)) |
| 240 | { next[i] = null; } |
| 241 | else |
| 242 | { |
| 243 | slot.quantity = stackQuantity(slot.quantity) - amount; |
| 244 | slot.origins = split.remain; |
| 245 | delete slot.origin; |
| 246 | } |
| 247 | remaining -= amount; |
| 248 | } |
| 249 | if (remaining > 0) |
| 250 | { return { ok: false, slots, taken: null }; } |
| 251 | return { |
| 252 | ok: true, |
| 253 | slots: compact ? next.filter((slot) => slot) : next, |
| 254 | taken: attachOrigins({ |
| 255 | itemId, |
| 256 | quantity: stackQuantity(quantity), |
| 257 | instance: instance ?? null, |
| 258 | origins: mergeOriginLists([], takenOrigins) |
| 259 | }) |
| 260 | }; |
| 261 | } |
| 262 | |
| 263 | export function countItemInSlots (slots, itemId) |
| 264 | { |
| 265 | let total = 0; |
| 266 | for (const slot of slots ?? []) |
| 267 | { |
| 268 | if (slot?.itemId === itemId) |
| 269 | { total += stackQuantity(slot.quantity); } |
| 270 | } |
| 271 | return total; |
| 272 | } |
| 273 | |
| 274 | export function countCoinsInSlots (slots) |
| 275 | { |
| 276 | return countItemInSlots(slots, ItemId.coins); |
| 277 | } |
| 278 | |
| 279 | /** Add coin stacks, splitting at max stack size (999B). */ |
| 280 | export function addCoinsToSlots (slots, amount, { growable = false } = {}) |
| 281 | { |
| 282 | const def = itemDefinition(ItemId.coins); |
| 283 | if (!def || amount <= 0) |
| 284 | { return { ok: true, slots: (slots ?? []).map(cloneStack), remainder: 0 }; } |
| 285 | let next = (slots ?? []).map(cloneStack); |
| 286 | if (!growable && next.length < INVENTORY_SLOTS) |
| 287 | { |
| 288 | const padded = emptySlots(); |
| 289 | for (let i = 0; i < next.length; i++) |
| 290 | { padded[i] = next[i]; } |
| 291 | next = padded; |
| 292 | } |
| 293 | let remaining = stackQuantity(amount); |
| 294 | const insert = growable ? insertStackGrowable : insertStack; |
| 295 | while (remaining > 0) |
| 296 | { |
| 297 | const chunk = Math.min(remaining, def.maxStackSize ?? 99); |
| 298 | const result = insert(next, { itemId: ItemId.coins, quantity: chunk }, def); |
| 299 | next = result.slots; |
| 300 | const placed = chunk - (result.remainder ?? 0); |
| 301 | remaining -= placed; |
| 302 | if (!result.ok || result.remainder > 0) |
| 303 | { return { ok: false, slots: next, remainder: remaining }; } |
| 304 | } |
| 305 | return { ok: true, slots: next, remainder: 0 }; |
| 306 | } |
| 307 | |
| 308 | /** Convert legacy currency counters into coin stacks. */ |
| 309 | export function migrateBagCoins (bag) |
| 310 | { |
| 311 | let inventory = bag.inventory ?? emptySlots(); |
| 312 | let vault = bag.vault ?? []; |
| 313 | if (bag.currency > 0) |
| 314 | { |
| 315 | const added = addCoinsToSlots(inventory, stackQuantity(bag.currency), { growable: false }); |
| 316 | inventory = added.slots; |
| 317 | } |
| 318 | if (bag.vaultCurrency > 0) |
| 319 | { |
| 320 | const added = addCoinsToSlots(vault, stackQuantity(bag.vaultCurrency), { growable: true }); |
| 321 | vault = added.slots; |
| 322 | } |
| 323 | return { |
| 324 | ...bag, |
| 325 | inventory, |
| 326 | vault, |
| 327 | currency: 0, |
| 328 | vaultCurrency: 0 |
| 329 | }; |
| 330 | } |
| 331 | |
| 332 | export function migrateInheritanceCoins (inheritance) |
| 333 | { |
| 334 | if (!inheritance?.currency) |
| 335 | { return inheritance; } |
| 336 | const added = addCoinsToSlots(inheritance.slots ?? emptySlots(), stackQuantity(inheritance.currency), { growable: true }); |
| 337 | return { |
| 338 | ...inheritance, |
| 339 | slots: added.slots, |
| 340 | currency: 0 |
| 341 | }; |
| 342 | } |
| 343 | |
| 344 | /** Trim excess blank cheques in inventory (e.g. after legacy saves or bugs). */ |
| 345 | export function normalizeInventorySlots (slots) |
| 346 | { |
| 347 | return capItemTotalInSlots(slots ?? emptySlots(), ItemId.blankCheque, BLANK_CHEQUE_CARRY_CAP); |
| 348 | } |
| 349 | |
| 350 | export function capItemTotalInSlots (slots, itemId, cap) |
| 351 | { |
| 352 | const next = (slots ?? []).map(cloneStack); |
| 353 | let total = countItemInSlots(next, itemId); |
| 354 | if (total <= cap) |
| 355 | { return next; } |
| 356 | let toRemove = total - cap; |
| 357 | for (let i = next.length - 1; i >= 0 && toRemove > 0; i--) |
| 358 | { |
| 359 | const slot = next[i]; |
| 360 | if (!slot || slot.itemId !== itemId) |
| 361 | { continue; } |
| 362 | const beforeQty = stackQuantity(slot.quantity); |
| 363 | const drop = Math.min(beforeQty, toRemove); |
| 364 | const { remain } = splitOrigins(normalizeOrigins({ ...slot, quantity: beforeQty }), drop); |
| 365 | slot.quantity = beforeQty - drop; |
| 366 | slot.origins = remain; |
| 367 | delete slot.origin; |
| 368 | toRemove -= drop; |
| 369 | if (slot.quantity <= 0) |
| 370 | { next[i] = null; } |
| 371 | } |
| 372 | return next; |
| 373 | } |
| 374 | |
| 375 | export function normalizeVault (raw) |
| 376 | { |
| 377 | if (Array.isArray(raw)) |
| 378 | { return { slots: raw.map(cloneStack), reserved: [] }; } |
| 379 | return { |
| 380 | slots: (raw?.slots ?? []).map(cloneStack), |
| 381 | reserved: (raw?.reserved ?? []).map(cloneStack) |
| 382 | }; |
| 383 | } |
| 384 | |
| 385 | export function serializeVault (vault) |
| 386 | { |
| 387 | const normalized = normalizeVault(vault); |
| 388 | return { |
| 389 | slots: publicSlots(normalized.slots), |
| 390 | reserved: publicSlots(normalized.reserved) |
| 391 | }; |
| 392 | } |
| 393 | |
| 394 | export function publicSlots (slots) |
| 395 | { |
| 396 | return (slots ?? emptySlots()).map((slot) => |
| 397 | { |
| 398 | if (!slot) |
| 399 | { return null; } |
| 400 | const payload = attachOrigins(cloneStack(slot)); |
| 401 | const next = { |
| 402 | itemId: payload.itemId, |
| 403 | quantity: payload.quantity, |
| 404 | origins: payload.origins, |
| 405 | instance: payload.instance ?? null |
| 406 | }; |
| 407 | // Cheque reservations keep chequeId so redeem can match the paper to vault goods. |
| 408 | if (payload.chequeId != null) |
| 409 | { next.chequeId = payload.chequeId; } |
| 410 | return next; |
| 411 | }); |
| 412 | } |
| 413 | |
| 414 | export function publicEquipment (equipment) |
| 415 | { |
| 416 | const slots = {}; |
| 417 | for (const slot of EQUIPMENT_SLOTS) |
| 418 | { slots[slot] = equipment?.slots?.[slot] ? cloneStack(equipment.slots[slot]) : null; } |
| 419 | return { slots }; |
| 420 | } |