import { itemDefinition } from "../definitions/items.js"; import { consumeFillCharge, insertStack, stackQuantity, takeFromSlot } from "../items/inventory.js"; import { resolveDyeConsumeSlot } from "./dye.js"; export { recipesForStation, recipeDefinition, recipeStationIds } from "./stationRecipes.js"; /** Fixed craft time for every station recipe (ms). */ export const CRAFT_DURATION_MS = 2000; function takeItemPreservingSlots (slots, itemId, quantity) { let remaining = Math.max(0, stackQuantity(quantity)); if (!remaining) { return { ok: false, slots }; } let next = slots.map((slot) => slot ? structuredClone(slot) : null); for (let i = 0; i < next.length && remaining > 0; i++) { if (!next[i] || next[i].itemId !== itemId) { continue; } const taken = takeFromSlot(next, i, remaining); if (!taken.ok || !taken.taken) { continue; } next = taken.slots; remaining -= stackQuantity(taken.taken.quantity); } return { ok: remaining === 0, slots: next }; } /** * Recipe shape: `{ output: { itemId, quantity }, inputs: [{ itemId, quantity }] }`. * Returns whether the player has the inputs and, after consuming them (and optional dye * charge), room for the output. Freed slots from consumed stacks count toward output space. */ export function canCraftFitInventory (slots, recipe, options = {}) { if (!recipe?.output?.itemId || !Array.isArray(recipe.inputs)) { return false; } let next = slots; for (const input of recipe.inputs) { const qty = input.quantity | 0; if (qty <= 0) { continue; } const taken = takeItemPreservingSlots(next, input.itemId, qty); if (!taken.ok) { return false; } next = taken.slots; } if (recipe.consumeDye) { const consumeSlot = resolveDyeConsumeSlot(next, options); if (consumeSlot == null) { return false; } const dyeCheck = consumeFillCharge(next, consumeSlot, { kind: "dye" }); if (!dyeCheck.ok) { return false; } next = dyeCheck.slots; } const def = itemDefinition(recipe.output.itemId); if (!def) { return false; } const outStack = { itemId: recipe.output.itemId, quantity: Math.max(1, recipe.output.quantity | 0 || 1), ...(recipe.dyeColor ? { instance: { kind: "clothing", dyeColor: recipe.dyeColor } } : {}) }; const added = insertStack(next, outStack, def); return added.ok && added.remainder === 0; } export function countItemInSlots (slots, itemId) { let total = 0; for (const slot of slots ?? []) { if (slot?.itemId === itemId) { total += stackQuantity(slot.quantity); } } return total; } export function hasRecipeInputs (slots, recipe) { if (!recipe?.inputs?.length) { return false; } for (const input of recipe.inputs) { const need = input.quantity | 0; if (need <= 0) { continue; } if (countItemInSlots(slots, input.itemId) < need) { return false; } } return true; } /** Whether the player meets skill, has inputs, and bag space after consumption. */ export function canStartCraft (slots, recipe, skillLevel = 1, options = {}) { if (!recipe?.output?.itemId) { return false; } const minLevel = recipe.minSkillLevel | 0 || 1; if ((skillLevel | 0) < minLevel) { return false; } if (!hasRecipeInputs(slots, recipe)) { return false; } return canCraftFitInventory(slots, recipe, options); }