BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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