Cakejoke

[CC] COS-CJE navigate API

Dec 22nd, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. local x, y, z, f
  2. local posFilePath = "/data/pos.txt"
  3.  
  4. function createPosFile(path)
  5.     if not fs.exists(path) or fs.isDir(path) then
  6.         if fs.isReadOnly(string.sub(path, string.find(path, ".*/"))) then
  7.             return "ERR_DIR_READ_ONLY"
  8.         end
  9.         fs.delete(path:sub(1, path:find(".*/")))
  10.         fs.makeDir(path:sub(1, path:find(".*/")))
  11.     end
  12. end
  13.  
  14. function getPosFilePath()
  15.     return posFilePath
  16. end
  17.  
  18. function setPosFilePath(path)
  19.     if not path then
  20.         return "ERR_MISSING_PATH"
  21.     elseif fs.isReadOnly(string.sub(path, string.find(path, ".*/"))) then
  22.         return "ERR_DIR_READ_ONLY"
  23.     end
  24.     if not fs.exists(path) or fs.isDir(path) then
  25.         createPosFile(path)
  26.     end
  27.     posFilePath = path
  28. end
  29.  
  30. function readFromPosFile()
  31.     if not fs.exists(posFilePath) or fs.isDir(posFilePath) then
  32.         return "ERR_NO_POS_FILE"
  33.     elseif fs.isReadOnly(posFilePath) then
  34.         return "ERR_DIR_READ_ONLY"
  35.     end
  36.     local posFile = fs.open(posFilePath, "r")
  37.     x, y, z, f = unpack(textutils.unserialize(posFile.readLine()))
  38.     posFile.close()
  39.     return x, y, z, f
  40. end
  41.  
  42. function writeToPosFile(xPar, yPar, zPar, fPar)
  43.     if xPar and yPar and zPar and fPar then
  44.         x = xPar
  45.         y = yPar
  46.         z = zPar
  47.         f = fPar
  48.     end
  49.     local posFile = fs.open(posFilePath, "w")
  50.     posFile.write(textutils.serialize({ x, y, z, f }))
  51.     posFile.close()
  52.     return true
  53. end
  54.  
  55. function syncPos()
  56.     if peripheral.getType("right") ~= "modem" and peripheral.getType("left") ~= "modem" then
  57.         return false
  58.     end
  59.     local doClose = rednet.isOpen("right") or rednet.isOpen("left")
  60.     if peripheral.getType("right") == "compass" then
  61.         f = peripheral.call("right", "getFacing")
  62.         x, y, z = gps.locate()
  63.     elseif peripheral.getType("left") == "compass" then
  64.         f = peripheral.call("left", "getFacing")
  65.         x, y, z = gps.locate()
  66.     else
  67.         forward(1, 1)
  68.         xTemp, yTemp, zTemp = gps.locate()
  69.         back()
  70.         x, y, z = gps.locate()
  71.         if zTemp > z then
  72.             f = 0
  73.         elseif xTemp < x then
  74.             f = 1
  75.         elseif zTemp < z then
  76.             f = 2
  77.         elseif xTemp > x then
  78.             f = 3
  79.         end
  80.     end
  81.     return writeToPosFile(x, y, z, f)
  82. end
  83.  
  84. local function updatePos(fPar)
  85.     if fPar < 0 then
  86.         return false
  87.     end
  88.     fPar = fPar % 4
  89.     if fPar == 0 then
  90.         z = z + 1
  91.     elseif fPar == 1 then
  92.         x = x - 1
  93.     elseif fPar == 2 then
  94.         z = z - 1
  95.     elseif fPar == 3 then
  96.         x = x + 1
  97.     end
  98.     return true
  99. end
  100.  
  101. local function init()
  102.     readFromPosFile()
  103. end
  104.  
  105. function up(n, doBreak)
  106.     init()
  107.     if not n then
  108.         n = 1
  109.     end
  110.     if turtle.getFuelLevel() < n or y + n > 255 then
  111.         return false
  112.     end
  113.     for i = 1, n do
  114.         y = y + 1
  115.         if not turtle.up() then
  116.             if doBreak then
  117.                 while not turtle.up() do
  118.                     if not turtle.digUp() then
  119.                         y = y - 1
  120.                         return false
  121.                     end
  122.                 end
  123.             else
  124.                 y = y - 1
  125.                 return false
  126.             end
  127.         else
  128.             writeToPosFile(x, y, z, f)
  129.         end
  130.     end
  131.     return true
  132. end
  133.  
  134. function down(n, doBreak)
  135.     init()
  136.     if not n then
  137.         n = 1
  138.     end
  139.     if turtle.getFuelLevel() < n or y - n < 0 then
  140.         return false
  141.     end
  142.     for i = 1, n do
  143.         y = y - 1
  144.         if not turtle.down() then
  145.             if doBreak then
  146.                 while not turtle.down() do
  147.                     if not turtle.digDown() then
  148.                         y = y + 1
  149.                         return false
  150.                     end
  151.                 end
  152.             else
  153.                 y = y + 1
  154.                 return false
  155.             end
  156.         else
  157.             writeToPosFile(x, y, z, f)
  158.         end
  159.     end
  160.     return true
  161. end
  162.  
  163. function forward(n, blockedBehaviour)
  164.     init()
  165.     if not n then
  166.         n = 1
  167.     end
  168.     if not blockedBehaviour then
  169.         blockedBehaviour = 0
  170.     end
  171.     if turtle.getFuelLevel() < n then
  172.         return false
  173.     end
  174.     for i = 1, n do
  175.         if not updatePos(f) then
  176.             return false
  177.         end
  178.         if not turtle.forward() then
  179.             if blockedBehaviour == 1 then
  180.                 while not turtle.forward() do
  181.                     if not turtle.dig() then
  182.                         updatePos(f + 2)
  183.                         return false
  184.                     end
  185.                 end
  186.             elseif blockedBehaviour == 2 then
  187.                 while not turtle.forward() do
  188.                     if not up(1, true) then
  189.                         updatePos(f + 2)
  190.                         return false
  191.                     end
  192.                 end
  193.             else
  194.                 updatePos(f + 2)
  195.                 return false
  196.             end
  197.         else
  198.             writeToPosFile(x, y, z, f)
  199.         end
  200.     end
  201.     return true
  202. end
  203.  
  204. function back(n)
  205.     init()
  206.     if not n then
  207.         n = 1
  208.     end
  209.     if turtle.getFuelLevel() < n then
  210.         return false
  211.     end
  212.     for i = 1, n do
  213.         if not updatePos(f + 2) then
  214.             break
  215.         end
  216.         if not turtle.back() then
  217.             updatePos(f)
  218.             return false
  219.         else
  220.             writeToPosFile(x, y, z, f)
  221.         end
  222.     end
  223.     return true
  224. end
  225.  
  226. function turnLeft(n)
  227.     init()
  228.     if not n then
  229.         n = 1
  230.     end
  231.     for i = 1, n do
  232.         f = f == 0 and 3 or f - 1
  233.         writeToPosFile(x, y, z, f)
  234.         turtle.turnLeft()
  235.     end
  236. end
  237.  
  238. function turnRight(n)
  239.     init()
  240.     if not n then
  241.         n = 1
  242.     end
  243.     for i = 1, n do
  244.         f = f == 3 and 0 or f + 1
  245.         writeToPosFile(x, y, z, f)
  246.         turtle.turnRight()
  247.     end
  248. end
  249.  
  250. function turnLeftTo(fPar)
  251.     while f ~= fPar do
  252.         turnLeft()
  253.     end
  254. end
  255.  
  256. function turnRightTo(fPar)
  257.     while f ~= fPar do
  258.         turnRight()
  259.     end
  260. end
Advertisement
Add Comment
Please, Sign In to add comment