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)
|
return ui.getMissingIngredients(recipe, O.getItemTotal)
|
||||||
end
|
end
|
||||||
|
|
||||||
function O.craftItem(recipeIdx)
|
function O.craftItem(recipeIdx, batches)
|
||||||
|
batches = batches or 1
|
||||||
local recipe = cfg.CRAFTABLE[recipeIdx]
|
local recipe = cfg.CRAFTABLE[recipeIdx]
|
||||||
if not recipe then
|
if not recipe then
|
||||||
log.error("CRAFT", "Invalid recipe index: %s", tostring(recipeIdx))
|
log.error("CRAFT", "Invalid recipe index: %s", tostring(recipeIdx))
|
||||||
@@ -1181,7 +1182,11 @@ function O.craftItem(recipeIdx)
|
|||||||
return false, "Turtle offline"
|
return false, "Turtle offline"
|
||||||
end
|
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
|
activity.crafting = true
|
||||||
state.needsRedraw = true
|
state.needsRedraw = true
|
||||||
@@ -1191,6 +1196,27 @@ function O.craftItem(recipeIdx)
|
|||||||
local slotMap = {}
|
local slotMap = {}
|
||||||
local reservedSlots = {}
|
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
|
for gridPos = 1, 9 do
|
||||||
local itemName = recipe.grid[gridPos]
|
local itemName = recipe.grid[gridPos]
|
||||||
if itemName then
|
if itemName then
|
||||||
@@ -1204,11 +1230,12 @@ function O.craftItem(recipeIdx)
|
|||||||
for slot, slotItem in pairs(chest.list()) do
|
for slot, slotItem in pairs(chest.list()) do
|
||||||
local key = source.chest .. ":" .. slot
|
local key = source.chest .. ":" .. slot
|
||||||
if slotItem.name == itemName and not reservedSlots[key] then
|
if slotItem.name == itemName and not reservedSlots[key] then
|
||||||
|
local pullCount = math.min(batches, slotItem.count)
|
||||||
slotMap[tostring(turtleSlot)] = {
|
slotMap[tostring(turtleSlot)] = {
|
||||||
chestName = source.chest,
|
chestName = source.chest,
|
||||||
chestSlot = slot,
|
chestSlot = slot,
|
||||||
itemName = itemName,
|
itemName = itemName,
|
||||||
count = 1,
|
count = pullCount,
|
||||||
}
|
}
|
||||||
reservedSlots[key] = true
|
reservedSlots[key] = true
|
||||||
found = true
|
found = true
|
||||||
|
|||||||
Reference in New Issue
Block a user