Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GPS_ERROR_MESSAGE = "GPS SYSTEM DOES NOT WORK"
- PARAM_ERROR = "INVALID PARAMETER HAS BEEN PASSED"
- ACCEPTABLE_COLORS = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black }
- ACCEPTABLE_DIRECTIONS = { "North", "East", "South", "West" }
- -- One line color print
- function printColor(color, text)
- for index, value in ipairs(ACCEPTABLE_COLORS) do
- if (color == value) then break end
- if (index == tableLength(ACCEPTABLE_COLORS) and color ~= value) then
- print(text)
- return
- end
- end
- term.setTextColor(color)
- print(text)
- term.setTextColor(colors.white)
- end
- -- Safer and simpler way of getting coordinates in library
- function getCoordinates()
- local startX, startY, startZ = gps.locate(1)
- if (not startX) then
- printError(GPS_ERROR_MESSAGE)
- else
- return startX, startY, startZ
- end
- end
- -- While detecting a block in front, keep digging it away
- function keepDigging()
- while (turtle.detect()) do
- turtle.dig()
- end
- end
- -- returns the length of the entire table
- function tableLength(table)
- local count = 0
- for _ in pairs(table) do count = count + 1 end
- return count
- end
- --
- -- Figure out if the turtle is looking at North, South, West, or East
- function getDirection()
- local direction
- local startX, _, startZ = getCoordinates()
- keepDigging()
- if (turtle.forward()) then
- local endX, _, endZ = getCoordinates()
- if (startX > endX) then
- direction = "West"
- elseif (startX < endX) then
- direction = "East"
- elseif (startZ > endZ) then
- direction = "North"
- elseif (startZ < endZ) then
- direction = "South"
- end
- turtle.back()
- end
- return direction
- end
- -- Recalibrates turtle to face North, South, West, or East (but with knowledge of given direction)
- function faceDirectionP(directionToFace, facedDirection)
- if (facedDirection == directionToFace) then return true end
- for key, value in ipairs(ACCEPTABLE_DIRECTIONS) do
- if (facedDirection == value) then
- facedDirection = key
- elseif (directionToFace == value) then
- directionToFace = key
- end
- end
- local difference = math.abs(facedDirection - directionToFace)
- local command
- if (facedDirection < directionToFace) then
- command = turtle.turnRight
- else
- command = turtle.turnLeft
- end
- while (difference > 0) do
- command()
- difference = difference - 1
- end
- end
- -- Recalibrates turtle to face North, South, West, or East
- function faceDirection(directionToFace)
- local facing = getDirection()
- faceDirectionP(directionToFace, facing)
- end
- -- Moves the turtle to the given coordinates and gives verification at the end
- function moveTo(endX, endY, endZ)
- local startX, startY, startZ = getCoordinates()
- local distX = math.abs(startX - endX)
- local distZ = math.abs(startZ - endZ)
- local distY = math.abs(startY - endY)
- local changeY
- local detectorY
- local diggerY
- if (endY > startY) then
- changeY = turtle.up
- detectorY = turtle.detectUp
- diggerY = turtle.digUp
- elseif (endY < startY) then
- changeY = turtle.down
- detectorY = turtle.detectDown
- diggerY = turtle.digDown
- end
- while (distY > 0) do
- changeY()
- while (detectorY()) do
- diggerY()
- end
- distY = distY - 1
- end
- if (endX > startX) then
- faceDirection("East")
- elseif (endX < startX) then
- faceDirection("West")
- end
- while (distX > 0) do
- keepDigging()
- turtle.forward()
- distX = distX - 1
- end
- if (endZ > startZ) then
- faceDirection("South")
- elseif (endZ < startZ) then
- faceDirection("North")
- end
- while (distZ > 0) do
- keepDigging()
- turtle.forward()
- distZ = distZ - 1
- end
- end
- -- Goes through the turtles inventory, slots 1-16, refueling as much as it can
- function refuelAll()
- for slot = 1, 16, 1 do
- turtle.select(slot)
- turtle.refuel(turtle.getItemCount(slot))
- end
- end
- -- Goes through the turtles inventory from slots 1-16 and find the first empty slot...returns -1 if no empty slots
- function findEmptySpace()
- for slot = 1, 16, 1 do
- turtle.select(slot)
- if (turtle.getItemDetail() == nil) then
- return slot
- end
- end
- return -1
- end
- -- Same as last one, just 1-16 slots...returns -1 if no slot
- function findSlot(...)
- for slot = 1, 16, 1 do
- turtle.select(slot)
- local item = turtle.getItemDetail()
- if (item ~= nil) then
- for v in ipairs(arg) do
- if (string.match(item.name, v)) then
- return slot
- end
- end
- end
- end
- return -1
- end
- -- Does the same thing, just 1-16
- function findSlots(...)
- local listOfSlots = {}
- local curIdx = 1
- for slot = 1, 16, 1 do
- turtle.select(slot)
- local item = turtle.getItemDetail()
- if (item ~= nil) then
- for v in ipairs(arg) do
- if (string.match(item.name, v)) then
- listOfSlots[curIdx] = slot
- curIdx = curIdx + 1
- end
- end
- end
- end
- return listOfSlots
- end
- -- Reverses the turn in case it is needed for quarrying or navigating a set pattern
- function oppositeTurn(turnType)
- if (turnType == turtle.turnLeft) then
- return turtle.turnRight
- elseif (turnType == turtle.turnRight) then
- return turtle.turnLeft
- else
- return error("WAS NOT GIVEN A TURTLE TURN METHOD!")
- end
- end
- -- Assume turtle is in backleft corner of Quarry, one block behind.
- function quarryP(length, width, height, facedDirection)
- local startX, startY, startZ = getCoordinates()
- local firstTurn = turtle.turnRight
- local changeY
- if (height >= 0) then
- changeY = turtle.up
- else
- changeY = turtle.down
- endX
- keepDigging()
- turtle.forward()
- for d = height,
- for l = 1, length do
- firstTurn()
- keepDigging()
- turtle.forward()
- end
- end
- -- Assume turtle is in backleft corner again, one block behind
- function quarry(length, width, height)
- local facedDirection = getDirection()
- quarryP(length, width, height, facedDirection)
- end
Add Comment
Please, Sign In to add comment