Refactor crafting helpers to delegate functionality to shared UI module

This commit is contained in:
MayaTheShy
2026-03-22 01:56:40 -04:00
parent 8cdb2d3b98
commit 39b95b3663

View File

@@ -1007,73 +1007,34 @@ local function drawDashboard()
end end
------------------------------------------------- -------------------------------------------------
-- Crafting helpers -- Crafting helpers (delegated to shared ui module)
------------------------------------------------- -------------------------------------------------
--- Get ingredient counts from a recipe's grid --- Get stock total for an item from catalogue
local function getItemTotal(itemName)
local have = 0
if cache.catalogue[itemName] then
for _, src in ipairs(cache.catalogue[itemName]) do
have = have + src.total
end
end
return have
end
local function getRecipeIngredients(recipe) local function getRecipeIngredients(recipe)
local ingredients = {} return ui.getRecipeIngredients(recipe)
for _, item in ipairs(recipe.grid) do
if item then
ingredients[item] = (ingredients[item] or 0) + 1
end
end
return ingredients
end end
--- Check if a recipe can be crafted with current stock
local function canCraftRecipe(recipe) local function canCraftRecipe(recipe)
local ingredients = getRecipeIngredients(recipe) return ui.canCraftRecipe(recipe, getItemTotal)
for itemName, needed in pairs(ingredients) do
local have = 0
if cache.catalogue[itemName] then
for _, src in ipairs(cache.catalogue[itemName]) do
have = have + src.total
end
end
if have < needed then return false end
end
return true
end end
--- How many times a recipe can be crafted
local function maxCraftBatches(recipe) local function maxCraftBatches(recipe)
local ingredients = getRecipeIngredients(recipe) return ui.maxCraftBatches(recipe, getItemTotal)
local minBatches = math.huge
for itemName, needed in pairs(ingredients) do
local have = 0
if cache.catalogue[itemName] then
for _, src in ipairs(cache.catalogue[itemName]) do
have = have + src.total
end
end
local batches = math.floor(have / needed)
if batches < minBatches then minBatches = batches end
end
if minBatches == math.huge then return 0 end
return minBatches
end end
--- Get list of missing ingredients for a recipe
local function getMissingIngredients(recipe) local function getMissingIngredients(recipe)
local ingredients = getRecipeIngredients(recipe) return ui.getMissingIngredients(recipe, getItemTotal)
local missing = {}
for itemName, needed in pairs(ingredients) do
local have = 0
if cache.catalogue[itemName] then
for _, src in ipairs(cache.catalogue[itemName]) do
have = have + src.total
end
end
if have < needed then
table.insert(missing, {
name = itemName,
have = have,
need = needed,
})
end
end
return missing
end end
--- Execute a craft via the networked turtle. --- Execute a craft via the networked turtle.