BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

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