BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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