BlueGit

timfa questward

Files

questward / shared / crafting / recipes.js

recipes.js

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