Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Computercraft API For turtle control.
- --Made by Andrew Lalis; andrewlalisofficial@gmail.com
- local orientation = "none"
- --Local function to validate cardinal directions.
- local function isCardinal(dir)
- return (dir == "n" or dir == "s" or dir == "e" or dir == "w")
- end
- --Local function to turn around.
- local function turnAround()
- turtle.turnRight()
- turtle.turnRight()
- end
- --Sets the starting orientation of the turtle.
- function setOrientation(cardinal)
- if (isCardinal(cardinal)) then
- orientation = cardinal
- else
- error("Unknown cardinal direction. Must be n, s, e, or w.",2)
- end
- end
- --Changes the turtle's orientation.
- function changeOrientation(cardinal)
- if (not isCardinal(cardinal)) then
- error("Unknown cardinal direction. Must be n, s, e, or w.", 2)
- return false
- else
- if (orientation == "n") then
- if (cardinal == "w") then
- turtle.turnLeft()
- elseif (cardinal == "e") then
- turtle.turnRight()
- elseif (cardinal == "s") then
- turnAround()
- end
- elseif (orientation == "s") then
- if (cardinal == "w") then
- turtle.turnRight()
- elseif (cardinal == "e") then
- turtle.turnLeft()
- elseif (cardinal == "n") then
- turnAround()
- end
- elseif (orientation == "e") then
- if (cardinal == "n") then
- turtle.turnLeft()
- elseif (cardinal == "s") then
- turtle.turnRight()
- elseif (cardinal == "w") then
- turnAround()
- end
- else
- if (cardinal == "n") then
- turtle.turnRight()
- elseif (cardinal == "s") then
- turtle.turnLeft()
- elseif (cardinal == "e") then
- turnAround()
- end
- end
- orientation = cardinal
- return true
- end
- end
- --Determines if the turtle has an item, and if so, returns the following table.
- --{count=X, slots={1,2,3,..}}, or nil, if not found.
- function getItemCount(name, damage)
- local dmg = damage or 0
- local result = {count=0, slots={}}
- for i=1,16 do
- local data = turtle.getItemDetail(i)
- if (data ~= nil and data.name == name and data.damage == dmg) then
- result.count = result.count + data.count
- table.insert(result.slots, i)
- end
- end
- if (result.count > 0) then
- return result
- else
- return nil
- end
- end
- --Selects an item by name. Returns success value.
- function select(name, damage)
- local item = getItemCount(name, damage)
- if (item ~= nil) then
- turtle.select(item.slots[1])
- return true
- else
- return false
- end
- end
- --Places an item by name. Returns success value.
- function place(name, damage, direction, orient)
- if (not select(name, damage)) then
- return false
- end
- local dir = direction or nil
- local newOrient = orient or nil
- local previousOrient = orientation
- local success
- if (dir == "up") then
- success = changeOrientation(newOrient) and turtle.placeUp()
- elseif (dir == "down") then
- success = changeOrientation(newOrient) and turtle.placeDown()
- elseif (dir == "right") then
- turtle.turnRight()
- success = turtle.place()
- turtle.turnLeft()
- elseif (dir == "left") then
- turtle.turnLeft()
- success = turtle.place()
- turtle.turnRight()
- elseif (dir == "back") then
- turnAround()
- success = turtle.place()
- turnAround()
- else
- success = turtle.place()
- end
- if (orientation ~= previousOrient) then
- changeOrientation(previousOrient)
- end
- return success
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement