feat: enhance crafting function to support batch processing with ingredient validation
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user