import { itemDefinition, itemDisplayName, itemAssetPathForStack, formatQuantity, ItemId } from "/shared/definitions/items.js"; import { craftingStationDefinition, CraftingStationId } from "/shared/definitions/craftingStations.js"; import { CRAFT_DURATION_MS, canStartCraft, countItemInSlots, hasRecipeInputs, recipesForStation } from "/shared/crafting/recipes.js"; import { resolveTailorCraft } from "/shared/crafting/tailor.js"; import { listDyePickerChoices, resolveDyeDisplayStack } from "/shared/crafting/dye.js"; import { potionKeyDisplayName, potionStacksForKey } from "/shared/crafting/potionCharge.js"; import { stackQuantity } from "/shared/items/inventory.js"; import { CLOTH_TIER_ORDER, CLOTH_DISPLAY_NAME, ClothTier, MATERIAL_TIER_ORDER, MATERIAL_DISPLAY_NAME, WOOD_TIER_ORDER, WoodTier, MaterialTier, LEATHER_TIER_ORDER, LEATHER_DISPLAY_NAME, LeatherTier, GEM_TIER_ORDER, GemTier, woodTierForMetal, clothTierForWood } from "/shared/definitions/materialTiers.js"; import { craftDurationMs } from "/shared/definitions/gathering.js"; import { craftDurationMultiplier } from "/shared/equipment/jewelryEffects.js"; import { applyItemIcon } from "../render/potionIcon.js"; export function initCrafting ({ socket, MessageType, getInventorySlots, getEquipment, getSkillLevel }) { const backdrop = document.getElementById("craft-backdrop"); const panel = document.getElementById("craft-panel"); const stationName = document.getElementById("craft-station-name"); const recipeGrid = document.getElementById("craft-recipe-grid"); const detailEmpty = document.getElementById("craft-detail-empty"); const detailBody = document.getElementById("craft-detail-body"); const materialControls = document.getElementById("craft-material-controls"); const materialTierSelect = document.getElementById("craft-material-tier"); const dyeControls = document.getElementById("craft-dye-controls"); const dyePickBtn = document.getElementById("craft-dye-pick"); const dyeClearBtn = document.getElementById("craft-dye-clear"); const dyePreview = document.getElementById("craft-dye-preview"); const outputImg = document.getElementById("craft-output-img"); const outputName = document.getElementById("craft-output-name"); const inputsList = document.getElementById("craft-inputs"); const statusEl = document.getElementById("craft-status"); const progressWrap = document.getElementById("craft-progress"); const progressFill = document.getElementById("craft-progress-fill"); const craftButton = document.getElementById("craft-button"); const craftBatchButton = document.getElementById("craft-batch-button"); const craftBatchAllButton = document.getElementById("craft-batch-all-button"); const dyeBackdrop = document.getElementById("dye-picker-backdrop"); const dyePanel = document.getElementById("dye-picker-panel"); const dyeGrid = document.getElementById("dye-picker-grid"); const qtyBackdrop = document.getElementById("craft-qty-backdrop"); const qtyPanel = document.getElementById("craft-qty-panel"); const qtyName = document.getElementById("craft-qty-name"); const qtyHint = document.getElementById("craft-qty-hint"); const qtyInput = document.getElementById("craft-qty-input"); const qtyConfirm = document.getElementById("craft-qty-confirm"); const qtyClose = document.getElementById("craft-qty-close"); let open = false; let stationId = null; let recipes = []; let selectedRecipeId = null; let selectedCraftGroup = null; let selectedMaterial = ClothTier.cotton; let dyeSlotIndex = null; let dyeColorKey = null; let crafting = false; let craftTimer = null; let craftAnimFrame = null; let craftStartedAt = 0; document.getElementById("craft-close").addEventListener("click", () => close()); backdrop.addEventListener("click", () => close()); craftButton.addEventListener("click", () => startCraft()); craftBatchButton?.addEventListener("click", () => openBatchQtyModal()); craftBatchAllButton?.addEventListener("click", () => startCraft({ batchAll: true })); materialTierSelect?.addEventListener("change", () => { selectedMaterial = materialTierSelect.value || selectedMaterial; if (usesCraftGroups()) { const rows = groupedDisplayRecipes(); if (!rows.some((row) => row.group === selectedCraftGroup)) { selectedCraftGroup = rows[0]?.group ?? null; } syncSelectedRecipeFromMaterial(); } renderRecipeGrid(); renderDetail(); }); dyePickBtn?.addEventListener("click", () => { if (!isTailorStation()) { return; } openDyePicker(); }); dyeClearBtn?.addEventListener("click", () => { clearDyeSelection(); renderDetail(); }); document.getElementById("dye-picker-close")?.addEventListener("click", () => closeDyePicker()); dyeBackdrop?.addEventListener("click", () => closeDyePicker()); qtyClose?.addEventListener("click", () => closeBatchQtyModal()); qtyBackdrop?.addEventListener("click", () => closeBatchQtyModal()); qtyConfirm?.addEventListener("click", () => confirmBatchQtyModal()); qtyPanel?.addEventListener("mousedown", (event) => { event.stopPropagation(); }); qtyBackdrop?.addEventListener("mousedown", (event) => { event.preventDefault(); }); qtyInput?.addEventListener("keydown", (event) => { if (event.key === "Enter") { confirmBatchQtyModal(); } }); function dyeCraftOptions () { return { dyeColorKey, dyeSlotIndex }; } function clearDyeSelection () { dyeColorKey = null; dyeSlotIndex = null; } function selectedDyeStack (slots = getInventorySlots?.() ?? []) { return resolveDyeDisplayStack(slots, dyeCraftOptions()); } function isTailorStation () { return stationId === CraftingStationId.tailorBench; } function isAnvilStation () { return stationId === CraftingStationId.anvil; } function isWoodworkingStation () { return stationId === CraftingStationId.woodworkingBench; } function isCraftingBenchStation () { return stationId === CraftingStationId.craftingBench; } function isSpinningWheelStation () { return stationId === CraftingStationId.spinningWheel; } function isLoomStation () { return stationId === CraftingStationId.loom; } function isLeatherworkingStation () { return stationId === CraftingStationId.leatherworkingStation; } function isJeweleringStation () { return stationId === CraftingStationId.jeweleringStation; } function usesMaterialDropdown () { return isTailorStation() || isAnvilStation() || isWoodworkingStation() || isCraftingBenchStation() || isSpinningWheelStation() || isLoomStation() || isLeatherworkingStation() || isJeweleringStation(); } /** One grid cell per craft group; material dropdown picks the tier variant. */ function usesCraftGroups () { return isAnvilStation() || isWoodworkingStation() || isCraftingBenchStation() || isSpinningWheelStation() || isLoomStation() || isLeatherworkingStation() || isJeweleringStation(); } function craftGroupKey (recipe) { return recipe.craftGroup ?? recipe.recipeId; } /** Map UI material selection to a recipe's xpMaterial tier. */ function recipeXpMaterialForSelection (recipe, material = selectedMaterial) { if (isCraftingBenchStation() && (recipe.craftGroup === "bow" || recipe.craftGroup === "fishing-rod")) { return woodTierForMetal(material); } if (isCraftingBenchStation() && recipe.craftGroup === "fishing-net") { return clothTierForWood(woodTierForMetal(material)); } return material; } function recipeMatchesMaterial (recipe, material = selectedMaterial) { if (!recipe?.xpMaterial) { return true; } return recipe.xpMaterial === recipeXpMaterialForSelection(recipe, material); } function selectedRecipe () { return recipes.find((recipe) => recipe.recipeId === selectedRecipeId) ?? null; } function stationSkillLevel () { const def = craftingStationDefinition(stationId); return getSkillLevel?.(def?.skillId) ?? 1; } function activeRecipe () { const base = selectedRecipe(); if (!base) { return null; } if (!base.tailorTemplate) { return base; } const slots = getInventorySlots?.() ?? []; const dyeStack = selectedDyeStack(slots); const resolved = resolveTailorCraft({ templateId: base.tailorTemplate, clothTier: selectedMaterial, dyeStack }); return resolved.ok ? resolved.recipe : base; } function refresh () { if (!open) { return; } renderRecipeGrid(); renderDetail(); } function materialOptionsForStation () { if (isTailorStation() || isSpinningWheelStation() || isLoomStation()) { return { order: CLOTH_TIER_ORDER, names: CLOTH_DISPLAY_NAME, fallback: ClothTier.cotton }; } if (isLeatherworkingStation()) { return { order: LEATHER_TIER_ORDER, names: LEATHER_DISPLAY_NAME, fallback: LeatherTier.softLeather }; } if (isJeweleringStation()) { return { order: GEM_TIER_ORDER, names: MATERIAL_DISPLAY_NAME, fallback: GemTier.tourmaline }; } if (isWoodworkingStation()) { return { order: WOOD_TIER_ORDER, names: MATERIAL_DISPLAY_NAME, fallback: WoodTier.pine }; } // Crafting bench mixes metal tools, wood bows, and cloth fishing gear. if (isCraftingBenchStation()) { const names = {}; for (const metal of MATERIAL_TIER_ORDER) { const wood = woodTierForMetal(metal); const cloth = clothTierForWood(wood); names[metal] = `${MATERIAL_DISPLAY_NAME[metal] ?? metal}/` + `${MATERIAL_DISPLAY_NAME[wood] ?? wood}/` + `${CLOTH_DISPLAY_NAME[cloth] ?? cloth}`; } return { order: MATERIAL_TIER_ORDER, names, fallback: MaterialTier.bronze }; } return { order: MATERIAL_TIER_ORDER, names: MATERIAL_DISPLAY_NAME, fallback: MaterialTier.bronze }; } function fillMaterialTiers () { if (!materialTierSelect) { return; } const { order, names, fallback } = materialOptionsForStation(); if (!order.includes(selectedMaterial)) { selectedMaterial = fallback; } materialTierSelect.replaceChildren(); for (const tier of order) { const option = document.createElement("option"); option.value = tier; option.textContent = names[tier] ?? tier; materialTierSelect.append(option); } materialTierSelect.value = selectedMaterial; } function groupedDisplayRecipes () { if (!usesCraftGroups()) { return recipes.map((recipe) => ({ group: craftGroupKey(recipe), recipe })); } const byGroup = new Map(); for (const recipe of recipes) { const group = craftGroupKey(recipe); if (!byGroup.has(group)) { byGroup.set(group, []); } byGroup.get(group).push(recipe); } const rows = []; for (const [group, variants] of byGroup) { if (!variants.some((entry) => recipeMatchesMaterial(entry))) { continue; } const recipe = variants.find((entry) => recipeMatchesMaterial(entry)) ?? variants[0]; rows.push({ group, recipe, variants }); } return rows; } function syncSelectedRecipeFromMaterial () { if (!usesCraftGroups() || !selectedCraftGroup) { return; } const variants = recipes.filter((recipe) => craftGroupKey(recipe) === selectedCraftGroup); if (!variants.length) { return; } let match = variants.find((entry) => recipeMatchesMaterial(entry)); if (!match) { match = variants[0]; if (match?.xpMaterial && !(isCraftingBenchStation() && (match.craftGroup === "bow" || match.craftGroup === "fishing-rod" || match.craftGroup === "fishing-net"))) { selectedMaterial = match.xpMaterial; if (materialTierSelect) { materialTierSelect.value = selectedMaterial; } } } selectedRecipeId = match.recipeId; } function renderRecipeGrid () { recipeGrid.replaceChildren(); for (const row of groupedDisplayRecipes()) { recipeGrid.append(renderRecipeCell(row)); } } function renderRecipeCell ({ group, recipe }) { const def = itemDefinition(recipe.output.itemId); const stack = { itemId: recipe.output.itemId, quantity: recipe.output.quantity | 0 || 1 }; const cell = document.createElement("button"); cell.type = "button"; cell.className = "craft-recipe-cell"; const selected = usesCraftGroups() ? group === selectedCraftGroup : recipe.recipeId === selectedRecipeId; if (selected) { cell.classList.add("selected"); } const skill = stationSkillLevel(); const minLevel = recipe.minSkillLevel | 0 || 1; if ((skill | 0) < minLevel) { cell.classList.add("level-locked"); } else if (canStartCraft(getInventorySlots?.() ?? [], recipe, skill, dyeCraftOptions())) { cell.classList.add("craftable"); } const img = document.createElement("img"); img.alt = def?.displayName ?? "Item"; applyItemIcon(img, stack, itemAssetPathForStack(stack)); const label = document.createElement("span"); label.className = "craft-recipe-name"; if (recipe.tailorTemplate) { label.textContent = def?.displayName?.replace(/^Cotton\s+/i, "") ?? "Garment"; } else if (recipe.craftGroupLabel) { label.textContent = recipe.craftGroupLabel; } else { label.textContent = def?.displayName ?? "Item"; } cell.append(img, label); cell.addEventListener("click", () => { if (crafting) { return; } selectedCraftGroup = group; if (usesCraftGroups()) { syncSelectedRecipeFromMaterial(); } else { selectedRecipeId = recipe.recipeId; } renderRecipeGrid(); renderDetail(); }); return cell; } function renderDetail () { if (materialControls) { materialControls.hidden = !usesMaterialDropdown(); } if (!isTailorStation()) { clearDyeSelection(); } if (dyeControls) { dyeControls.hidden = !isTailorStation(); } const recipe = activeRecipe(); if (!recipe) { detailEmpty.hidden = false; detailBody.hidden = true; return; } detailEmpty.hidden = true; detailBody.hidden = false; const outputStack = { itemId: recipe.output.itemId, quantity: Math.max(1, recipe.output.quantity | 0 || 1), ...(recipe.dyeColor ? { instance: { kind: "clothing", dyeColor: recipe.dyeColor } } : {}) }; applyItemIcon(outputImg, outputStack, itemAssetPathForStack(outputStack)); outputImg.alt = itemDisplayName(outputStack); const qtySuffix = outputStack.quantity > 1 ? ` ×${formatQuantity(outputStack.quantity)}` : ""; outputName.textContent = `${itemDisplayName(outputStack)}${qtySuffix}`; inputsList.replaceChildren(); const slots = getInventorySlots?.() ?? []; for (const input of recipe.inputs ?? []) { inputsList.append(renderInputRow(input, slots)); } if (recipe.consumeDye) { const dye = selectedDyeStack(slots); const row = document.createElement("li"); row.className = "craft-input-row"; row.textContent = dye ? `Dye — 1 use (${dye.instance?.fillLevel ?? "?"}/5, partial bottles first)` : "Dye — none selected"; inputsList.append(row); } if (recipe.consumePotionKey) { inputsList.append(renderPotionChargeRow(recipe.consumePotionKey, slots)); } if (dyePreview && dyeClearBtn) { const dye = selectedDyeStack(slots); if (dye?.itemId === ItemId.dye) { dyePreview.hidden = false; dyeClearBtn.hidden = false; applyItemIcon(dyePreview, dye, itemAssetPathForStack(dye)); } else { dyePreview.hidden = true; dyeClearBtn.hidden = true; } } updateCraftState(slots, recipe); } function renderInputRow (input, slots) { const need = input.quantity | 0; const have = countItemInSlots(slots, input.itemId); const stack = { itemId: input.itemId, quantity: need }; const row = document.createElement("li"); row.className = "craft-input-row"; if (have < need) { row.classList.add("short"); } const img = document.createElement("img"); img.alt = itemDisplayName(stack); applyItemIcon(img, stack, itemAssetPathForStack(stack)); const text = document.createElement("span"); text.textContent = `${itemDisplayName(stack)} — ${formatQuantity(have)} / ${formatQuantity(need)}`; row.append(img, text); return row; } function renderPotionChargeRow (potionKey, slots) { const matches = potionStacksForKey(slots, potionKey); const totalFill = matches.reduce((sum, entry) => sum + entry.fill * stackQuantity(entry.stack.quantity), 0); const label = potionKeyDisplayName(potionKey); const sample = matches[0]?.stack ?? { itemId: ItemId.potion, quantity: 1, instance: { kind: "potion", potionKey, displayName: label, fillLevel: 4 } }; const row = document.createElement("li"); row.className = "craft-input-row"; if (totalFill < 1) { row.classList.add("short"); } const img = document.createElement("img"); img.alt = label; applyItemIcon(img, sample, itemAssetPathForStack(sample)); const text = document.createElement("span"); text.textContent = totalFill >= 1 ? `${label} — 1 use (${totalFill} left)` : `${label} — none`; row.append(img, text); return row; } function updateCraftState (slots, recipe) { // Recipe craft UI (not alchemy): always offer Craft 1 / Craft x / Craft all. const batchVisible = stationId != null; if (craftButton) { craftButton.textContent = batchVisible ? "Craft 1" : "Craft"; } if (craftBatchButton) { craftBatchButton.hidden = !batchVisible; } if (craftBatchAllButton) { craftBatchAllButton.hidden = !batchVisible; } if (crafting) { craftButton.disabled = true; if (craftBatchButton) { craftBatchButton.disabled = true; } if (craftBatchAllButton) { craftBatchAllButton.disabled = true; } return; } const skill = stationSkillLevel(); const minLevel = recipe.minSkillLevel | 0 || 1; if (skill < minLevel) { statusEl.textContent = `Requires skill level ${minLevel} (you have ${skill}).`; craftButton.disabled = true; if (craftBatchButton) { craftBatchButton.disabled = true; } if (craftBatchAllButton) { craftBatchAllButton.disabled = true; } return; } const slotsSnapshot = slots ?? getInventorySlots?.() ?? []; const craftOpts = dyeCraftOptions(); if (canStartCraft(slotsSnapshot, recipe, skill, craftOpts)) { statusEl.textContent = ""; craftButton.disabled = false; if (craftBatchButton) { craftBatchButton.disabled = !batchVisible; } if (craftBatchAllButton) { craftBatchAllButton.disabled = !batchVisible; } return; } if (!hasRecipeInputs(slotsSnapshot, recipe)) { statusEl.textContent = "Missing materials."; } else if (recipe.consumeDye && dyeColorKey != null) { statusEl.textContent = "Not enough bag space (need room for the result or a partially used dye)."; } else if (recipe.consumePotionKey) { statusEl.textContent = "Not enough bag space (need room for the result or a partially used brew)."; } else { statusEl.textContent = "Not enough bag space for the result."; } craftButton.disabled = true; if (craftBatchButton) { craftBatchButton.disabled = true; } if (craftBatchAllButton) { craftBatchAllButton.disabled = true; } } function openDyePicker () { if (!isTailorStation()) { return; } const slots = getInventorySlots?.() ?? []; dyeGrid.replaceChildren(); const choices = listDyePickerChoices(slots); if (!choices.length) { const empty = document.createElement("p"); empty.className = "hint"; empty.textContent = "No dyes in your inventory."; dyeGrid.append(empty); } else { for (const entry of choices) { const cell = document.createElement("button"); cell.type = "button"; cell.className = "craft-recipe-cell craft-dye-cell"; cell.title = "Dye"; const img = document.createElement("img"); applyItemIcon(img, entry.displayStack, itemAssetPathForStack(entry.displayStack)); img.alt = "Dye"; cell.append(img); cell.addEventListener("click", () => { dyeColorKey = entry.colorKey; dyeSlotIndex = entry.displayIndex; closeDyePicker(); renderDetail(); }); dyeGrid.append(cell); } } dyePanel.hidden = false; dyeBackdrop.hidden = false; } function closeDyePicker () { dyePanel.hidden = true; dyeBackdrop.hidden = true; } function maxCraftableCount (slots, recipe) { let max = Number.POSITIVE_INFINITY; for (const input of recipe?.inputs ?? []) { const need = input.quantity | 0; if (need <= 0) { continue; } max = Math.min(max, Math.floor(countItemInSlots(slots, input.itemId) / need)); } if (!Number.isFinite(max)) { return 0; } return Math.max(0, max | 0); } function openBatchQtyModal () { const recipe = activeRecipe(); if (!recipe || crafting) { return; } const slots = getInventorySlots?.() ?? []; if (!canStartCraft(slots, recipe, stationSkillLevel(), dyeCraftOptions())) { renderDetail(); return; } const max = Math.max(1, maxCraftableCount(slots, recipe)); const outStack = { itemId: recipe.output.itemId, quantity: Math.max(1, recipe.output.quantity | 0 || 1) }; if (qtyName) { qtyName.textContent = itemDisplayName(outStack); } if (qtyHint) { qtyHint.textContent = `Max ${formatQuantity(max)}`; } if (qtyInput) { qtyInput.min = "1"; qtyInput.max = String(max); qtyInput.value = "1"; } if (qtyPanel) { qtyPanel.hidden = false; } if (qtyBackdrop) { qtyBackdrop.hidden = false; } qtyInput?.focus(); qtyInput?.select(); } function closeBatchQtyModal () { if (qtyPanel) { qtyPanel.hidden = true; } if (qtyBackdrop) { qtyBackdrop.hidden = true; } } function confirmBatchQtyModal () { const max = Math.max(1, Number(qtyInput?.max) | 0 || 1); const count = Math.min(max, Math.max(1, Number(qtyInput?.value) | 0 || 1)); closeBatchQtyModal(); startCraft({ batchCount: count }); } function startCraft (options = {}) { const recipe = activeRecipe(); if (!recipe || crafting) { return; } const slots = getInventorySlots?.() ?? []; if (!canStartCraft(slots, recipe, stationSkillLevel(), dyeCraftOptions())) { renderDetail(); return; } crafting = true; craftButton.disabled = true; if (craftBatchButton) { craftBatchButton.disabled = true; } if (craftBatchAllButton) { craftBatchAllButton.disabled = true; } progressWrap.classList.add("is-active"); progressFill.style.width = "0%"; statusEl.textContent = options.batchAll ? "Crafting all…" : options.batchCount > 1 ? `Crafting ${options.batchCount}…` : "Crafting…"; craftStartedAt = performance.now(); const equipment = { slots: getEquipment?.() ?? {} }; const durationMs = Math.max( 1, Math.floor(craftDurationMs(recipe, CRAFT_DURATION_MS) * craftDurationMultiplier(equipment)) ); const payload = { type: MessageType.startCraft, stationId, recipeId: selectedRecipeId }; if (options.batchCount != null) { payload.batchCount = options.batchCount | 0; } if (options.batchAll) { payload.batchAll = true; } if (isTailorStation()) { payload.clothTier = selectedMaterial; payload.dyeSlotIndex = dyeSlotIndex; } socket.send(payload); animateProgress(durationMs); craftTimer = window.setTimeout(() => finishCraft(true), durationMs); } function animateProgress (durationMs) { const tick = (now) => { if (!crafting) { return; } const elapsed = now - craftStartedAt; const pct = Math.min(100, (elapsed / durationMs) * 100); progressFill.style.width = `${pct}%`; if (pct < 100) { craftAnimFrame = requestAnimationFrame(tick); } }; craftAnimFrame = requestAnimationFrame(tick); } function stopCraftAnimation () { if (craftTimer != null) { clearTimeout(craftTimer); craftTimer = null; } if (craftAnimFrame != null) { cancelAnimationFrame(craftAnimFrame); craftAnimFrame = null; } crafting = false; progressWrap.classList.remove("is-active"); progressFill.style.width = "0%"; } function finishCraft (timedOut) { stopCraftAnimation(); if (timedOut) { statusEl.textContent = "Waiting for server…"; } renderDetail(); } function openStation (nextStationId) { const def = craftingStationDefinition(nextStationId); if (!def) { return; } stationId = nextStationId; recipes = recipesForStation(nextStationId); const { fallback } = materialOptionsForStation(); selectedMaterial = fallback; dyeSlotIndex = null; dyeColorKey = null; fillMaterialTiers(); const first = groupedDisplayRecipes()[0]; selectedCraftGroup = first?.group ?? null; if (usesCraftGroups()) { syncSelectedRecipeFromMaterial(); } else { selectedRecipeId = first?.recipe?.recipeId ?? recipes[0]?.recipeId ?? null; } stationName.textContent = def.displayName; open = true; panel.hidden = false; backdrop.hidden = false; statusEl.textContent = recipes.length ? "Select a recipe." : "No recipes defined for this station yet."; renderRecipeGrid(); renderDetail(); } function close () { if (crafting) { socket.send({ type: MessageType.cancelCraft }); stopCraftAnimation(); } closeDyePicker(); closeBatchQtyModal(); open = false; stationId = null; recipes = []; selectedRecipeId = null; selectedCraftGroup = null; dyeSlotIndex = null; dyeColorKey = null; panel.hidden = true; backdrop.hidden = true; detailEmpty.hidden = false; detailBody.hidden = true; statusEl.textContent = ""; } function handleCraftResult (message) { if (!open || message.recipeId !== selectedRecipeId) { return; } if (message.ok && message.continuing) { const recipe = activeRecipe(); if (!recipe) { stopCraftAnimation(); renderDetail(); return; } crafting = true; craftButton.disabled = true; if (craftBatchButton) { craftBatchButton.disabled = true; } if (craftBatchAllButton) { craftBatchAllButton.disabled = true; } progressWrap.classList.add("is-active"); progressFill.style.width = "0%"; statusEl.textContent = "Crafting…"; craftStartedAt = performance.now(); const equipment = { slots: getEquipment?.() ?? {} }; const durationMs = Math.max( 1, Math.floor(craftDurationMs(recipe, CRAFT_DURATION_MS) * craftDurationMultiplier(equipment)) ); if (craftTimer != null) { clearTimeout(craftTimer); } if (craftAnimFrame != null) { cancelAnimationFrame(craftAnimFrame); craftAnimFrame = null; } animateProgress(durationMs); craftTimer = window.setTimeout(() => finishCraft(true), durationMs); renderDetail(); return; } stopCraftAnimation(); if (message.ok) { statusEl.textContent = message.burned ? "The food burned." : "Craft complete."; } else { statusEl.textContent = message.error ?? "Could not craft."; } renderDetail(); } return { openStation, close, refresh, handleCraftResult, isOpen () { return open; }, isBlocking () { if (!open) { return false; } if (qtyPanel && !qtyPanel.hidden) { return true; } if (backdrop && !backdrop.hidden) { return true; } const x = GameInput.mousePosition.x; const y = GameInput.mousePosition.y; const box = panel.getBoundingClientRect(); return x >= box.left && x < box.right && y >= box.top && y < box.bottom; }, reset () { close(); } }; }