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