TheDarBear

moveApi

Feb 2nd, 2021 (edited)
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. function loadPosition()
  2.     local file = fs.open("pos.txt", "r")
  3.     local xf = tonumber(file.readLine())
  4.     local zf = tonumber(file.readLine())
  5.     local xx = tonumber(file.readLine())
  6.     local yy = tonumber(file.readLine())
  7.     local zz = tonumber(file.readLine())
  8.     dir = 0
  9.     if xf == 0 then
  10.         if zf == -1 then
  11.             dir = 1
  12.         elseif zf == 1 then
  13.             dir = 3
  14.         end
  15.     elseif zf == 0 then
  16.         if xf == -1 then
  17.             dir = 4
  18.         elseif xf == 1 then
  19.             dir = 2
  20.         end
  21.     end
  22.     return {dir = dir, xfacing = xf, zfacing = zf, x = xx, y = yy, z = zz}
  23. end
  24.  
  25. function getChunkPosition()
  26.     local pos = loadPosition()
  27.     return {cx = pos.x / 16, cz = pos.z / 16, x = pos.x % 16, z = pos.z % 16}
  28. end
  29.  
  30. function ensureFuel()
  31.     if turtle.getFuelLevel() == 0 then
  32.         turtle.refuel(1)
  33.     end
  34. end
  35.  
  36. function turnRight()
  37.     local pos = loadPosition()
  38.     ensureFuel()
  39.     if pos.xfacing == -1 then
  40.         pos.zfacing = -1
  41.         pos.xfacing = 0
  42.     elseif pos.xfacing == 1 then
  43.         pos.zfacing = 1
  44.         pos.xfacing = 0
  45.     elseif pos.zfacing == -1 then
  46.         pos.xfacing = 1
  47.         pos.zfacing = 0
  48.     elseif pos.zfacing == 1 then
  49.         pos.xfacing = -1
  50.         pos.zfacing = 0
  51.     end
  52.     turtle.turnRight()
  53.     pos.dir = pos.dir + 1
  54.     if pos.dir == 5 then pos.dir = 1 end
  55.     savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
  56. end
  57.  
  58. function turnLeft()
  59.     local pos = loadPosition()
  60.     ensureFuel()
  61.     if pos.xfacing == -1 then
  62.         pos.zfacing = 1
  63.         pos.xfacing = 0
  64.     elseif pos.xfacing == 1 then
  65.         pos.zfacing = -1
  66.         pos.xfacing = 0
  67.     elseif pos.zfacing == -1 then
  68.         pos.xfacing = -1
  69.         pos.zfacing = 0
  70.     elseif pos.zfacing == 1 then
  71.         pos.xfacing = 1
  72.         pos.zfacing = 0
  73.     end
  74.     pos.dir = pos.dir - 1
  75.     if pos.dir == 0 then pos.dir = 4 end
  76.     turtle.turnLeft()
  77.     savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
  78. end
  79.  
  80. function directions()
  81.     return {NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4, NEGZ = 1, POSX = 2, POSZ = 3, NEGX = 4, UP = 5, DOWN = 6}
  82. end
  83.  
  84. function getMovementVector(dir)
  85.     if(tonumber(dir) == 1) then
  86.         return {dx = 0, dy = 0, dz = -1}
  87.     elseif(tonumber(dir) == 2) then
  88.         return {dx = 1, dy = 0, dz = 0}
  89.     elseif(tonumber(dir) == 3) then
  90.         return {dx = 0, dy = 0, dz = 1}
  91.     elseif(tonumber(dir) == 4) then
  92.         return {dx = -1, dy = 0, dz = 0}
  93.     elseif(tonumber(dir) == 5) then
  94.         return {dx = 0, dy = 1, dz = 0}
  95.     end
  96.     return {dx = 0, dy = -1, dz = 0}
  97. end
  98.  
  99. function face(direction)
  100.     local pos = loadPosition()
  101.     dist = ((direction - pos.dir) + 4) % 4
  102.     if dist == 1 then
  103.         turnRight()
  104.     elseif dist == 2 then
  105.         turnRight()
  106.         turnRight()
  107.     elseif dist == 3 then
  108.         turnLeft()
  109.     end
  110. end
  111.  
  112. function savePosition(xf, zf, x,y,z)
  113.     fs.delete("pos.txt")
  114.     local file = fs.open("pos.txt", "w")
  115.     file.writeLine(xf)
  116.     file.writeLine(zf)
  117.     file.writeLine(x)
  118.     file.writeLine(y)
  119.     file.writeLine(z)
  120.     file.close()
  121. end
  122.  
  123. function clearFront()
  124.     while turtle.detect() do
  125.         ensureFuel()
  126.         turtle.dig()
  127.     end
  128. end
  129.  
  130. function clearUp()
  131.     while turtle.detectUp() do
  132.         ensureFuel()
  133.         turtle.digUp()
  134.     end
  135. end
  136.  
  137. function clearDown()
  138.     while turtle.detectDown() do
  139.         ensureFuel()
  140.         turtle.digDown()
  141.     end
  142. end
  143.  
  144. function up()
  145.     local pos = loadPosition()
  146.     clearUp()
  147.     ensureFuel()
  148.     turtle.up()
  149.     pos.y = pos.y + 1
  150.     savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
  151. end
  152.  
  153. function down()
  154.     local pos = loadPosition()
  155.     clearDown()
  156.     ensureFuel()
  157.     turtle.down()
  158.     pos.y = pos.y - 1
  159.     savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
  160. end
  161.  
  162. function forward()
  163.     local pos = loadPosition()
  164.     clearFront()
  165.     ensureFuel()
  166.     turtle.forward()
  167.     pos.x = pos.x + pos.xfacing
  168.     pos.z = pos.z + pos.zfacing
  169.     savePosition(pos.xfacing, pos.zfacing, pos.x, pos.y, pos.z)
  170. end
  171.  
  172. function move(direction)
  173.     if(direction == 5) then
  174.         up()
  175.     elseif(direction == 6) then
  176.         down()
  177.     else
  178.         face(direction)
  179.         forward()
  180.     end
  181. end
  182.  
  183. function moveAmt(direction, amt)
  184.    for i = 1,tonumber(amt),1 do
  185.       move(direction)
  186.    end
  187. end
  188.  
  189. function goToChunkX(x)
  190.    local pos = getChunkPosition()
  191.    local deltaX = x - pos.x  
  192.    if(deltaX > 0) then
  193.       moveAmt(2, deltaX)
  194.    else
  195.       moveAmt(4, -deltaX)
  196.    end
  197. end
  198.  
  199. function goToChunkZ(z)
  200.    local pos = getChunkPosition()
  201.    local deltaZ = z - pos.z  
  202.    if(deltaZ > 0) then
  203.       moveAmt(3, deltaZ)
  204.    else
  205.       moveAmt(1, -deltaZ)
  206.    end
  207. end
  208.  
  209. function goToChunkXZ(x, z)
  210.     goToChunkX(x)
  211.     goToChunkZ(z)
  212. end
  213.  
  214. function goToX(x)
  215.    local pos = loadPosition()
  216.    local deltaX = x - pos.x  
  217.    if(deltaX > 0) then
  218.       moveAmt(2, deltaX)
  219.    else
  220.       moveAmt(4, -deltaX)
  221.    end
  222. end
  223.  
  224. function goToY(y)
  225.     local pos = loadPosition()
  226.     local deltaY = y - pos.y
  227.     if(deltaY > 0) then
  228.        moveAmt(5, deltaY)
  229.     else
  230.        moveAmt(6, -deltaY)
  231.     end
  232. end
  233.  
  234. function goToZ(z)
  235.    local pos = loadPosition()
  236.    local deltaZ = z - pos.z  
  237.    if(deltaZ > 0) then
  238.       moveAmt(3, deltaZ)
  239.    else
  240.       moveAmt(1, -deltaZ)
  241.    end
  242. end
  243.  
  244. function goTo(x, y, z)
  245.     goToX(x)
  246.     goToZ(z)
  247.     goToY(y)
  248. end
Advertisement
Add Comment
Please, Sign In to add comment