BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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