Elas

moveto

Apr 10th, 2022 (edited)
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.96 KB | None | 0 0
  1. x = tonumber(arg[1])
  2. y = tonumber(arg[2])
  3. z = tonumber(arg[3])
  4.  
  5. int_x, int_y, int_z = gps.locate()
  6.  
  7. function updateCords()
  8.     int_x, int_y, int_z = gps.locate()
  9.     return int_x, int_y, int_z
  10.  
  11. end
  12.  
  13. function facingWhere(facing)
  14.     print("Figuring out where I am facing")
  15.  
  16.     int_x, int_y, int_z = updateCords()
  17.  
  18.     if not turtle.detect() then
  19.         turtle.forward()
  20.     else
  21.         turtle.dig()
  22.         turtle.forward()
  23.     end
  24.  
  25.     print("Getting coordinates..")
  26.     cmp_x, cmp_y, cmp_z = gps.locate()
  27.     print("I am at: "..cmp_x.." "..cmp_y.." "..cmp_z)
  28.  
  29.     if (int_x - cmp_x == 0) == false then
  30.         if (int_x - cmp_x == -1) then
  31.                 facing = "east"
  32.         elseif (int_x - cmp_x == 1) then
  33.                 facing = "west"
  34.         end
  35.     elseif (int_z - cmp_z == 0) == false then
  36.         if (int_z - cmp_z == -1) then
  37.                 facing = "south"
  38.         elseif (int_z - cmp_z == 1) then
  39.                 facing = "north"
  40.         end
  41.     end
  42.  
  43.     return facing
  44.  
  45. end
  46.  
  47. function face(dir, facing)
  48.     --facing = facingWhere()
  49.  
  50.     print("Turnig to face "..dir..". I am facing "..tostring(facing))
  51.  
  52.     if dir == facing then
  53.         return facing
  54.     end
  55.  
  56.     if facing == "north" then
  57.         if dir == "west" then
  58.             turtle.turnLeft()
  59.             facing = "west"
  60.             return facing
  61.         elseif dir == "east" then
  62.             turtle.turnRight()
  63.             facing = "east"
  64.             return facing
  65.         elseif dir == "south" then
  66.             turtle.turnRight()
  67.             turtle.turnRight()
  68.             facing = "south"
  69.             return facing
  70.         end
  71.     elseif facing == "west" then
  72.         if dir == "north" then
  73.             turtle.turnRight()
  74.             facing = "north"
  75.             return facing
  76.         elseif dir == "south" then
  77.             turtle.turnLeft()
  78.             facing = "south"
  79.             return facing
  80.         elseif dir == "east" then
  81.             turtle.turnRight()
  82.             turtle.turnRight()
  83.             facing = "east"
  84.             return facing
  85.         end
  86.     elseif facing == "south" then
  87.         if dir == "west" then
  88.             turtle.turnRight()
  89.             facing = "west"
  90.             return facing
  91.         elseif dir == "east" then
  92.             turtle.turnLeft()
  93.             facing = "east"
  94.             return facing
  95.         elseif dir == "north" then
  96.             turtle.turnRight()
  97.             turtle.turnRight()
  98.             facing = "north"
  99.             return facing
  100.         end
  101.     elseif facing == "east" then
  102.         if dir == "north" then
  103.             turtle.turnLeft()
  104.             facing = "north"
  105.             return facing
  106.         elseif dir == "south" then
  107.             turtle.turnRight()
  108.             facing = "south"
  109.             return facing
  110.         elseif dir == "west" then
  111.             turtle.turnRight()
  112.             turtle.turnRight()
  113.             facing = "west"
  114.             return facing
  115.         end
  116.     end
  117.  
  118.     if facing == nil then
  119.         facing = facingWhere(facing)
  120.         face(dir, facing)
  121.     end
  122.  
  123.     if (facing == dir) == false then
  124.         go(dir)
  125.     end
  126. end
  127.  
  128. function go(direction, coord)
  129.     int_x, int_y, int_z = updateCords()
  130.     print("GOING")
  131.  
  132.     facing = face(direction, facing)
  133.  
  134.     if coord == "z" then
  135.         while (int_z == z) == false do
  136.             if direction == facing then
  137.                 if turtle.detect() then
  138.                     turtle.dig()
  139.                     turtle.forward()
  140.                 else
  141.                     turtle.forward()
  142.                 end
  143.             end
  144.             int_x, int_y, int_z = updateCords()
  145.         end
  146.     elseif coord == "x" then
  147.         while (int_x == x) == false do
  148.             if direction == facing then
  149.                 if turtle.detect() then
  150.                     turtle.dig()
  151.                     turtle.forward()
  152.                 else
  153.                     turtle.forward()
  154.                 end
  155.             end
  156.             int_x, int_y, int_z = updateCords()
  157.         end
  158.     end
  159. end
  160.  
  161. function moveTo(x, y, z)
  162.     facing = facingWhere(facing)
  163.     z_want = ""
  164.     x_want = ""
  165.     print("Target: "..x.." "..y.." "..z)
  166.  
  167.     if x == int_x and y == int_y and z == int_z then
  168.         print("Already at destination.")
  169.         return
  170.     end
  171.  
  172.  
  173.     while true do
  174.         int_x, int_y, int_z = updateCords()
  175.  
  176.         if z > int_z then
  177.             z_want = "south"
  178.         elseif z < int_z then
  179.             z_want = "north"
  180.         end
  181.        
  182.         if x > int_x then
  183.             x_want = "east"
  184.         elseif x < int_x then
  185.             x_want = "west"
  186.         end
  187.  
  188.         if (z == int_z) == false then
  189.             go(z_want, "z")
  190.         elseif (x == int_x) == false then
  191.             go(x_want, "x")
  192.         end
  193.  
  194.  
  195.         int_x, int_y, int_z = updateCords()
  196.  
  197.         if x == int_x and z == int_z then
  198.             print("Arrived at destination.")
  199.             break
  200.         end
  201.     end
  202. end
  203.  
  204. moveTo(x, y, z)
Add Comment
Please, Sign In to add comment