| 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 | metalTierForWood, |
| 10 | clothTierForWood |
| 11 | } from "./materialTiers.js"; |
| 12 | import { EquipmentKind } from "./equipmentKinds.js"; |
| 13 | import { AttackType } from "./attackTypes.js"; |
| 14 | import { SkillId } from "./skills.js"; |
| 15 | |
| 16 | /** Which tier list drives a family (`wood` → pine…elder, `metal` → bronze…dragon). */ |
| 17 | export const TierAxis = Object.freeze({ |
| 18 | wood: "wood", |
| 19 | metal: "metal", |
| 20 | cloth: "cloth" |
| 21 | }); |
| 22 | |
| 23 | /** Stable family ids — one template per intermediate / gather product shape. */ |
| 24 | export const ItemFamilyId = Object.freeze({ |
| 25 | log: "log", |
| 26 | handle: "handle", |
| 27 | ore: "ore", |
| 28 | bar: "bar", |
| 29 | pickaxeHead: "pickaxeHead", |
| 30 | axeHead: "axeHead", |
| 31 | pickaxe: "pickaxe", |
| 32 | axe: "axe", |
| 33 | plantClippers: "plantClippers", |
| 34 | arrowShaft: "arrowShaft", |
| 35 | arrowTip: "arrowTip", |
| 36 | arrow: "arrow", |
| 37 | unstrungBow: "unstrungBow", |
| 38 | bow: "bow", |
| 39 | bowstring: "bowstring" |
| 40 | }); |
| 41 | |
| 42 | const DEFAULT_STACK = 99; |
| 43 | |
| 44 | /** |
| 45 | * Family templates: display suffix, sprite, stacking, and tier axis. |
| 46 | * Per-tier itemIds stay in woodItems / metalItems / rangedItems; defs are built here. |
| 47 | */ |
| 48 | export const ITEM_FAMILIES = Object.freeze({ |
| 49 | [ItemFamilyId.log]: Object.freeze({ |
| 50 | familyId: ItemFamilyId.log, |
| 51 | axis: TierAxis.wood, |
| 52 | label: "log", |
| 53 | description: (name) => `A sturdy ${name.toLowerCase()} log.`, |
| 54 | category: ItemCategory.ingredient, |
| 55 | iconKey: "wood", |
| 56 | defaultState: "log", |
| 57 | stackable: true, |
| 58 | maxStackSize: DEFAULT_STACK, |
| 59 | seedValue: (tier) => (tier === WoodTier.pine ? 5 : 2), |
| 60 | tintFromTier: true, |
| 61 | npcShopAllowed: true |
| 62 | }), |
| 63 | [ItemFamilyId.handle]: Object.freeze({ |
| 64 | familyId: ItemFamilyId.handle, |
| 65 | axis: TierAxis.wood, |
| 66 | label: "handle", |
| 67 | description: (name) => `Shaped ${name.toLowerCase()} haft. Combine with a matching toolhead.`, |
| 68 | category: ItemCategory.intermediate, |
| 69 | iconKey: "wood", |
| 70 | defaultState: "handle", |
| 71 | stackable: true, |
| 72 | maxStackSize: DEFAULT_STACK, |
| 73 | seedValue: 3, |
| 74 | tintFromTier: true |
| 75 | }), |
| 76 | [ItemFamilyId.ore]: Object.freeze({ |
| 77 | familyId: ItemFamilyId.ore, |
| 78 | axis: TierAxis.metal, |
| 79 | label: "ore", |
| 80 | description: (name) => `Mined ${name.toLowerCase()} ore. Smelt at a furnace.`, |
| 81 | category: ItemCategory.ingredient, |
| 82 | iconKey: "metal", |
| 83 | defaultState: "ore", |
| 84 | stackable: true, |
| 85 | maxStackSize: DEFAULT_STACK, |
| 86 | seedValue: 2, |
| 87 | tintFromTier: true |
| 88 | }), |
| 89 | [ItemFamilyId.bar]: Object.freeze({ |
| 90 | familyId: ItemFamilyId.bar, |
| 91 | axis: TierAxis.metal, |
| 92 | label: "bar", |
| 93 | description: (name) => `Smelted ${name.toLowerCase()}. Smith at an anvil.`, |
| 94 | category: ItemCategory.intermediate, |
| 95 | iconKey: "metal", |
| 96 | defaultState: "bar", |
| 97 | stackable: true, |
| 98 | maxStackSize: DEFAULT_STACK, |
| 99 | seedValue: 4, |
| 100 | tintFromTier: true |
| 101 | }), |
| 102 | [ItemFamilyId.pickaxeHead]: Object.freeze({ |
| 103 | familyId: ItemFamilyId.pickaxeHead, |
| 104 | axis: TierAxis.metal, |
| 105 | label: "pickaxe head", |
| 106 | description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`, |
| 107 | category: ItemCategory.intermediate, |
| 108 | iconKey: "pickaxe", |
| 109 | defaultState: "pickaxe-head", |
| 110 | stackable: true, |
| 111 | maxStackSize: DEFAULT_STACK, |
| 112 | seedValue: 5, |
| 113 | tintFromTier: true |
| 114 | }), |
| 115 | [ItemFamilyId.axeHead]: Object.freeze({ |
| 116 | familyId: ItemFamilyId.axeHead, |
| 117 | axis: TierAxis.metal, |
| 118 | label: "axe head", |
| 119 | description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`, |
| 120 | category: ItemCategory.intermediate, |
| 121 | iconKey: "axe", |
| 122 | defaultState: "axe-head", |
| 123 | stackable: true, |
| 124 | maxStackSize: DEFAULT_STACK, |
| 125 | seedValue: 5, |
| 126 | tintFromTier: true |
| 127 | }), |
| 128 | [ItemFamilyId.arrowShaft]: Object.freeze({ |
| 129 | familyId: ItemFamilyId.arrowShaft, |
| 130 | axis: TierAxis.wood, |
| 131 | label: "arrow shaft", |
| 132 | description: () => "A straight wooden shaft, ready for fletching.", |
| 133 | category: ItemCategory.intermediate, |
| 134 | iconKey: "arrow", |
| 135 | defaultState: "shaft", |
| 136 | stackable: true, |
| 137 | maxStackSize: DEFAULT_STACK, |
| 138 | seedValue: 1, |
| 139 | tintFromTier: true |
| 140 | }), |
| 141 | [ItemFamilyId.arrowTip]: Object.freeze({ |
| 142 | familyId: ItemFamilyId.arrowTip, |
| 143 | axis: TierAxis.metal, |
| 144 | label: "arrow tip", |
| 145 | description: (name) => `A smithed ${name.toLowerCase()} point for arrows.`, |
| 146 | category: ItemCategory.intermediate, |
| 147 | iconKey: "arrow", |
| 148 | defaultState: "tip", |
| 149 | stackable: true, |
| 150 | maxStackSize: DEFAULT_STACK, |
| 151 | seedValue: 1, |
| 152 | tintFromTier: true |
| 153 | }), |
| 154 | [ItemFamilyId.pickaxe]: Object.freeze({ |
| 155 | familyId: ItemFamilyId.pickaxe, |
| 156 | axis: TierAxis.metal, |
| 157 | label: "pickaxe", |
| 158 | description: (name) => `A ${name.toLowerCase()} pickaxe for breaking stone and ore.`, |
| 159 | category: ItemCategory.tool, |
| 160 | iconKey: "pickaxe", |
| 161 | defaultState: "pickaxe", |
| 162 | stackable: false, |
| 163 | maxStackSize: 1, |
| 164 | seedValue: 8, |
| 165 | tintFromTier: true, |
| 166 | equipmentKind: EquipmentKind.pickaxeTool |
| 167 | }), |
| 168 | [ItemFamilyId.axe]: Object.freeze({ |
| 169 | familyId: ItemFamilyId.axe, |
| 170 | axis: TierAxis.metal, |
| 171 | label: "axe", |
| 172 | description: (name) => `A ${name.toLowerCase()} axe for felling trees.`, |
| 173 | category: ItemCategory.tool, |
| 174 | iconKey: "axe", |
| 175 | defaultState: "axe", |
| 176 | stackable: false, |
| 177 | maxStackSize: 1, |
| 178 | seedValue: 8, |
| 179 | tintFromTier: true, |
| 180 | equipmentKind: EquipmentKind.axeTool |
| 181 | }), |
| 182 | [ItemFamilyId.plantClippers]: Object.freeze({ |
| 183 | familyId: ItemFamilyId.plantClippers, |
| 184 | axis: TierAxis.metal, |
| 185 | label: "plant clippers", |
| 186 | description: () => "Fine shears for cutting herbs and blooms.", |
| 187 | category: ItemCategory.tool, |
| 188 | iconKey: "bronze-plant-clippers", |
| 189 | defaultState: "bronze-plant-clippers", |
| 190 | stackable: false, |
| 191 | maxStackSize: 1, |
| 192 | seedValue: 10, |
| 193 | tintFromTier: true, |
| 194 | npcShopAllowed: true, |
| 195 | equipmentKind: EquipmentKind.plantClippers, |
| 196 | equipmentTintFromTier: true |
| 197 | }), |
| 198 | [ItemFamilyId.arrow]: Object.freeze({ |
| 199 | familyId: ItemFamilyId.arrow, |
| 200 | axis: TierAxis.metal, |
| 201 | label: "arrow", |
| 202 | description: () => "A fletched arrow with matching shaft and head.", |
| 203 | category: ItemCategory.finished, |
| 204 | iconKey: "arrow", |
| 205 | defaultState: "full-arrow", |
| 206 | stackable: true, |
| 207 | maxStackSize: 999, |
| 208 | seedValue: 2, |
| 209 | dualTintFromTier: true, |
| 210 | woodTierFromMetal: true, |
| 211 | equipmentKind: EquipmentKind.arrowAmmo, |
| 212 | equipmentSlot: "offhand", |
| 213 | hideEquipSprite: true |
| 214 | }), |
| 215 | [ItemFamilyId.unstrungBow]: Object.freeze({ |
| 216 | familyId: ItemFamilyId.unstrungBow, |
| 217 | axis: TierAxis.wood, |
| 218 | label: "unstrung bow", |
| 219 | description: () => "A carved bow frame awaiting a string.", |
| 220 | category: ItemCategory.intermediate, |
| 221 | iconKey: "bow", |
| 222 | defaultState: "bow", |
| 223 | stackable: false, |
| 224 | maxStackSize: 1, |
| 225 | seedValue: 6, |
| 226 | tintFromTier: true |
| 227 | }), |
| 228 | [ItemFamilyId.bow]: Object.freeze({ |
| 229 | familyId: ItemFamilyId.bow, |
| 230 | axis: TierAxis.wood, |
| 231 | label: "bow", |
| 232 | description: (name) => `A ${name.toLowerCase()} bow strung with matching yarn.`, |
| 233 | category: ItemCategory.finished, |
| 234 | iconKey: "bow", |
| 235 | defaultState: "bow", |
| 236 | stackable: false, |
| 237 | maxStackSize: 1, |
| 238 | seedValue: 12, |
| 239 | tintFromTier: true, |
| 240 | clothTierFromWood: true, |
| 241 | equipmentKind: EquipmentKind.bow, |
| 242 | equipmentTintFromTier: true, |
| 243 | equipmentAttack: Object.freeze({ |
| 244 | mode: "ranged", |
| 245 | rangeTiles: 8, |
| 246 | hitSkillId: SkillId.archery, |
| 247 | damageSkillId: SkillId.dexterity, |
| 248 | attackType: AttackType.piercing, |
| 249 | cooldownMs: 1200 |
| 250 | }) |
| 251 | }), |
| 252 | [ItemFamilyId.bowstring]: Object.freeze({ |
| 253 | familyId: ItemFamilyId.bowstring, |
| 254 | axis: TierAxis.cloth, |
| 255 | label: "bowstring", |
| 256 | description: () => "Twisted yarn spun tight for a bow.", |
| 257 | category: ItemCategory.intermediate, |
| 258 | iconKey: "cloth", |
| 259 | defaultState: "yarn", |
| 260 | stackable: true, |
| 261 | maxStackSize: DEFAULT_STACK, |
| 262 | seedValue: 2, |
| 263 | tintFromTier: true |
| 264 | }) |
| 265 | }); |
| 266 | |
| 267 | export function tierOrderForAxis (axis) |
| 268 | { |
| 269 | if (axis === TierAxis.wood) |
| 270 | { return WOOD_TIER_ORDER; } |
| 271 | if (axis === TierAxis.metal) |
| 272 | { return MATERIAL_TIER_ORDER; } |
| 273 | if (axis === TierAxis.cloth) |
| 274 | { return CLOTH_TIER_ORDER; } |
| 275 | return []; |
| 276 | } |
| 277 | |
| 278 | function buildFamilyEquipment (family, tier) |
| 279 | { |
| 280 | if (!family.equipmentKind) |
| 281 | { return null; } |
| 282 | const equipment = { |
| 283 | slot: family.equipmentSlot ?? "weapon", |
| 284 | tier, |
| 285 | kind: family.equipmentKind |
| 286 | }; |
| 287 | if (family.equipmentTintFromTier) |
| 288 | { equipment.tintFromTier = true; } |
| 289 | if (family.hideEquipSprite) |
| 290 | { equipment.hideEquipSprite = true; } |
| 291 | if (family.equipmentAttack) |
| 292 | { equipment.attack = family.equipmentAttack; } |
| 293 | return Object.freeze(equipment); |
| 294 | } |
| 295 | |
| 296 | export function familyDefinition (familyId) |
| 297 | { |
| 298 | return ITEM_FAMILIES[familyId] ?? null; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Build one tiered item definition from a family template. |
| 303 | * @param {string} familyId |
| 304 | * @param {string} tier |
| 305 | * @param {number} itemId stable numeric id (inventory / saves) |
| 306 | * @param {object} [extra] per-tier overrides (key, seedValue, description, …) |
| 307 | */ |
| 308 | export function buildTierItemDef (familyId, tier, itemId, extra = {}) |
| 309 | { |
| 310 | const family = familyDefinition(familyId); |
| 311 | if (!family) |
| 312 | { throw new Error(`Unknown item family: ${familyId}`); } |
| 313 | const name = family.axis === TierAxis.cloth |
| 314 | ? (CLOTH_DISPLAY_NAME[tier] ?? tier) |
| 315 | : (MATERIAL_DISPLAY_NAME[tier] ?? tier); |
| 316 | const key = extra.key ?? `${tier}-${family.label.replace(/ /g, "-")}`; |
| 317 | const seedValue = extra.seedValue ?? ( |
| 318 | typeof family.seedValue === "function" ? family.seedValue(tier) : family.seedValue |
| 319 | ); |
| 320 | const description = extra.description ?? family.description(name); |
| 321 | const equipment = extra.equipment ?? buildFamilyEquipment(family, tier); |
| 322 | return Object.freeze({ |
| 323 | itemId, |
| 324 | familyId, |
| 325 | key, |
| 326 | displayName: extra.displayName ?? `${name} ${family.label}`, |
| 327 | description, |
| 328 | seedValue, |
| 329 | stackable: extra.stackable ?? family.stackable, |
| 330 | maxStackSize: extra.maxStackSize ?? family.maxStackSize, |
| 331 | uniqueInstance: false, |
| 332 | category: extra.category ?? family.category, |
| 333 | iconKey: extra.iconKey ?? family.iconKey, |
| 334 | defaultState: extra.defaultState ?? family.defaultState, |
| 335 | tier, |
| 336 | ...(family.tintFromTier ? { tintFromTier: true } : {}), |
| 337 | ...(family.npcShopAllowed ? { npcShopAllowed: true } : {}), |
| 338 | ...(family.woodTierFromMetal ? { woodTier: metalTierForWood(tier), dualTintFromTier: true } : {}), |
| 339 | ...(family.dualTintFromTier && !family.woodTierFromMetal ? { dualTintFromTier: true } : {}), |
| 340 | ...(family.clothTierFromWood ? { clothTier: clothTierForWood(tier) } : {}), |
| 341 | ...(equipment ? { equipment } : {}) |
| 342 | }); |
| 343 | } |
| 344 | |
| 345 | /** Build `{ [itemId]: def }` from a tier → itemId map. */ |
| 346 | export function buildFamilyDefinitionMap (familyId, tierToItemId, extraByTier = {}) |
| 347 | { |
| 348 | const defs = {}; |
| 349 | for (const [tier, id] of Object.entries(tierToItemId)) |
| 350 | { |
| 351 | const itemId = id | 0; |
| 352 | if (!itemId) |
| 353 | { continue; } |
| 354 | defs[itemId] = buildTierItemDef(familyId, tier, itemId, extraByTier[tier] ?? {}); |
| 355 | } |
| 356 | return defs; |
| 357 | } |
| 358 | |
| 359 | /** Resolve itemId for a family tier from a lookup table. */ |
| 360 | export function itemIdForFamilyTier (familyTierMap, tier) |
| 361 | { |
| 362 | return familyTierMap[tier] ?? null; |
| 363 | } |
| 364 | |
| 365 | /** Reverse lookup: tier for an item id within a family map, or null. */ |
| 366 | export function tierForFamilyItemId (familyTierMap, itemId) |
| 367 | { |
| 368 | const want = itemId | 0; |
| 369 | for (const [tier, id] of Object.entries(familyTierMap)) |
| 370 | { |
| 371 | if ((id | 0) === want) |
| 372 | { return tier; } |
| 373 | } |
| 374 | return null; |
| 375 | } |