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