Refactor crafting helpers to delegate functionality to shared UI module
This commit is contained in:
@@ -1007,73 +1007,34 @@ local function drawDashboard()
|
||||
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 ingredients = {}
|
||||
for _, item in ipairs(recipe.grid) do
|
||||
if item then
|
||||
ingredients[item] = (ingredients[item] or 0) + 1
|
||||
end
|
||||
end
|
||||
return ingredients
|
||||
return ui.getRecipeIngredients(recipe)
|
||||
end
|
||||
|
||||
--- Check if a recipe can be crafted with current stock
|
||||
local function canCraftRecipe(recipe)
|
||||
local ingredients = getRecipeIngredients(recipe)
|
||||
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
|
||||
return ui.canCraftRecipe(recipe, getItemTotal)
|
||||
end
|
||||
|
||||
--- How many times a recipe can be crafted
|
||||
local function maxCraftBatches(recipe)
|
||||
local ingredients = getRecipeIngredients(recipe)
|
||||
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
|
||||
return ui.maxCraftBatches(recipe, getItemTotal)
|
||||
end
|
||||
|
||||
--- Get list of missing ingredients for a recipe
|
||||
local function getMissingIngredients(recipe)
|
||||
local ingredients = getRecipeIngredients(recipe)
|
||||
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
|
||||
return ui.getMissingIngredients(recipe, getItemTotal)
|
||||
end
|
||||
|
||||
--- Execute a craft via the networked turtle.
|
||||
|
||||
Reference in New Issue
Block a user