| 1 | import { equipImageName, spriteImageName } from "./tileSprites.js"; |
| 2 | import { SkillId } from "./skills.js"; |
| 3 | import { MaterialTier } 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 | |
| 10 | export const ItemId = Object.freeze({ |
| 11 | paper: 1, |
| 12 | apple: 2, |
| 13 | stick: 3, |
| 14 | blankCheque: 4, |
| 15 | clothShirt: 5, |
| 16 | letter: 6, |
| 17 | filledCheque: 7, |
| 18 | coins: 8, |
| 19 | ...CraftingItemId |
| 20 | }); |
| 21 | |
| 22 | export const EQUIPMENT_SLOT = Object.freeze({ |
| 23 | head: "head", |
| 24 | chest: "chest", |
| 25 | legs: "legs", |
| 26 | boots: "boots", |
| 27 | weapon: "weapon", |
| 28 | offhand: "offhand" |
| 29 | }); |
| 30 | |
| 31 | export const EQUIPMENT_DRAW_ORDER = Object.freeze([ |
| 32 | EQUIPMENT_SLOT.boots, |
| 33 | EQUIPMENT_SLOT.legs, |
| 34 | EQUIPMENT_SLOT.chest, |
| 35 | EQUIPMENT_SLOT.head, |
| 36 | EQUIPMENT_SLOT.offhand, |
| 37 | EQUIPMENT_SLOT.weapon |
| 38 | ]); |
| 39 | |
| 40 | export const DEFAULT_MAX_STACK = 99; |
| 41 | export const BLANK_CHEQUE_CARRY_CAP = 99; |
| 42 | export const COIN_MAX_STACK = 999_000_000_000; |
| 43 | |
| 44 | /** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */ |
| 45 | export const COIN_SPRITE_TIERS = Object.freeze([ |
| 46 | 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000, |
| 47 | 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000, |
| 48 | 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, |
| 49 | 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000, |
| 50 | 999_999_999_999_999 |
| 51 | ]); |
| 52 | |
| 53 | export const ITEM_DEFINITIONS = Object.freeze({ |
| 54 | [ItemId.paper]: Object.freeze({ |
| 55 | itemId: ItemId.paper, |
| 56 | key: "paper", |
| 57 | displayName: "Paper", |
| 58 | description: "Blank paper. Right-click to write a letter.", |
| 59 | seedValue: 2, |
| 60 | stackable: true, |
| 61 | maxStackSize: DEFAULT_MAX_STACK, |
| 62 | uniqueInstance: false, |
| 63 | category: "document" |
| 64 | }), |
| 65 | [ItemId.letter]: Object.freeze({ |
| 66 | itemId: ItemId.letter, |
| 67 | key: "letter", |
| 68 | displayName: "Letter", |
| 69 | description: "A written letter.", |
| 70 | seedValue: 2, |
| 71 | stackable: false, |
| 72 | maxStackSize: 1, |
| 73 | uniqueInstance: true, |
| 74 | category: "document", |
| 75 | defaultState: "letter-opened" |
| 76 | }), |
| 77 | [ItemId.apple]: Object.freeze({ |
| 78 | itemId: ItemId.apple, |
| 79 | key: "apple", |
| 80 | displayName: "Apple", |
| 81 | description: "Restores a little health when eaten.", |
| 82 | seedValue: 1, |
| 83 | stackable: true, |
| 84 | maxStackSize: DEFAULT_MAX_STACK, |
| 85 | uniqueInstance: false, |
| 86 | category: "food", |
| 87 | healing: 4 |
| 88 | }), |
| 89 | [ItemId.stick]: Object.freeze({ |
| 90 | itemId: ItemId.stick, |
| 91 | key: "stick", |
| 92 | displayName: "Stick", |
| 93 | description: "A wooden stick. Can be wielded.", |
| 94 | seedValue: 3, |
| 95 | stackable: false, |
| 96 | maxStackSize: 1, |
| 97 | uniqueInstance: false, |
| 98 | category: "finished", |
| 99 | equipment: Object.freeze({ |
| 100 | slot: EQUIPMENT_SLOT.weapon, |
| 101 | tier: MaterialTier.bronze, |
| 102 | kind: EquipmentKind.clubWeapon, |
| 103 | attack: Object.freeze({ |
| 104 | mode: "melee", |
| 105 | rangeTiles: 1, |
| 106 | hitSkillId: SkillId.clubfighting, |
| 107 | damageSkillId: SkillId.strength, |
| 108 | attackType: AttackType.blunt, |
| 109 | cooldownMs: 700, |
| 110 | baseDamage: 2 |
| 111 | }) |
| 112 | }) |
| 113 | }), |
| 114 | [ItemId.blankCheque]: Object.freeze({ |
| 115 | itemId: ItemId.blankCheque, |
| 116 | key: "blank-cheque", |
| 117 | displayName: "Blank cheque", |
| 118 | description: "Free bank paper. Write a cheque at a bank to reserve vault goods.", |
| 119 | seedValue: 5, |
| 120 | stackable: true, |
| 121 | maxStackSize: DEFAULT_MAX_STACK, |
| 122 | uniqueInstance: false, |
| 123 | category: "document", |
| 124 | marketplaceSellable: false, |
| 125 | marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace." |
| 126 | }), |
| 127 | [ItemId.filledCheque]: Object.freeze({ |
| 128 | itemId: ItemId.filledCheque, |
| 129 | key: "filled-cheque", |
| 130 | displayName: "Cheque", |
| 131 | description: "A bearer cheque linked to reserved goods in the issuer's bank.", |
| 132 | seedValue: 5, |
| 133 | stackable: false, |
| 134 | maxStackSize: 1, |
| 135 | uniqueInstance: true, |
| 136 | category: "document", |
| 137 | defaultState: "filled-cheque", |
| 138 | marketplaceSellable: true |
| 139 | }), |
| 140 | [ItemId.clothShirt]: Object.freeze({ |
| 141 | itemId: ItemId.clothShirt, |
| 142 | key: "cloth-shirt", |
| 143 | displayName: "Cloth shirt", |
| 144 | description: "A placeholder shirt worn on the chest.", |
| 145 | seedValue: 8, |
| 146 | stackable: false, |
| 147 | maxStackSize: 1, |
| 148 | uniqueInstance: false, |
| 149 | category: "finished", |
| 150 | equipment: Object.freeze({ |
| 151 | slot: EQUIPMENT_SLOT.chest, |
| 152 | tier: MaterialTier.bronze, |
| 153 | kind: EquipmentKind.clothArmour |
| 154 | }) |
| 155 | }), |
| 156 | [ItemId.coins]: Object.freeze({ |
| 157 | itemId: ItemId.coins, |
| 158 | key: "coins", |
| 159 | displayName: "Coin", |
| 160 | displayNamePlural: "Coins", |
| 161 | description: "Currency carried in your bag.", |
| 162 | seedValue: 1, |
| 163 | stackable: true, |
| 164 | maxStackSize: COIN_MAX_STACK, |
| 165 | uniqueInstance: false, |
| 166 | category: "currency" |
| 167 | }), |
| 168 | ...CRAFTING_ITEM_STUBS |
| 169 | }); |
| 170 | |
| 171 | export function itemDefinition (itemId) |
| 172 | { |
| 173 | return ITEM_DEFINITIONS[itemId] ?? null; |
| 174 | } |
| 175 | |
| 176 | export { formatQuantity }; |
| 177 | |
| 178 | export function itemDisplayName (stack, def = itemDefinition(stack?.itemId)) |
| 179 | { |
| 180 | if (!def) |
| 181 | { return "Item"; } |
| 182 | const qty = stack?.quantity ?? 1; |
| 183 | if (stack?.itemId === ItemId.coins) |
| 184 | { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); } |
| 185 | return def.displayName ?? "Item"; |
| 186 | } |
| 187 | |
| 188 | export function stackDisplayLabel (stack) |
| 189 | { |
| 190 | const name = itemDisplayName(stack); |
| 191 | const qty = stack?.quantity ?? 1; |
| 192 | if (qty <= 1) |
| 193 | { return name; } |
| 194 | return `${name} (${formatQuantity(qty)})`; |
| 195 | } |
| 196 | |
| 197 | export function coinSpriteTierForQuantity (quantity) |
| 198 | { |
| 199 | const qty = Math.max(0, Math.floor(Number(quantity) || 0)); |
| 200 | let tier = COIN_SPRITE_TIERS[0]; |
| 201 | for (const candidate of COIN_SPRITE_TIERS) |
| 202 | { |
| 203 | if (qty >= candidate) |
| 204 | { tier = candidate; } |
| 205 | else |
| 206 | { break; } |
| 207 | } |
| 208 | return tier; |
| 209 | } |
| 210 | |
| 211 | /** Stack-aware description (letters include open state and author). */ |
| 212 | export function itemDescription (stack, { localCharacterId } = {}) |
| 213 | { |
| 214 | const def = itemDefinition(stack?.itemId); |
| 215 | if (!def) |
| 216 | { return ""; } |
| 217 | if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter") |
| 218 | { |
| 219 | const state = stack.instance.opened === false ? "unopened" : "opened"; |
| 220 | const authorId = stack.instance.authorCharacterId; |
| 221 | const byYou = authorId != null && Number(authorId) === Number(localCharacterId); |
| 222 | const author = byYou |
| 223 | ? "you" |
| 224 | : (stack.instance.authorDisplayName?.trim() || "someone"); |
| 225 | return `An ${state} letter, written by ${author}.`; |
| 226 | } |
| 227 | if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque") |
| 228 | { |
| 229 | const reserved = itemDefinition(stack.instance.itemId); |
| 230 | const qty = stack.instance.quantity | 0; |
| 231 | const name = reserved?.displayName ?? "goods"; |
| 232 | return `Bearer cheque for ${qty}× ${name}.`; |
| 233 | } |
| 234 | return def.description ?? ""; |
| 235 | } |
| 236 | |
| 237 | export function itemOriginSummary (stack) |
| 238 | { |
| 239 | return formatOriginBreakdown(stack); |
| 240 | } |
| 241 | |
| 242 | export function itemDefaultState (def) |
| 243 | { |
| 244 | return def?.defaultState ?? def?.key ?? "paper"; |
| 245 | } |
| 246 | |
| 247 | export function itemSpriteName (itemId, instance = null) |
| 248 | { |
| 249 | const def = itemDefinition(itemId); |
| 250 | if (!def) |
| 251 | { return spriteImageName("item", "paper", "paper"); } |
| 252 | if (itemId === ItemId.letter) |
| 253 | { return spriteImageName("item", "letter", letterVisualState(instance)); } |
| 254 | return def.sprite ?? spriteImageName("item", def.key, itemDefaultState(def)); |
| 255 | } |
| 256 | |
| 257 | export function itemEquipSprite (itemId) |
| 258 | { |
| 259 | const def = itemDefinition(itemId); |
| 260 | const equipment = def?.equipment; |
| 261 | if (!equipment) |
| 262 | { return null; } |
| 263 | return equipment.equipSprite |
| 264 | ?? equipment.overlaySpriteId |
| 265 | ?? equipImageName("item", def.key, itemDefaultState(def)); |
| 266 | } |
| 267 | |
| 268 | export function letterVisualState (instance) |
| 269 | { |
| 270 | return instance?.opened === false ? "letter-closed" : "letter-opened"; |
| 271 | } |
| 272 | |
| 273 | export function itemAssetPath (itemId, companion = null, instance = null) |
| 274 | { |
| 275 | const def = itemDefinition(itemId); |
| 276 | const key = def?.iconKey ?? def?.key ?? "paper"; |
| 277 | const state = itemId === ItemId.letter |
| 278 | ? letterVisualState(instance) |
| 279 | : itemDefaultState(def ?? { key }); |
| 280 | const suffix = companion ? `-${companion}` : ""; |
| 281 | return `/assets/items/${key}/${state}${suffix}.png`; |
| 282 | } |
| 283 | |
| 284 | export function itemAssetPathForStack (stack) |
| 285 | { |
| 286 | if (!stack) |
| 287 | { return itemAssetPath(null); } |
| 288 | if (stack.itemId === ItemId.coins) |
| 289 | { |
| 290 | const tier = coinSpriteTierForQuantity(stack.quantity); |
| 291 | return `/assets/items/coins/coins-${tier}.png`; |
| 292 | } |
| 293 | return itemAssetPath(stack.itemId, null, stack.instance); |
| 294 | } |
| 295 | |
| 296 | export const LETTER_MAX_BODY_CHARS = 2000; |
| 297 | |
| 298 | export const STARTER_ITEMS = Object.freeze([ |
| 299 | Object.freeze({ itemId: ItemId.paper, quantity: 5 }), |
| 300 | Object.freeze({ itemId: ItemId.apple, quantity: 3 }), |
| 301 | Object.freeze({ itemId: ItemId.stick, quantity: 1 }), |
| 302 | Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }), |
| 303 | Object.freeze({ itemId: ItemId.coins, quantity: 25 }) |
| 304 | ]); |