From 62a9ab811d630246c5612a3da005d0f15c556b40 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Wed, 25 Mar 2026 21:45:39 -0400 Subject: [PATCH] feat: enhance crafting function to support batch processing with ingredient validation --- manager/operations.lua | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/manager/operations.lua b/manager/operations.lua index 51c18d5..83d7301 100644 --- a/manager/operations.lua +++ b/manager/operations.lua @@ -1162,7 +1162,8 @@ function O.getMissingIngredients(recipe) return ui.getMissingIngredients(recipe, O.getItemTotal) end -function O.craftItem(recipeIdx) +function O.craftItem(recipeIdx, batches) + batches = batches or 1 local recipe = cfg.CRAFTABLE[recipeIdx] if not recipe then log.error("CRAFT", "Invalid recipe index: %s", tostring(recipeIdx)) @@ -1181,7 +1182,11 @@ function O.craftItem(recipeIdx) return false, "Turtle offline" end - log.info("CRAFT", "Starting craft: %s (turtle: %s)", recipe.output, ctx.craftTurtleName) + -- Clamp batches to 64 (max stack size per slot) + batches = math.min(batches, 64) + + log.info("CRAFT", "Starting craft: %s x%d (%d batches, turtle: %s)", + recipe.output, batches * recipe.count, batches, ctx.craftTurtleName) activity.crafting = true state.needsRedraw = true @@ -1191,6 +1196,27 @@ function O.craftItem(recipeIdx) local slotMap = {} local reservedSlots = {} + -- Sum how many of each ingredient we need total + local ingredientTotals = {} + for gridPos = 1, 9 do + local itemName = recipe.grid[gridPos] + if itemName then + ingredientTotals[itemName] = (ingredientTotals[itemName] or 0) + batches + end + end + + -- Check we have enough of each ingredient + for itemName, needed in pairs(ingredientTotals) do + local have = O.getItemTotal(itemName) + if have < needed then + log.error("CRAFT", "Not enough %s: have %d, need %d", itemName, have, needed) + activity.crafting = false + state.needsRedraw = true + state.smelterNeedsRedraw = true + return false, string.format("Need %d %s, have %d", needed, itemName, have) + end + end + for gridPos = 1, 9 do local itemName = recipe.grid[gridPos] if itemName then @@ -1204,11 +1230,12 @@ function O.craftItem(recipeIdx) for slot, slotItem in pairs(chest.list()) do local key = source.chest .. ":" .. slot if slotItem.name == itemName and not reservedSlots[key] then + local pullCount = math.min(batches, slotItem.count) slotMap[tostring(turtleSlot)] = { chestName = source.chest, chestSlot = slot, itemName = itemName, - count = 1, + count = pullCount, } reservedSlots[key] = true found = true