BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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