Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x, y, z, f
- local posFilePath = "/data/pos.txt"
- function createPosFile(path)
- if not fs.exists(path) or fs.isDir(path) then
- if fs.isReadOnly(string.sub(path, string.find(path, ".*/"))) then
- return "ERR_DIR_READ_ONLY"
- end
- fs.delete(path:sub(1, path:find(".*/")))
- fs.makeDir(path:sub(1, path:find(".*/")))
- end
- end
- function getPosFilePath()
- return posFilePath
- end
- function setPosFilePath(path)
- if not path then
- return "ERR_MISSING_PATH"
- elseif fs.isReadOnly(string.sub(path, string.find(path, ".*/"))) then
- return "ERR_DIR_READ_ONLY"
- end
- if not fs.exists(path) or fs.isDir(path) then
- createPosFile(path)
- end
- posFilePath = path
- end
- function readFromPosFile()
- if not fs.exists(posFilePath) or fs.isDir(posFilePath) then
- return "ERR_NO_POS_FILE"
- elseif fs.isReadOnly(posFilePath) then
- return "ERR_DIR_READ_ONLY"
- end
- local posFile = fs.open(posFilePath, "r")
- x, y, z, f = unpack(textutils.unserialize(posFile.readLine()))
- posFile.close()
- return x, y, z, f
- end
- function writeToPosFile(xPar, yPar, zPar, fPar)
- if xPar and yPar and zPar and fPar then
- x = xPar
- y = yPar
- z = zPar
- f = fPar
- end
- local posFile = fs.open(posFilePath, "w")
- posFile.write(textutils.serialize({ x, y, z, f }))
- posFile.close()
- return true
- end
- function syncPos()
- if peripheral.getType("right") ~= "modem" and peripheral.getType("left") ~= "modem" then
- return false
- end
- local doClose = rednet.isOpen("right") or rednet.isOpen("left")
- if peripheral.getType("right") == "compass" then
- f = peripheral.call("right", "getFacing")
- x, y, z = gps.locate()
- elseif peripheral.getType("left") == "compass" then
- f = peripheral.call("left", "getFacing")
- x, y, z = gps.locate()
- else
- forward(1, 1)
- xTemp, yTemp, zTemp = gps.locate()
- back()
- x, y, z = gps.locate()
- if zTemp > z then
- f = 0
- elseif xTemp < x then
- f = 1
- elseif zTemp < z then
- f = 2
- elseif xTemp > x then
- f = 3
- end
- end
- return writeToPosFile(x, y, z, f)
- end
- local function updatePos(fPar)
- if fPar < 0 then
- return false
- end
- fPar = fPar % 4
- if fPar == 0 then
- z = z + 1
- elseif fPar == 1 then
- x = x - 1
- elseif fPar == 2 then
- z = z - 1
- elseif fPar == 3 then
- x = x + 1
- end
- return true
- end
- local function init()
- readFromPosFile()
- end
- function up(n, doBreak)
- init()
- if not n then
- n = 1
- end
- if turtle.getFuelLevel() < n or y + n > 255 then
- return false
- end
- for i = 1, n do
- y = y + 1
- if not turtle.up() then
- if doBreak then
- while not turtle.up() do
- if not turtle.digUp() then
- y = y - 1
- return false
- end
- end
- else
- y = y - 1
- return false
- end
- else
- writeToPosFile(x, y, z, f)
- end
- end
- return true
- end
- function down(n, doBreak)
- init()
- if not n then
- n = 1
- end
- if turtle.getFuelLevel() < n or y - n < 0 then
- return false
- end
- for i = 1, n do
- y = y - 1
- if not turtle.down() then
- if doBreak then
- while not turtle.down() do
- if not turtle.digDown() then
- y = y + 1
- return false
- end
- end
- else
- y = y + 1
- return false
- end
- else
- writeToPosFile(x, y, z, f)
- end
- end
- return true
- end
- function forward(n, blockedBehaviour)
- init()
- if not n then
- n = 1
- end
- if not blockedBehaviour then
- blockedBehaviour = 0
- end
- if turtle.getFuelLevel() < n then
- return false
- end
- for i = 1, n do
- if not updatePos(f) then
- return false
- end
- if not turtle.forward() then
- if blockedBehaviour == 1 then
- while not turtle.forward() do
- if not turtle.dig() then
- updatePos(f + 2)
- return false
- end
- end
- elseif blockedBehaviour == 2 then
- while not turtle.forward() do
- if not up(1, true) then
- updatePos(f + 2)
- return false
- end
- end
- else
- updatePos(f + 2)
- return false
- end
- else
- writeToPosFile(x, y, z, f)
- end
- end
- return true
- end
- function back(n)
- init()
- if not n then
- n = 1
- end
- if turtle.getFuelLevel() < n then
- return false
- end
- for i = 1, n do
- if not updatePos(f + 2) then
- break
- end
- if not turtle.back() then
- updatePos(f)
- return false
- else
- writeToPosFile(x, y, z, f)
- end
- end
- return true
- end
- function turnLeft(n)
- init()
- if not n then
- n = 1
- end
- for i = 1, n do
- f = f == 0 and 3 or f - 1
- writeToPosFile(x, y, z, f)
- turtle.turnLeft()
- end
- end
- function turnRight(n)
- init()
- if not n then
- n = 1
- end
- for i = 1, n do
- f = f == 3 and 0 or f + 1
- writeToPosFile(x, y, z, f)
- turtle.turnRight()
- end
- end
- function turnLeftTo(fPar)
- while f ~= fPar do
- turnLeft()
- end
- end
- function turnRightTo(fPar)
- while f ~= fPar do
- turnRight()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment