BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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