feat: add new crafting recipes for cobblestone and stone variants; enhance auto-crafting logic for excess items

This commit is contained in:
MayaTheShy
2026-03-25 18:11:23 -04:00
parent f327f82677
commit 1606d60a06
5 changed files with 203 additions and 9 deletions

View File

@@ -856,13 +856,13 @@ end
-------------------------------------------------
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
-- Phase 1: Explicit rules from auto_craft.lua
for _, rule in ipairs(cfg.AUTO_CRAFT_RULES) do
local inputName = rule.input
local reserve = rule.reserve or 0
@@ -876,15 +876,12 @@ function O.autoCraft()
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
@@ -906,6 +903,75 @@ function O.autoCraft()
end
end
-- Phase 2: Smart excess-to-craft — auto-discover recipes for over-stocked items
if cfg.AUTO_CRAFT_FROM_EXCESS then
-- Track outputs we've already handled via explicit rules to avoid duplicates
local handledOutputs = {}
for _, rule in ipairs(cfg.AUTO_CRAFT_RULES) do
handledOutputs[rule.output] = true
end
for itemName, maxCount in pairs(cfg.STOCK_LIMITS) do
if catalogue[itemName] then
local totalInStorage = 0
for _, src in ipairs(catalogue[itemName]) do
totalInStorage = totalInStorage + src.total
end
local excess = totalInStorage - maxCount
if excess > 0 then
-- Find all crafting recipes that use this item
local usingRecipes = cfg.recipeBook.findRecipesUsing(itemName)
for _, recipe in ipairs(usingRecipes) do
if not handledOutputs[recipe.output] then
-- Check how much of the output we already have
local outputTotal = O.getItemTotal(recipe.output)
if outputTotal < cfg.AUTO_CRAFT_OUTPUT_CAP then
local ingredients = cfg.recipeBook.getIngredients(recipe)
local inputPerCraft = ingredients[itemName] or 0
if inputPerCraft > 0 then
-- Only craft up to the output cap
local outputRoom = cfg.AUTO_CRAFT_OUTPUT_CAP - outputTotal
local maxBatches = math.floor(excess / inputPerCraft)
local batchesByRoom = math.ceil(outputRoom / recipe.count)
local batches = math.min(maxBatches, batchesByRoom, 64)
if batches > 0 then
-- Check all other ingredients are available
local canCraft = true
for ingr, needed in pairs(ingredients) do
if ingr ~= itemName then
local have = O.getItemTotal(ingr)
if have < needed * batches then
canCraft = false
break
end
end
end
if canCraft then
local craftCount = batches * recipe.count
log.info("AUTOCRAFT", "Smart: %s over limit, crafting %d x %s",
itemName, craftCount, recipe.output)
local ok, err = O.recursiveCraft(recipe.output, craftCount)
if ok then
didWork = true
handledOutputs[recipe.output] = true
log.info("AUTOCRAFT", "Smart-crafted %s x%d", recipe.output, craftCount)
-- Recalculate excess after crafting
break
else
log.warn("AUTOCRAFT", "Smart craft failed %s: %s", recipe.output, tostring(err))
end
end
end
end
end
end
end
end
end
end
end
return didWork
end