Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("recipes")
- os.loadAPI("moveAPI.lua")
- resources = {}
- position = 1
- regex = "([ri])(%d+)x?(%d*)"
- function moveTo(spot)
- local currentLevel = math.floor(position / 10)
- local currentNumber = position % 10
- local newLevel = math.floor(spot / 10)
- local newNumber = spot % 10
- moveAPI.moveRelative(newNumber - currentNumber, newLevel - currentLevel)
- position = spot
- end
- function findFreeSlot()
- local freeSlot = -1
- local check = 16
- while freeSlot == -1 do
- turtle.select(check)
- if turtle.getItemCount() == 0 then
- freeSlot = check
- end
- check = check - 1
- end
- return freeSlot
- end
- function gatherResources(recipe, times)
- local list = listResources(recipe, 1)
- for i = 1, #list do
- if(resources["i" .. list[i].item] == nil) then
- resources["i" .. list[i].item] = { item = list[i].item, amount = 0 }
- end
- resources["i" .. list[i].item].amount = resources["i" .. list[i].item].amount + list[i].amount
- end
- for key, value in pairs(resources) do
- value.slot = findFreeSlot()
- moveTo(value.item)
- turtle.turnRight()
- turtle.select(value.slot)
- turtle.suck(value.amount * times)
- turtle.turnLeft()
- print(key .. ": " .. value.amount)
- end
- end
- function listResources(recipe, times)
- local result = {}
- for cur in recipes.list[recipe].recipe:gmatch("%S+") do
- local _, _, type, pos, amount = string.find(cur, regex)
- if amount == "" then
- amount = "1"
- end
- if type == "i" then
- result[#result + 1] = { item = pos, amount = amount * times }
- else
- local list = listResources(type .. pos, tonumber(amount) * times)
- for i = 1, #list do
- result[#result + 1] = { item = list[i].item, amount = list[i].amount }
- end
- end
- end
- return result
- end
- function craft(recipe, times)
- for subRecipe in recipes.list[recipe].recipe:gmatch("r%d+x?%d*") do
- craft(subRecipe, times)
- end
- moveTo(tonumber(recipe:match("%d+")))
- turtle.turnLeft()
- for req in recipes.list[recipe].recipe:gmatch("%S+") do
- local _, _, type, pos, amount = string.find(req , regex)
- turtle.select(resources[type .. pos].slot)
- if type == "i" then
- turtle.drop(tonumber(amount) * times)
- else
- turtle.drop()
- end
- end
- resources[recipe] = { slot = findFreeSlot() }
- turtle.select(1)
- os.sleep(times + 2)
- while turtle.getItemCount() < times do
- os.sleep(0.5)
- end
- turtle.transferTo(resources[recipe].slot)
- turtle.turnRight()
- end
- function mainCraft(recipe, times)
- gatherResources(recipe, times)
- craft(recipe, times)
- moveTo(1)
- end
- function ask()
- local recipeList = recipes.createTree()
- local options = "["
- for k, v in pairs(recipeList) do
- options = options .. k .. ","
- end
- print("What do you want to make?")
- print(options:sub(1, #options - 1) .. "]")
- local chosenGroup = read()
- local options = "["
- for k, v in pairs(recipeList[chosenGroup]) do
- options = options .. k .. ","
- end
- print("\nWhat do you want to make?")
- print(options:sub(1, #options - 1) .. "]")
- local chosenItem = read()
- print("\nHow many times?")
- local amount = tonumber(read())
- moveAPI.move("u b b dl d d d d")
- mainCraft(recipeList[chosenGroup][chosenItem].iden, amount)
- moveAPI.move("u u u u dr f f d")
- end
- ask()
Add Comment
Please, Sign In to add comment