BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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