| 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 | MaterialTier, |
| 10 | GatherMaterialId, |
| 11 | WoodTier, |
| 12 | ORE_BEARING_MATERIALS, |
| 13 | associatedBarTier, |
| 14 | tierMineSkillLevel, |
| 15 | tierSmeltSkillLevel, |
| 16 | tierRank, |
| 17 | miningDurationMs, |
| 18 | oreExhaustDurationMs, |
| 19 | smeltingDurationMs, |
| 20 | SMELT_DURATION_PER_RANK_MS, |
| 21 | EXHAUSTED_ORE_BOULDER_STATE, |
| 22 | oreBoulderState, |
| 23 | materialFromOreBoulderState, |
| 24 | isOreBoulderState |
| 25 | } from "./materialTiers.js"; |
| 26 | import { herbDefinition, herbGatherSkillLevel } from "./herbs.js"; |
| 27 | |
| 28 | export { |
| 29 | ORE_BEARING_MATERIALS, |
| 30 | associatedBarTier, |
| 31 | tierMineSkillLevel, |
| 32 | tierSmeltSkillLevel, |
| 33 | miningDurationMs, |
| 34 | oreExhaustDurationMs, |
| 35 | smeltingDurationMs, |
| 36 | SMELT_DURATION_PER_RANK_MS, |
| 37 | EXHAUSTED_ORE_BOULDER_STATE, |
| 38 | oreBoulderState, |
| 39 | materialFromOreBoulderState, |
| 40 | isOreBoulderState, |
| 41 | EquipmentKind, |
| 42 | WoodTier |
| 43 | }; |
| 44 | |
| 45 | /** Same curve as mining: (rank×8s) − (toolRank×3s), min 0.5s. */ |
| 46 | export const woodcuttingDurationMs = miningDurationMs; |
| 47 | |
| 48 | /** Same curve as ore: 10s × wood tier rank. */ |
| 49 | export const treeExhaustDurationMs = oreExhaustDurationMs; |
| 50 | |
| 51 | /** Ore item produced when mining a material node. */ |
| 52 | export const ORE_ITEM_BY_MATERIAL = Object.freeze({ |
| 53 | [GatherMaterialId.tin]: CraftingItemId.tinOre, |
| 54 | [GatherMaterialId.copper]: CraftingItemId.copperOre, |
| 55 | [MaterialTier.iron]: MetalItemId.ironOre, |
| 56 | [GatherMaterialId.silver]: MetalItemId.silverOre, |
| 57 | [GatherMaterialId.gold]: MetalItemId.goldOre, |
| 58 | [MaterialTier.mithril]: MetalItemId.mithrilOre, |
| 59 | [MaterialTier.adamantium]: MetalItemId.adamantiumOre, |
| 60 | [MaterialTier.orichalcum]: MetalItemId.orichalcumOre, |
| 61 | [MaterialTier.dragon]: MetalItemId.dragonOre, |
| 62 | [GatherMaterialId.coal]: MetalItemId.coal |
| 63 | }); |
| 64 | |
| 65 | export const BAR_ITEM_BY_MATERIAL = Object.freeze({ |
| 66 | [MaterialTier.bronze]: CraftingItemId.bronzeBar, |
| 67 | [MaterialTier.iron]: MetalItemId.ironBar, |
| 68 | [MaterialTier.steel]: MetalItemId.steelBar, |
| 69 | [GatherMaterialId.silver]: MetalItemId.silverBar, |
| 70 | [GatherMaterialId.gold]: MetalItemId.goldBar, |
| 71 | [MaterialTier.mithril]: MetalItemId.mithrilBar, |
| 72 | [MaterialTier.adamantium]: MetalItemId.adamantiumBar, |
| 73 | [MaterialTier.orichalcum]: MetalItemId.orichalcumBar, |
| 74 | [MaterialTier.dragon]: MetalItemId.dragonBar |
| 75 | }); |
| 76 | |
| 77 | export function oreItemIdForMaterial (materialId) |
| 78 | { |
| 79 | return ORE_ITEM_BY_MATERIAL[materialId] ?? null; |
| 80 | } |
| 81 | |
| 82 | export function barItemIdForMaterial (materialId) |
| 83 | { |
| 84 | return BAR_ITEM_BY_MATERIAL[materialId] ?? null; |
| 85 | } |
| 86 | |
| 87 | export function logItemIdForWoodTier (woodTier) |
| 88 | { |
| 89 | return logItemIdForWood(woodTier); |
| 90 | } |
| 91 | |
| 92 | /** Mining XP awarded per successful ore (scales with associated bar rank). */ |
| 93 | export function miningXpForMaterial (materialId) |
| 94 | { |
| 95 | return Math.max(1, tierRank(materialId) * 5); |
| 96 | } |
| 97 | |
| 98 | /** Woodcutting XP awarded per successful chop (scales with wood tier rank). */ |
| 99 | export function woodcuttingXpForWood (woodTier) |
| 100 | { |
| 101 | return Math.max(1, tierRank(woodTier) * 5); |
| 102 | } |
| 103 | |
| 104 | /** Smithing XP awarded per successful smelt (scales with bar rank). */ |
| 105 | export function smeltingXpForMaterial (materialId) |
| 106 | { |
| 107 | return Math.max(1, tierRank(materialId) * 5); |
| 108 | } |
| 109 | |
| 110 | /** Mine skill / tool gates use the associated bar's equip tier. */ |
| 111 | export function gatherEquipTier (materialId) |
| 112 | { |
| 113 | return associatedBarTier(materialId); |
| 114 | } |
| 115 | |
| 116 | export function canMineMaterial (miningLevel, toolTier, materialId) |
| 117 | { |
| 118 | const gateTier = gatherEquipTier(materialId); |
| 119 | const needSkill = tierMineSkillLevel(gateTier); |
| 120 | if ((miningLevel | 0) < needSkill) |
| 121 | { |
| 122 | return { ok: false, reason: `Requires Mining ${needSkill}.` }; |
| 123 | } |
| 124 | if (tierRank(toolTier) < tierRank(gateTier)) |
| 125 | { |
| 126 | return { ok: false, reason: "You need a pickaxe to mine this!" }; |
| 127 | } |
| 128 | return { ok: true }; |
| 129 | } |
| 130 | |
| 131 | export function canChopWood (woodcuttingLevel, toolTier, woodTier) |
| 132 | { |
| 133 | const needSkill = tierMineSkillLevel(woodTier); |
| 134 | if ((woodcuttingLevel | 0) < needSkill) |
| 135 | { |
| 136 | return { ok: false, reason: `Requires Woodcutting ${needSkill}.` }; |
| 137 | } |
| 138 | if (tierRank(toolTier) < tierRank(woodTier)) |
| 139 | { |
| 140 | return { ok: false, reason: "You need an axe to chop this!" }; |
| 141 | } |
| 142 | return { ok: true }; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Tools of a given equipment kind in bag slots and/or the weapon slot. |
| 147 | * Gathering uses inventory tools without requiring them to be equipped. |
| 148 | */ |
| 149 | export function listGatherTools (inventorySlots, equipmentSlots, kind) |
| 150 | { |
| 151 | const tools = []; |
| 152 | for (const stack of inventorySlots ?? []) |
| 153 | { |
| 154 | if (!stack?.itemId) |
| 155 | { continue; } |
| 156 | const def = itemDefinition(stack.itemId); |
| 157 | if (def?.equipment?.kind !== kind) |
| 158 | { continue; } |
| 159 | tools.push({ stack, def, tier: def.equipment.tier, source: "inventory" }); |
| 160 | } |
| 161 | const weapon = equipmentSlots?.weapon; |
| 162 | if (weapon?.itemId) |
| 163 | { |
| 164 | const def = itemDefinition(weapon.itemId); |
| 165 | if (def?.equipment?.kind === kind) |
| 166 | { tools.push({ stack: weapon, def, tier: def.equipment.tier, source: "equipment" }); } |
| 167 | } |
| 168 | return tools; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Best pickaxe the player can use for this ore (highest tier that meets gates). |
| 173 | * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} |
| 174 | */ |
| 175 | export function resolveMiningPickaxe ({ inventorySlots, equipmentSlots, skills, materialId }) |
| 176 | { |
| 177 | const miningLevel = skillLevel(skills, SkillId.mining); |
| 178 | const gateTier = gatherEquipTier(materialId); |
| 179 | const needSkill = tierMineSkillLevel(gateTier); |
| 180 | if ((miningLevel | 0) < needSkill) |
| 181 | { return { ok: false, reason: `Requires Mining ${needSkill}.` }; } |
| 182 | |
| 183 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.pickaxeTool); |
| 184 | const usable = tools |
| 185 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 186 | .filter((tool) => tierRank(tool.tier) >= tierRank(gateTier)) |
| 187 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 188 | |
| 189 | if (!usable.length) |
| 190 | { return { ok: false, reason: "You need a pickaxe to mine this!" }; } |
| 191 | return { ok: true, tool: usable[0] }; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Best axe the player can use for this wood tier. |
| 196 | * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} |
| 197 | */ |
| 198 | export function resolveWoodcuttingAxe ({ inventorySlots, equipmentSlots, skills, woodTier }) |
| 199 | { |
| 200 | const woodcuttingLevel = skillLevel(skills, SkillId.woodcutting); |
| 201 | const needSkill = tierMineSkillLevel(woodTier); |
| 202 | if ((woodcuttingLevel | 0) < needSkill) |
| 203 | { return { ok: false, reason: `Requires Woodcutting ${needSkill}.` }; } |
| 204 | |
| 205 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.axeTool); |
| 206 | const usable = tools |
| 207 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 208 | .filter((tool) => tierRank(tool.tier) >= tierRank(woodTier)) |
| 209 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 210 | |
| 211 | if (!usable.length) |
| 212 | { return { ok: false, reason: "You need an axe to chop this!" }; } |
| 213 | return { ok: true, tool: usable[0] }; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Best plant clippers for herbalism nodes (flowers). Cotton needs no tool. |
| 218 | * @returns {{ ok: true, tool: object|null } | { ok: false, reason: string }} |
| 219 | */ |
| 220 | export function resolvePlantClippers ({ inventorySlots, equipmentSlots, skills, requireTool = true }) |
| 221 | { |
| 222 | if (!requireTool) |
| 223 | { return { ok: true, tool: null }; } |
| 224 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.plantClippers) |
| 225 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 226 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 227 | if (!tools.length) |
| 228 | { return { ok: false, reason: "You need plant clippers to gather this!" }; } |
| 229 | return { ok: true, tool: tools[0] }; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Skill gate for folklore herb nodes (`BlockType.herbPlant`, state = herbId). |
| 234 | * @returns {{ ok: true, herb: object } | { ok: false, reason: string }} |
| 235 | */ |
| 236 | export function resolveHerbGather ({ herbId, skills }) |
| 237 | { |
| 238 | const herb = herbDefinition(herbId); |
| 239 | if (!herb) |
| 240 | { return { ok: false, reason: "There is nothing to pick there." }; } |
| 241 | const need = herbGatherSkillLevel(herb.tier); |
| 242 | const level = skillLevel(skills, SkillId.herbalism); |
| 243 | if (level < need) |
| 244 | { return { ok: false, reason: `Requires Herbalism ${need}.` }; } |
| 245 | return { ok: true, herb }; |
| 246 | } |
| 247 | |
| 248 | /** Herbalism gather duration. Flowers are quick (~1s); cotton a bit longer; clipper herbs mid. */ |
| 249 | export function herbalismDurationMs (plantKind = null, toolTier = null, herbTier = null) |
| 250 | { |
| 251 | if (plantKind === "flower") |
| 252 | { return 1000; } |
| 253 | if (plantKind === "herb") |
| 254 | { |
| 255 | const tier = Math.max(1, herbTier | 0 || 1); |
| 256 | const rank = toolTier ? tierRank(toolTier) : 1; |
| 257 | return Math.max(800, 1600 + tier * 250 - (rank - 1) * 150); |
| 258 | } |
| 259 | const rank = toolTier ? tierRank(toolTier) : 1; |
| 260 | return Math.max(500, 2500 - (rank - 1) * 250); |
| 261 | } |
| 262 | |
| 263 | export function herbalismXpForPick (kind, herbTier = null) |
| 264 | { |
| 265 | if (kind === "flower") |
| 266 | { return 4; } |
| 267 | if (kind === "herb") |
| 268 | { return 4 + Math.max(1, herbTier | 0 || 1) * 2; } |
| 269 | return 3; |
| 270 | } |
| 271 | |
| 272 | /** Plant node exhaust then respawn (ms). */ |
| 273 | export const PLANT_EXHAUST_MS = 15_000; |
| 274 | |
| 275 | /** Craft duration: furnace recipes scale by bar tier; others use the fixed craft duration. */ |
| 276 | export function craftDurationMs (recipe, fallbackMs = 2000) |
| 277 | { |
| 278 | if (recipe?.xpMaterial) |
| 279 | { return smeltingDurationMs(recipe.xpMaterial); } |
| 280 | return Math.max(1, fallbackMs | 0 || 2000); |
| 281 | } |
| 282 | |
| 283 | /** Re-export ItemId helpers used by recipes. */ |
| 284 | export { ItemId, MaterialTier, GatherMaterialId }; |