Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- v0.3 by meigrafd @ 28.08.2015
- Requires:
- ComputerCraft >=1.4
- A Turtle have 16 Slots. For crafting the first 3 rows and slots are used and must match an valid recipe. All other Slots must be free!
- Place a valid recipe into Turtle before first Script Start up.
- Place Chests around the Turtle as Input and Drop.. Only use one Chest for one Type of Item!
- Script will inspect each Side and trys to detect the Item in each Chest.
- After that it saves all data for future startups and starts auto-crafting.
- For a later use of another recipe simple delete the directory 'recipe'.
- --]]
- ---[[ CONFIG - START ]]
- -- Chest Side for crafted item. Only valid sides: top, bottom, left, right, front, back
- -- NOTE: Fastest will be "top", "bottom" or "front". For all other it must turn around..
- -- BTW: Put the most needed Items in the 'fastest' chest to speedup crafting :)
- local OutDirection = "front"
- -- Use Redstone signal to turn crafting on/off ?
- local ControlSignal = true
- -- Redstone signal Side
- -- NOTE: "left" doesnt work.
- local ControlSide = "right"
- ---[[ CONFIG - END ]]
- ---[[ functions ]]
- -- http://stackoverflow.com/questions/1426954/split-string-in-lua
- function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table, cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function stringMatch(text, find)
- if string.find(string.lower(text), string.lower(find)) then
- return true
- else
- return false
- end
- end
- function lappend(list, delimiter, value)
- if list == '' then
- local list = value
- else
- local list = list..delimiter..value
- end
- return list
- end
- local turn = function(side)
- if side == "left" then
- turtle.turnLeft()
- elseif side == "right" then
- turtle.turnRight()
- elseif side == "front" then
- turtle.turnRight()
- turtle.turnRight()
- elseif side == "back" then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- local suck = function(direction, amount)
- amount = amount or 1
- s = {
- ["top"] = function() turtle.suckUp(amount); end,
- ["bottom"] = function() turtle.suckDown(amount); end,
- ["front"] = function() turtle.suck(amount); end,
- ["left"] = function() turn("left"); turtle.suck(amount); turn("right"); end,
- ["right"] = function() turn("right"); turtle.suck(amount); turn("left"); end,
- ["back"] = function() turn("back"); turtle.suck(amount); turn("front"); end
- }
- s[direction]()
- end
- local drop = function(direction, amount)
- amount = amount or 1
- d = {
- ["top"] = function() turtle.dropUp(amount); end,
- ["bottom"] = function() turtle.dropDown(amount); end,
- ["front"] = function() turtle.drop(amount); end,
- ["left"] = function() turn("left"); turtle.drop(amount); turn("right"); end,
- ["right"] = function() turn("right"); turtle.drop(amount); turn("left"); end,
- ["back"] = function() turn("back"); turtle.drop(amount); turn("front"); end
- }
- d[direction]()
- end
- local craft = function()
- turtle.select(OutSlot)
- turtle.craft()
- drop(OutDirection, turtle.getItemCount(OutSlot))
- Count = Count + 1
- print("crafted: "..Count)
- end
- local saveRecipe = function()
- print("Trying to get Recipe..")
- for i=1, #InSlots do
- local data = turtle.getItemDetail(InSlots[i])
- if data then
- local n = data.name
- local d = data.damage
- local c = data.count
- --maybe same name but hopeful different damage
- print(InSlots[i]..": "..n.." -> damage: "..d)
- RecipeItemName[InSlots[i]] = n
- RecipeItemDamage[InSlots[i]] = d
- RecipeItemCount[InSlots[i]] = c
- -- create own dir for each slot and save data..
- if not fs.isDir("recipe/"..InSlots[i]) then
- fs.makeDir("recipe/"..InSlots[i])
- end
- if fs.exists("recipe/"..InSlots[i].."/name") then
- fs.delete("recipe/"..InSlots[i].."/name")
- end
- local fileObject = fs.open("recipe/"..InSlots[i].."/name", "w")
- fileObject.write(tostring(n))
- fileObject.close()
- if fs.exists("recipe/"..InSlots[i].."/damage") then
- fs.delete("recipe/"..InSlots[i].."/damage")
- end
- local fileObject = fs.open("recipe/"..InSlots[i].."/damage", "w")
- fileObject.write(tostring(d))
- fileObject.close()
- if fs.exists("recipe/"..InSlots[i].."/count") then
- fs.delete("recipe/"..InSlots[i].."/count")
- end
- local fileObject = fs.open("recipe/"..InSlots[i].."/count", "w")
- fileObject.write(tostring(c))
- fileObject.close()
- end
- end
- end
- --[[ Internal values ]]
- -- Mapping of slots in the turtles inventory used for recipe.
- InSlots = {1,2,3, 5,6,7, 9,10,11}
- OutSlot = 16
- ChestItem = {}
- RecipeItemName = {}
- RecipeItemDamage = {}
- RecipeItemCount = {}
- Count = 0
- --[[ Main program ]]
- if not fs.isDir("recipe") then
- -- Verify valid recipe
- if turtle.craft(0) == false then
- print("ERROR: Invalid recipe! Please set up correctly.")
- error()
- end
- fs.makeDir("recipe")
- saveRecipe()
- -- craft recipe once to get all slots free
- craft()
- else
- -- get recipe out saved data files for each slot..
- print("Trying to get saved Recipe..")
- local flistTable = fs.find("/recipe/*/name")
- for _,file in pairs(flistTable) do
- local directory = fs.getDir(file)
- local tmp = split(directory,"/")
- local slot = tmp[2]
- local fileObject = fs.open(directory.."/name", "r")
- local n = fileObject.readLine()
- fileObject.close()
- local fileObject = fs.open(directory.."/damage", "r")
- local d = fileObject.readLine()
- fileObject.close()
- local fileObject = fs.open(directory.."/count", "r")
- local c = fileObject.readLine()
- fileObject.close()
- print(slot..": "..n.." -> damage: "..d)
- RecipeItemName[slot] = n
- RecipeItemDamage[slot] = d
- RecipeItemCount[slot] = c
- end
- end
- -- Verify needed items for recipe with items in each chest..
- print("Trying to get Sides for each Item..")
- turtle.select(OutSlot)
- for i,side in pairs(rs.getSides()) do
- if (side ~= OutDirection) and (peripheral.isPresent(side)) then
- suck(side)
- for slot in pairs(RecipeItemName) do
- local data = turtle.getItemDetail(OutSlot)
- if data then
- local n = data.name
- local d = data.damage
- local c = data.count
- --maybe same name but hopeful also same damage -> same item
- if (stringMatch(RecipeItemName[slot], n)) and (stringMatch(RecipeItemDamage[slot], d)) then
- ChestItem[slot] = side
- print("for slot "..slot..": "..side..": "..n)
- end
- end
- end
- drop(side)
- end
- end
- -- FIXME: Sort by value, not key (table[slot]=item -> sort by item to suck all same items from a chest (faster))
- table.sort(RecipeItemName)
- -- main loop
- print("Setup complete.")
- Err0r = false
- while true do
- -- check if redstone signal required and one..
- if (ControlSignal == true) then
- if (rs.getInput(ControlSide) == true) then
- goForIt = true
- else
- goForIt = false
- end
- else
- goForIt = true
- end
- if (goForIt == true) then
- -- suck in all the required slots
- for slot in pairs(RecipeItemName) do
- turtle.select(tonumber(slot))
- while turtle.getItemCount(slot) == 0 do
- suck(ChestItem[slot], tonumber(RecipeItemCount[slot]))
- end
- end
- if turtle.craft(0) == false then
- drop(OutDirection, turtle.getItemCount(OutSlot))
- if Err0r == false then
- print("ERROR: Invalid recipe! Please set up correctly.")
- Err0r = true
- -- clear/drop all slots
- for i=1,16 do
- turtle.select(i)
- drop(OutDirection, turtle.getItemCount(i))
- end
- end
- else
- if Err0r == true then Err0r = false end
- craft()
- end
- else
- os.sleep(1)
- end
- end
- --
- -- EOF
- --
Add Comment
Please, Sign In to add comment