Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local turtleX = 15
- local turtleY = 65
- local turtleZ = 15
- local Compass = {"W", "N", "E", "S"}
- -- Direction = Compass index
- local Direction = "N"
- -- -1 = RIGHT
- local coords = {["X"] = turtleX, ["Y"] = turtleY, ["Z"] = turtleZ}
- local targetCoords = {["X"] = 0, ["Y"] = 70, ["Z"] = 0}
- function RotateToDirection(targetDirection)
- local CurrentDirectionIndex = 0
- local TargetDirectionIndex = 0
- for index, dir in pairs(Compass) do
- if dir == Direction then
- CurrentDirectionIndex = index
- end
- end
- for index, dir in pairs(Compass) do
- if dir == targetDirection then
- TargetDirectionIndex = index
- end
- end
- local diff = TargetDirectionIndex - CurrentDirectionIndex
- print("Current Direction: " .. Direction .. ". \n Current Direction Index: " .. tostring(CurrentDirectionIndex))
- print("Current Target Direction: " .. targetDirection .. ". \n Current Target Direction Index: " .. tostring(TargetDirectionIndex))
- print("Current diff: " .. tostring(diff))
- if diff == 0 then
- -- Already facing direction.
- elseif diff == 3 or diff == -3 then
- turtle.turnRight()
- elseif diff == -1 or diff == 1 then
- turtle.turnLeft()
- else
- turtle.turnRight()
- turtle.turnRight()
- end
- Direction = Compass[TargetDirectionIndex]
- end
- -- Move to correct Y
- for y = coords.Y + 1, targetCoords.Y, 1 do
- local checkUp = turtle.inspectUp()
- if checkUp == false then
- turtle.up()
- else
- turtle.digUp()
- turtle.up()
- end
- coords["Y"] = coords["Y"] + 1
- end
- -- Move to correct X
- if coords.X < targetCoords.X then
- RotateToDirection("E")
- for x = coords.X + 1, targetCoords.X, 1 do
- local check = turtle.inspect()
- if check == false then
- turtle.forward()
- else
- turtle.dig()
- turtle.forward()
- end
- end
- else
- RotateToDirection("W")
- for x = coords.X - 1, targetCoords.X, -1 do
- local check = turtle.inspect()
- if check == false then
- turtle.forward()
- else
- turtle.dig()
- turtle.forward()
- end
- end
- end
- -- Move to correct Z
- if coords.Z < targetCoords.Z then
- RotateToDirection("S")
- for z = coords.Z + 1, targetCoords.Z, 1 do
- local check = turtle.inspect()
- if check == false then
- turtle.forward()
- else
- turtle.dig()
- turtle.forward()
- end
- end
- else
- RotateToDirection("N")
- for z = coords.Z - 1, targetCoords.Z, -1 do
- local check = turtle.inspect()
- if check == false then
- turtle.forward()
- else
- turtle.dig()
- turtle.forward()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement