Crafting improvements

master
D4VID 10 months ago
parent bb0ba44c11
commit d9127165c5

@ -1,7 +1,7 @@
local component = require("component") local component = require("component")
local robot = require("robot") local robot = require("robot")
local sides = require("sides") local sides = require("sides")
local event = require("event") local io = require("io")
local recipes = require('recipes') local recipes = require('recipes')
@ -70,8 +70,9 @@ end
local function confirm() local function confirm()
while true do while true do
print("Proceed? [y/n]") print("Proceed? [y/n]")
local _, _, ascii, _, _ = event.pull("key_down") -- local _, _, ascii, _, _ = term.pull("key_down")
local ch = string.char(ascii) -- local ch = string.char(ascii)
local ch = io.read()
if ch == 'y' then if ch == 'y' then
return true return true
elseif ch == 'n' then elseif ch == 'n' then
@ -147,8 +148,13 @@ end
---Returns number of items fetched ---Returns number of items fetched
---@param item string ---@param item string
---@param output_slot number ---@param output_slot number
---@param count number how many items to take
---@return boolean ---@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, ";") local name, damage = parse_item(item, ";")
robot.select(output_slot) robot.select(output_slot)
local size = inventory.getInventorySize(sides.forward) local size = inventory.getInventorySize(sides.forward)
@ -161,7 +167,7 @@ local function fetch_item(item, output_slot)
local stack = inventory.getStackInSlot(sides.forward, slot) local stack = inventory.getStackInSlot(sides.forward, slot)
if stack then if stack then
if stack.name == name and stack.damage == damage then if stack.name == name and stack.damage == damage then
inventory.suckFromSlot(sides.forward, slot, 1) inventory.suckFromSlot(sides.forward, slot, count)
return true return true
end end
end end
@ -173,15 +179,16 @@ end
---accepts a recipe table containing result and ingredients ---accepts a recipe table containing result and ingredients
---@param recipe table ---@param recipe table
---@param count number how many items to craft
---@return boolean ---@return boolean
local function craft_single_recipe(recipe) local function craft_single_recipe(recipe, count)
if inventory.getStackInInternalSlot(RESULT_SLOT) then if inventory.getStackInInternalSlot(RESULT_SLOT) then
print('Cannot craft - output slot occupied') print('Cannot craft - output slot occupied')
return false return false
end end
for i, ingredient in pairs(recipe.shape) do for i, ingredient in pairs(recipe.shape) do
-- print('Fetching ' .. ingredient) -- 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) print('Failed to fetch ' .. ingredient)
confirm() confirm()
end end
@ -191,7 +198,7 @@ local function craft_single_recipe(recipe)
robot.select(RESULT_SLOT) robot.select(RESULT_SLOT)
-- craft the resulting item -- craft the resulting item
local crafted = crafting.craft(1) local crafted = crafting.craft()
if not crafted then if not crafted then
print('Crafting failed') print('Crafting failed')
return false return false
@ -278,16 +285,56 @@ local function craft_recipe_recursion(recipe, available_items, currently_availab
end end
---accepts a recipe table ---accepts a recipe table
---@param item string ---@param search string search for an craftable item
---@return boolean ---@return boolean
local function craft_item(item) local function craft_item(search)
print('Trying to craft ' .. item) search = string.lower(search) -- convert to unified lowercase
local recipe = recipes[item]
if not recipe then local matches = {}
print('Cannot craft ' .. item) 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 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 end
local recipe = recipes[selected]
print('Trying to craft ' .. recipe.result)
local available_items = read_chest_contents() local available_items = read_chest_contents()
if not available_items then if not available_items then
print('Not facing a chest') print('Not facing a chest')
@ -332,7 +379,7 @@ local function craft_item(item)
-- proceed to crafting the things -- proceed to crafting the things
for i, rec in ipairs(crafting_order) do for i, rec in ipairs(crafting_order) do
print('Step #' .. i .. ' - ' .. rec.result) print('Step #' .. i .. ' - ' .. rec.result)
if not craft_single_recipe(rec) then if not craft_single_recipe(rec, 1) then
print('Unexpected error') print('Unexpected error')
return false return false
end end
@ -350,6 +397,3 @@ end
craft_item(target) craft_item(target)
-- craft_item('minecraft:iron_pickaxe;0')
-- craft_item('opencomputers:keyboard;0')
-- craft_item('opencomputers:material;10')

Loading…
Cancel
Save