einsteinmaster96

movement.lua

Jan 4th, 2025 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | Gaming | 0 0
  1. current_x = 0
  2. current_y = 0
  3. dir_x = 0
  4. dir_y = 1
  5.  
  6. function turnLeft()
  7.     tmp_dx = dir_x
  8.     tmp_dy = dir_y
  9.     dir_x = -tmp_dy
  10.     dir_y = tmp_dx
  11.     turtle.turnLeft()
  12. end
  13.  
  14. function turnRight()
  15.     tmp_dx = dir_x
  16.     tmp_dy = dir_y
  17.     dir_x = tmp_dy
  18.     dir_y = -tmp_dx
  19.     turtle.turnRight()
  20. end
  21.  
  22. function set_dir(dx,dy)
  23.     while dx ~= dir_x or dy ~= dir_y do
  24.         turnLeft()
  25.     end
  26. end
  27.  
  28. function move_back_to ( y )
  29.     while dir_y ~= -1 do
  30.         turnLeft()
  31.     end
  32.     while current_y > y do
  33.         turtle.dig()
  34.         res = turtle.forward()
  35.         if res then
  36.             current_y = current_y - 1
  37.         end
  38.     end
  39. end
  40.  
  41. function move_forward_to ( y )
  42.     while dir_y ~= 1 do
  43.         turnLeft()
  44.     end
  45.     while current_y < y do
  46.         turtle.dig()
  47.         res = turtle.forward()
  48.         if res then
  49.             current_y = current_y + 1
  50.         end
  51.     end
  52. end
  53.  
  54. function move_left_to ( x )
  55.     while dir_x ~= -1 do
  56.         turnLeft()
  57.     end
  58.     while current_x > x do
  59.         turtle.dig()
  60.         res = turtle.forward()
  61.         if res then
  62.             current_x = current_x - 1
  63.         end
  64.     end
  65. end
  66.  
  67. function move_right_to ( x )
  68.     while dir_x ~= 1 do
  69.         turnRight()
  70.     end
  71.     while current_x < x do
  72.         turtle.dig()
  73.         res = turtle.forward()
  74.         if res then
  75.             current_x = current_x + 1
  76.         end
  77.     end
  78. end
  79.  
  80. function move_to ( x , y )
  81.     if x > current_x then
  82.         move_right_to(x)
  83.     end
  84.     if x < current_x then
  85.         move_left_to(x)
  86.     end
  87.     if y > current_y then
  88.         move_forward_to(y)
  89.     end
  90.     if y < current_y then
  91.         move_back_to(y)
  92.     end
  93. end
  94.  
  95. current_z = 0
  96. function move_z_to(z)
  97.     while current_z < z do
  98.         if not turtle.up() then
  99.             turtle.digUp()
  100.         else
  101.             current_z = current_z + 1
  102.         end
  103.     end
  104.     while current_z > z do
  105.         if not turtle.down() then
  106.             turtle.digDown()
  107.         else
  108.             current_z = current_z - 1
  109.         end
  110.     end
  111. end
  112.  
Advertisement
Add Comment
Please, Sign In to add comment