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