BlueGit

timfa questward

Files

questward / shared / definitions / gathering.js

gathering.js

gathering.js 9.3 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. */
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/** Mine skill / tool gates use the associated bar's equip tier. */
111export function gatherEquipTier (materialId)
112{
113 return associatedBarTier(materialId);
114}
115
116export function canMineMaterial (miningLevel, toolTier, materialId)
117{
118 const gateTier = gatherEquipTier(materialId);
119 const needSkill = tierMineSkillLevel(gateTier);
120 if ((miningLevel | 0) < needSkill)
121 {
122 return { ok: false, reason: `Requires Mining ${needSkill}.` };
123 }
124 if (!toolTierMeetsResource(toolTier, gateTier))
125 {
126 return { ok: false, reason: "You need a pickaxe to mine this!" };
127 }
128 return { ok: true };
129}
130
131export function canChopWood (woodcuttingLevel, toolTier, woodTier)
132{
133 const needSkill = tierMineSkillLevel(woodTier);
134 if ((woodcuttingLevel | 0) < needSkill)
135 {
136 return { ok: false, reason: `Requires Woodcutting ${needSkill}.` };
137 }
138 if (!toolTierMeetsResource(toolTier, woodTier))
139 {
140 return { ok: false, reason: "You need an axe to chop this!" };
141 }
142 return { ok: true };
143}
144
145/**
146 * Gathering tools may be one material tier below the resource node.
147 * Pine/bronze (rank 1) still need a bronze tool; birch accepts bronze or iron, etc.
148 */
149export function toolTierMeetsResource (toolTier, resourceTier)
150{
151 return tierRank(toolTier) + 1 >= tierRank(resourceTier);
152}
153
154/**
155 * Tools of a given equipment kind in bag slots and/or the weapon slot.
156 * Gathering uses inventory tools without requiring them to be equipped.
157 */
158export function listGatherTools (inventorySlots, equipmentSlots, kind)
159{
160 const tools = [];
161 for (const stack of inventorySlots ?? [])
162 {
163 if (!stack?.itemId)
164 { continue; }
165 const def = itemDefinition(stack.itemId);
166 if (def?.equipment?.kind !== kind)
167 { continue; }
168 tools.push({ stack, def, tier: def.equipment.tier, source: "inventory" });
169 }
170 const weapon = equipmentSlots?.weapon;
171 if (weapon?.itemId)
172 {
173 const def = itemDefinition(weapon.itemId);
174 if (def?.equipment?.kind === kind)
175 { tools.push({ stack: weapon, def, tier: def.equipment.tier, source: "equipment" }); }
176 }
177 return tools;
178}
179
180/**
181 * Best pickaxe the player can use for this ore (highest tier that meets gates).
182 * @returns {{ ok: true, tool: object } | { ok: false, reason: string }}
183 */
184export function resolveMiningPickaxe ({ inventorySlots, equipmentSlots, skills, materialId })
185{
186 const miningLevel = skillLevel(skills, SkillId.mining);
187 const gateTier = gatherEquipTier(materialId);
188 const needSkill = tierMineSkillLevel(gateTier);
189 if ((miningLevel | 0) < needSkill)
190 { return { ok: false, reason: `Requires Mining ${needSkill}.` }; }
191
192 const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.pickaxeTool);
193 const usable = tools
194 .filter((tool) => canEquipItem(skills, tool.def).ok)
195 .filter((tool) => toolTierMeetsResource(tool.tier, gateTier))
196 .sort((a, b) => tierRank(b.tier) - tierRank(a.tier));
197
198 if (!usable.length)
199 { return { ok: false, reason: "You need a pickaxe to mine this!" }; }
200 return { ok: true, tool: usable[0] };
201}
202
203/**
204 * Best axe the player can use for this wood tier.
205 * @returns {{ ok: true, tool: object } | { ok: false, reason: string }}
206 */
207export function resolveWoodcuttingAxe ({ inventorySlots, equipmentSlots, skills, woodTier })
208{
209 const woodcuttingLevel = skillLevel(skills, SkillId.woodcutting);
210 const needSkill = tierMineSkillLevel(woodTier);
211 if ((woodcuttingLevel | 0) < needSkill)
212 { return { ok: false, reason: `Requires Woodcutting ${needSkill}.` }; }
213
214 const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.axeTool);
215 const usable = tools
216 .filter((tool) => canEquipItem(skills, tool.def).ok)
217 .filter((tool) => toolTierMeetsResource(tool.tier, woodTier))
218 .sort((a, b) => tierRank(b.tier) - tierRank(a.tier));
219
220 if (!usable.length)
221 { return { ok: false, reason: "You need an axe to chop this!" }; }
222 return { ok: true, tool: usable[0] };
223}
224
225/**
226 * Best plant clippers for herbalism nodes (flowers). Cotton needs no tool.
227 * @returns {{ ok: true, tool: object|null } | { ok: false, reason: string }}
228 */
229export function resolvePlantClippers ({ inventorySlots, equipmentSlots, skills, requireTool = true })
230{
231 if (!requireTool)
232 { return { ok: true, tool: null }; }
233 const tools = listGatherTools(inventorySlots, equipmentSlots, EquipmentKind.plantClippers)
234 .filter((tool) => canEquipItem(skills, tool.def).ok)
235 .sort((a, b) => tierRank(b.tier) - tierRank(a.tier));
236 if (!tools.length)
237 { return { ok: false, reason: "You need plant clippers to gather this!" }; }
238 return { ok: true, tool: tools[0] };
239}
240
241/**
242 * Skill gate for folklore herb nodes (`BlockType.herbPlant`, state = herbId).
243 * @returns {{ ok: true, herb: object } | { ok: false, reason: string }}
244 */
245export function resolveHerbGather ({ herbId, skills })
246{
247 const herb = herbDefinition(herbId);
248 if (!herb)
249 { return { ok: false, reason: "There is nothing to pick there." }; }
250 const need = herbGatherSkillLevel(herb.tier);
251 const level = skillLevel(skills, SkillId.herbalism);
252 if (level < need)
253 { return { ok: false, reason: `Requires Herbalism ${need}.` }; }
254 return { ok: true, herb };
255}
256
257/** Herbalism gather duration. Flowers are quick (~1s); cotton a bit longer; clipper herbs mid. */
258export function herbalismDurationMs (plantKind = null, toolTier = null, herbTier = null)
259{
260 if (plantKind === "flower")
261 { return 1000; }
262 if (plantKind === "herb")
263 {
264 const tier = Math.max(1, herbTier | 0 || 1);
265 const rank = toolTier ? tierRank(toolTier) : 1;
266 return Math.max(800, 1600 + tier * 250 - (rank - 1) * 150);
267 }
268 const rank = toolTier ? tierRank(toolTier) : 1;
269 return Math.max(500, 2500 - (rank - 1) * 250);
270}
271
272export function herbalismXpForPick (kind, herbTier = null)
273{
274 if (kind === "flower")
275 { return 4; }
276 if (kind === "herb")
277 { return 4 + Math.max(1, herbTier | 0 || 1) * 2; }
278 return 3;
279}
280
281/** Plant node exhaust then respawn (ms). */
282export const PLANT_EXHAUST_MS = 15_000;
283
284/** Craft duration: furnace recipes scale by bar tier; others use the fixed craft duration. */
285export function craftDurationMs (recipe, fallbackMs = 2000)
286{
287 if (recipe?.xpMaterial)
288 { return smeltingDurationMs(recipe.xpMaterial); }
289 return Math.max(1, fallbackMs | 0 || 2000);
290}
291
292/** Re-export ItemId helpers used by recipes. */
293export { ItemId, MaterialTier, GatherMaterialId };