BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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