Advertisement
t0x1c_adr1an

go_to

Apr 27th, 2021 (edited)
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. -- The way the face of the turtle is oriented
  2. -- N = 0; E = 1; S = 2; W = 3
  3. if #arg == 4 then
  4.     home_cords = vector.new(arg[1], arg[2], arg[3])
  5.     turtle_face = arg[4]
  6. end
  7.  
  8. local pos = vector.new(gps.locate())
  9. local diff = pos:sub(home_cords)
  10.  
  11. -- Helper functions
  12. local function around()
  13.     turtle.turnLeft()
  14.     turtle.turnLeft()
  15.     if turtle_face + 2 == 4 then
  16.         turtle_face = 0
  17.     elseif turtle_face + 2 == 5 then
  18.         turtle_face = 1
  19.     else
  20.         turtle_face = turtle_face + 2
  21.     end
  22. end
  23.  
  24. local function Left()
  25.     turtle.turnLeft()
  26.     if turtle_face - 1 == -1 then
  27.         turtle_face = 3
  28.     else
  29.         turtle_face = turtle_face - 1
  30.     end
  31. end
  32.  
  33. local function Right()
  34.     turtle.turnRight()
  35.     if turtle_face + 1 == 4 then
  36.         turtle_face = 0
  37.     else
  38.         turtle_face = turtle_face + 1
  39.     end
  40. end
  41.  
  42. local function update(cords)
  43.     pos = vector.new(gps.locate())
  44.     diff = pos:sub(cords)
  45. end
  46.  
  47. local function check_stuck()
  48.     while turtle_face ~= 0 do
  49.         Right()
  50.     end
  51.  
  52.     for x=0,3 do
  53.         if turtle.detect() then
  54.             Left()
  55.         else
  56.             turtle.forward()
  57.         end
  58.     end
  59. end
  60.  
  61. -- Main function
  62. local function return_to(cords)
  63.     --- Cord is a table, from the vector API, in {x='x', y='y', z='z'}
  64.     while diff.x ~= 0 or diff.y ~= 0 or diff.z ~= 0 do
  65.         pos = vector.new(gps.locate())
  66.  
  67.         -- For the x-field
  68.         if diff.x < 0 then
  69.             print('diff x < 0')
  70.             if turtle_face ~= 1 then
  71.                 while turtle_face ~= 1 do
  72.                     Right()
  73.                     print('face (if) : ', turtle_face)
  74.                 end
  75.             end
  76.  
  77.             while not turtle.detect() and diff.x < 0 and turtle_face == 1 do
  78.                 print('diff neg x: ', diff)
  79.                 print('pos: ', pos)
  80.                 print('face: ', turtle_face)
  81.                 turtle.forward()
  82.                 update(cords)
  83.             end
  84.            
  85.         elseif diff.x > 0 then
  86.             print('diff x > 0')
  87.             if turtle_face ~= 3 then
  88.                 while turtle_face ~= 3 do
  89.                     Right()
  90.                     print('face (if) : ', turtle_face)
  91.                 end
  92.             end
  93.  
  94.             while not turtle.detect() and diff.x > 0 and turtle_face == 3 do
  95.                 print('diff pos x: ', diff)
  96.                 print('pos: ', pos)
  97.                 print('face: ', turtle_face)
  98.                 turtle.forward()
  99.                 update(cords)
  100.             end
  101.         end
  102.  
  103.         -- For the y-field
  104.         if diff.y < 0 then
  105.             while not turtle.detectUp() and diff.y < 0 do
  106.                 print('diff neg y: ', diff)
  107.                 print('pos: ', pos)
  108.                 print('face: ', turtle_face)
  109.                 turtle.up()
  110.                 update(cords)
  111.             end
  112.         elseif diff.y > 0 then
  113.             while not turtle.detectDown() and diff.y > 0 do
  114.                 print('diff pos y: ', diff)
  115.                 print('pos: ', pos)
  116.                 print('face: ', turtle_face)
  117.                 turtle.down()
  118.                 update(cords)
  119.             end
  120.         end
  121.  
  122.         -- For the z-field
  123.         if diff.z < 0 then
  124.             print('diff z < 0')
  125.             if turtle_face ~= 0 then
  126.                 while turtle_face ~= 0 do
  127.                     Right()
  128.                     print('face (if) : ', turtle_face)
  129.                 end
  130.             end
  131.  
  132.             while not turtle.detect() and diff.z < 0 and turtle_face == 0 do
  133.                 print('diff neg y: ', diff)
  134.                 print('pos: ', pos)
  135.                 print('face: ', turtle_face)
  136.                 turtle.forward()
  137.                 update(cords)
  138.             end
  139.         elseif diff.z > 0 then
  140.             print('diff z > 0')
  141.             if turtle_face ~= 2 then
  142.                 while turtle_face ~= 2 do
  143.                     Right()
  144.                     print('face (if) : ', turtle_face)
  145.                 end
  146.             end
  147.  
  148.             while not turtle.detect() and diff.z > 0 and turtle_face == 2 do
  149.                 print('diff neg y: ', diff)
  150.                 print('pos: ', pos)
  151.                 print('face: ', turtle_face)
  152.                 turtle.forward()
  153.                 update(cords)
  154.             end
  155.  
  156.         end
  157.  
  158.     end
  159. end
  160.  
  161. return_to(home_cords)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement