import { ItemCategory } from "./itemCategories.js"; import { WoodTier, MATERIAL_DISPLAY_NAME, WOOD_TIER_ORDER, MATERIAL_TIER_ORDER, CLOTH_TIER_ORDER, CLOTH_DISPLAY_NAME, woodTierForMetal, clothTierForWood, leatherTierForMetal } from "./materialTiers.js"; import { EquipmentKind } from "./equipmentKinds.js"; import { AttackType } from "./attackTypes.js"; import { SkillId } from "./skills.js"; import { COMBAT_RANGE_TILES } from "../spells/constants.js"; /** Which tier list drives a family (`wood` → pine…elder, `metal` → bronze…dragon). */ export const TierAxis = Object.freeze({ wood: "wood", metal: "metal", cloth: "cloth" }); /** Stable family ids — one template per intermediate / gather product shape. */ export const ItemFamilyId = Object.freeze({ log: "log", handle: "handle", ore: "ore", bar: "bar", pickaxeHead: "pickaxeHead", axeHead: "axeHead", pickaxe: "pickaxe", axe: "axe", warAxeHead: "warAxeHead", warAxe: "warAxe", swordBlade: "swordBlade", sword: "sword", plantClippers: "plantClippers", shears: "shears", arrowShaft: "arrowShaft", arrowTip: "arrowTip", arrow: "arrow", unstrungBow: "unstrungBow", bow: "bow", bowstring: "bowstring", quarterstaff: "quarterstaff" }); const DEFAULT_STACK = 99; /** * Family templates: display suffix, sprite, stacking, and tier axis. * Per-tier itemIds stay in woodItems / metalItems / rangedItems; defs are built here. */ export const ITEM_FAMILIES = Object.freeze({ [ItemFamilyId.log]: Object.freeze({ familyId: ItemFamilyId.log, axis: TierAxis.wood, label: "log", description: (name) => `A sturdy ${name.toLowerCase()} log.`, category: ItemCategory.ingredient, iconKey: "wood", defaultState: "log", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: (tier) => (tier === WoodTier.pine ? 5 : 2), tintFromTier: true, npcShopAllowed: true }), [ItemFamilyId.handle]: Object.freeze({ familyId: ItemFamilyId.handle, axis: TierAxis.wood, label: "handle", description: (name) => `Shaped ${name.toLowerCase()} haft. Combine with a matching toolhead.`, category: ItemCategory.intermediate, iconKey: "wood", defaultState: "handle", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 3, tintFromTier: true }), [ItemFamilyId.ore]: Object.freeze({ familyId: ItemFamilyId.ore, axis: TierAxis.metal, label: "ore", description: (name) => `Mined ${name.toLowerCase()} ore. Smelt at a furnace.`, category: ItemCategory.ingredient, iconKey: "metal", defaultState: "ore", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 2, tintFromTier: true }), [ItemFamilyId.bar]: Object.freeze({ familyId: ItemFamilyId.bar, axis: TierAxis.metal, label: "bar", description: (name) => `Smelted ${name.toLowerCase()}. Smith at an anvil.`, category: ItemCategory.intermediate, iconKey: "metal", defaultState: "bar", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 4, tintFromTier: true }), [ItemFamilyId.pickaxeHead]: Object.freeze({ familyId: ItemFamilyId.pickaxeHead, axis: TierAxis.metal, label: "pickaxe head", description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`, category: ItemCategory.intermediate, iconKey: "pickaxe", defaultState: "pickaxe-head", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 5, tintFromTier: true }), [ItemFamilyId.axeHead]: Object.freeze({ familyId: ItemFamilyId.axeHead, axis: TierAxis.metal, label: "axe head", description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`, category: ItemCategory.intermediate, iconKey: "axe", defaultState: "axe-head", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 5, tintFromTier: true }), [ItemFamilyId.arrowShaft]: Object.freeze({ familyId: ItemFamilyId.arrowShaft, axis: TierAxis.wood, label: "arrow shaft", description: () => "A straight wooden shaft, ready for fletching.", category: ItemCategory.intermediate, iconKey: "arrow", defaultState: "shaft", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 1, tintFromTier: true }), [ItemFamilyId.arrowTip]: Object.freeze({ familyId: ItemFamilyId.arrowTip, axis: TierAxis.metal, label: "arrow tip", description: (name) => `A smithed ${name.toLowerCase()} point for arrows.`, category: ItemCategory.intermediate, iconKey: "arrow", defaultState: "tip", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 1, tintFromTier: true }), [ItemFamilyId.pickaxe]: Object.freeze({ familyId: ItemFamilyId.pickaxe, axis: TierAxis.metal, label: "pickaxe", description: (name) => `A ${name.toLowerCase()} pickaxe for breaking stone and ore.`, category: ItemCategory.tool, iconKey: "pickaxe", defaultState: "pickaxe", stackable: false, maxStackSize: 1, seedValue: 8, dualTintFromTier: true, woodTierFromMetal: true, equipmentKind: EquipmentKind.pickaxeTool }), [ItemFamilyId.axe]: Object.freeze({ familyId: ItemFamilyId.axe, axis: TierAxis.metal, label: "axe", description: (name) => `A ${name.toLowerCase()} axe for felling trees.`, category: ItemCategory.tool, iconKey: "axe", defaultState: "axe", stackable: false, maxStackSize: 1, seedValue: 8, dualTintFromTier: true, woodTierFromMetal: true, equipmentKind: EquipmentKind.axeTool }), [ItemFamilyId.warAxeHead]: Object.freeze({ familyId: ItemFamilyId.warAxeHead, axis: TierAxis.metal, label: "war axe head", description: (name) => `A heavy ${name.toLowerCase()} axe head, meant for battle rather than timber.`, category: ItemCategory.intermediate, iconKey: "waraxe", defaultState: "waraxe-head", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 8, tintFromTier: true }), [ItemFamilyId.warAxe]: Object.freeze({ familyId: ItemFamilyId.warAxe, axis: TierAxis.metal, label: "war axe", description: (name) => `A ${name.toLowerCase()} war axe, hafted for the press of combat.`, category: ItemCategory.tool, iconKey: "waraxe", defaultState: "waraxe", stackable: false, maxStackSize: 1, seedValue: 12, /** Red = wood haft, blue = metal head. */ dualTintFromTier: true, woodTierFromMetal: true, equipmentKind: EquipmentKind.metalWeapon, equipmentAttack: Object.freeze({ mode: "melee", rangeTiles: 1, hitSkillId: SkillId.axefighting, damageSkillId: SkillId.strength, attackType: AttackType.chopping, cooldownMs: 800 }) }), [ItemFamilyId.plantClippers]: Object.freeze({ familyId: ItemFamilyId.plantClippers, axis: TierAxis.metal, label: "plant clippers", description: () => "Sharp clippers for cutting herbs and blooms.", category: ItemCategory.tool, iconKey: "plant-clippers", defaultState: "plant-clippers", stackable: false, maxStackSize: 1, seedValue: 10, /** Icon: red = metal blades, blue = leather grips. Equip art is magenta only (grips hidden in hand). */ dualTintFromTier: true, leatherTierFromMetal: true, equipmentTintFromTier: true, npcShopAllowed: true, equipmentKind: EquipmentKind.plantClippers }), [ItemFamilyId.shears]: Object.freeze({ familyId: ItemFamilyId.shears, axis: TierAxis.metal, label: "shears", description: () => "All-metal shears for clipping fleece from animals.", category: ItemCategory.tool, iconKey: "shears", defaultState: "shears", stackable: false, maxStackSize: 1, seedValue: 10, tintFromTier: true, npcShopAllowed: true, equipmentKind: EquipmentKind.shears, equipmentTintFromTier: true }), [ItemFamilyId.arrow]: Object.freeze({ familyId: ItemFamilyId.arrow, axis: TierAxis.metal, label: "arrow", description: () => "A fletched arrow with matching shaft and head.", category: ItemCategory.finished, iconKey: "arrow", defaultState: "full-arrow", stackable: true, maxStackSize: 999, seedValue: 2, dualTintFromTier: true, woodTierFromMetal: true, equipmentKind: EquipmentKind.arrowAmmo, equipmentSlot: "offhand", hideEquipSprite: true }), [ItemFamilyId.unstrungBow]: Object.freeze({ familyId: ItemFamilyId.unstrungBow, axis: TierAxis.wood, label: "unstrung bow", description: () => "A carved bow frame awaiting a string.", category: ItemCategory.intermediate, iconKey: "bow", defaultState: "unstrung-bow", stackable: false, maxStackSize: 1, seedValue: 6, /** Red = wood; blue unused (same dual shader as bows/arrows). */ dualTintFromTier: true }), [ItemFamilyId.bow]: Object.freeze({ familyId: ItemFamilyId.bow, axis: TierAxis.wood, label: "bow", description: (name) => `A ${name.toLowerCase()} bow strung with matching yarn.`, category: ItemCategory.finished, iconKey: "bow", defaultState: "bow", stackable: false, maxStackSize: 1, seedValue: 12, /** Red = wood, blue = string (cloth tier). */ dualTintFromTier: true, clothTierFromWood: true, equipmentKind: EquipmentKind.bow, equipmentAttack: Object.freeze({ mode: "ranged", rangeTiles: COMBAT_RANGE_TILES, hitSkillId: SkillId.archery, damageSkillId: SkillId.dexterity, attackType: AttackType.piercing, cooldownMs: 1200 }) }), [ItemFamilyId.bowstring]: Object.freeze({ familyId: ItemFamilyId.bowstring, axis: TierAxis.cloth, label: "bowstring", description: () => "Twisted yarn spun tight for a bow.", category: ItemCategory.intermediate, iconKey: "bow", defaultState: "bowstring", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 2, tintFromTier: true }), [ItemFamilyId.quarterstaff]: Object.freeze({ familyId: ItemFamilyId.quarterstaff, axis: TierAxis.wood, label: "quarterstaff", description: (name) => `A ${name.toLowerCase()} quarterstaff, long enough to keep foes at a distance.`, category: ItemCategory.finished, iconKey: "quarterstaff", defaultState: "quarterstaff", stackable: false, maxStackSize: 1, seedValue: 8, tintFromTier: true, equipmentKind: EquipmentKind.clubWeapon, equipmentSlot: "weapon", equipmentTintFromTier: true, equipmentAttack: Object.freeze({ mode: "melee", rangeTiles: 1, hitSkillId: SkillId.clubfighting, damageSkillId: SkillId.strength, attackType: AttackType.crushing, cooldownMs: 700 }) }), [ItemFamilyId.swordBlade]: Object.freeze({ familyId: ItemFamilyId.swordBlade, axis: TierAxis.metal, label: "sword blade", description: (name) => `A smithed ${name.toLowerCase()} blade. Haft it with leather at a crafting bench.`, category: ItemCategory.intermediate, iconKey: "sword", defaultState: "sword-blade", stackable: true, maxStackSize: DEFAULT_STACK, seedValue: 6, tintFromTier: true }), [ItemFamilyId.sword]: Object.freeze({ familyId: ItemFamilyId.sword, axis: TierAxis.metal, label: "sword", description: (name) => `A ${name.toLowerCase()} sword, leather-wrapped for a sure grip.`, category: ItemCategory.tool, iconKey: "sword", defaultState: "sword", stackable: false, maxStackSize: 1, seedValue: 10, /** Red = metal, blue = leather wrap (wood haft hidden under the wrap). */ dualTintFromTier: true, leatherTierFromMetal: true, equipmentKind: EquipmentKind.metalWeapon, equipmentAttack: Object.freeze({ mode: "melee", rangeTiles: 1, hitSkillId: SkillId.swordfighting, damageSkillId: SkillId.strength, attackType: AttackType.slashing, cooldownMs: 700 }) }) }); export function tierOrderForAxis (axis) { if (axis === TierAxis.wood) { return WOOD_TIER_ORDER; } if (axis === TierAxis.metal) { return MATERIAL_TIER_ORDER; } if (axis === TierAxis.cloth) { return CLOTH_TIER_ORDER; } return []; } function buildFamilyEquipment (family, tier) { if (!family.equipmentKind) { return null; } const equipment = { slot: family.equipmentSlot ?? "weapon", tier, kind: family.equipmentKind }; if (family.equipmentTintFromTier) { equipment.tintFromTier = true; } if (family.hideEquipSprite) { equipment.hideEquipSprite = true; } if (family.equipmentAttack) { equipment.attack = family.equipmentAttack; } return Object.freeze(equipment); } export function familyDefinition (familyId) { return ITEM_FAMILIES[familyId] ?? null; } /** * Build one tiered item definition from a family template. * @param {string} familyId * @param {string} tier * @param {number} itemId stable numeric id (inventory / saves) * @param {object} [extra] per-tier overrides (key, seedValue, description, …) */ export function buildTierItemDef (familyId, tier, itemId, extra = {}) { const family = familyDefinition(familyId); if (!family) { throw new Error(`Unknown item family: ${familyId}`); } const name = family.axis === TierAxis.cloth ? (CLOTH_DISPLAY_NAME[tier] ?? tier) : (MATERIAL_DISPLAY_NAME[tier] ?? tier); const key = extra.key ?? `${tier}-${family.label.replace(/ /g, "-")}`; const seedValue = extra.seedValue ?? ( typeof family.seedValue === "function" ? family.seedValue(tier) : family.seedValue ); const description = extra.description ?? family.description(name); const equipment = extra.equipment ?? buildFamilyEquipment(family, tier); return Object.freeze({ itemId, familyId, key, displayName: extra.displayName ?? `${name} ${family.label}`, description, seedValue, stackable: extra.stackable ?? family.stackable, maxStackSize: extra.maxStackSize ?? family.maxStackSize, uniqueInstance: false, category: extra.category ?? family.category, iconKey: extra.iconKey ?? family.iconKey, defaultState: extra.defaultState ?? family.defaultState, tier, ...(family.tintFromTier ? { tintFromTier: true } : {}), ...(family.npcShopAllowed ? { npcShopAllowed: true } : {}), ...(family.woodTierFromMetal ? { woodTier: woodTierForMetal(tier), dualTintFromTier: true } : {}), ...(family.leatherTierFromMetal ? { leatherTier: leatherTierForMetal(tier), dualTintFromTier: true } : {}), ...(family.dualTintFromTier && !family.woodTierFromMetal && !family.leatherTierFromMetal ? { dualTintFromTier: true } : {}), ...(family.clothTierFromWood ? { clothTier: clothTierForWood(tier) } : {}), ...(equipment ? { equipment } : {}) }); } /** Build `{ [itemId]: def }` from a tier → itemId map. */ export function buildFamilyDefinitionMap (familyId, tierToItemId, extraByTier = {}) { const defs = {}; for (const [tier, id] of Object.entries(tierToItemId)) { const itemId = id | 0; if (!itemId) { continue; } defs[itemId] = buildTierItemDef(familyId, tier, itemId, extraByTier[tier] ?? {}); } return defs; } /** Resolve itemId for a family tier from a lookup table. */ export function itemIdForFamilyTier (familyTierMap, tier) { return familyTierMap[tier] ?? null; } /** Reverse lookup: tier for an item id within a family map, or null. */ export function tierForFamilyItemId (familyTierMap, itemId) { const want = itemId | 0; for (const [tier, id] of Object.entries(familyTierMap)) { if ((id | 0) === want) { return tier; } } return null; }