BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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