Advertisement
Guest User

move.lua

a guest
Feb 23rd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. --Move API Beta
  2.  
  3. x = 0
  4. y = 0
  5. z = 0
  6. dir = 1
  7. xdir = {0,1,0,-1}
  8. zdir = {-1,0,1,0}
  9.  
  10.  
  11. if fs.exists("move/pos.lua") then
  12.     h = fs.open("move/pos.lua","r")
  13.     dir = tonumber(h.readLine())
  14.     x = tonumber(h.readLine())
  15.     y = tonumber(h.readLine())
  16.     z = tonumber(h.readLine())
  17.     h.close()
  18. end
  19.  
  20. function getx()
  21.     return x
  22. end
  23.  
  24. function gety()
  25.     return y
  26. end
  27.  
  28. function getz()
  29.     return z
  30. end
  31.  
  32. function getd()
  33.     return dir
  34. end
  35.  
  36. function getnext()
  37.     tx = x + xdir[dir]
  38.     tz = z + zdir[dir]
  39.     return tx, y, tz  
  40. end
  41.  
  42.  
  43. function forward()
  44.     turtle.suckDown()
  45.     turtle.dig()
  46.     if turtle.forward() then
  47.         x = x + xdir[dir]
  48.         z = z + zdir[dir]
  49.         record()
  50.         return true
  51.     else
  52.         return false
  53.     end
  54. end
  55.  
  56. function back()
  57.     if turtle.back() then
  58.         x = x - xdir[dir]
  59.         z = z - zdir[dir]
  60.         record()
  61.         return true
  62.     else
  63.         return false
  64.     end
  65. end
  66.  
  67. function left()
  68.     if turtle.turnLeft() then
  69.         dir = dir - 1
  70.         if dir < 1 then
  71.             dir = 4
  72.         end
  73.         record()
  74.         return true
  75.     end
  76.     return false
  77. end
  78.  
  79. function right()
  80.     if turtle.turnRight() then
  81.         dir = dir + 1
  82.         if dir > 4 then
  83.             dir = 1
  84.         end
  85.         record()
  86.         return true
  87.     end
  88.     return false
  89. end
  90.  
  91. function up()
  92.     turtle.digUp()
  93.     if turtle.up() then
  94.         y = y + 1
  95.         record()
  96.         return true
  97.     end
  98.     return false
  99. end
  100.  
  101. function down()
  102.     turtle.digDown()
  103.     if turtle.down() then
  104.         y = y - 1
  105.         record()
  106.         return true
  107.     end
  108.     return false
  109. end
  110.  
  111. function face(d)
  112.     while d < dir do
  113.         left()
  114.     end
  115.     while d > dir do
  116.         right()
  117.     end
  118. end
  119.  
  120. function move(tx, ty, tz)
  121.     while tx > x do
  122.         face(2)
  123.         forward()
  124.     end
  125.     while tx < x do
  126.         face(4)
  127.         forward()
  128.     end
  129.     while tz > z do
  130.         face(3)
  131.         forward()
  132.     end
  133.     while tz < z do
  134.         face(1)
  135.         forward()
  136.     end
  137.     count = 0
  138.     while ty > y do
  139.         count = count + 1
  140.         up()
  141.         if count > 300 then
  142.             print("Couldn't move up!")
  143.             return "top"
  144.         end
  145.     end
  146.     while ty < y do
  147.         count = count + 1
  148.         down()
  149.         if count > 300 then
  150.             print("Couldn't move down!")
  151.             return "bed"
  152.         end
  153.     end  
  154.     return "success"
  155. end
  156.  
  157. function record()
  158.     h = fs.open("move/pos.lua","w")
  159.     h.writeLine(dir)
  160.     h.writeLine(x)
  161.     h.writeLine(y)
  162.     h.writeLine(z)
  163.     h.close()
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement