Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function loadPosition()
- local file = fs.open("pos.txt", "r")
- local xf = tonumber(file.readLine())
- local zf = tonumber(file.readLine())
- local xx = tonumber(file.readLine())
- local yy = tonumber(file.readLine())
- local zz = tonumber(file.readLine())
- dir = 0
- if xf == 0 then
- if zf == -1 then
- dir = 1
- elseif zf == 1 then
- dir = 3
- end
- elseif zf == 0 then
- if xf == -1 then
- dir = 4
- elseif xf == 1 then
- dir = 2
- end
- end
- return {dir = dir, xfacing = xf, zfacing = zf, x = xx, y = yy, z = zz}
- end
- function getChunkPosition()
- local pos = loadPosition()
- return {cx = pos.x / 16, cz = pos.z / 16, x = pos.x % 16, z = pos.z % 16}
- end
- function ensureFuel()
- if turtle.getFuelLevel() == 0 then
- turtle.refuel(1)
- end
- end
- function turnRight()
- local pos = loadPosition()
- ensureFuel()
- if pos.xfacing == -1 then
- pos.zfacing = -1
- pos.xfacing = 0
- elseif pos.xfacing == 1 then
- pos.zfacing = 1
- pos.xfacing = 0
- elseif pos.zfacing == -1 then
- pos.xfacing = 1
- pos.zfacing = 0
- elseif pos.zfacing == 1 then
- pos.xfacing = -1
- pos.zfacing = 0
- end
- turtle.turnRight()
- pos.dir = pos.dir + 1
- if pos.dir == 5 then pos.dir = 1 end
- savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
- end
- function turnLeft()
- local pos = loadPosition()
- ensureFuel()
- if pos.xfacing == -1 then
- pos.zfacing = 1
- pos.xfacing = 0
- elseif pos.xfacing == 1 then
- pos.zfacing = -1
- pos.xfacing = 0
- elseif pos.zfacing == -1 then
- pos.xfacing = -1
- pos.zfacing = 0
- elseif pos.zfacing == 1 then
- pos.xfacing = 1
- pos.zfacing = 0
- end
- pos.dir = pos.dir - 1
- if pos.dir == 0 then pos.dir = 4 end
- turtle.turnLeft()
- savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
- end
- function directions()
- return {NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4, NEGZ = 1, POSX = 2, POSZ = 3, NEGX = 4, UP = 5, DOWN = 6}
- end
- function getMovementVector(dir)
- if(tonumber(dir) == 1) then
- return {dx = 0, dy = 0, dz = -1}
- elseif(tonumber(dir) == 2) then
- return {dx = 1, dy = 0, dz = 0}
- elseif(tonumber(dir) == 3) then
- return {dx = 0, dy = 0, dz = 1}
- elseif(tonumber(dir) == 4) then
- return {dx = -1, dy = 0, dz = 0}
- elseif(tonumber(dir) == 5) then
- return {dx = 0, dy = 1, dz = 0}
- end
- return {dx = 0, dy = -1, dz = 0}
- end
- function face(direction)
- local pos = loadPosition()
- dist = ((direction - pos.dir) + 4) % 4
- if dist == 1 then
- turnRight()
- elseif dist == 2 then
- turnRight()
- turnRight()
- elseif dist == 3 then
- turnLeft()
- end
- end
- function savePosition(xf, zf, x,y,z)
- fs.delete("pos.txt")
- local file = fs.open("pos.txt", "w")
- file.writeLine(xf)
- file.writeLine(zf)
- file.writeLine(x)
- file.writeLine(y)
- file.writeLine(z)
- file.close()
- end
- function clearFront()
- while turtle.detect() do
- ensureFuel()
- turtle.dig()
- end
- end
- function clearUp()
- while turtle.detectUp() do
- ensureFuel()
- turtle.digUp()
- end
- end
- function clearDown()
- while turtle.detectDown() do
- ensureFuel()
- turtle.digDown()
- end
- end
- function up()
- local pos = loadPosition()
- clearUp()
- ensureFuel()
- turtle.up()
- pos.y = pos.y + 1
- savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
- end
- function down()
- local pos = loadPosition()
- clearDown()
- ensureFuel()
- turtle.down()
- pos.y = pos.y - 1
- savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
- end
- function forward()
- local pos = loadPosition()
- clearFront()
- ensureFuel()
- turtle.forward()
- pos.x = pos.x + pos.xfacing
- pos.z = pos.z + pos.zfacing
- savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
- end
- function move(direction)
- if(direction == 5) then
- up()
- elseif(direction == 6) then
- down()
- else
- face(direction)
- forward()
- end
- end
- function moveAmt(direction, amt)
- for i = 1,tonumber(amt),1 do
- move(direction)
- end
- end
- function goToChunkX(x)
- local pos = getChunkPosition()
- local deltaX = x - pos.x
- if(deltaX > 0) then
- moveAmt(2, deltaX)
- else
- moveAmt(4, -deltaX)
- end
- end
- function goToChunkZ(z)
- local pos = getChunkPosition()
- local deltaZ = z - pos.z
- if(deltaZ > 0) then
- moveAmt(3, deltaZ)
- else
- moveAmt(1, -deltaZ)
- end
- end
- function goToChunkXZ(x, z)
- goToChunkX(x)
- goToChunkZ(z)
- end
- function goToX(x)
- local pos = loadPosition()
- local deltaX = x - pos.x
- if(deltaX > 0) then
- moveAmt(2, deltaX)
- else
- moveAmt(4, -deltaX)
- end
- end
- function goToY(y)
- local pos = loadPosition()
- local deltaY = y - pos.y
- if(deltaY > 0) then
- moveAmt(5, deltaY)
- else
- moveAmt(6, -deltaY)
- end
- end
- function goToZ(z)
- local pos = loadPosition()
- local deltaZ = z - pos.z
- if(deltaZ > 0) then
- moveAmt(3, deltaZ)
- else
- moveAmt(1, -deltaZ)
- end
- end
- function goTo(x, y, z)
- goToX(x)
- goToZ(z)
- goToY(y)
- end
Advertisement
Add Comment
Please, Sign In to add comment