Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --navigation api to keep track of location
- dirTable = {}
- dirTable[0], dirTable[1], dirTable[2], dirTable[3] = 'front', 'right', 'back', 'left'
- pos = {x = 0, y = 0, z = 0, dir = 0}
- if not fs.exists('position.txt') then
- local position = fs.open('position.txt', 'w')
- pos.dir = pos.dir % 4
- position.write(textutils.serialise(pos))
- position.close()
- else
- local position = fs.open('position.txt', 'r')
- pos = textutils.unserialise(position.readAll())
- position.close()
- end
- function updatePos()
- pos.dir = pos.dir % 4
- local position = fs.open('position.txt', 'w')
- position.write(textutils.serialise(pos))
- position.close()
- end
- function returnPos()
- return pos.x, pos.y, pos.z, pos.dir
- end
- function calibrate()
- pos.x, pos.y, pos.z, pos.dir = 0, 0, 0, 0
- updatePos()
- end
- function direction(isForward)
- local xDir = 0
- local yDir = 0
- pos.dir = pos.dir % 4
- if pos.dir == 0 then
- xDir = 1
- yDir = 0
- elseif pos.dir == 1 then
- xDir = 0
- yDir = 1
- elseif pos.dir == 2 then
- xDir = -1
- yDir = 0
- elseif pos.dir == 3 then
- xDir = 0
- yDir = -1
- end
- if isForward == true then
- pos.x = pos.x + xDir
- pos.y = pos.y + yDir
- else
- pos.x = pos.x - xDir
- pos.y = pos.y - yDir
- end
- updatePos()
- end
- function forward(x)
- for i = 1, x do
- while not turtle.forward() do
- turtle.dig()
- end
- direction(true)
- end
- updatePos()
- end
- function back(x)
- for i = 1, x do
- repeat until turtle.back()
- direction(false)
- end
- updatePos()
- end
- function up(x)
- for i = 1, x do
- while not turtle.up() do
- turtle.digUp()
- end
- pos.z = pos.z + 1
- end
- updatePos()
- end
- function down(x)
- for i = 1, x do
- while not turtle.down() do
- turtle.digDown()
- end
- pos.z = pos.z - 1
- end
- updatePos()
- end
- function turnRight(x)
- for i = 1, x do
- turtle.turnRight()
- pos.dir = pos.dir + 1
- end
- updatePos()
- end
- function turnLeft(x)
- for i = 1, x do
- turtle.turnLeft()
- pos.dir = pos.dir + 3
- end
- updatePos()
- end
- function turnAround()
- turnLeft(2)
- end
- function goDir(dir)
- while pos.dir % 4 ~= dir do
- turnLeft()
- end
- updatePos()
- end
Add Comment
Please, Sign In to add comment