import { ItemId, itemDefinition } from "./items.js"; import { CraftingItemId } from "./craftingItems.js"; import { MetalItemId } from "./metalItems.js"; import { logItemIdForWood } from "./woodItems.js"; import { EquipmentKind } from "./equipmentKinds.js"; import { canEquipItem } from "../equipment/stats.js"; import { SkillId, skillLevel } from "./skills.js"; import { MaterialTier, GatherMaterialId, WoodTier, ORE_BEARING_MATERIALS, associatedBarTier, tierMineSkillLevel, tierSmeltSkillLevel, tierRank, miningDurationMs, oreExhaustDurationMs, smeltingDurationMs, SMELT_DURATION_PER_RANK_MS, EXHAUSTED_ORE_BOULDER_STATE, oreBoulderState, materialFromOreBoulderState, isOreBoulderState, isGemstoneMaterial, metalTierForCloth, MATERIAL_DISPLAY_NAME } from "./materialTiers.js"; import { gemItemId } from "./gemItems.js"; import { herbDefinition, herbGatherSkillLevel } from "./herbs.js"; export { ORE_BEARING_MATERIALS, associatedBarTier, tierMineSkillLevel, tierSmeltSkillLevel, miningDurationMs, oreExhaustDurationMs, smeltingDurationMs, SMELT_DURATION_PER_RANK_MS, EXHAUSTED_ORE_BOULDER_STATE, oreBoulderState, materialFromOreBoulderState, isOreBoulderState, isGemstoneMaterial, EquipmentKind, WoodTier }; /** Same curve as mining: (rank×8s) − (toolRank×3s), min 0.5s. */ export const woodcuttingDurationMs = miningDurationMs; /** Same curve as ore: 10s × wood tier rank (before seasonal regrow scaling). */ export const treeExhaustDurationMs = oreExhaustDurationMs; /** Ore item produced when mining a material node. */ export const ORE_ITEM_BY_MATERIAL = Object.freeze({ [GatherMaterialId.tin]: CraftingItemId.tinOre, [GatherMaterialId.copper]: CraftingItemId.copperOre, [MaterialTier.iron]: MetalItemId.ironOre, [GatherMaterialId.silver]: MetalItemId.silverOre, [GatherMaterialId.gold]: MetalItemId.goldOre, [MaterialTier.mithril]: MetalItemId.mithrilOre, [MaterialTier.adamantium]: MetalItemId.adamantiumOre, [MaterialTier.orichalcum]: MetalItemId.orichalcumOre, [MaterialTier.dragon]: MetalItemId.dragonOre, [GatherMaterialId.coal]: MetalItemId.coal, [GatherMaterialId.salt]: CraftingItemId.salt, [GatherMaterialId.tourmaline]: gemItemId(GatherMaterialId.tourmaline), [GatherMaterialId.alexandrite]: gemItemId(GatherMaterialId.alexandrite), [GatherMaterialId.opal]: gemItemId(GatherMaterialId.opal), [GatherMaterialId.diamond]: gemItemId(GatherMaterialId.diamond), [GatherMaterialId.emerald]: gemItemId(GatherMaterialId.emerald), [GatherMaterialId.ruby]: gemItemId(GatherMaterialId.ruby), [GatherMaterialId.jadeite]: gemItemId(GatherMaterialId.jadeite) }); export const BAR_ITEM_BY_MATERIAL = Object.freeze({ [MaterialTier.bronze]: CraftingItemId.bronzeBar, [MaterialTier.iron]: MetalItemId.ironBar, [MaterialTier.steel]: MetalItemId.steelBar, [GatherMaterialId.silver]: MetalItemId.silverBar, [GatherMaterialId.gold]: MetalItemId.goldBar, [GatherMaterialId.electrum]: MetalItemId.electrumBar, [MaterialTier.mithril]: MetalItemId.mithrilBar, [MaterialTier.adamantium]: MetalItemId.adamantiumBar, [MaterialTier.orichalcum]: MetalItemId.orichalcumBar, [MaterialTier.dragon]: MetalItemId.dragonBar }); export function oreItemIdForMaterial (materialId) { return ORE_ITEM_BY_MATERIAL[materialId] ?? null; } export function barItemIdForMaterial (materialId) { return BAR_ITEM_BY_MATERIAL[materialId] ?? null; } export function logItemIdForWoodTier (woodTier) { return logItemIdForWood(woodTier); } /** Mining XP awarded per successful ore (scales with associated bar rank). */ export function miningXpForMaterial (materialId) { return Math.max(1, tierRank(materialId) * 5); } /** Woodcutting XP awarded per successful chop (scales with wood tier rank). */ export function woodcuttingXpForWood (woodTier) { return Math.max(1, tierRank(woodTier) * 5); } /** Crafting XP for stripping bark from a log (half of wood tier rank, rounded up). */ export function woodworkingXpForDebark (woodTier) { return Math.max(1, Math.ceil(tierRank(woodTier) / 2)); } /** Smithing XP awarded per successful smelt (scales with bar rank). */ export function smeltingXpForMaterial (materialId) { return Math.max(1, tierRank(materialId) * 5); } /** XP for one consumed craft ingredient: its material tier rank (untiered = 1). */ export function itemCraftXpValue (itemId) { const def = itemDefinition(itemId); const rawTier = def?.tier ?? def?.equipment?.tier ?? null; if (typeof rawTier === "number" && Number.isFinite(rawTier)) { return Math.max(1, Math.floor(rawTier)); } return Math.max(1, tierRank(rawTier)); } /** Craft XP multiplier per skill (base XP is tier rank per consumed input). */ export const CRAFT_XP_MULTIPLIER_BY_SKILL = Object.freeze({ [SkillId.smithing]: 3, [SkillId.jewelering]: 3, [SkillId.crafting]: 3, [SkillId.woodworking]: 3, [SkillId.leatherworking]: 3, [SkillId.tailoring]: 3, [SkillId.cooking]: 3, [SkillId.alchemy]: 2 }); export function craftXpMultiplierForSkill (skillId) { return CRAFT_XP_MULTIPLIER_BY_SKILL[skillId] ?? 1; } /** Sum of `itemCraftXpValue × quantity` over recipe inputs, scaled by skill. */ export function craftXpForConsumedInputs (inputs, skillId = null) { let total = 0; for (const input of inputs ?? []) { const quantity = Math.max(0, input?.quantity | 0); if (!quantity) { continue; } total += itemCraftXpValue(input.itemId) * quantity; } if (!skillId) { return total; } return total * craftXpMultiplierForSkill(skillId); } /** Mine skill / tool gates use the associated bar's equip tier. */ export function gatherEquipTier (materialId) { return associatedBarTier(materialId); } export function canChopWood (woodcuttingLevel, toolTier, woodTier) { const needSkill = tierMineSkillLevel(woodTier); if ((woodcuttingLevel | 0) < needSkill) { return { ok: false, reason: `Requires Woodcutting ${needSkill}.` }; } if (!toolTierMeetsResource(toolTier, woodTier)) { return { ok: false, reason: "You need an axe to chop this!" }; } return { ok: true }; } /** * Gathering tools may be one material tier below the resource node. * Pine/bronze (rank 1) still need a bronze tool; birch accepts bronze or iron, etc. * Gemstones are stricter: pickaxe tier must be equal or higher. */ export function toolTierMeetsResource (toolTier, resourceTier) { return tierRank(toolTier) + 1 >= tierRank(resourceTier); } export function toolTierMeetsGemstone (toolTier, gemTier) { return tierRank(toolTier) >= tierRank(gemTier); } export function canMineMaterial (miningLevel, toolTier, materialId) { const gateTier = gatherEquipTier(materialId); const needSkill = tierMineSkillLevel(gateTier); if ((miningLevel | 0) < needSkill) { return { ok: false, reason: `Requires Mining ${needSkill}.` }; } const toolOk = isGemstoneMaterial(materialId) ? toolTierMeetsGemstone(toolTier, gateTier) : toolTierMeetsResource(toolTier, gateTier); if (!toolOk) { return { ok: false, reason: "You need a pickaxe to mine this!" }; } return { ok: true }; } /** * Tools of a given equipment kind in bag slots and/or the weapon slot. * Gathering uses inventory tools without requiring them to be equipped. */ export function listGatherTools (inventorySlots, equipmentSlots, kind) { const tools = []; for (const stack of inventorySlots ?? []) { if (!stack?.itemId) { continue; } const def = itemDefinition(stack.itemId); if (def?.equipment?.kind !== kind) { continue; } tools.push({ stack, def, tier: def.equipment.tier, source: "inventory" }); } const weapon = equipmentSlots?.weapon; if (weapon?.itemId) { const def = itemDefinition(weapon.itemId); if (def?.equipment?.kind === kind) { tools.push({ stack: weapon, def, tier: def.equipment.tier, source: "equipment" }); } } return tools; } /** * Best pickaxe the player can use for this ore (highest tier that meets gates). * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} */ export function resolveMiningPickaxe ({ inventorySlots, equipmentSlots, skills, materialId }) { const miningLevel = skillLevel(skills, SkillId.mining); const gateTier = gatherEquipTier(materialId); const needSkill = tierMineSkillLevel(gateTier); if ((miningLevel | 0) < needSkill) { return { ok: false, reason: `Requires Mining ${needSkill}.` }; } const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.pickaxeTool); const meetTool = isGemstoneMaterial(materialId) ? (tool) => toolTierMeetsGemstone(tool.tier, gateTier) : (tool) => toolTierMeetsResource(tool.tier, gateTier); const usable = tools .filter((tool) => canEquipItem(skills, tool.def).ok) .filter(meetTool) .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); if (!usable.length) { return { ok: false, reason: "You need a pickaxe to mine this!" }; } return { ok: true, tool: usable[0] }; } /** * Best axe the player can use for this wood tier. * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} */ export function resolveWoodcuttingAxe ({ inventorySlots, equipmentSlots, skills, woodTier }) { const woodcuttingLevel = skillLevel(skills, SkillId.woodcutting); const needSkill = tierMineSkillLevel(woodTier); if ((woodcuttingLevel | 0) < needSkill) { return { ok: false, reason: `Requires Woodcutting ${needSkill}.` }; } const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.axeTool); const usable = tools .filter((tool) => canEquipItem(skills, tool.def).ok) .filter((tool) => toolTierMeetsResource(tool.tier, woodTier)) .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); if (!usable.length) { return { ok: false, reason: "You need an axe to chop this!" }; } return { ok: true, tool: usable[0] }; } /** * Best fishing tool (net or rod) the player can use. * Prefers highest-tier usable rod; falls back to net. * @returns {{ ok: true, tool: object, kind: string } | { ok: false, reason: string }} */ export function resolveFishingTool ({ inventorySlots, equipmentSlots, skills }) { const fishingLevel = skillLevel(skills, SkillId.fishing); const rods = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.fishingRod) .filter((tool) => canEquipItem(skills, tool.def).ok) .filter((tool) => (fishingLevel | 0) >= tierMineSkillLevel(tool.tier)) .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); if (rods.length) { return { ok: true, tool: rods[0], kind: EquipmentKind.fishingRod }; } const nets = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.fishingNet) .filter((tool) => canEquipItem(skills, tool.def).ok); if (nets.length) { return { ok: true, tool: nets[0], kind: EquipmentKind.fishingNet }; } return { ok: false, reason: "You need a fishing net or rod." }; } /** * Best plant clippers for herbalism nodes. * @param {{ inventorySlots: object[], equipmentSlots: object, skills: object, requireTool?: boolean, minClothTier?: string|null }} args * @returns {{ ok: true, tool: object|null } | { ok: false, reason: string }} */ export function resolvePlantClippers ({ inventorySlots, equipmentSlots, skills, requireTool = true, minClothTier = null }) { if (!requireTool) { return { ok: true, tool: null }; } const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.plantClippers) .filter((tool) => canEquipItem(skills, tool.def).ok) .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); if (!tools.length) { return { ok: false, reason: "You need plant clippers to gather this!" }; } if (minClothTier) { const needMetal = metalTierForCloth(minClothTier); const usable = tools.filter((tool) => tierRank(tool.tier) >= tierRank(needMetal)); if (!usable.length) { return { ok: false, reason: `You need ${MATERIAL_DISPLAY_NAME[needMetal] ?? needMetal} plant clippers or better.` }; } return { ok: true, tool: usable[0] }; } return { ok: true, tool: tools[0] }; } /** * Best shears for clipping fleece (inventory or weapon slot). * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} */ export function resolveShears ({ inventorySlots, equipmentSlots, skills, clothTier }) { const needMetal = metalTierForCloth(clothTier); const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.shears) .filter((tool) => canEquipItem(skills, tool.def).ok) .filter((tool) => tierRank(tool.tier) >= tierRank(needMetal)) .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); if (!tools.length) { return { ok: false, reason: `You need ${MATERIAL_DISPLAY_NAME[needMetal] ?? needMetal} shears or better.` }; } return { ok: true, tool: tools[0] }; } /** * Skill gate for folklore herb nodes (`BlockType.herbPlant`, state = herbId). * @returns {{ ok: true, herb: object } | { ok: false, reason: string }} */ export function resolveHerbGather ({ herbId, skills }) { const herb = herbDefinition(herbId); if (!herb) { return { ok: false, reason: "There is nothing to pick there." }; } const need = herbGatherSkillLevel(herb.tier); const level = skillLevel(skills, SkillId.herbalism); if (level < need) { return { ok: false, reason: `Requires Herbalism ${need}.` }; } return { ok: true, herb }; } /** Herbalism gather duration. Flowers are quick (~1s); cotton a bit longer; clipper herbs mid. */ export function herbalismDurationMs (plantKind = null, toolTier = null, herbTier = null) { if (plantKind === "flower") { return 1000; } if (plantKind === "herb") { const tier = Math.max(1, herbTier | 0 || 1); const rank = toolTier ? tierRank(toolTier) : 1; return Math.max(800, 1600 + tier * 250 - (rank - 1) * 150); } if (plantKind === "flax") { const rank = toolTier ? tierRank(toolTier) : 1; return Math.max(800, 2200 - (rank - 1) * 200); } if (plantKind === "silk") { const rank = toolTier ? tierRank(toolTier) : 1; return Math.max(900, 2800 - (rank - 1) * 200); } const rank = toolTier ? tierRank(toolTier) : 1; return Math.max(500, 2500 - (rank - 1) * 250); } export function herbalismXpForPick (kind, herbTier = null) { if (kind === "flower") { return 4; } if (kind === "herb") { return 4 + Math.max(1, herbTier | 0 || 1) * 2; } if (kind === "flax") { return 5; } if (kind === "silk") { return 8; } return 3; } /** Plant node exhaust then respawn (ms), before seasonal regrow scaling. */ export const PLANT_EXHAUST_MS = 15_000; /** Craft duration: furnace recipes scale by bar tier; others use the fixed craft duration. */ export function craftDurationMs (recipe, fallbackMs = 2000) { if (recipe?.xpMaterial) { return smeltingDurationMs(recipe.xpMaterial); } return Math.max(1, fallbackMs | 0 || 2000); } /** Re-export ItemId helpers used by recipes. */ export { ItemId, MaterialTier, GatherMaterialId };