BlueGit

timfa questward

Files

questward / shared / crafting / recipes.js

recipes.js

recipes.js 3.2 KiB Download Raw
1import { itemDefinition } from "../definitions/items.js";
2import { consumeFillCharge, insertStack, stackQuantity, takeFromSlot } from "../items/inventory.js";
3import { resolveDyeConsumeSlot } from "./dye.js";
4
5export { recipesForStation, recipeDefinition, recipeStationIds } from "./stationRecipes.js";
6
7/** Fixed craft time for every station recipe (ms). */
8export const CRAFT_DURATION_MS = 2000;
9
10function takeItemPreservingSlots (slots, itemId, quantity)
11{
12 let remaining = Math.max(0, stackQuantity(quantity));
13 if (!remaining)
14 { return { ok: false, slots }; }
15 let next = slots.map((slot) => slot ? structuredClone(slot) : null);
16 for (let i = 0; i < next.length && remaining > 0; i++)
17 {
18 if (!next[i] || next[i].itemId !== itemId)
19 { continue; }
20 const taken = takeFromSlot(next, i, remaining);
21 if (!taken.ok || !taken.taken)
22 { continue; }
23 next = taken.slots;
24 remaining -= stackQuantity(taken.taken.quantity);
25 }
26 return { ok: remaining === 0, slots: next };
27}
28
29/**
30 * Recipe shape: `{ output: { itemId, quantity }, inputs: [{ itemId, quantity }] }`.
31 * Returns whether the player has the inputs and, after consuming them (and optional dye
32 * charge), room for the output. Freed slots from consumed stacks count toward output space.
33 */
34export function canCraftFitInventory (slots, recipe, options = {})
35{
36 if (!recipe?.output?.itemId || !Array.isArray(recipe.inputs))
37 { return false; }
38 let next = slots;
39 for (const input of recipe.inputs)
40 {
41 const qty = input.quantity | 0;
42 if (qty <= 0)
43 { continue; }
44 const taken = takeItemPreservingSlots(next, input.itemId, qty);
45 if (!taken.ok)
46 { return false; }
47 next = taken.slots;
48 }
49 if (recipe.consumeDye)
50 {
51 const consumeSlot = resolveDyeConsumeSlot(next, options);
52 if (consumeSlot == null)
53 { return false; }
54 const dyeCheck = consumeFillCharge(next, consumeSlot, { kind: "dye" });
55 if (!dyeCheck.ok)
56 { return false; }
57 next = dyeCheck.slots;
58 }
59 const def = itemDefinition(recipe.output.itemId);
60 if (!def)
61 { return false; }
62 const outStack = {
63 itemId: recipe.output.itemId,
64 quantity: Math.max(1, recipe.output.quantity | 0 || 1),
65 ...(recipe.dyeColor ? { instance: { kind: "clothing", dyeColor: recipe.dyeColor } } : {})
66 };
67 const added = insertStack(next, outStack, def);
68 return added.ok && added.remainder === 0;
69}
70
71export function countItemInSlots (slots, itemId)
72{
73 let total = 0;
74 for (const slot of slots ?? [])
75 {
76 if (slot?.itemId === itemId)
77 { total += stackQuantity(slot.quantity); }
78 }
79 return total;
80}
81
82export function hasRecipeInputs (slots, recipe)
83{
84 if (!recipe?.inputs?.length)
85 { return false; }
86 for (const input of recipe.inputs)
87 {
88 const need = input.quantity | 0;
89 if (need <= 0)
90 { continue; }
91 if (countItemInSlots(slots, input.itemId) < need)
92 { return false; }
93 }
94 return true;
95}
96
97/** Whether the player meets skill, has inputs, and bag space after consumption. */
98export function canStartCraft (slots, recipe, skillLevel = 1, options = {})
99{
100 if (!recipe?.output?.itemId)
101 { return false; }
102 const minLevel = recipe.minSkillLevel | 0 || 1;
103 if ((skillLevel | 0) < minLevel)
104 { return false; }
105 if (!hasRecipeInputs(slots, recipe))
106 { return false; }
107 return canCraftFitInventory(slots, recipe, options);
108}