| 1 | import { equipImageName, spriteImageName } from "./tileSprites.js"; |
| 2 | import { SkillId } from "./skills.js"; |
| 3 | import { MaterialTier, WoodTier } from "./materialTiers.js"; |
| 4 | import { AttackType } from "./attackTypes.js"; |
| 5 | import { EquipmentKind } from "./equipmentKinds.js"; |
| 6 | import { formatQuantity } from "../items/formatQuantity.js"; |
| 7 | import { formatOriginBreakdown } from "../items/origin.js"; |
| 8 | import { CraftingItemId, CRAFTING_ITEM_STUBS } from "./craftingItems.js"; |
| 9 | import { MetalItemId, METAL_ITEM_DEFINITIONS } from "./metalItems.js"; |
| 10 | import { WoodItemId, WOOD_ITEM_DEFINITIONS, isLogDebarked } from "./woodItems.js"; |
| 11 | import { ItemFamilyId } from "./tierFamilies.js"; |
| 12 | import { ArmourItemId, ARMOUR_ITEM_DEFINITIONS } from "./armourItems.js"; |
| 13 | import { ClothItemId, CLOTH_ITEM_DEFINITIONS } from "./clothItems.js"; |
| 14 | import { LeatherItemId, LEATHER_ITEM_DEFINITIONS } from "./leatherItems.js"; |
| 15 | import { LeatherArmourItemId, LEATHER_ARMOUR_ITEM_DEFINITIONS } from "./leatherArmourItems.js"; |
| 16 | import { WizardItemId, WIZARD_ITEM_DEFINITIONS } from "./wizardItems.js"; |
| 17 | import { DyeItemId, FLOWER_ITEM_DEFINITION, DYE_ITEM_DEFINITION, FLOWER_COLOR_DISPLAY } from "./dyes.js"; |
| 18 | import { HERB_ITEM_DEFINITIONS, HerbItemId } from "./herbs.js"; |
| 19 | import { RANGED_ITEM_DEFINITIONS, RangedItemId } from "./rangedItems.js"; |
| 20 | import { POTION_MAX_STACK, alchemyPotionDefinition } from "./alchemy.js"; |
| 21 | import { SpellbookItemId, SPELLBOOK_ITEM_DEFINITIONS } from "./spellItems.js"; |
| 22 | import { FISH_ITEM_DEFINITIONS, FishingToolItemId } from "./fishItems.js"; |
| 23 | import { GemItemId, GEM_ITEM_DEFINITIONS } from "./gemItems.js"; |
| 24 | import { JewelryItemId, JEWELRY_ITEM_DEFINITIONS } from "./jewelryItems.js"; |
| 25 | import { SwordItemId, SWORD_ITEM_DEFINITIONS } from "./swordItems.js"; |
| 26 | import { WarAxeItemId, WAR_AXE_ITEM_DEFINITIONS } from "./warAxeItems.js"; |
| 27 | import { EssenceItemId, ESSENCE_ITEM_DEFINITIONS } from "./essenceItems.js"; |
| 28 | import { RunestoneItemId, QUEST_UNIQUE_ITEM_DEFINITIONS } from "./questUniqueItems.js"; |
| 29 | import { |
| 30 | chequeReservedGoodsStack, |
| 31 | chequeExpiryTooltipLine |
| 32 | } from "../items/cheque.js"; |
| 33 | |
| 34 | export const ItemId = Object.freeze({ |
| 35 | paper: 1, |
| 36 | apple: 2, |
| 37 | stick: 3, |
| 38 | blankCheque: 4, |
| 39 | /** Cotton shirt (ClothItemId.cottonShirt). */ |
| 40 | clothShirt: 5, |
| 41 | letter: 6, |
| 42 | filledCheque: 7, |
| 43 | /** Written cheque past its 30-day life — no longer redeemable. */ |
| 44 | expiredCheque: 590, |
| 45 | coins: 8, |
| 46 | ...CraftingItemId, |
| 47 | lantern: 26, |
| 48 | bones: 27, |
| 49 | skull: 28, |
| 50 | glassBottle: 29, |
| 51 | potion: 32, |
| 52 | recipePaper: 33, |
| 53 | egg: 275, |
| 54 | cookedEgg: 504, |
| 55 | burntEgg: 505, |
| 56 | ...SpellbookItemId, |
| 57 | ...MetalItemId, |
| 58 | ...WoodItemId, |
| 59 | ...FishingToolItemId, |
| 60 | ...ArmourItemId, |
| 61 | ...ClothItemId, |
| 62 | ...LeatherItemId, |
| 63 | ...LeatherArmourItemId, |
| 64 | ...WizardItemId, |
| 65 | ...DyeItemId, |
| 66 | ...HerbItemId, |
| 67 | ...RangedItemId, |
| 68 | ...GemItemId, |
| 69 | ...JewelryItemId, |
| 70 | ...SwordItemId, |
| 71 | ...WarAxeItemId, |
| 72 | ...RunestoneItemId |
| 73 | }); |
| 74 | |
| 75 | export const EQUIPMENT_SLOT = Object.freeze({ |
| 76 | head: "head", |
| 77 | chest: "chest", |
| 78 | legs: "legs", |
| 79 | boots: "boots", |
| 80 | weapon: "weapon", |
| 81 | offhand: "offhand", |
| 82 | ring: "ring", |
| 83 | bracelet: "bracelet", |
| 84 | amulet: "amulet" |
| 85 | }); |
| 86 | |
| 87 | /** |
| 88 | * Back → front. |
| 89 | * Ring / bracelet sit under clothing; amulet draws over clothing / offhand. |
| 90 | */ |
| 91 | export const EQUIPMENT_DRAW_ORDER = Object.freeze([ |
| 92 | EQUIPMENT_SLOT.boots, |
| 93 | EQUIPMENT_SLOT.ring, |
| 94 | EQUIPMENT_SLOT.bracelet, |
| 95 | EQUIPMENT_SLOT.chest, |
| 96 | EQUIPMENT_SLOT.weapon, |
| 97 | EQUIPMENT_SLOT.legs, |
| 98 | EQUIPMENT_SLOT.head, |
| 99 | EQUIPMENT_SLOT.offhand, |
| 100 | EQUIPMENT_SLOT.amulet |
| 101 | ]); |
| 102 | |
| 103 | export const DEFAULT_MAX_STACK = 99; |
| 104 | export const BLANK_CHEQUE_CARRY_CAP = 99; |
| 105 | export const COIN_MAX_STACK = 999_000_000_000; |
| 106 | |
| 107 | /** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */ |
| 108 | export const COIN_SPRITE_TIERS = Object.freeze([ |
| 109 | 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000, |
| 110 | 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000, |
| 111 | 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, |
| 112 | 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000, |
| 113 | 999_999_999_999_999 |
| 114 | ]); |
| 115 | |
| 116 | export const ITEM_DEFINITIONS = Object.freeze({ |
| 117 | [ItemId.paper]: Object.freeze({ |
| 118 | itemId: ItemId.paper, |
| 119 | key: "paper", |
| 120 | displayName: "Paper", |
| 121 | description: "Blank sheet of paper.", |
| 122 | seedValue: 2, |
| 123 | stackable: true, |
| 124 | maxStackSize: DEFAULT_MAX_STACK, |
| 125 | uniqueInstance: false, |
| 126 | category: "document" |
| 127 | }), |
| 128 | [ItemId.letter]: Object.freeze({ |
| 129 | itemId: ItemId.letter, |
| 130 | key: "letter", |
| 131 | displayName: "Letter", |
| 132 | description: "A written letter.", |
| 133 | seedValue: 2, |
| 134 | stackable: false, |
| 135 | maxStackSize: 1, |
| 136 | uniqueInstance: true, |
| 137 | category: "document", |
| 138 | defaultState: "letter-opened", |
| 139 | marketplaceSellable: false, |
| 140 | marketplaceSellBlockedReason: "Letters cannot be sold on the marketplace." |
| 141 | // Letters are not `untradeable`: P2P trade uses authorship rules in shared/items/trade.js |
| 142 | // (same spirit as private ground letters). Use `untradeable: true` for future bound/quest items. |
| 143 | }), |
| 144 | [ItemId.apple]: Object.freeze({ |
| 145 | itemId: ItemId.apple, |
| 146 | key: "apple", |
| 147 | displayName: "Apple", |
| 148 | description: "Round and juicy!", |
| 149 | seedValue: 1, |
| 150 | stackable: true, |
| 151 | maxStackSize: DEFAULT_MAX_STACK, |
| 152 | uniqueInstance: false, |
| 153 | category: "food", |
| 154 | healing: 4 |
| 155 | }), |
| 156 | [ItemId.egg]: Object.freeze({ |
| 157 | itemId: ItemId.egg, |
| 158 | key: "egg", |
| 159 | displayName: "Egg", |
| 160 | description: "A fresh egg, still warm from the nest. Bake it in an oven before eating.", |
| 161 | seedValue: 1, |
| 162 | stackable: true, |
| 163 | maxStackSize: DEFAULT_MAX_STACK, |
| 164 | uniqueInstance: false, |
| 165 | category: "ingredient", |
| 166 | iconKey: "egg", |
| 167 | defaultState: "egg" |
| 168 | }), |
| 169 | [ItemId.cookedEgg]: Object.freeze({ |
| 170 | itemId: ItemId.cookedEgg, |
| 171 | key: "cooked-egg", |
| 172 | displayName: "Cooked egg", |
| 173 | description: "An egg baked until firm, ready to eat.", |
| 174 | seedValue: 2, |
| 175 | stackable: true, |
| 176 | maxStackSize: DEFAULT_MAX_STACK, |
| 177 | uniqueInstance: false, |
| 178 | category: "food", |
| 179 | iconKey: "egg", |
| 180 | defaultState: "egg", |
| 181 | healing: 3 |
| 182 | }), |
| 183 | [ItemId.burntEgg]: Object.freeze({ |
| 184 | itemId: ItemId.burntEgg, |
| 185 | key: "burnt-egg", |
| 186 | displayName: "Burnt egg", |
| 187 | description: "An egg left too long in the oven — dry and inedible.", |
| 188 | seedValue: 1, |
| 189 | stackable: true, |
| 190 | maxStackSize: DEFAULT_MAX_STACK, |
| 191 | uniqueInstance: false, |
| 192 | category: "food", |
| 193 | iconKey: "egg", |
| 194 | defaultState: "egg" |
| 195 | }), |
| 196 | [ItemId.stick]: Object.freeze({ |
| 197 | itemId: ItemId.stick, |
| 198 | key: "stick", |
| 199 | displayName: "Stick", |
| 200 | description: "A wooden stick. Simple and sturdy.", |
| 201 | seedValue: 3, |
| 202 | stackable: false, |
| 203 | maxStackSize: 1, |
| 204 | uniqueInstance: false, |
| 205 | category: "finished", |
| 206 | equipment: Object.freeze({ |
| 207 | slot: EQUIPMENT_SLOT.weapon, |
| 208 | tier: WoodTier.pine, |
| 209 | kind: EquipmentKind.clubWeapon, |
| 210 | attack: Object.freeze({ |
| 211 | mode: "melee", |
| 212 | rangeTiles: 1, |
| 213 | hitSkillId: SkillId.clubfighting, |
| 214 | damageSkillId: SkillId.strength, |
| 215 | attackType: AttackType.blunt, |
| 216 | cooldownMs: 700 |
| 217 | }) |
| 218 | }) |
| 219 | }), |
| 220 | [ItemId.blankCheque]: Object.freeze({ |
| 221 | itemId: ItemId.blankCheque, |
| 222 | key: "blank-cheque", |
| 223 | displayName: "Blank cheque", |
| 224 | description: "Bank stationery for writing cheques against deposited goods.", |
| 225 | seedValue: 5, |
| 226 | stackable: true, |
| 227 | maxStackSize: DEFAULT_MAX_STACK, |
| 228 | uniqueInstance: false, |
| 229 | category: "document", |
| 230 | marketplaceSellable: false, |
| 231 | marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace." |
| 232 | }), |
| 233 | [ItemId.filledCheque]: Object.freeze({ |
| 234 | itemId: ItemId.filledCheque, |
| 235 | key: "filled-cheque", |
| 236 | displayName: "Cheque", |
| 237 | description: "A bearer cheque linked to reserved goods in the issuer's bank.", |
| 238 | seedValue: 5, |
| 239 | stackable: false, |
| 240 | maxStackSize: 1, |
| 241 | uniqueInstance: true, |
| 242 | category: "document", |
| 243 | defaultState: "filled-cheque", |
| 244 | marketplaceSellable: true |
| 245 | }), |
| 246 | [ItemId.expiredCheque]: Object.freeze({ |
| 247 | itemId: ItemId.expiredCheque, |
| 248 | key: "expired-cheque", |
| 249 | displayName: "Expired cheque", |
| 250 | description: "A cheque whose claim has lapsed. The reserved goods were returned to the issuer.", |
| 251 | seedValue: 1, |
| 252 | stackable: true, |
| 253 | maxStackSize: DEFAULT_MAX_STACK, |
| 254 | uniqueInstance: false, |
| 255 | category: "document", |
| 256 | /** Reuse blank stationery art — visually distinct from a live written cheque. */ |
| 257 | iconKey: "blank-cheque", |
| 258 | defaultState: "blank-cheque", |
| 259 | marketplaceSellable: false, |
| 260 | marketplaceSellBlockedReason: "Expired cheques cannot be sold on the marketplace." |
| 261 | }), |
| 262 | [ItemId.coins]: Object.freeze({ |
| 263 | itemId: ItemId.coins, |
| 264 | key: "coins", |
| 265 | displayName: "Coin", |
| 266 | displayNamePlural: "Coins", |
| 267 | description: "Hard currency of the realm.", |
| 268 | seedValue: 1, |
| 269 | stackable: true, |
| 270 | maxStackSize: COIN_MAX_STACK, |
| 271 | uniqueInstance: false, |
| 272 | category: "currency" |
| 273 | }), |
| 274 | [ItemId.lantern]: Object.freeze({ |
| 275 | itemId: ItemId.lantern, |
| 276 | key: "lantern", |
| 277 | displayName: "Lantern", |
| 278 | description: "A handheld lamp that pushes back the night.", |
| 279 | seedValue: 12, |
| 280 | stackable: false, |
| 281 | maxStackSize: 1, |
| 282 | uniqueInstance: false, |
| 283 | category: "tool", |
| 284 | equipment: Object.freeze({ |
| 285 | slot: EQUIPMENT_SLOT.offhand, |
| 286 | lightRadiusTiles: 6 |
| 287 | }) |
| 288 | }), |
| 289 | [ItemId.bones]: Object.freeze({ |
| 290 | itemId: ItemId.bones, |
| 291 | key: "bones", |
| 292 | displayName: "Bones", |
| 293 | description: "Bones of a dead creature.", |
| 294 | seedValue: 1, |
| 295 | stackable: true, |
| 296 | maxStackSize: DEFAULT_MAX_STACK, |
| 297 | uniqueInstance: false, |
| 298 | category: "ingredient" |
| 299 | }), |
| 300 | [ItemId.skull]: Object.freeze({ |
| 301 | itemId: ItemId.skull, |
| 302 | key: "skull", |
| 303 | displayName: "Skull", |
| 304 | description: "Unlucky.", |
| 305 | seedValue: 1, |
| 306 | stackable: true, |
| 307 | maxStackSize: DEFAULT_MAX_STACK, |
| 308 | uniqueInstance: false, |
| 309 | category: "misc" |
| 310 | }), |
| 311 | [ItemId.glassBottle]: Object.freeze({ |
| 312 | itemId: ItemId.glassBottle, |
| 313 | key: "glass-bottle", |
| 314 | displayName: "Glass bottle", |
| 315 | description: "A clear bottle, ready to hold a brew or dye.", |
| 316 | seedValue: 2, |
| 317 | stackable: true, |
| 318 | maxStackSize: DEFAULT_MAX_STACK, |
| 319 | uniqueInstance: false, |
| 320 | category: "tool", |
| 321 | iconKey: "potion", |
| 322 | defaultState: "potion-0", |
| 323 | npcShopAllowed: true |
| 324 | }), |
| 325 | [ItemId.potion]: Object.freeze({ |
| 326 | itemId: ItemId.potion, |
| 327 | key: "potion", |
| 328 | displayName: "Potion", |
| 329 | description: "A bottled brew. Sip with care.", |
| 330 | seedValue: 8, |
| 331 | stackable: true, |
| 332 | maxStackSize: POTION_MAX_STACK, |
| 333 | uniqueInstance: false, |
| 334 | category: "potion", |
| 335 | potion: true, |
| 336 | iconKey: "potion" |
| 337 | }), |
| 338 | [ItemId.recipePaper]: Object.freeze({ |
| 339 | itemId: ItemId.recipePaper, |
| 340 | key: "recipe-paper", |
| 341 | displayName: "Recipe paper", |
| 342 | description: "An alchemy recipe written for a cauldron.", |
| 343 | seedValue: 2, |
| 344 | stackable: false, |
| 345 | maxStackSize: 1, |
| 346 | uniqueInstance: true, |
| 347 | category: "document", |
| 348 | defaultState: "recipe-paper", |
| 349 | iconKey: "paper" |
| 350 | }), |
| 351 | ...CRAFTING_ITEM_STUBS, |
| 352 | ...LEATHER_ITEM_DEFINITIONS, |
| 353 | ...LEATHER_ARMOUR_ITEM_DEFINITIONS, |
| 354 | ...WIZARD_ITEM_DEFINITIONS, |
| 355 | ...METAL_ITEM_DEFINITIONS, |
| 356 | ...WOOD_ITEM_DEFINITIONS, |
| 357 | ...ARMOUR_ITEM_DEFINITIONS, |
| 358 | ...CLOTH_ITEM_DEFINITIONS, |
| 359 | ...HERB_ITEM_DEFINITIONS, |
| 360 | ...RANGED_ITEM_DEFINITIONS, |
| 361 | ...SPELLBOOK_ITEM_DEFINITIONS, |
| 362 | ...FISH_ITEM_DEFINITIONS, |
| 363 | ...GEM_ITEM_DEFINITIONS, |
| 364 | ...JEWELRY_ITEM_DEFINITIONS, |
| 365 | ...SWORD_ITEM_DEFINITIONS, |
| 366 | ...WAR_AXE_ITEM_DEFINITIONS, |
| 367 | ...ESSENCE_ITEM_DEFINITIONS, |
| 368 | ...QUEST_UNIQUE_ITEM_DEFINITIONS, |
| 369 | [DyeItemId.flower]: FLOWER_ITEM_DEFINITION, |
| 370 | [DyeItemId.dye]: DYE_ITEM_DEFINITION |
| 371 | }); |
| 372 | |
| 373 | export function itemDefinition (itemId) |
| 374 | { |
| 375 | return ITEM_DEFINITIONS[itemId] ?? null; |
| 376 | } |
| 377 | |
| 378 | /** Every defined item for editor pickers (shop stock, dev tools, …). */ |
| 379 | export function listAllItemPickerEntries () |
| 380 | { |
| 381 | return Object.values(ITEM_DEFINITIONS) |
| 382 | .filter((def) => def?.itemId != null) |
| 383 | .map((def) => ({ |
| 384 | itemId: def.itemId, |
| 385 | displayName: def.displayName ?? String(def.itemId), |
| 386 | key: def.key ?? String(def.itemId) |
| 387 | })) |
| 388 | .sort((a, b) => a.displayName.localeCompare(b.displayName) || (a.itemId - b.itemId)); |
| 389 | } |
| 390 | |
| 391 | /** Garments that accept optional `instance.dyeColor` (tailor / cloth clothing). */ |
| 392 | export function isDyeableClothing (itemIdOrDef) |
| 393 | { |
| 394 | const def = typeof itemIdOrDef === "number" |
| 395 | ? itemDefinition(itemIdOrDef) |
| 396 | : itemIdOrDef; |
| 397 | return !!def?.dyeable; |
| 398 | } |
| 399 | |
| 400 | export { formatQuantity }; |
| 401 | |
| 402 | export function itemDisplayName (stack, def = itemDefinition(stack?.itemId)) |
| 403 | { |
| 404 | if (!def) |
| 405 | { return "Item"; } |
| 406 | const qty = stack?.quantity ?? 1; |
| 407 | if (stack?.itemId === ItemId.coins) |
| 408 | { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); } |
| 409 | if (stack?.itemId === ItemId.potion && stack.instance?.displayName) |
| 410 | { return stack.instance.displayName; } |
| 411 | if (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye") |
| 412 | { |
| 413 | if (stack.instance.colorId && FLOWER_COLOR_DISPLAY[stack.instance.colorId]) |
| 414 | { return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId]} dye`; } |
| 415 | return "Dye"; |
| 416 | } |
| 417 | if (stack?.itemId === ItemId.flower && stack.instance?.colorId) |
| 418 | { |
| 419 | return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId] ?? "Flower"} flower`; |
| 420 | } |
| 421 | if (stack?.itemId === ItemId.recipePaper && stack.instance?.displayName) |
| 422 | { |
| 423 | return String(stack.instance.displayName).replace(/\s+Recipe$/u, " recipe"); |
| 424 | } |
| 425 | if (stack?.itemId === CraftingItemId.tinderbox && stack.instance?.kind === "tinderbox") |
| 426 | { |
| 427 | const charges = Math.max(0, stack.instance.charges | 0); |
| 428 | return `${def.displayName} (${charges})`; |
| 429 | } |
| 430 | if (def.familyId === ItemFamilyId.log && isLogDebarked(stack)) |
| 431 | { return `Debarked ${def.displayName}`; } |
| 432 | if (stack?.instance?.kind === "tipped-arrow" && stack.instance.element) |
| 433 | { |
| 434 | const el = String(stack.instance.element); |
| 435 | const label = el.charAt(0).toUpperCase() + el.slice(1); |
| 436 | return `${label} ${def.displayName ?? "arrow"}`; |
| 437 | } |
| 438 | return def.displayName ?? "Item"; |
| 439 | } |
| 440 | |
| 441 | export function stackDisplayLabel (stack) |
| 442 | { |
| 443 | const name = itemDisplayName(stack); |
| 444 | const qty = stack?.quantity ?? 1; |
| 445 | if (qty <= 1) |
| 446 | { return name; } |
| 447 | return `${name} (${formatQuantity(qty)})`; |
| 448 | } |
| 449 | |
| 450 | export function coinSpriteTierForQuantity (quantity) |
| 451 | { |
| 452 | const qty = Math.max(0, Math.floor(Number(quantity) || 0)); |
| 453 | let tier = COIN_SPRITE_TIERS[0]; |
| 454 | for (const candidate of COIN_SPRITE_TIERS) |
| 455 | { |
| 456 | if (qty >= candidate) |
| 457 | { tier = candidate; } |
| 458 | else |
| 459 | { break; } |
| 460 | } |
| 461 | return tier; |
| 462 | } |
| 463 | |
| 464 | /** Stack-aware description (letters include open state and author). */ |
| 465 | export function itemDescription (stack, { localCharacterId } = {}) |
| 466 | { |
| 467 | const def = itemDefinition(stack?.itemId); |
| 468 | if (!def) |
| 469 | { return ""; } |
| 470 | if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter") |
| 471 | { |
| 472 | const state = stack.instance.opened === false ? "unopened" : "opened"; |
| 473 | const authorId = stack.instance.authorCharacterId; |
| 474 | const byYou = authorId != null && Number(authorId) === Number(localCharacterId); |
| 475 | const author = byYou |
| 476 | ? "you" |
| 477 | : (stack.instance.authorDisplayName?.trim() || "someone"); |
| 478 | return `An ${state} letter, written by ${author}.`; |
| 479 | } |
| 480 | if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque") |
| 481 | { |
| 482 | const goods = chequeReservedGoodsStack(stack); |
| 483 | const qty = stack.instance.quantity | 0; |
| 484 | const name = goods |
| 485 | ? itemDisplayName(goods) |
| 486 | : (itemDefinition(stack.instance.itemId)?.displayName ?? "goods"); |
| 487 | return `Bearer cheque for ${qty}× ${name}.`; |
| 488 | } |
| 489 | if (stack.itemId === ItemId.expiredCheque) |
| 490 | { return def.description ?? ""; } |
| 491 | if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion") |
| 492 | { |
| 493 | if (stack.instance.unstable) |
| 494 | { return "The brew smells wrong — best sipped with caution."; } |
| 495 | const potion = alchemyPotionDefinition(stack.instance.potionKey); |
| 496 | return potion?.description |
| 497 | ?? def.description |
| 498 | ?? "A carefully bottled draught."; |
| 499 | } |
| 500 | if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper") |
| 501 | { |
| 502 | const authorId = stack.instance.authorCharacterId; |
| 503 | const byYou = authorId != null && Number(authorId) === Number(localCharacterId); |
| 504 | const author = byYou |
| 505 | ? "you" |
| 506 | : (stack.instance.authorDisplayName?.trim() || "someone"); |
| 507 | return `Written by ${author}.`; |
| 508 | } |
| 509 | return def.description ?? ""; |
| 510 | } |
| 511 | |
| 512 | export function itemOriginSummary (stack) |
| 513 | { |
| 514 | // Recipe papers already say "Written by …" in the description — avoid a duplicate origin line. |
| 515 | if (stack?.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper") |
| 516 | { return ""; } |
| 517 | return formatOriginBreakdown(stack); |
| 518 | } |
| 519 | |
| 520 | /** Extra tooltip lines after the description (e.g. cheque expiry). */ |
| 521 | export function itemExtraTooltipLines (stack, { now = Date.now() } = {}) |
| 522 | { |
| 523 | const line = chequeExpiryTooltipLine(stack, now); |
| 524 | return line ? [line] : []; |
| 525 | } |
| 526 | |
| 527 | export function itemDefaultState (def, instance = null) |
| 528 | { |
| 529 | if (def?.familyId === ItemFamilyId.log && isLogDebarked({ instance })) |
| 530 | { return "log-debarked"; } |
| 531 | return def?.defaultState ?? def?.key ?? "paper"; |
| 532 | } |
| 533 | |
| 534 | export function itemSpriteName (itemId, instance = null, quantity = null) |
| 535 | { |
| 536 | const def = itemDefinition(itemId); |
| 537 | if (!def) |
| 538 | { return spriteImageName("item", "paper", "paper"); } |
| 539 | if (itemId === ItemId.coins) |
| 540 | { |
| 541 | const tier = coinSpriteTierForQuantity(quantity ?? 1); |
| 542 | return spriteImageName("item", "coins", `coins-${tier}`); |
| 543 | } |
| 544 | if (itemId === ItemId.letter) |
| 545 | { return spriteImageName("item", "letter", letterVisualState(instance)); } |
| 546 | if (instance?.kind === "tipped-arrow") |
| 547 | { return spriteImageName("item", "arrow", "tipped-arrow"); } |
| 548 | const folderKey = def.iconKey ?? def.key; |
| 549 | return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def, instance)); |
| 550 | } |
| 551 | |
| 552 | /** Stack-aware sprite key (coins use quantity tiers like inventory icons). */ |
| 553 | export function itemSpriteNameForStack (stack) |
| 554 | { |
| 555 | if (!stack?.itemId) |
| 556 | { return itemSpriteName(null); } |
| 557 | return itemSpriteName(stack.itemId, stack.instance ?? null, stack.quantity); |
| 558 | } |
| 559 | |
| 560 | export function itemEquipSprite (itemId) |
| 561 | { |
| 562 | const def = itemDefinition(itemId); |
| 563 | if (!def) |
| 564 | { return null; } |
| 565 | const folderKey = def.iconKey ?? def.key; |
| 566 | const equipment = def.equipment; |
| 567 | if (equipment) |
| 568 | { |
| 569 | if (equipment.hideEquipSprite) |
| 570 | { return null; } |
| 571 | return equipment.equipSprite |
| 572 | ?? equipment.overlaySpriteId |
| 573 | ?? equipImageName("item", folderKey, itemDefaultState(def)); |
| 574 | } |
| 575 | /** Inventory tools (e.g. tinderbox) may still show equip art during gather/firestarting. */ |
| 576 | if (def.gatherEquipSprite) |
| 577 | { return equipImageName("item", folderKey, itemDefaultState(def)); } |
| 578 | return null; |
| 579 | } |
| 580 | |
| 581 | /** Largest equipped light radius (e.g. lantern in offhand). Includes opal bracelet night vision. */ |
| 582 | export function equippedLightRadiusTiles (equipment) |
| 583 | { |
| 584 | let best = 0; |
| 585 | for (const stack of Object.values(equipment?.slots ?? {})) |
| 586 | { |
| 587 | if (!stack) |
| 588 | { continue; } |
| 589 | const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0; |
| 590 | if (radius > best) |
| 591 | { best = radius; } |
| 592 | // Opal bracelet: same range as a torch (see OPAL_NIGHT_VISION_RANGE_TILES). |
| 593 | if ((stack.itemId | 0) === JewelryItemId.opalBracelet) |
| 594 | { best = Math.max(best, 8); } |
| 595 | } |
| 596 | return best; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Intensity for the entity light from equipment. |
| 601 | * Opal bracelet is half-bright when it is the sole (or stronger) light source. |
| 602 | */ |
| 603 | export function equippedLightIntensity (equipment) |
| 604 | { |
| 605 | let itemLight = 0; |
| 606 | let hasOpal = false; |
| 607 | for (const stack of Object.values(equipment?.slots ?? {})) |
| 608 | { |
| 609 | if (!stack) |
| 610 | { continue; } |
| 611 | const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0; |
| 612 | if (radius > itemLight) |
| 613 | { itemLight = radius; } |
| 614 | if ((stack.itemId | 0) === JewelryItemId.opalBracelet) |
| 615 | { hasOpal = true; } |
| 616 | } |
| 617 | if (hasOpal && itemLight < 8) |
| 618 | { return 0.5; } |
| 619 | return 1; |
| 620 | } |
| 621 | |
| 622 | export function letterVisualState (instance) |
| 623 | { |
| 624 | return instance?.opened === false ? "letter-closed" : "letter-opened"; |
| 625 | } |
| 626 | |
| 627 | export function itemAssetPath (itemId, companion = null, instance = null) |
| 628 | { |
| 629 | const def = itemDefinition(itemId); |
| 630 | if (def?.herb) |
| 631 | { |
| 632 | const suffix = companion ? `-${companion}` : ""; |
| 633 | return `/assets/herbs/${def.key}${suffix}.png`; |
| 634 | } |
| 635 | const key = def?.iconKey ?? def?.key ?? "paper"; |
| 636 | const state = itemId === ItemId.letter |
| 637 | ? letterVisualState(instance) |
| 638 | : itemDefaultState(def ?? { key }, instance); |
| 639 | const suffix = companion ? `-${companion}` : ""; |
| 640 | return `/assets/items/${key}/${state}${suffix}.png`; |
| 641 | } |
| 642 | |
| 643 | export function itemAssetPathForStack (stack) |
| 644 | { |
| 645 | if (!stack) |
| 646 | { return itemAssetPath(null); } |
| 647 | if (stack.itemId === ItemId.coins) |
| 648 | { |
| 649 | const tier = coinSpriteTierForQuantity(stack.quantity); |
| 650 | return `/assets/items/coins/coins-${tier}.png`; |
| 651 | } |
| 652 | if (stack.itemId === ItemId.potion) |
| 653 | { |
| 654 | const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4)); |
| 655 | return `/assets/items/potion/potion-${fill}.png`; |
| 656 | } |
| 657 | if (stack.itemId === ItemId.dye) |
| 658 | { |
| 659 | const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4)); |
| 660 | return `/assets/items/potion/potion-${fill}.png`; |
| 661 | } |
| 662 | if (stack.itemId === ItemId.glassBottle) |
| 663 | { return `/assets/items/potion/potion-0.png`; } |
| 664 | if (stack.itemId === ItemId.flower) |
| 665 | { return `/assets/items/flower/flower.png`; } |
| 666 | if (stack.instance?.kind === "tipped-arrow") |
| 667 | { return `/assets/items/arrow/tipped-arrow.png`; } |
| 668 | return itemAssetPath(stack.itemId, null, stack.instance); |
| 669 | } |
| 670 | |
| 671 | export const LETTER_MAX_BODY_CHARS = 2000; |
| 672 | |
| 673 | export const STARTER_ITEMS = Object.freeze([ |
| 674 | Object.freeze({ itemId: ItemId.paper, quantity: 5 }), |
| 675 | Object.freeze({ itemId: ItemId.apple, quantity: 3 }), |
| 676 | Object.freeze({ itemId: ItemId.stick, quantity: 1 }), |
| 677 | Object.freeze({ itemId: ItemId.coins, quantity: 25 }) |
| 678 | ]); |