| 1 | import { ItemId, itemDefinition } from "./items.js"; |
| 2 | import { CraftingItemId } from "./craftingItems.js"; |
| 3 | import { MetalItemId } from "./metalItems.js"; |
| 4 | import { EquipmentKind } from "./equipmentKinds.js"; |
| 5 | import { canEquipItem } from "../equipment/stats.js"; |
| 6 | import { SkillId, skillLevel } from "./skills.js"; |
| 7 | import { |
| 8 | MaterialTier, |
| 9 | GatherMaterialId, |
| 10 | ORE_BEARING_MATERIALS, |
| 11 | associatedBarTier, |
| 12 | tierMineSkillLevel, |
| 13 | tierSmeltSkillLevel, |
| 14 | tierRank, |
| 15 | miningDurationMs, |
| 16 | oreExhaustDurationMs, |
| 17 | smeltingDurationMs, |
| 18 | SMELT_DURATION_PER_RANK_MS, |
| 19 | EXHAUSTED_ORE_BOULDER_STATE, |
| 20 | oreBoulderState, |
| 21 | materialFromOreBoulderState, |
| 22 | isOreBoulderState |
| 23 | } from "./materialTiers.js"; |
| 24 | |
| 25 | export { |
| 26 | ORE_BEARING_MATERIALS, |
| 27 | associatedBarTier, |
| 28 | tierMineSkillLevel, |
| 29 | tierSmeltSkillLevel, |
| 30 | miningDurationMs, |
| 31 | oreExhaustDurationMs, |
| 32 | smeltingDurationMs, |
| 33 | SMELT_DURATION_PER_RANK_MS, |
| 34 | EXHAUSTED_ORE_BOULDER_STATE, |
| 35 | oreBoulderState, |
| 36 | materialFromOreBoulderState, |
| 37 | isOreBoulderState, |
| 38 | EquipmentKind |
| 39 | }; |
| 40 | |
| 41 | /** Ore item produced when mining a material node. */ |
| 42 | export const ORE_ITEM_BY_MATERIAL = Object.freeze({ |
| 43 | [GatherMaterialId.tin]: CraftingItemId.tinOre, |
| 44 | [GatherMaterialId.copper]: CraftingItemId.copperOre, |
| 45 | [MaterialTier.iron]: MetalItemId.ironOre, |
| 46 | [MaterialTier.mithril]: MetalItemId.mithrilOre, |
| 47 | [MaterialTier.adamantium]: MetalItemId.adamantiumOre, |
| 48 | [MaterialTier.orichalcum]: MetalItemId.orichalcumOre, |
| 49 | [MaterialTier.dragon]: MetalItemId.dragonOre, |
| 50 | [GatherMaterialId.coal]: MetalItemId.coal |
| 51 | }); |
| 52 | |
| 53 | export const BAR_ITEM_BY_MATERIAL = Object.freeze({ |
| 54 | [MaterialTier.bronze]: CraftingItemId.bronzeBar, |
| 55 | [MaterialTier.iron]: MetalItemId.ironBar, |
| 56 | [MaterialTier.steel]: MetalItemId.steelBar, |
| 57 | [MaterialTier.mithril]: MetalItemId.mithrilBar, |
| 58 | [MaterialTier.adamantium]: MetalItemId.adamantiumBar, |
| 59 | [MaterialTier.orichalcum]: MetalItemId.orichalcumBar, |
| 60 | [MaterialTier.dragon]: MetalItemId.dragonBar |
| 61 | }); |
| 62 | |
| 63 | export function oreItemIdForMaterial (materialId) |
| 64 | { |
| 65 | return ORE_ITEM_BY_MATERIAL[materialId] ?? null; |
| 66 | } |
| 67 | |
| 68 | export function barItemIdForMaterial (materialId) |
| 69 | { |
| 70 | return BAR_ITEM_BY_MATERIAL[materialId] ?? null; |
| 71 | } |
| 72 | |
| 73 | /** Mining XP awarded per successful ore (scales with associated bar rank). */ |
| 74 | export function miningXpForMaterial (materialId) |
| 75 | { |
| 76 | return Math.max(1, tierRank(materialId) * 5); |
| 77 | } |
| 78 | |
| 79 | /** Smithing XP awarded per successful smelt (scales with bar rank). */ |
| 80 | export function smeltingXpForMaterial (materialId) |
| 81 | { |
| 82 | return Math.max(1, tierRank(materialId) * 5); |
| 83 | } |
| 84 | |
| 85 | /** Mine skill / tool gates use the associated bar's equip tier. */ |
| 86 | export function gatherEquipTier (materialId) |
| 87 | { |
| 88 | return associatedBarTier(materialId); |
| 89 | } |
| 90 | |
| 91 | export function canMineMaterial (miningLevel, toolTier, materialId) |
| 92 | { |
| 93 | const gateTier = gatherEquipTier(materialId); |
| 94 | const needSkill = tierMineSkillLevel(gateTier); |
| 95 | if ((miningLevel | 0) < needSkill) |
| 96 | { |
| 97 | return { ok: false, reason: `Requires Mining ${needSkill}.` }; |
| 98 | } |
| 99 | if (tierRank(toolTier) < tierRank(gateTier)) |
| 100 | { |
| 101 | return { ok: false, reason: "You need a pickaxe to mine this!" }; |
| 102 | } |
| 103 | return { ok: true }; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tools of a given equipment kind in bag slots and/or the weapon slot. |
| 108 | * Gathering uses inventory tools without requiring them to be equipped. |
| 109 | */ |
| 110 | export function listGatherTools (inventorySlots, equipmentSlots, kind) |
| 111 | { |
| 112 | const tools = []; |
| 113 | for (const stack of inventorySlots ?? []) |
| 114 | { |
| 115 | if (!stack?.itemId) |
| 116 | { continue; } |
| 117 | const def = itemDefinition(stack.itemId); |
| 118 | if (def?.equipment?.kind !== kind) |
| 119 | { continue; } |
| 120 | tools.push({ stack, def, tier: def.equipment.tier, source: "inventory" }); |
| 121 | } |
| 122 | const weapon = equipmentSlots?.weapon; |
| 123 | if (weapon?.itemId) |
| 124 | { |
| 125 | const def = itemDefinition(weapon.itemId); |
| 126 | if (def?.equipment?.kind === kind) |
| 127 | { tools.push({ stack: weapon, def, tier: def.equipment.tier, source: "equipment" }); } |
| 128 | } |
| 129 | return tools; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Best pickaxe the player can use for this ore (highest tier that meets gates). |
| 134 | * @returns {{ ok: true, tool: object } | { ok: false, reason: string }} |
| 135 | */ |
| 136 | export function resolveMiningPickaxe ({ inventorySlots, equipmentSlots, skills, materialId }) |
| 137 | { |
| 138 | const miningLevel = skillLevel(skills, SkillId.mining); |
| 139 | const gateTier = gatherEquipTier(materialId); |
| 140 | const needSkill = tierMineSkillLevel(gateTier); |
| 141 | if ((miningLevel | 0) < needSkill) |
| 142 | { return { ok: false, reason: `Requires Mining ${needSkill}.` }; } |
| 143 | |
| 144 | const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.pickaxeTool); |
| 145 | const usable = tools |
| 146 | .filter((tool) => canEquipItem(skills, tool.def).ok) |
| 147 | .filter((tool) => tierRank(tool.tier) >= tierRank(gateTier)) |
| 148 | .sort((a, b) => tierRank(b.tier) - tierRank(a.tier)); |
| 149 | |
| 150 | if (!usable.length) |
| 151 | { return { ok: false, reason: "You need a pickaxe to mine this!" }; } |
| 152 | return { ok: true, tool: usable[0] }; |
| 153 | } |
| 154 | |
| 155 | /** Craft duration: furnace recipes scale by bar tier; others use the fixed craft duration. */ |
| 156 | export function craftDurationMs (recipe, fallbackMs = 2000) |
| 157 | { |
| 158 | if (recipe?.xpMaterial) |
| 159 | { return smeltingDurationMs(recipe.xpMaterial); } |
| 160 | return Math.max(1, fallbackMs | 0 || 2000); |
| 161 | } |
| 162 | |
| 163 | /** Re-export ItemId helpers used by recipes. */ |
| 164 | export { ItemId, MaterialTier, GatherMaterialId }; |