Guest User

move.lua

a guest
Feb 14th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 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("oreMine/pos.lua") then
  12.     h = fs.open("oreMine/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.  
  37. function forward()
  38.     turtle.dig()
  39.     if turtle.forward() then
  40.         x = x + xdir[dir]
  41.         z = z + zdir[dir]
  42.         record()
  43.         return true
  44.     else
  45.         return false
  46.     end
  47. end
  48.  
  49. function back()
  50.     if turtle.back() then
  51.         x = x - xdir[dir]
  52.         z = z - zdir[dir]
  53.         record()
  54.         return true
  55.     else
  56.         return false
  57.     end
  58. end
  59.  
  60. function left()
  61.     if turtle.turnLeft() then
  62.         dir = dir - 1
  63.         if dir < 1 then
  64.             dir = 4
  65.         end
  66.         record()
  67.         return true
  68.     end
  69.     return false
  70. end
  71.  
  72. function right()
  73.     if turtle.turnRight() then
  74.         dir = dir + 1
  75.         if dir > 4 then
  76.             dir = 1
  77.         end
  78.         record()
  79.         return true
  80.     end
  81.     return false
  82. end
  83.  
  84. function up()
  85.     turtle.digUp()
  86.     if turtle.up() then
  87.         y = y + 1
  88.         record()
  89.         return true
  90.     end
  91.     return false
  92. end
  93.  
  94. function down()
  95.     turtle.digDown()
  96.     if turtle.down() then
  97.         y = y - 1
  98.         record()
  99.         return true
  100.     end
  101.     return false
  102. end
  103.  
  104. function face(d)
  105.     while d < dir do
  106.         left()
  107.     end
  108.     while d > dir do
  109.         right()
  110.     end
  111. end
  112.  
  113. function move(tx, ty, tz)
  114.     while tx > x do
  115.         face(2)
  116.         forward()
  117.     end
  118.     while tx < x do
  119.         face(4)
  120.         forward()
  121.     end
  122.     while tz > z do
  123.         face(3)
  124.         forward()
  125.     end
  126.     while tz < z do
  127.         face(1)
  128.         forward()
  129.     end
  130.     count = 0
  131.     while ty > y do
  132.         count = count + 1
  133.         up()
  134.         if count > 300 then
  135.             print("Couldn't move up!")
  136.             return "top"
  137.         end
  138.     end
  139.     while ty < y do
  140.         count = count + 1
  141.         down()
  142.         if count > 300 then
  143.             print("Couldn't move down!")
  144.             return "bed"
  145.         end
  146.     end  
  147.     return "success"
  148. end
  149.  
  150. function record()
  151.     h = fs.open("oreMine/pos.lua","w")
  152.     h.writeLine(dir)
  153.     h.writeLine(x)
  154.     h.writeLine(y)
  155.  
  156.     h.writeLine(z)
  157.     h.close()
  158. end
Advertisement
Add Comment
Please, Sign In to add comment