From d9127165c55d9256f854b6c97d7e69e8452b31f8 Mon Sep 17 00:00:00 2001 From: D4VID Date: Thu, 14 Nov 2024 21:32:04 +0100 Subject: [PATCH] Crafting improvements --- craft.lua | 80 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 18 deletions(-) diff --git a/craft.lua b/craft.lua index b1e2e0a..aaefad5 100644 --- a/craft.lua +++ b/craft.lua @@ -1,7 +1,7 @@ local component = require("component") local robot = require("robot") local sides = require("sides") -local event = require("event") +local io = require("io") local recipes = require('recipes') @@ -70,8 +70,9 @@ end local function confirm() while true do print("Proceed? [y/n]") - local _, _, ascii, _, _ = event.pull("key_down") - local ch = string.char(ascii) + -- local _, _, ascii, _, _ = term.pull("key_down") + -- local ch = string.char(ascii) + local ch = io.read() if ch == 'y' then return true elseif ch == 'n' then @@ -147,8 +148,13 @@ end ---Returns number of items fetched ---@param item string ---@param output_slot number +---@param count number how many items to take ---@return boolean -local function fetch_item(item, output_slot) +local function fetch_item(item, output_slot, count) + if count == nil then + count = 1 + end + local name, damage = parse_item(item, ";") robot.select(output_slot) local size = inventory.getInventorySize(sides.forward) @@ -161,7 +167,7 @@ local function fetch_item(item, output_slot) local stack = inventory.getStackInSlot(sides.forward, slot) if stack then if stack.name == name and stack.damage == damage then - inventory.suckFromSlot(sides.forward, slot, 1) + inventory.suckFromSlot(sides.forward, slot, count) return true end end @@ -173,15 +179,16 @@ end ---accepts a recipe table containing result and ingredients ---@param recipe table +---@param count number how many items to craft ---@return boolean -local function craft_single_recipe(recipe) +local function craft_single_recipe(recipe, count) if inventory.getStackInInternalSlot(RESULT_SLOT) then print('Cannot craft - output slot occupied') return false end for i, ingredient in pairs(recipe.shape) do -- print('Fetching ' .. ingredient) - if not fetch_item(ingredient, slot_map[i]) then + if not fetch_item(ingredient, slot_map[i], count) then print('Failed to fetch ' .. ingredient) confirm() end @@ -191,7 +198,7 @@ local function craft_single_recipe(recipe) robot.select(RESULT_SLOT) -- craft the resulting item - local crafted = crafting.craft(1) + local crafted = crafting.craft() if not crafted then print('Crafting failed') return false @@ -278,16 +285,56 @@ local function craft_recipe_recursion(recipe, available_items, currently_availab end ---accepts a recipe table ----@param item string +---@param search string search for an craftable item ---@return boolean -local function craft_item(item) - print('Trying to craft ' .. item) - local recipe = recipes[item] - if not recipe then - print('Cannot craft ' .. item) +local function craft_item(search) + search = string.lower(search) -- convert to unified lowercase + + local matches = {} + for key, value in pairs(recipes) do + if string.find(value.result, search) then + print(#matches+1 .. ") " .. value.result) + matches[#matches+1] = key + elseif value.name ~= nil then + if string.find(value.name, search) then + print(#matches+1 .. ") " .. value.result) + matches[#matches+1] = key + end + end + end + + local selected + if #matches == 0 then + print(selected .. ' not found') return false + elseif #matches == 1 then + selected = matches[1] + else + local input = io.read() + if not input then + print('Interrupted') + return false + end + + local index = tonumber(input) + if not index then + print('Invalid index') + return false + end + + if index < 1 or index > #matches then + print('Invalid index') + return false + end + + selected = matches[index] end + + local recipe = recipes[selected] + + print('Trying to craft ' .. recipe.result) + local available_items = read_chest_contents() if not available_items then print('Not facing a chest') @@ -332,7 +379,7 @@ local function craft_item(item) -- proceed to crafting the things for i, rec in ipairs(crafting_order) do print('Step #' .. i .. ' - ' .. rec.result) - if not craft_single_recipe(rec) then + if not craft_single_recipe(rec, 1) then print('Unexpected error') return false end @@ -350,6 +397,3 @@ end craft_item(target) --- craft_item('minecraft:iron_pickaxe;0') --- craft_item('opencomputers:keyboard;0') --- craft_item('opencomputers:material;10')