import { equipImageName, spriteImageName } from "./tileSprites.js"; import { SkillId } from "./skills.js"; import { MaterialTier } from "./materialTiers.js"; import { AttackType } from "./attackTypes.js"; import { EquipmentKind } from "./equipmentKinds.js"; import { formatQuantity } from "../items/formatQuantity.js"; import { formatOriginBreakdown } from "../items/origin.js"; import { CraftingItemId, CRAFTING_ITEM_STUBS } from "./craftingItems.js"; export const ItemId = Object.freeze({ paper: 1, apple: 2, stick: 3, blankCheque: 4, clothShirt: 5, letter: 6, filledCheque: 7, coins: 8, ...CraftingItemId }); export const EQUIPMENT_SLOT = Object.freeze({ head: "head", chest: "chest", legs: "legs", boots: "boots", weapon: "weapon", offhand: "offhand" }); export const EQUIPMENT_DRAW_ORDER = Object.freeze([ EQUIPMENT_SLOT.boots, EQUIPMENT_SLOT.legs, EQUIPMENT_SLOT.chest, EQUIPMENT_SLOT.head, EQUIPMENT_SLOT.offhand, EQUIPMENT_SLOT.weapon ]); export const DEFAULT_MAX_STACK = 99; export const BLANK_CHEQUE_CARRY_CAP = 99; export const COIN_MAX_STACK = 999_000_000_000; /** Coin sprite tiers (coins-.png); pick the largest n where stack quantity >= n. */ export const COIN_SPRITE_TIERS = Object.freeze([ 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000, 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000, 999_999_999_999_999 ]); export const ITEM_DEFINITIONS = Object.freeze({ [ItemId.paper]: Object.freeze({ itemId: ItemId.paper, key: "paper", displayName: "Paper", description: "Blank paper. Right-click to write a letter.", seedValue: 2, stackable: true, maxStackSize: DEFAULT_MAX_STACK, uniqueInstance: false, category: "document" }), [ItemId.letter]: Object.freeze({ itemId: ItemId.letter, key: "letter", displayName: "Letter", description: "A written letter.", seedValue: 2, stackable: false, maxStackSize: 1, uniqueInstance: true, category: "document", defaultState: "letter-opened" }), [ItemId.apple]: Object.freeze({ itemId: ItemId.apple, key: "apple", displayName: "Apple", description: "Restores a little health when eaten.", seedValue: 1, stackable: true, maxStackSize: DEFAULT_MAX_STACK, uniqueInstance: false, category: "food", healing: 4 }), [ItemId.stick]: Object.freeze({ itemId: ItemId.stick, key: "stick", displayName: "Stick", description: "A wooden stick. Can be wielded.", seedValue: 3, stackable: false, maxStackSize: 1, uniqueInstance: false, category: "finished", equipment: Object.freeze({ slot: EQUIPMENT_SLOT.weapon, tier: MaterialTier.bronze, kind: EquipmentKind.clubWeapon, attack: Object.freeze({ mode: "melee", rangeTiles: 1, hitSkillId: SkillId.clubfighting, damageSkillId: SkillId.strength, attackType: AttackType.blunt, cooldownMs: 700, baseDamage: 2 }) }) }), [ItemId.blankCheque]: Object.freeze({ itemId: ItemId.blankCheque, key: "blank-cheque", displayName: "Blank cheque", description: "Free bank paper. Write a cheque at a bank to reserve vault goods.", seedValue: 5, stackable: true, maxStackSize: DEFAULT_MAX_STACK, uniqueInstance: false, category: "document", marketplaceSellable: false, marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace." }), [ItemId.filledCheque]: Object.freeze({ itemId: ItemId.filledCheque, key: "filled-cheque", displayName: "Cheque", description: "A bearer cheque linked to reserved goods in the issuer's bank.", seedValue: 5, stackable: false, maxStackSize: 1, uniqueInstance: true, category: "document", defaultState: "filled-cheque", marketplaceSellable: true }), [ItemId.clothShirt]: Object.freeze({ itemId: ItemId.clothShirt, key: "cloth-shirt", displayName: "Cloth shirt", description: "A placeholder shirt worn on the chest.", seedValue: 8, stackable: false, maxStackSize: 1, uniqueInstance: false, category: "finished", equipment: Object.freeze({ slot: EQUIPMENT_SLOT.chest, tier: MaterialTier.bronze, kind: EquipmentKind.clothArmour }) }), [ItemId.coins]: Object.freeze({ itemId: ItemId.coins, key: "coins", displayName: "Coin", displayNamePlural: "Coins", description: "Currency carried in your bag.", seedValue: 1, stackable: true, maxStackSize: COIN_MAX_STACK, uniqueInstance: false, category: "currency" }), ...CRAFTING_ITEM_STUBS }); export function itemDefinition (itemId) { return ITEM_DEFINITIONS[itemId] ?? null; } export { formatQuantity }; export function itemDisplayName (stack, def = itemDefinition(stack?.itemId)) { if (!def) { return "Item"; } const qty = stack?.quantity ?? 1; if (stack?.itemId === ItemId.coins) { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); } return def.displayName ?? "Item"; } export function stackDisplayLabel (stack) { const name = itemDisplayName(stack); const qty = stack?.quantity ?? 1; if (qty <= 1) { return name; } return `${name} (${formatQuantity(qty)})`; } export function coinSpriteTierForQuantity (quantity) { const qty = Math.max(0, Math.floor(Number(quantity) || 0)); let tier = COIN_SPRITE_TIERS[0]; for (const candidate of COIN_SPRITE_TIERS) { if (qty >= candidate) { tier = candidate; } else { break; } } return tier; } /** Stack-aware description (letters include open state and author). */ export function itemDescription (stack, { localCharacterId } = {}) { const def = itemDefinition(stack?.itemId); if (!def) { return ""; } if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter") { const state = stack.instance.opened === false ? "unopened" : "opened"; const authorId = stack.instance.authorCharacterId; const byYou = authorId != null && Number(authorId) === Number(localCharacterId); const author = byYou ? "you" : (stack.instance.authorDisplayName?.trim() || "someone"); return `An ${state} letter, written by ${author}.`; } if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque") { const reserved = itemDefinition(stack.instance.itemId); const qty = stack.instance.quantity | 0; const name = reserved?.displayName ?? "goods"; return `Bearer cheque for ${qty}× ${name}.`; } return def.description ?? ""; } export function itemOriginSummary (stack) { return formatOriginBreakdown(stack); } export function itemDefaultState (def) { return def?.defaultState ?? def?.key ?? "paper"; } export function itemSpriteName (itemId, instance = null) { const def = itemDefinition(itemId); if (!def) { return spriteImageName("item", "paper", "paper"); } if (itemId === ItemId.letter) { return spriteImageName("item", "letter", letterVisualState(instance)); } return def.sprite ?? spriteImageName("item", def.key, itemDefaultState(def)); } export function itemEquipSprite (itemId) { const def = itemDefinition(itemId); const equipment = def?.equipment; if (!equipment) { return null; } return equipment.equipSprite ?? equipment.overlaySpriteId ?? equipImageName("item", def.key, itemDefaultState(def)); } export function letterVisualState (instance) { return instance?.opened === false ? "letter-closed" : "letter-opened"; } export function itemAssetPath (itemId, companion = null, instance = null) { const def = itemDefinition(itemId); const key = def?.iconKey ?? def?.key ?? "paper"; const state = itemId === ItemId.letter ? letterVisualState(instance) : itemDefaultState(def ?? { key }); const suffix = companion ? `-${companion}` : ""; return `/assets/items/${key}/${state}${suffix}.png`; } export function itemAssetPathForStack (stack) { if (!stack) { return itemAssetPath(null); } if (stack.itemId === ItemId.coins) { const tier = coinSpriteTierForQuantity(stack.quantity); return `/assets/items/coins/coins-${tier}.png`; } return itemAssetPath(stack.itemId, null, stack.instance); } export const LETTER_MAX_BODY_CHARS = 2000; export const STARTER_ITEMS = Object.freeze([ Object.freeze({ itemId: ItemId.paper, quantity: 5 }), Object.freeze({ itemId: ItemId.apple, quantity: 3 }), Object.freeze({ itemId: ItemId.stick, quantity: 1 }), Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }), Object.freeze({ itemId: ItemId.coins, quantity: 25 }) ]);