| 1 | import { ItemCategory } from "./itemCategories.js"; |
| 2 | import { |
| 3 | WoodTier, |
| 4 | MATERIAL_DISPLAY_NAME, |
| 5 | WOOD_TIER_ORDER, |
| 6 | MATERIAL_TIER_ORDER, |
| 7 | CLOTH_TIER_ORDER, |
| 8 | CLOTH_DISPLAY_NAME, |
| 9 | woodTierForMetal, |
| 10 | clothTierForWood, |
| 11 | leatherTierForMetal |
| 12 | } from "./materialTiers.js"; |
| 13 | import { EquipmentKind } from "./equipmentKinds.js"; |
| 14 | import { AttackType } from "./attackTypes.js"; |
| 15 | import { SkillId } from "./skills.js"; |
| 16 | import { COMBAT_RANGE_TILES } from "../spells/constants.js"; |
| 17 | |
| 18 | /** Which tier list drives a family (`wood` → pine…elder, `metal` → bronze…dragon). */ |
| 19 | export const TierAxis = Object.freeze({ |
| 20 | wood: "wood", |
| 21 | metal: "metal", |
| 22 | cloth: "cloth" |
| 23 | }); |
| 24 | |
| 25 | /** Stable family ids — one template per intermediate / gather product shape. */ |
| 26 | export const ItemFamilyId = Object.freeze({ |
| 27 | log: "log", |
| 28 | handle: "handle", |
| 29 | ore: "ore", |
| 30 | bar: "bar", |
| 31 | pickaxeHead: "pickaxeHead", |
| 32 | axeHead: "axeHead", |
| 33 | pickaxe: "pickaxe", |
| 34 | axe: "axe", |
| 35 | warAxeHead: "warAxeHead", |
| 36 | warAxe: "warAxe", |
| 37 | swordBlade: "swordBlade", |
| 38 | sword: "sword", |
| 39 | plantClippers: "plantClippers", |
| 40 | arrowShaft: "arrowShaft", |
| 41 | arrowTip: "arrowTip", |
| 42 | arrow: "arrow", |
| 43 | unstrungBow: "unstrungBow", |
| 44 | bow: "bow", |
| 45 | bowstring: "bowstring", |
| 46 | quarterstaff: "quarterstaff" |
| 47 | }); |
| 48 | |
| 49 | const DEFAULT_STACK = 99; |
| 50 | |
| 51 | /** |
| 52 | * Family templates: display suffix, sprite, stacking, and tier axis. |
| 53 | * Per-tier itemIds stay in woodItems / metalItems / rangedItems; defs are built here. |
| 54 | */ |
| 55 | export const ITEM_FAMILIES = Object.freeze({ |
| 56 | [ItemFamilyId.log]: Object.freeze({ |
| 57 | familyId: ItemFamilyId.log, |
| 58 | axis: TierAxis.wood, |
| 59 | label: "log", |
| 60 | description: (name) => `A sturdy ${name.toLowerCase()} log.`, |
| 61 | category: ItemCategory.ingredient, |
| 62 | iconKey: "wood", |
| 63 | defaultState: "log", |
| 64 | stackable: true, |
| 65 | maxStackSize: DEFAULT_STACK, |
| 66 | seedValue: (tier) => (tier === WoodTier.pine ? 5 : 2), |
| 67 | tintFromTier: true, |
| 68 | npcShopAllowed: true |
| 69 | }), |
| 70 | [ItemFamilyId.handle]: Object.freeze({ |
| 71 | familyId: ItemFamilyId.handle, |
| 72 | axis: TierAxis.wood, |
| 73 | label: "handle", |
| 74 | description: (name) => `Shaped ${name.toLowerCase()} haft. Combine with a matching toolhead.`, |
| 75 | category: ItemCategory.intermediate, |
| 76 | iconKey: "wood", |
| 77 | defaultState: "handle", |
| 78 | stackable: true, |
| 79 | maxStackSize: DEFAULT_STACK, |
| 80 | seedValue: 3, |
| 81 | tintFromTier: true |
| 82 | }), |
| 83 | [ItemFamilyId.ore]: Object.freeze({ |
| 84 | familyId: ItemFamilyId.ore, |
| 85 | axis: TierAxis.metal, |
| 86 | label: "ore", |
| 87 | description: (name) => `Mined ${name.toLowerCase()} ore. Smelt at a furnace.`, |
| 88 | category: ItemCategory.ingredient, |
| 89 | iconKey: "metal", |
| 90 | defaultState: "ore", |
| 91 | stackable: true, |
| 92 | maxStackSize: DEFAULT_STACK, |
| 93 | seedValue: 2, |
| 94 | tintFromTier: true |
| 95 | }), |
| 96 | [ItemFamilyId.bar]: Object.freeze({ |
| 97 | familyId: ItemFamilyId.bar, |
| 98 | axis: TierAxis.metal, |
| 99 | label: "bar", |
| 100 | description: (name) => `Smelted ${name.toLowerCase()}. Smith at an anvil.`, |
| 101 | category: ItemCategory.intermediate, |
| 102 | iconKey: "metal", |
| 103 | defaultState: "bar", |
| 104 | stackable: true, |
| 105 | maxStackSize: DEFAULT_STACK, |
| 106 | seedValue: 4, |
| 107 | tintFromTier: true |
| 108 | }), |
| 109 | [ItemFamilyId.pickaxeHead]: Object.freeze({ |
| 110 | familyId: ItemFamilyId.pickaxeHead, |
| 111 | axis: TierAxis.metal, |
| 112 | label: "pickaxe head", |
| 113 | description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`, |
| 114 | category: ItemCategory.intermediate, |
| 115 | iconKey: "pickaxe", |
| 116 | defaultState: "pickaxe-head", |
| 117 | stackable: true, |
| 118 | maxStackSize: DEFAULT_STACK, |
| 119 | seedValue: 5, |
| 120 | tintFromTier: true |
| 121 | }), |
| 122 | [ItemFamilyId.axeHead]: Object.freeze({ |
| 123 | familyId: ItemFamilyId.axeHead, |
| 124 | axis: TierAxis.metal, |
| 125 | label: "axe head", |
| 126 | description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`, |
| 127 | category: ItemCategory.intermediate, |
| 128 | iconKey: "axe", |
| 129 | defaultState: "axe-head", |
| 130 | stackable: true, |
| 131 | maxStackSize: DEFAULT_STACK, |
| 132 | seedValue: 5, |
| 133 | tintFromTier: true |
| 134 | }), |
| 135 | [ItemFamilyId.arrowShaft]: Object.freeze({ |
| 136 | familyId: ItemFamilyId.arrowShaft, |
| 137 | axis: TierAxis.wood, |
| 138 | label: "arrow shaft", |
| 139 | description: () => "A straight wooden shaft, ready for fletching.", |
| 140 | category: ItemCategory.intermediate, |
| 141 | iconKey: "arrow", |
| 142 | defaultState: "shaft", |
| 143 | stackable: true, |
| 144 | maxStackSize: DEFAULT_STACK, |
| 145 | seedValue: 1, |
| 146 | tintFromTier: true |
| 147 | }), |
| 148 | [ItemFamilyId.arrowTip]: Object.freeze({ |
| 149 | familyId: ItemFamilyId.arrowTip, |
| 150 | axis: TierAxis.metal, |
| 151 | label: "arrow tip", |
| 152 | description: (name) => `A smithed ${name.toLowerCase()} point for arrows.`, |
| 153 | category: ItemCategory.intermediate, |
| 154 | iconKey: "arrow", |
| 155 | defaultState: "tip", |
| 156 | stackable: true, |
| 157 | maxStackSize: DEFAULT_STACK, |
| 158 | seedValue: 1, |
| 159 | tintFromTier: true |
| 160 | }), |
| 161 | [ItemFamilyId.pickaxe]: Object.freeze({ |
| 162 | familyId: ItemFamilyId.pickaxe, |
| 163 | axis: TierAxis.metal, |
| 164 | label: "pickaxe", |
| 165 | description: (name) => `A ${name.toLowerCase()} pickaxe for breaking stone and ore.`, |
| 166 | category: ItemCategory.tool, |
| 167 | iconKey: "pickaxe", |
| 168 | defaultState: "pickaxe", |
| 169 | stackable: false, |
| 170 | maxStackSize: 1, |
| 171 | seedValue: 8, |
| 172 | dualTintFromTier: true, |
| 173 | woodTierFromMetal: true, |
| 174 | equipmentKind: EquipmentKind.pickaxeTool |
| 175 | }), |
| 176 | [ItemFamilyId.axe]: Object.freeze({ |
| 177 | familyId: ItemFamilyId.axe, |
| 178 | axis: TierAxis.metal, |
| 179 | label: "axe", |
| 180 | description: (name) => `A ${name.toLowerCase()} axe for felling trees.`, |
| 181 | category: ItemCategory.tool, |
| 182 | iconKey: "axe", |
| 183 | defaultState: "axe", |
| 184 | stackable: false, |
| 185 | maxStackSize: 1, |
| 186 | seedValue: 8, |
| 187 | dualTintFromTier: true, |
| 188 | woodTierFromMetal: true, |
| 189 | equipmentKind: EquipmentKind.axeTool |
| 190 | }), |
| 191 | [ItemFamilyId.warAxeHead]: Object.freeze({ |
| 192 | familyId: ItemFamilyId.warAxeHead, |
| 193 | axis: TierAxis.metal, |
| 194 | label: "war axe head", |
| 195 | description: (name) => `A heavy ${name.toLowerCase()} axe head, meant for battle rather than timber.`, |
| 196 | category: ItemCategory.intermediate, |
| 197 | iconKey: "waraxe", |
| 198 | defaultState: "waraxe-head", |
| 199 | stackable: true, |
| 200 | maxStackSize: DEFAULT_STACK, |
| 201 | seedValue: 8, |
| 202 | tintFromTier: true |
| 203 | }), |
| 204 | [ItemFamilyId.warAxe]: Object.freeze({ |
| 205 | familyId: ItemFamilyId.warAxe, |
| 206 | axis: TierAxis.metal, |
| 207 | label: "war axe", |
| 208 | description: (name) => `A ${name.toLowerCase()} war axe, hafted for the press of combat.`, |
| 209 | category: ItemCategory.tool, |
| 210 | iconKey: "waraxe", |
| 211 | defaultState: "waraxe", |
| 212 | stackable: false, |
| 213 | maxStackSize: 1, |
| 214 | seedValue: 12, |
| 215 | /** Red = wood haft, blue = metal head. */ |
| 216 | dualTintFromTier: true, |
| 217 | woodTierFromMetal: true, |
| 218 | equipmentKind: EquipmentKind.metalWeapon, |
| 219 | equipmentAttack: Object.freeze({ |
| 220 | mode: "melee", |
| 221 | rangeTiles: 1, |
| 222 | hitSkillId: SkillId.axefighting, |
| 223 | damageSkillId: SkillId.strength, |
| 224 | attackType: AttackType.chopping, |
| 225 | cooldownMs: 800 |
| 226 | }) |
| 227 | }), |
| 228 | [ItemFamilyId.plantClippers]: Object.freeze({ |
| 229 | familyId: ItemFamilyId.plantClippers, |
| 230 | axis: TierAxis.metal, |
| 231 | label: "plant clippers", |
| 232 | description: () => "Fine shears for cutting herbs and blooms.", |
| 233 | category: ItemCategory.tool, |
| 234 | iconKey: "bronze-plant-clippers", |
| 235 | defaultState: "bronze-plant-clippers", |
| 236 | stackable: false, |
| 237 | maxStackSize: 1, |
| 238 | seedValue: 10, |
| 239 | tintFromTier: true, |
| 240 | npcShopAllowed: true, |
| 241 | equipmentKind: EquipmentKind.plantClippers, |
| 242 | equipmentTintFromTier: true |
| 243 | }), |
| 244 | [ItemFamilyId.arrow]: Object.freeze({ |
| 245 | familyId: ItemFamilyId.arrow, |
| 246 | axis: TierAxis.metal, |
| 247 | label: "arrow", |
| 248 | description: () => "A fletched arrow with matching shaft and head.", |
| 249 | category: ItemCategory.finished, |
| 250 | iconKey: "arrow", |
| 251 | defaultState: "full-arrow", |
| 252 | stackable: true, |
| 253 | maxStackSize: 999, |
| 254 | seedValue: 2, |
| 255 | dualTintFromTier: true, |
| 256 | woodTierFromMetal: true, |
| 257 | equipmentKind: EquipmentKind.arrowAmmo, |
| 258 | equipmentSlot: "offhand", |
| 259 | hideEquipSprite: true |
| 260 | }), |
| 261 | [ItemFamilyId.unstrungBow]: Object.freeze({ |
| 262 | familyId: ItemFamilyId.unstrungBow, |
| 263 | axis: TierAxis.wood, |
| 264 | label: "unstrung bow", |
| 265 | description: () => "A carved bow frame awaiting a string.", |
| 266 | category: ItemCategory.intermediate, |
| 267 | iconKey: "bow", |
| 268 | defaultState: "unstrung-bow", |
| 269 | stackable: false, |
| 270 | maxStackSize: 1, |
| 271 | seedValue: 6, |
| 272 | /** Red = wood; blue unused (same dual shader as bows/arrows). */ |
| 273 | dualTintFromTier: true |
| 274 | }), |
| 275 | [ItemFamilyId.bow]: Object.freeze({ |
| 276 | familyId: ItemFamilyId.bow, |
| 277 | axis: TierAxis.wood, |
| 278 | label: "bow", |
| 279 | description: (name) => `A ${name.toLowerCase()} bow strung with matching yarn.`, |
| 280 | category: ItemCategory.finished, |
| 281 | iconKey: "bow", |
| 282 | defaultState: "bow", |
| 283 | stackable: false, |
| 284 | maxStackSize: 1, |
| 285 | seedValue: 12, |
| 286 | /** Red = wood, blue = string (cloth tier). */ |
| 287 | dualTintFromTier: true, |
| 288 | clothTierFromWood: true, |
| 289 | equipmentKind: EquipmentKind.bow, |
| 290 | equipmentAttack: Object.freeze({ |
| 291 | mode: "ranged", |
| 292 | rangeTiles: COMBAT_RANGE_TILES, |
| 293 | hitSkillId: SkillId.archery, |
| 294 | damageSkillId: SkillId.dexterity, |
| 295 | attackType: AttackType.piercing, |
| 296 | cooldownMs: 1200 |
| 297 | }) |
| 298 | }), |
| 299 | [ItemFamilyId.bowstring]: Object.freeze({ |
| 300 | familyId: ItemFamilyId.bowstring, |
| 301 | axis: TierAxis.cloth, |
| 302 | label: "bowstring", |
| 303 | description: () => "Twisted yarn spun tight for a bow.", |
| 304 | category: ItemCategory.intermediate, |
| 305 | iconKey: "bow", |
| 306 | defaultState: "bowstring", |
| 307 | stackable: true, |
| 308 | maxStackSize: DEFAULT_STACK, |
| 309 | seedValue: 2, |
| 310 | tintFromTier: true |
| 311 | }), |
| 312 | [ItemFamilyId.quarterstaff]: Object.freeze({ |
| 313 | familyId: ItemFamilyId.quarterstaff, |
| 314 | axis: TierAxis.wood, |
| 315 | label: "quarterstaff", |
| 316 | description: (name) => `A ${name.toLowerCase()} quarterstaff, long enough to keep foes at a distance.`, |
| 317 | category: ItemCategory.finished, |
| 318 | iconKey: "quarterstaff", |
| 319 | defaultState: "quarterstaff", |
| 320 | stackable: false, |
| 321 | maxStackSize: 1, |
| 322 | seedValue: 8, |
| 323 | tintFromTier: true, |
| 324 | equipmentKind: EquipmentKind.clubWeapon, |
| 325 | equipmentSlot: "weapon", |
| 326 | equipmentTintFromTier: true, |
| 327 | equipmentAttack: Object.freeze({ |
| 328 | mode: "melee", |
| 329 | rangeTiles: 1, |
| 330 | hitSkillId: SkillId.clubfighting, |
| 331 | damageSkillId: SkillId.strength, |
| 332 | attackType: AttackType.crushing, |
| 333 | cooldownMs: 700 |
| 334 | }) |
| 335 | }), |
| 336 | [ItemFamilyId.swordBlade]: Object.freeze({ |
| 337 | familyId: ItemFamilyId.swordBlade, |
| 338 | axis: TierAxis.metal, |
| 339 | label: "sword blade", |
| 340 | description: (name) => `A smithed ${name.toLowerCase()} blade. Haft it with leather at a crafting bench.`, |
| 341 | category: ItemCategory.intermediate, |
| 342 | iconKey: "sword", |
| 343 | defaultState: "sword-blade", |
| 344 | stackable: true, |
| 345 | maxStackSize: DEFAULT_STACK, |
| 346 | seedValue: 6, |
| 347 | tintFromTier: true |
| 348 | }), |
| 349 | [ItemFamilyId.sword]: Object.freeze({ |
| 350 | familyId: ItemFamilyId.sword, |
| 351 | axis: TierAxis.metal, |
| 352 | label: "sword", |
| 353 | description: (name) => `A ${name.toLowerCase()} sword, leather-wrapped for a sure grip.`, |
| 354 | category: ItemCategory.tool, |
| 355 | iconKey: "sword", |
| 356 | defaultState: "sword", |
| 357 | stackable: false, |
| 358 | maxStackSize: 1, |
| 359 | seedValue: 10, |
| 360 | /** Red = metal, blue = leather wrap (wood haft hidden under the wrap). */ |
| 361 | dualTintFromTier: true, |
| 362 | leatherTierFromMetal: true, |
| 363 | equipmentKind: EquipmentKind.metalWeapon, |
| 364 | equipmentAttack: Object.freeze({ |
| 365 | mode: "melee", |
| 366 | rangeTiles: 1, |
| 367 | hitSkillId: SkillId.swordfighting, |
| 368 | damageSkillId: SkillId.strength, |
| 369 | attackType: AttackType.slashing, |
| 370 | cooldownMs: 700 |
| 371 | }) |
| 372 | }) |
| 373 | }); |
| 374 | |
| 375 | export function tierOrderForAxis (axis) |
| 376 | { |
| 377 | if (axis === TierAxis.wood) |
| 378 | { return WOOD_TIER_ORDER; } |
| 379 | if (axis === TierAxis.metal) |
| 380 | { return MATERIAL_TIER_ORDER; } |
| 381 | if (axis === TierAxis.cloth) |
| 382 | { return CLOTH_TIER_ORDER; } |
| 383 | return []; |
| 384 | } |
| 385 | |
| 386 | function buildFamilyEquipment (family, tier) |
| 387 | { |
| 388 | if (!family.equipmentKind) |
| 389 | { return null; } |
| 390 | const equipment = { |
| 391 | slot: family.equipmentSlot ?? "weapon", |
| 392 | tier, |
| 393 | kind: family.equipmentKind |
| 394 | }; |
| 395 | if (family.equipmentTintFromTier) |
| 396 | { equipment.tintFromTier = true; } |
| 397 | if (family.hideEquipSprite) |
| 398 | { equipment.hideEquipSprite = true; } |
| 399 | if (family.equipmentAttack) |
| 400 | { equipment.attack = family.equipmentAttack; } |
| 401 | return Object.freeze(equipment); |
| 402 | } |
| 403 | |
| 404 | export function familyDefinition (familyId) |
| 405 | { |
| 406 | return ITEM_FAMILIES[familyId] ?? null; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Build one tiered item definition from a family template. |
| 411 | * @param {string} familyId |
| 412 | * @param {string} tier |
| 413 | * @param {number} itemId stable numeric id (inventory / saves) |
| 414 | * @param {object} [extra] per-tier overrides (key, seedValue, description, …) |
| 415 | */ |
| 416 | export function buildTierItemDef (familyId, tier, itemId, extra = {}) |
| 417 | { |
| 418 | const family = familyDefinition(familyId); |
| 419 | if (!family) |
| 420 | { throw new Error(`Unknown item family: ${familyId}`); } |
| 421 | const name = family.axis === TierAxis.cloth |
| 422 | ? (CLOTH_DISPLAY_NAME[tier] ?? tier) |
| 423 | : (MATERIAL_DISPLAY_NAME[tier] ?? tier); |
| 424 | const key = extra.key ?? `${tier}-${family.label.replace(/ /g, "-")}`; |
| 425 | const seedValue = extra.seedValue ?? ( |
| 426 | typeof family.seedValue === "function" ? family.seedValue(tier) : family.seedValue |
| 427 | ); |
| 428 | const description = extra.description ?? family.description(name); |
| 429 | const equipment = extra.equipment ?? buildFamilyEquipment(family, tier); |
| 430 | return Object.freeze({ |
| 431 | itemId, |
| 432 | familyId, |
| 433 | key, |
| 434 | displayName: extra.displayName ?? `${name} ${family.label}`, |
| 435 | description, |
| 436 | seedValue, |
| 437 | stackable: extra.stackable ?? family.stackable, |
| 438 | maxStackSize: extra.maxStackSize ?? family.maxStackSize, |
| 439 | uniqueInstance: false, |
| 440 | category: extra.category ?? family.category, |
| 441 | iconKey: extra.iconKey ?? family.iconKey, |
| 442 | defaultState: extra.defaultState ?? family.defaultState, |
| 443 | tier, |
| 444 | ...(family.tintFromTier ? { tintFromTier: true } : {}), |
| 445 | ...(family.npcShopAllowed ? { npcShopAllowed: true } : {}), |
| 446 | ...(family.woodTierFromMetal ? { woodTier: woodTierForMetal(tier), dualTintFromTier: true } : {}), |
| 447 | ...(family.leatherTierFromMetal |
| 448 | ? { leatherTier: leatherTierForMetal(tier), dualTintFromTier: true } |
| 449 | : {}), |
| 450 | ...(family.dualTintFromTier && !family.woodTierFromMetal && !family.leatherTierFromMetal |
| 451 | ? { dualTintFromTier: true } |
| 452 | : {}), |
| 453 | ...(family.clothTierFromWood ? { clothTier: clothTierForWood(tier) } : {}), |
| 454 | ...(equipment ? { equipment } : {}) |
| 455 | }); |
| 456 | } |
| 457 | |
| 458 | /** Build `{ [itemId]: def }` from a tier → itemId map. */ |
| 459 | export function buildFamilyDefinitionMap (familyId, tierToItemId, extraByTier = {}) |
| 460 | { |
| 461 | const defs = {}; |
| 462 | for (const [tier, id] of Object.entries(tierToItemId)) |
| 463 | { |
| 464 | const itemId = id | 0; |
| 465 | if (!itemId) |
| 466 | { continue; } |
| 467 | defs[itemId] = buildTierItemDef(familyId, tier, itemId, extraByTier[tier] ?? {}); |
| 468 | } |
| 469 | return defs; |
| 470 | } |
| 471 | |
| 472 | /** Resolve itemId for a family tier from a lookup table. */ |
| 473 | export function itemIdForFamilyTier (familyTierMap, tier) |
| 474 | { |
| 475 | return familyTierMap[tier] ?? null; |
| 476 | } |
| 477 | |
| 478 | /** Reverse lookup: tier for an item id within a family map, or null. */ |
| 479 | export function tierForFamilyItemId (familyTierMap, itemId) |
| 480 | { |
| 481 | const want = itemId | 0; |
| 482 | for (const [tier, id] of Object.entries(familyTierMap)) |
| 483 | { |
| 484 | if ((id | 0) === want) |
| 485 | { return tier; } |
| 486 | } |
| 487 | return null; |
| 488 | } |