| 1 | import { itemDefinition } from "../definitions/items.js"; |
| 2 | import { insertStack, stackQuantity, takeFromSlot } from "../items/inventory.js"; |
| 3 | |
| 4 | export { recipesForStation, recipeDefinition, recipeStationIds } from "./stationRecipes.js"; |
| 5 | |
| 6 | /** Fixed craft time for every station recipe (ms). */ |
| 7 | export const CRAFT_DURATION_MS = 2000; |
| 8 | |
| 9 | function 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, room for the output. |
| 31 | * Freed slots from consumed stacks count toward output space. |
| 32 | */ |
| 33 | export function canCraftFitInventory (slots, recipe) |
| 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 | const def = itemDefinition(recipe.output.itemId); |
| 49 | if (!def) |
| 50 | { return false; } |
| 51 | const added = insertStack(next, { |
| 52 | itemId: recipe.output.itemId, |
| 53 | quantity: Math.max(1, recipe.output.quantity | 0 || 1) |
| 54 | }, def); |
| 55 | return added.ok && added.remainder === 0; |
| 56 | } |
| 57 | |
| 58 | export function countItemInSlots (slots, itemId) |
| 59 | { |
| 60 | let total = 0; |
| 61 | for (const slot of slots ?? []) |
| 62 | { |
| 63 | if (slot?.itemId === itemId) |
| 64 | { total += stackQuantity(slot.quantity); } |
| 65 | } |
| 66 | return total; |
| 67 | } |
| 68 | |
| 69 | export function hasRecipeInputs (slots, recipe) |
| 70 | { |
| 71 | if (!recipe?.inputs?.length) |
| 72 | { return false; } |
| 73 | for (const input of recipe.inputs) |
| 74 | { |
| 75 | const need = input.quantity | 0; |
| 76 | if (need <= 0) |
| 77 | { continue; } |
| 78 | if (countItemInSlots(slots, input.itemId) < need) |
| 79 | { return false; } |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /** Whether the player meets skill, has inputs, and bag space after consumption. */ |
| 85 | export function canStartCraft (slots, recipe, skillLevel = 1) |
| 86 | { |
| 87 | if (!recipe?.output?.itemId) |
| 88 | { return false; } |
| 89 | const minLevel = recipe.minSkillLevel | 0 || 1; |
| 90 | if ((skillLevel | 0) < minLevel) |
| 91 | { return false; } |
| 92 | if (!hasRecipeInputs(slots, recipe)) |
| 93 | { return false; } |
| 94 | return canCraftFitInventory(slots, recipe); |
| 95 | } |