| 1 | import { ItemId, itemDefinition } from "./items.js"; |
| 2 | import { CraftingItemId } from "./craftingItems.js"; |
| 3 | import { MetalItemId } from "./metalItems.js"; |
| 4 | import { logItemIdForWood } from "./woodItems.js"; |
| 5 | import { EquipmentKind } from "./equipmentKinds.js"; |
| 6 | import { canEquipItem } from "../equipment/stats.js"; |
| 7 | import { SkillId, skillLevel } from "./skills.js"; |
| 8 | import |
| 9 | { |
| 10 | MaterialTier, |
| 11 | GatherMaterialId, |
| 12 | WoodTier, |
| 13 | ORE_BEARING_MATERIALS, |
| 14 | associatedBarTier, |
| 15 | tierMineSkillLevel, |
| 16 | tierSmeltSkillLevel, |
| 17 | tierRank, |
| 18 | miningDurationMs, |
| 19 | oreExhaustDurationMs, |
| 20 | smeltingDurationMs, |
| 21 | SMELT_DURATION_PER_RANK_MS, |
| 22 | EXHAUSTED_ORE_BOULDER_STATE, |
| 23 | oreBoulderState, |
| 24 | materialFromOreBoulderState, |
| 25 | isOreBoulderState, |
| 26 | isGemstoneMaterial, |
| 27 | metalTierForCloth, |
| 28 | MATERIAL_DISPLAY_NAME |
| 29 | } from "./materialTiers.js"; |
| 30 | import { gemItemId } from "./gemItems.js"; |
| 31 | import { herbDefinition, herbGatherSkillLevel } from "./herbs.js"; |
| 32 | |
| 33 | export |
| 34 | { |
| 35 | ORE_BEARING_MATERIALS, |
| 36 | associatedBarTier, |
| 37 | tierMineSkillLevel, |
| 38 | tierSmeltSkillLevel, |
| 39 | miningDurationMs, |
| 40 | oreExhaustDurationMs, |
| 41 | smeltingDurationMs, |
| 42 | SMELT_DURATION_PER_RANK_MS, |
| 43 | EXHAUSTED_ORE_BOULDER_STATE, |
| 44 | oreBoulderState, |
| 45 | materialFromOreBoulderState, |
| 46 | isOreBoulderState, |
| 47 | isGemstoneMaterial, |
| 48 | EquipmentKind, |
| 49 | WoodTier |
| 50 | }; |
| 51 | |
| 52 | /** Same curve as mining: (rank×8s) − (toolRank×3s), min 0.5s. */ |
| 53 | export const woodcuttingDurationMs = miningDurationMs; |
| 54 | |
| 55 | /** Same curve as ore: 10s × wood tier rank (before seasonal regrow scaling). */ |
| 56 | export const treeExhaustDurationMs = oreExhaustDurationMs; |
| 57 | |
| 58 | /** Ore item produced when mining a material node. */ |
| 59 | export const ORE_ITEM_BY_MATERIAL = Object.freeze({ |
| 60 | [GatherMaterialId.tin]: CraftingItemId.tinOre, |
| 61 | [GatherMaterialId.copper]: CraftingItemId.copperOre, |
| 62 | [MaterialTier.iron]: MetalItemId.ironOre, |
| 63 | [GatherMaterialId.silver]: MetalItemId.silverOre, |
| 64 | [GatherMaterialId.gold]: MetalItemId.goldOre, |
| 65 | [MaterialTier.mithril]: MetalItemId.mithrilOre, |
| 66 | [MaterialTier.adamantium]: MetalItemId.adamantiumOre, |
| 67 | [MaterialTier.orichalcum]: MetalItemId.orichalcumOre, |
| 68 | [MaterialTier.dragon]: MetalItemId.dragonOre, |
| 69 | [GatherMaterialId.coal]: MetalItemId.coal, |
| 70 | [GatherMaterialId.salt]: CraftingItemId.salt, |
| 71 | [GatherMaterialId.tourmaline]: gemItemId(GatherMaterialId.tourmaline), |
| 72 | [GatherMaterialId.alexandrite]: gemItemId(GatherMaterialId.alexandrite), |
| 73 | [GatherMaterialId.opal]: gemItemId(GatherMaterialId.opal), |
| 74 | [GatherMaterialId.diamond]: gemItemId(GatherMaterialId.diamond), |
| 75 | [GatherMaterialId.emerald]: gemItemId(GatherMaterialId.emerald), |
| 76 | [GatherMaterialId.ruby]: gemItemId(GatherMaterialId.ruby), |
| 77 | [GatherMaterialId.jadeite]: gemItemId(GatherMaterialId.jadeite) |
| 78 | }); |
| 79 | |
| 80 | export const BAR_ITEM_BY_MATERIAL = Object.freeze({ |
| 81 | [MaterialTier.bronze]: CraftingItemId.bronzeBar, |
| 82 | [MaterialTier.iron]: MetalItemId.ironBar, |
| 83 | [MaterialTier.steel]: MetalItemId.steelBar, |
| 84 | [GatherMaterialId.silver]: MetalItemId.silverBar, |
| 85 | [GatherMaterialId.gold]: MetalItemId.goldBar, |
| 86 | [GatherMaterialId.electrum]: MetalItemId.electrumBar, |
| 87 | [MaterialTier.mithril]: MetalItemId.mithrilBar, |
| 88 | [MaterialTier.adamantium]: MetalItemId.adamantiumBar, |
| 89 | [MaterialTier.orichalcum]: MetalItemId.orichalcumBar, |
| 90 | [MaterialTier.dragon]: MetalItemId.dragonBar |
| 91 | }); |
| 92 | |
| 93 | export function oreItemIdForMaterial (materialId) |
| 94 | { |
| 95 | return ORE_ITEM_BY_MATERIAL[materialId] ?? null; |
| 96 | } |
| 97 | |
| 98 | export function barItemIdForMaterial (materialId) |
| 99 | { |
| 100 | return BAR_ITEM_BY_MATERIAL[materialId] ?? null; |
| 101 | } |
| 102 | |
| 103 | export function logItemIdForWoodTier (woodTier) |
| 104 | { |
| 105 | return logItemIdForWood(woodTier); |
| 106 | } |
| 107 | |
| 108 | /** Mining XP awarded per successful ore (scales with associated bar rank). */ |
| 109 | export function miningXpForMaterial (materialId) |
| 110 | { |
| 111 | return Math.max(1, tierRank(materialId) * 5); |
| 112 | } |
| 113 | |
| 114 | /** Woodcutting XP awarded per successful chop (scales with wood tier rank). */ |
| 115 | export function woodcuttingXpForWood (woodTier) |
| 116 | { |
| 117 | return Math.max(1, tierRank(woodTier) * 5); |
| 118 | } |
| 119 | |
| 120 | /** Crafting XP for stripping bark from a log (half of wood tier rank, rounded up). */ |
| 121 | export function woodworkingXpForDebark (woodTier) |
| 122 | { |
| 123 | return Math.max(1, Math.ceil(tierRank(woodTier) / 2)); |
| 124 | } |
| 125 | |
| 126 | /** Smithing XP awarded per successful smelt (scales with bar rank). */ |
| 127 | export function smeltingXpForMaterial (materialId) |
| 128 | { |
| 129 | return Math.max(1, tierRank(materialId) * 5); |
| 130 | } |
| 131 | |
| 132 | /** XP for one consumed craft ingredient: its material tier rank (untiered = 1). */ |
| 133 | export function itemCraftXpValue (itemId) |
| 134 | { |
| 135 | const def = itemDefinition(itemId); |
| 136 | const rawTier = def?.tier ?? def?.equipment?.tier ?? null; |
| 137 | if (typeof rawTier === "number" && Number.isFinite(rawTier)) |
| 138 | { return Math.max(1, Math.floor(rawTier)); } |
| 139 | return Math.max(1, tierRank(rawTier)); |
| 140 | } |
| 141 | |
| 142 | /** Craft XP multiplier per skill (base XP is tier rank per consumed input). */ |
| 143 | export const CRAFT_XP_MULTIPLIER_BY_SKILL = Object.freeze({ |
| 144 | [SkillId.smithing]: 3, |
| 145 | [SkillId.jewelering]: 3, |
| 146 | [SkillId.crafting]: 3, |
| 147 | [SkillId.woodworking]: 3, |
| 148 | [SkillId.leatherworking]: 3, |
| 149 | [SkillId.tailoring]: 3, |
| 150 | [SkillId.cooking]: 3, |
| 151 | [SkillId.alchemy]: 2 |
| 152 | }); |
| 153 | |
| 154 | export function craftXpMultiplierForSkill (skillId) |
| 155 | { |
| 156 | return CRAFT_XP_MULTIPLIER_BY_SKILL[skillId] ?? 1; |
| 157 | } |
| 158 | |
| 159 | /** Sum of `itemCraftXpValue × quantity` over recipe inputs, scaled by skill. */ |
| 160 | export function craftXpForConsumedInputs (inputs, skillId = null) |
| 161 | { |
| 162 | let total = 0; |
| 163 | for (const input of inputs ?? []) |
| 164 | { |
| 165 | const quantity = Math.max(0, input?.quantity | 0); |
| 166 | if (!quantity) |
| 167 | { continue; } |
| 168 | total += itemCraftXpValue(input.itemId) * quantity; |
| 169 | } |
| 170 | if (!skillId) |
| 171 | { return total; } |
| 172 | return total * craftXpMultiplierForSkill(skillId); |
| 173 | } |
| 174 | |
| 175 | /** Mine skill / tool gates use the associated bar's equip tier. */ |
| 176 | export function gatherEquipTier (materialId) |
| 177 | { |
| 178 | return associatedBarTier(materialId); |
| 179 | } |
| 180 | |
| 181 | export function canChopWood (woodcuttingLevel, toolTier, woodTier) |
| 182 | { |
| 183 | const needSkill = tierMineSkillLevel(woodTier); |
| 184 | if ((woodcuttingLevel | 0) < needSkill) |
| 185 | { |
| 186 | return { ok: false, reason: `Requires Woodcutting ${needSkill}.` }; |
| 187 | } |
| 188 | if (!toolTierMeetsResource(toolTier, woodTier)) |
| 189 | { |
| 190 | return { ok: false, reason: "You need an axe to chop this!" }; |
| 191 | } |
| 192 | return { ok: true }; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Gathering tools may be one material tier below the resource node. |
| 197 | * Pine/bronze (rank 1) still need a bronze tool; birch accepts bronze or iron, etc. |
| 198 | * Gemstones are stricter: pickaxe tier must be equal or higher. |
| 199 | */ |
| 200 | export function toolTierMeetsResource (toolTier, resourceTier) |
| 201 | { |
| 202 | return tierRank(toolTier) + 1 >= tierRank(resourceTier); |
| 203 | } |
| 204 | |
| 205 | export function toolTierMeetsGemstone (toolTier, gemTier) |
| 206 | { |
| 207 | return tierRank(toolTier) >= tierRank(gemTier); |
| 208 | } |
| 209 | |
| 210 | export function canMineMaterial (miningLevel, toolTier, materialId) |
| 211 | { |
| 212 | const gateTier = gatherEquipTier(materialId); |
| 213 | const needSkill = tierMineSkillLevel(gateTier); |
| 214 | if ((miningLevel | 0) < needSkill) |
| 215 | { |
| 216 | return { ok: false, reason: `Requires Mining ${needSkill}.` }; |
| 217 | } |
| 218 | const toolOk = isGemstoneMaterial(materialId) |
| 219 | ? toolTierMeetsGemstone(toolTier, gateTier) |
| 220 | : toolTierMeetsResource(toolTier, gateTier); |
| 221 | if (!toolOk) |
| 222 | { |
| 223 | return { ok: false, reason: "You need a pickaxe to mine this!" }; |
| 224 | } |
| 225 | return { ok: true }; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tools of a given equipment kind in bag slots and/or the weapon slot. |
| 230 | * Gathering uses inventory tools without requiring them to be equipped. |
| 231 | */ |
| 232 | export function listGatherTools (inventorySlots, equipmentSlots, kind) |
| 233 | { |
| 234 | const tools = []; |
| 235 | for (const stack of inventorySlots ?? []) |
| 236 | { |
| 237 | if (!stack?.itemId) |
| 238 | { continue; } |
| 239 | const def = itemDefinition(stack.itemId); |
| 240 | if (def?.equipment?.kind !== kind) |
| 241 | { continue; } |
| 242 | tools.push({ stack, def, tier: def.equipment.tier, source: "inventory" }); |
| 243 | } |
| 244 | const weapon = equipmentSlots?.weapon; |
| 245 | if (weapon?.itemId) |
| 246 | { |
| 247 | const def = itemDefinition(weapon.itemId); |
| 248 | if (def?.equipment?.kind === kind) |
| 249 | { tools.push({ stack: weapon, def, tier: def.equipment.tier, source: "equipment" }); } |
| 250 | } |
| 251 | return tools; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Best pickaxe the player can use for this ore (highest tier that meets gates). |
| 256 | * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} |
| 257 | */ |
| 258 | export function resolveMiningPickaxe ({ inventorySlots, equipmentSlots, skills, materialId }) |
| 259 | { |
| 260 | const miningLevel = skillLevel(skills, SkillId.mining); |
| 261 | const gateTier = gatherEquipTier(materialId); |
| 262 | const needSkill = tierMineSkillLevel(gateTier); |
| 263 | if ((miningLevel | 0) < needSkill) |
| 264 | { return { ok: false, reason: `Requires Mining ${needSkill}.` }; } |
| 265 | |
| 266 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.pickaxeTool); |
| 267 | const meetTool = isGemstoneMaterial(materialId) |
| 268 | ? (tool) => toolTierMeetsGemstone(tool.tier, gateTier) |
| 269 | : (tool) => toolTierMeetsResource(tool.tier, gateTier); |
| 270 | const usable = tools |
| 271 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 272 | .filter(meetTool) |
| 273 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 274 | |
| 275 | if (!usable.length) |
| 276 | { return { ok: false, reason: "You need a pickaxe to mine this!" }; } |
| 277 | return { ok: true, tool: usable[0] }; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Best axe the player can use for this wood tier. |
| 282 | * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} |
| 283 | */ |
| 284 | export function resolveWoodcuttingAxe ({ inventorySlots, equipmentSlots, skills, woodTier }) |
| 285 | { |
| 286 | const woodcuttingLevel = skillLevel(skills, SkillId.woodcutting); |
| 287 | const needSkill = tierMineSkillLevel(woodTier); |
| 288 | if ((woodcuttingLevel | 0) < needSkill) |
| 289 | { return { ok: false, reason: `Requires Woodcutting ${needSkill}.` }; } |
| 290 | |
| 291 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.axeTool); |
| 292 | const usable = tools |
| 293 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 294 | .filter((tool) => toolTierMeetsResource(tool.tier, woodTier)) |
| 295 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 296 | |
| 297 | if (!usable.length) |
| 298 | { return { ok: false, reason: "You need an axe to chop this!" }; } |
| 299 | return { ok: true, tool: usable[0] }; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Best fishing tool (net or rod) the player can use. |
| 304 | * Prefers highest-tier usable rod; falls back to net. |
| 305 | * @returns {{ ok: true, tool: object, kind: string } | { ok: false, reason: string }} |
| 306 | */ |
| 307 | export function resolveFishingTool ({ inventorySlots, equipmentSlots, skills }) |
| 308 | { |
| 309 | const fishingLevel = skillLevel(skills, SkillId.fishing); |
| 310 | const rods = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.fishingRod) |
| 311 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 312 | .filter((tool) => (fishingLevel | 0) >= tierMineSkillLevel(tool.tier)) |
| 313 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 314 | if (rods.length) |
| 315 | { return { ok: true, tool: rods[0], kind: EquipmentKind.fishingRod }; } |
| 316 | |
| 317 | const nets = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.fishingNet) |
| 318 | .filter((tool) => canEquipItem(skills, tool.def).ok); |
| 319 | if (nets.length) |
| 320 | { return { ok: true, tool: nets[0], kind: EquipmentKind.fishingNet }; } |
| 321 | |
| 322 | return { ok: false, reason: "You need a fishing net or rod." }; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Best plant clippers for herbalism nodes. |
| 327 | * @param {{ inventorySlots: object[], equipmentSlots: object, skills: object, requireTool?: boolean, minClothTier?: string|null }} args |
| 328 | * @returns {{ ok: true, tool: object|null } | { ok: false, reason: string }} |
| 329 | */ |
| 330 | export function resolvePlantClippers ({ |
| 331 | inventorySlots, |
| 332 | equipmentSlots, |
| 333 | skills, |
| 334 | requireTool = true, |
| 335 | minClothTier = null |
| 336 | }) |
| 337 | { |
| 338 | if (!requireTool) |
| 339 | { return { ok: true, tool: null }; } |
| 340 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.plantClippers) |
| 341 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 342 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 343 | if (!tools.length) |
| 344 | { return { ok: false, reason: "You need plant clippers to gather this!" }; } |
| 345 | if (minClothTier) |
| 346 | { |
| 347 | const needMetal = metalTierForCloth(minClothTier); |
| 348 | const usable = tools.filter((tool) => tierRank(tool.tier) >= tierRank(needMetal)); |
| 349 | if (!usable.length) |
| 350 | { |
| 351 | return { |
| 352 | ok: false, |
| 353 | reason: `You need ${MATERIAL_DISPLAY_NAME[needMetal] ?? needMetal} plant clippers or better.` |
| 354 | }; |
| 355 | } |
| 356 | return { ok: true, tool: usable[0] }; |
| 357 | } |
| 358 | return { ok: true, tool: tools[0] }; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Best shears for clipping fleece (inventory or weapon slot). |
| 363 | * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} |
| 364 | */ |
| 365 | export function resolveShears ({ inventorySlots, equipmentSlots, skills, clothTier }) |
| 366 | { |
| 367 | const needMetal = metalTierForCloth(clothTier); |
| 368 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.shears) |
| 369 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 370 | .filter((tool) => tierRank(tool.tier) >= tierRank(needMetal)) |
| 371 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 372 | if (!tools.length) |
| 373 | { |
| 374 | return { |
| 375 | ok: false, |
| 376 | reason: `You need ${MATERIAL_DISPLAY_NAME[needMetal] ?? needMetal} shears or better.` |
| 377 | }; |
| 378 | } |
| 379 | return { ok: true, tool: tools[0] }; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Skill gate for folklore herb nodes (`BlockType.herbPlant`, state = herbId). |
| 384 | * @returns {{ ok: true, herb: object } | { ok: false, reason: string }} |
| 385 | */ |
| 386 | export function resolveHerbGather ({ herbId, skills }) |
| 387 | { |
| 388 | const herb = herbDefinition(herbId); |
| 389 | if (!herb) |
| 390 | { return { ok: false, reason: "There is nothing to pick there." }; } |
| 391 | const need = herbGatherSkillLevel(herb.tier); |
| 392 | const level = skillLevel(skills, SkillId.herbalism); |
| 393 | if (level < need) |
| 394 | { return { ok: false, reason: `Requires Herbalism ${need}.` }; } |
| 395 | return { ok: true, herb }; |
| 396 | } |
| 397 | |
| 398 | /** Herbalism gather duration. Flowers are quick (~1s); cotton a bit longer; clipper herbs mid. */ |
| 399 | export function herbalismDurationMs (plantKind = null, toolTier = null, herbTier = null) |
| 400 | { |
| 401 | if (plantKind === "flower") |
| 402 | { return 1000; } |
| 403 | if (plantKind === "herb") |
| 404 | { |
| 405 | const tier = Math.max(1, herbTier | 0 || 1); |
| 406 | const rank = toolTier ? tierRank(toolTier) : 1; |
| 407 | return Math.max(800, 1600 + tier * 250 - (rank - 1) * 150); |
| 408 | } |
| 409 | if (plantKind === "flax") |
| 410 | { |
| 411 | const rank = toolTier ? tierRank(toolTier) : 1; |
| 412 | return Math.max(800, 2200 - (rank - 1) * 200); |
| 413 | } |
| 414 | if (plantKind === "silk") |
| 415 | { |
| 416 | const rank = toolTier ? tierRank(toolTier) : 1; |
| 417 | return Math.max(900, 2800 - (rank - 1) * 200); |
| 418 | } |
| 419 | const rank = toolTier ? tierRank(toolTier) : 1; |
| 420 | return Math.max(500, 2500 - (rank - 1) * 250); |
| 421 | } |
| 422 | |
| 423 | export function herbalismXpForPick (kind, herbTier = null) |
| 424 | { |
| 425 | if (kind === "flower") |
| 426 | { return 4; } |
| 427 | if (kind === "herb") |
| 428 | { return 4 + Math.max(1, herbTier | 0 || 1) * 2; } |
| 429 | if (kind === "flax") |
| 430 | { return 5; } |
| 431 | if (kind === "silk") |
| 432 | { return 8; } |
| 433 | return 3; |
| 434 | } |
| 435 | |
| 436 | /** Plant node exhaust then respawn (ms), before seasonal regrow scaling. */ |
| 437 | export const PLANT_EXHAUST_MS = 15_000; |
| 438 | |
| 439 | /** Craft duration: furnace recipes scale by bar tier; others use the fixed craft duration. */ |
| 440 | export function craftDurationMs (recipe, fallbackMs = 2000) |
| 441 | { |
| 442 | if (recipe?.xpMaterial) |
| 443 | { return smeltingDurationMs(recipe.xpMaterial); } |
| 444 | return Math.max(1, fallbackMs | 0 || 2000); |
| 445 | } |
| 446 | |
| 447 | /** Re-export ItemId helpers used by recipes. */ |
| 448 | export { ItemId, MaterialTier, GatherMaterialId }; |