| 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 { DyeItemId, FLOWER_ITEM_DEFINITION, DYE_ITEM_DEFINITION, FLOWER_COLOR_DISPLAY } from "./dyes.js"; |
| 15 | import { HERB_ITEM_DEFINITIONS, HerbItemId } from "./herbs.js"; |
| 16 | import { RANGED_ITEM_DEFINITIONS, RangedItemId } from "./rangedItems.js"; |
| 17 | import { POTION_MAX_STACK } from "./alchemy.js"; |
| 18 | import { SpellbookItemId, SPELLBOOK_ITEM_DEFINITIONS } from "./spellItems.js"; |
| 19 | |
| 20 | export const ItemId = Object.freeze({ |
| 21 | paper: 1, |
| 22 | apple: 2, |
| 23 | stick: 3, |
| 24 | blankCheque: 4, |
| 25 | /** Cotton shirt (ClothItemId.cottonShirt). */ |
| 26 | clothShirt: 5, |
| 27 | letter: 6, |
| 28 | filledCheque: 7, |
| 29 | coins: 8, |
| 30 | ...CraftingItemId, |
| 31 | lantern: 26, |
| 32 | bones: 27, |
| 33 | skull: 28, |
| 34 | glassBottle: 29, |
| 35 | potion: 32, |
| 36 | recipePaper: 33, |
| 37 | ...SpellbookItemId, |
| 38 | ...MetalItemId, |
| 39 | ...WoodItemId, |
| 40 | ...ArmourItemId, |
| 41 | ...ClothItemId, |
| 42 | ...LeatherItemId, |
| 43 | ...DyeItemId, |
| 44 | ...HerbItemId, |
| 45 | ...RangedItemId |
| 46 | }); |
| 47 | |
| 48 | export const EQUIPMENT_SLOT = Object.freeze({ |
| 49 | head: "head", |
| 50 | chest: "chest", |
| 51 | legs: "legs", |
| 52 | boots: "boots", |
| 53 | weapon: "weapon", |
| 54 | offhand: "offhand" |
| 55 | }); |
| 56 | |
| 57 | export const EQUIPMENT_DRAW_ORDER = Object.freeze([ |
| 58 | EQUIPMENT_SLOT.boots, |
| 59 | EQUIPMENT_SLOT.legs, |
| 60 | EQUIPMENT_SLOT.chest, |
| 61 | EQUIPMENT_SLOT.head, |
| 62 | EQUIPMENT_SLOT.offhand, |
| 63 | EQUIPMENT_SLOT.weapon |
| 64 | ]); |
| 65 | |
| 66 | export const DEFAULT_MAX_STACK = 99; |
| 67 | export const BLANK_CHEQUE_CARRY_CAP = 99; |
| 68 | export const COIN_MAX_STACK = 999_000_000_000; |
| 69 | |
| 70 | /** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */ |
| 71 | export const COIN_SPRITE_TIERS = Object.freeze([ |
| 72 | 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000, |
| 73 | 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000, |
| 74 | 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, |
| 75 | 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000, |
| 76 | 999_999_999_999_999 |
| 77 | ]); |
| 78 | |
| 79 | export const ITEM_DEFINITIONS = Object.freeze({ |
| 80 | [ItemId.paper]: Object.freeze({ |
| 81 | itemId: ItemId.paper, |
| 82 | key: "paper", |
| 83 | displayName: "Paper", |
| 84 | description: "Blank sheet of paper.", |
| 85 | seedValue: 2, |
| 86 | stackable: true, |
| 87 | maxStackSize: DEFAULT_MAX_STACK, |
| 88 | uniqueInstance: false, |
| 89 | category: "document" |
| 90 | }), |
| 91 | [ItemId.letter]: Object.freeze({ |
| 92 | itemId: ItemId.letter, |
| 93 | key: "letter", |
| 94 | displayName: "Letter", |
| 95 | description: "A written letter.", |
| 96 | seedValue: 2, |
| 97 | stackable: false, |
| 98 | maxStackSize: 1, |
| 99 | uniqueInstance: true, |
| 100 | category: "document", |
| 101 | defaultState: "letter-opened", |
| 102 | marketplaceSellable: false, |
| 103 | marketplaceSellBlockedReason: "Letters cannot be sold on the marketplace." |
| 104 | }), |
| 105 | [ItemId.apple]: Object.freeze({ |
| 106 | itemId: ItemId.apple, |
| 107 | key: "apple", |
| 108 | displayName: "Apple", |
| 109 | description: "Round and juicy!", |
| 110 | seedValue: 1, |
| 111 | stackable: true, |
| 112 | maxStackSize: DEFAULT_MAX_STACK, |
| 113 | uniqueInstance: false, |
| 114 | category: "food", |
| 115 | healing: 4 |
| 116 | }), |
| 117 | [ItemId.stick]: Object.freeze({ |
| 118 | itemId: ItemId.stick, |
| 119 | key: "stick", |
| 120 | displayName: "Stick", |
| 121 | description: "A wooden stick. Simple and sturdy.", |
| 122 | seedValue: 3, |
| 123 | stackable: false, |
| 124 | maxStackSize: 1, |
| 125 | uniqueInstance: false, |
| 126 | category: "finished", |
| 127 | equipment: Object.freeze({ |
| 128 | slot: EQUIPMENT_SLOT.weapon, |
| 129 | tier: WoodTier.pine, |
| 130 | kind: EquipmentKind.clubWeapon, |
| 131 | attack: Object.freeze({ |
| 132 | mode: "melee", |
| 133 | rangeTiles: 1, |
| 134 | hitSkillId: SkillId.clubfighting, |
| 135 | damageSkillId: SkillId.strength, |
| 136 | attackType: AttackType.blunt, |
| 137 | cooldownMs: 700 |
| 138 | }) |
| 139 | }) |
| 140 | }), |
| 141 | [ItemId.blankCheque]: Object.freeze({ |
| 142 | itemId: ItemId.blankCheque, |
| 143 | key: "blank-cheque", |
| 144 | displayName: "Blank cheque", |
| 145 | description: "Bank stationery for writing cheques against deposited goods.", |
| 146 | seedValue: 5, |
| 147 | stackable: true, |
| 148 | maxStackSize: DEFAULT_MAX_STACK, |
| 149 | uniqueInstance: false, |
| 150 | category: "document", |
| 151 | marketplaceSellable: false, |
| 152 | marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace." |
| 153 | }), |
| 154 | [ItemId.filledCheque]: Object.freeze({ |
| 155 | itemId: ItemId.filledCheque, |
| 156 | key: "filled-cheque", |
| 157 | displayName: "Cheque", |
| 158 | description: "A bearer cheque linked to reserved goods in the issuer's bank.", |
| 159 | seedValue: 5, |
| 160 | stackable: false, |
| 161 | maxStackSize: 1, |
| 162 | uniqueInstance: true, |
| 163 | category: "document", |
| 164 | defaultState: "filled-cheque", |
| 165 | marketplaceSellable: true |
| 166 | }), |
| 167 | [ItemId.coins]: Object.freeze({ |
| 168 | itemId: ItemId.coins, |
| 169 | key: "coins", |
| 170 | displayName: "Coin", |
| 171 | displayNamePlural: "Coins", |
| 172 | description: "Hard currency of the realm.", |
| 173 | seedValue: 1, |
| 174 | stackable: true, |
| 175 | maxStackSize: COIN_MAX_STACK, |
| 176 | uniqueInstance: false, |
| 177 | category: "currency" |
| 178 | }), |
| 179 | [ItemId.lantern]: Object.freeze({ |
| 180 | itemId: ItemId.lantern, |
| 181 | key: "lantern", |
| 182 | displayName: "Lantern", |
| 183 | description: "A handheld lamp that pushes back the night.", |
| 184 | seedValue: 12, |
| 185 | stackable: false, |
| 186 | maxStackSize: 1, |
| 187 | uniqueInstance: false, |
| 188 | category: "tool", |
| 189 | equipment: Object.freeze({ |
| 190 | slot: EQUIPMENT_SLOT.offhand, |
| 191 | lightRadiusTiles: 6 |
| 192 | }) |
| 193 | }), |
| 194 | [ItemId.bones]: Object.freeze({ |
| 195 | itemId: ItemId.bones, |
| 196 | key: "bones", |
| 197 | displayName: "Bones", |
| 198 | description: "Bones of a dead creature.", |
| 199 | seedValue: 1, |
| 200 | stackable: true, |
| 201 | maxStackSize: DEFAULT_MAX_STACK, |
| 202 | uniqueInstance: false, |
| 203 | category: "ingredient" |
| 204 | }), |
| 205 | [ItemId.skull]: Object.freeze({ |
| 206 | itemId: ItemId.skull, |
| 207 | key: "skull", |
| 208 | displayName: "Skull", |
| 209 | description: "Unlucky.", |
| 210 | seedValue: 1, |
| 211 | stackable: true, |
| 212 | maxStackSize: DEFAULT_MAX_STACK, |
| 213 | uniqueInstance: false, |
| 214 | category: "misc" |
| 215 | }), |
| 216 | [ItemId.glassBottle]: Object.freeze({ |
| 217 | itemId: ItemId.glassBottle, |
| 218 | key: "glass-bottle", |
| 219 | displayName: "Glass bottle", |
| 220 | description: "A clear bottle, ready to hold a brew or dye.", |
| 221 | seedValue: 2, |
| 222 | stackable: true, |
| 223 | maxStackSize: DEFAULT_MAX_STACK, |
| 224 | uniqueInstance: false, |
| 225 | category: "tool", |
| 226 | iconKey: "potion", |
| 227 | defaultState: "potion-0", |
| 228 | npcShopAllowed: true |
| 229 | }), |
| 230 | [ItemId.potion]: Object.freeze({ |
| 231 | itemId: ItemId.potion, |
| 232 | key: "potion", |
| 233 | displayName: "Potion", |
| 234 | description: "A bottled brew. Sip with care.", |
| 235 | seedValue: 8, |
| 236 | stackable: true, |
| 237 | maxStackSize: POTION_MAX_STACK, |
| 238 | uniqueInstance: false, |
| 239 | category: "potion", |
| 240 | potion: true, |
| 241 | iconKey: "potion" |
| 242 | }), |
| 243 | [ItemId.recipePaper]: Object.freeze({ |
| 244 | itemId: ItemId.recipePaper, |
| 245 | key: "recipe-paper", |
| 246 | displayName: "Recipe paper", |
| 247 | description: "An alchemy recipe written for a cauldron.", |
| 248 | seedValue: 2, |
| 249 | stackable: false, |
| 250 | maxStackSize: 1, |
| 251 | uniqueInstance: true, |
| 252 | category: "document", |
| 253 | defaultState: "recipe-paper", |
| 254 | iconKey: "paper" |
| 255 | }), |
| 256 | ...CRAFTING_ITEM_STUBS, |
| 257 | ...LEATHER_ITEM_DEFINITIONS, |
| 258 | ...METAL_ITEM_DEFINITIONS, |
| 259 | ...WOOD_ITEM_DEFINITIONS, |
| 260 | ...ARMOUR_ITEM_DEFINITIONS, |
| 261 | ...CLOTH_ITEM_DEFINITIONS, |
| 262 | ...HERB_ITEM_DEFINITIONS, |
| 263 | ...RANGED_ITEM_DEFINITIONS, |
| 264 | ...SPELLBOOK_ITEM_DEFINITIONS, |
| 265 | [DyeItemId.flower]: FLOWER_ITEM_DEFINITION, |
| 266 | [DyeItemId.dye]: DYE_ITEM_DEFINITION |
| 267 | }); |
| 268 | |
| 269 | export function itemDefinition (itemId) |
| 270 | { |
| 271 | return ITEM_DEFINITIONS[itemId] ?? null; |
| 272 | } |
| 273 | |
| 274 | /** Garments that accept optional `instance.dyeColor` (tailor / cloth clothing). */ |
| 275 | export function isDyeableClothing (itemIdOrDef) |
| 276 | { |
| 277 | const def = typeof itemIdOrDef === "number" |
| 278 | ? itemDefinition(itemIdOrDef) |
| 279 | : itemIdOrDef; |
| 280 | return !!def?.dyeable; |
| 281 | } |
| 282 | |
| 283 | export { formatQuantity }; |
| 284 | |
| 285 | export function itemDisplayName (stack, def = itemDefinition(stack?.itemId)) |
| 286 | { |
| 287 | if (!def) |
| 288 | { return "Item"; } |
| 289 | const qty = stack?.quantity ?? 1; |
| 290 | if (stack?.itemId === ItemId.coins) |
| 291 | { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); } |
| 292 | if (stack?.itemId === ItemId.potion && stack.instance?.displayName) |
| 293 | { return stack.instance.displayName; } |
| 294 | if (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye") |
| 295 | { return "Dye"; } |
| 296 | if (stack?.itemId === ItemId.flower && stack.instance?.colorId) |
| 297 | { |
| 298 | return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId] ?? "Flower"} flower`; |
| 299 | } |
| 300 | if (stack?.itemId === ItemId.recipePaper && stack.instance?.displayName) |
| 301 | { return stack.instance.displayName; } |
| 302 | return def.displayName ?? "Item"; |
| 303 | } |
| 304 | |
| 305 | export function stackDisplayLabel (stack) |
| 306 | { |
| 307 | const name = itemDisplayName(stack); |
| 308 | const qty = stack?.quantity ?? 1; |
| 309 | if (qty <= 1) |
| 310 | { return name; } |
| 311 | return `${name} (${formatQuantity(qty)})`; |
| 312 | } |
| 313 | |
| 314 | export function coinSpriteTierForQuantity (quantity) |
| 315 | { |
| 316 | const qty = Math.max(0, Math.floor(Number(quantity) || 0)); |
| 317 | let tier = COIN_SPRITE_TIERS[0]; |
| 318 | for (const candidate of COIN_SPRITE_TIERS) |
| 319 | { |
| 320 | if (qty >= candidate) |
| 321 | { tier = candidate; } |
| 322 | else |
| 323 | { break; } |
| 324 | } |
| 325 | return tier; |
| 326 | } |
| 327 | |
| 328 | /** Stack-aware description (letters include open state and author). */ |
| 329 | export function itemDescription (stack, { localCharacterId } = {}) |
| 330 | { |
| 331 | const def = itemDefinition(stack?.itemId); |
| 332 | if (!def) |
| 333 | { return ""; } |
| 334 | if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter") |
| 335 | { |
| 336 | const state = stack.instance.opened === false ? "unopened" : "opened"; |
| 337 | const authorId = stack.instance.authorCharacterId; |
| 338 | const byYou = authorId != null && Number(authorId) === Number(localCharacterId); |
| 339 | const author = byYou |
| 340 | ? "you" |
| 341 | : (stack.instance.authorDisplayName?.trim() || "someone"); |
| 342 | return `An ${state} letter, written by ${author}.`; |
| 343 | } |
| 344 | if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque") |
| 345 | { |
| 346 | const reserved = itemDefinition(stack.instance.itemId); |
| 347 | const qty = stack.instance.quantity | 0; |
| 348 | const name = reserved?.displayName ?? "goods"; |
| 349 | return `Bearer cheque for ${qty}× ${name}.`; |
| 350 | } |
| 351 | if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion") |
| 352 | { |
| 353 | const name = stack.instance.displayName ?? "Potion"; |
| 354 | return stack.instance.unstable |
| 355 | ? `${name}. The brew smells wrong — best sipped with caution.` |
| 356 | : `${name}. A carefully bottled draught.`; |
| 357 | } |
| 358 | if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper") |
| 359 | { |
| 360 | const authorId = stack.instance.authorCharacterId; |
| 361 | const byYou = authorId != null && Number(authorId) === Number(localCharacterId); |
| 362 | const author = byYou |
| 363 | ? "you" |
| 364 | : (stack.instance.authorDisplayName?.trim() || "someone"); |
| 365 | return `Written by ${author}.`; |
| 366 | } |
| 367 | return def.description ?? ""; |
| 368 | } |
| 369 | |
| 370 | export function itemOriginSummary (stack) |
| 371 | { |
| 372 | return formatOriginBreakdown(stack); |
| 373 | } |
| 374 | |
| 375 | export function itemDefaultState (def) |
| 376 | { |
| 377 | return def?.defaultState ?? def?.key ?? "paper"; |
| 378 | } |
| 379 | |
| 380 | export function itemSpriteName (itemId, instance = null) |
| 381 | { |
| 382 | const def = itemDefinition(itemId); |
| 383 | if (!def) |
| 384 | { return spriteImageName("item", "paper", "paper"); } |
| 385 | if (itemId === ItemId.letter) |
| 386 | { return spriteImageName("item", "letter", letterVisualState(instance)); } |
| 387 | const folderKey = def.iconKey ?? def.key; |
| 388 | return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def)); |
| 389 | } |
| 390 | |
| 391 | export function itemEquipSprite (itemId) |
| 392 | { |
| 393 | const def = itemDefinition(itemId); |
| 394 | const equipment = def?.equipment; |
| 395 | if (!equipment) |
| 396 | { return null; } |
| 397 | const folderKey = def.iconKey ?? def.key; |
| 398 | return equipment.equipSprite |
| 399 | ?? equipment.overlaySpriteId |
| 400 | ?? equipImageName("item", folderKey, itemDefaultState(def)); |
| 401 | } |
| 402 | |
| 403 | /** Largest equipped light radius (e.g. lantern in offhand). */ |
| 404 | export function equippedLightRadiusTiles (equipment) |
| 405 | { |
| 406 | let best = 0; |
| 407 | for (const stack of Object.values(equipment?.slots ?? {})) |
| 408 | { |
| 409 | if (!stack) |
| 410 | { continue; } |
| 411 | const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0; |
| 412 | if (radius > best) |
| 413 | { best = radius; } |
| 414 | } |
| 415 | return best; |
| 416 | } |
| 417 | |
| 418 | export function letterVisualState (instance) |
| 419 | { |
| 420 | return instance?.opened === false ? "letter-closed" : "letter-opened"; |
| 421 | } |
| 422 | |
| 423 | export function itemAssetPath (itemId, companion = null, instance = null) |
| 424 | { |
| 425 | const def = itemDefinition(itemId); |
| 426 | if (def?.herb) |
| 427 | { |
| 428 | const suffix = companion ? `-${companion}` : ""; |
| 429 | return `/assets/herbs/${def.key}${suffix}.png`; |
| 430 | } |
| 431 | const key = def?.iconKey ?? def?.key ?? "paper"; |
| 432 | const state = itemId === ItemId.letter |
| 433 | ? letterVisualState(instance) |
| 434 | : itemDefaultState(def ?? { key }); |
| 435 | const suffix = companion ? `-${companion}` : ""; |
| 436 | return `/assets/items/${key}/${state}${suffix}.png`; |
| 437 | } |
| 438 | |
| 439 | export function itemAssetPathForStack (stack) |
| 440 | { |
| 441 | if (!stack) |
| 442 | { return itemAssetPath(null); } |
| 443 | if (stack.itemId === ItemId.coins) |
| 444 | { |
| 445 | const tier = coinSpriteTierForQuantity(stack.quantity); |
| 446 | return `/assets/items/coins/coins-${tier}.png`; |
| 447 | } |
| 448 | if (stack.itemId === ItemId.potion) |
| 449 | { |
| 450 | const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4)); |
| 451 | return `/assets/items/potion/potion-${fill}.png`; |
| 452 | } |
| 453 | if (stack.itemId === ItemId.dye) |
| 454 | { |
| 455 | const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4)); |
| 456 | return `/assets/items/potion/potion-${fill}.png`; |
| 457 | } |
| 458 | if (stack.itemId === ItemId.glassBottle) |
| 459 | { return `/assets/items/potion/potion-0.png`; } |
| 460 | if (stack.itemId === ItemId.flower) |
| 461 | { return `/assets/items/flower/flower.png`; } |
| 462 | return itemAssetPath(stack.itemId, null, stack.instance); |
| 463 | } |
| 464 | |
| 465 | export const LETTER_MAX_BODY_CHARS = 2000; |
| 466 | |
| 467 | export const STARTER_ITEMS = Object.freeze([ |
| 468 | Object.freeze({ itemId: ItemId.paper, quantity: 5 }), |
| 469 | Object.freeze({ itemId: ItemId.apple, quantity: 3 }), |
| 470 | Object.freeze({ itemId: ItemId.stick, quantity: 1 }), |
| 471 | Object.freeze({ itemId: ItemId.coins, quantity: 25 }) |
| 472 | ]); |