BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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