From d30888cb69b67ddacbed8b169d9e19ab1c7898e4 Mon Sep 17 00:00:00 2001 From: D4VID Date: Tue, 24 Dec 2024 16:20:51 +0100 Subject: [PATCH] Allow crafting multiple items --- craft.lua | 98 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/craft.lua b/craft.lua index 7275b9b..6b95da3 100644 --- a/craft.lua +++ b/craft.lua @@ -326,8 +326,9 @@ end ---accepts a recipe table ---@param search string search for an craftable item +---@param amount number how many items to craft ---@return boolean -local function craft_item(search) +local function craft_item(search, amount) search = string.lower(search) -- convert to unified lowercase local matches = {} @@ -374,67 +375,72 @@ local function craft_item(search) local recipe = recipes[selected] - print('Trying to craft ' .. recipe.result) + print('Trying to craft ' .. amount .. 'x ' .. recipe.result) - local available_items = read_chest_contents() - if not available_items then - print('Not facing a chest') - return false - end + local repeating = false - local required_items, crafting_order = craft_recipe_recursion(recipe, clone(available_items), {}) + local count = math.ceil(amount / recipe.count) - print('\nRequires these items:') - local failed = false - for required_item, required_count in pairs(required_items) do - local available = available_items[required_item] - if not available then - available = 0 - end - if available < required_count then - print(available .. "/" .. required_count .. "\t" .. required_item .. "\t- Missing " .. required_count - available) - failed = true - else - print(available .. "/" .. required_count .. "\t" .. required_item) + for _ = 1, count do + local available_items = read_chest_contents() + if not available_items then + print('Not facing a chest') + return false end - end - if failed then - return false - end - if not confirm() then - print('Operation cancelled') - return false - end + local required_items, crafting_order = craft_recipe_recursion(recipe, clone(available_items), {}) - -- print('\nCrafting in order:') - -- for i, rec in ipairs(crafting_order) do - -- print(i, rec.result) - -- end - - -- if not confirm() then - -- print('Operation cancelled') - -- return false - -- end - - -- proceed to crafting the things - for i, rec in ipairs(crafting_order) do - print('Step #' .. i .. ' - ' .. rec.result) - if not craft_single_recipe(rec, 1) then - print('Unexpected error') + print('\nRequires these items:') + local failed = false + for required_item, required_count in pairs(required_items) do + local available = available_items[required_item] + if not available then + available = 0 + end + if available < required_count then + print(available .. "/" .. required_count .. "\t" .. required_item .. "\t- Missing " .. required_count - available) + failed = true + else + print(available .. "/" .. required_count .. "\t" .. required_item) + end + end + if failed then return false end + + if not repeating then + if not confirm() then + print('Operation cancelled') + return false + end + end + + -- proceed to crafting the things + local steps = #crafting_order + for i, rec in ipairs(crafting_order) do + print('Step ' .. i .. '/' .. steps .. ' - ' .. rec.result) + if not craft_single_recipe(rec, 1) then + print('Unexpected error') + return false + end + end + + repeating = true end return true end -local target = ... +local target, amount = ... if not target then - print("Usage: craft ") + print("Usage: craft [count]") return end -craft_item(target) +if not amount then + amount = 1 +end + +craft_item(target, amount)