BlueGit

timfa questward

Files

questward / shared / crafting / recipes.js

recipes.js

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