feat: implement auto-crafting feature for excess stock management
This commit is contained in:
@@ -851,6 +851,64 @@ function O.discardExcess()
|
||||
return didWork
|
||||
end
|
||||
|
||||
-------------------------------------------------
|
||||
-- Auto-craft excess stock into target items
|
||||
-------------------------------------------------
|
||||
|
||||
function O.autoCraft()
|
||||
if #cfg.AUTO_CRAFT_RULES == 0 then return false end
|
||||
if not ctx.craftEngine then return false end
|
||||
if not ctx.craftTurtleName then return false end
|
||||
|
||||
local catalogue = cache.catalogue
|
||||
local didWork = false
|
||||
|
||||
for _, rule in ipairs(cfg.AUTO_CRAFT_RULES) do
|
||||
local inputName = rule.input
|
||||
local reserve = rule.reserve or 0
|
||||
local outputName = rule.output
|
||||
|
||||
if catalogue[inputName] then
|
||||
local totalInStorage = 0
|
||||
for _, src in ipairs(catalogue[inputName]) do
|
||||
totalInStorage = totalInStorage + src.total
|
||||
end
|
||||
|
||||
local excess = totalInStorage - reserve
|
||||
if excess > 0 then
|
||||
-- Figure out how many input items per craft batch
|
||||
local recipe = cfg.recipeBook.getCraftingRecipe(outputName)
|
||||
if recipe then
|
||||
local ingredients = cfg.recipeBook.getIngredients(recipe)
|
||||
local inputPerCraft = ingredients[inputName] or 0
|
||||
if inputPerCraft > 0 then
|
||||
-- How many batches can we do with the excess?
|
||||
local batches = math.floor(excess / inputPerCraft)
|
||||
-- Limit to a reasonable amount per cycle to avoid blocking
|
||||
batches = math.min(batches, 64)
|
||||
if batches > 0 then
|
||||
local craftCount = batches * recipe.count
|
||||
log.info("AUTOCRAFT", "%s: %d excess (reserve %d), crafting %d x %s",
|
||||
inputName, excess, reserve, craftCount, outputName)
|
||||
local ok, err = O.recursiveCraft(outputName, craftCount)
|
||||
if ok then
|
||||
didWork = true
|
||||
log.info("AUTOCRAFT", "Crafted %s x%d", outputName, craftCount)
|
||||
else
|
||||
log.warn("AUTOCRAFT", "Failed to craft %s: %s", outputName, tostring(err))
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
log.warn("AUTOCRAFT", "No recipe found for output: %s", outputName)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return didWork
|
||||
end
|
||||
|
||||
-------------------------------------------------
|
||||
-- Low-stock alert checker
|
||||
-------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user