Advertisement
ItsNoah

MoveToLOc

Jun 11th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function moveForward(times)
  2.     if times then
  3.         for i = 1, times do
  4.             while not turtle.forward() do
  5.                 turtle.dig()
  6.             end
  7.         end
  8.     else
  9.         while not turtle.forward() do
  10.             turtle.dig()
  11.         end
  12.     end
  13. end
  14.  
  15. function moveUp(times)  
  16.     if times then
  17.         for i = 1, times do
  18.             while not turtle.up() do
  19.                 turtle.digUp()
  20.             end
  21.         end
  22.     else
  23.         while not turtle.up() do
  24.             turtle.digUp()
  25.         end
  26.     end
  27. end
  28.  
  29. function moveDown(times)  
  30.     if times then
  31.         for i = 1, times do
  32.             while not turtle.down() do
  33.                 turtle.digDown()
  34.             end
  35.         end
  36.     else
  37.         while not turtle.down() do
  38.             turtle.digDown()
  39.         end
  40.     end
  41. end
  42.  
  43. -- Location needs to be a vector.
  44. function moveToLocation(location)
  45.  
  46.     local startingPosition = 0
  47.     local currentPosition = 0
  48.  
  49.     if gps.locate(2 , false) then
  50.         startingPosition = vector.new(gps.locate(2 , false))
  51.         currentPosition = startingPosition
  52.  
  53.         -- Y Movement
  54.         if currentPosition.y > location.y then
  55.             -- We need to move down.
  56.             while currentPosition.y ~= location.y do
  57.                 moveDown()
  58.                 currentPosition = vector.new(gps.locate(2 , false))
  59.             end
  60.         else
  61.             -- We need to move up.
  62.             while currentPosition.y ~= location.y do
  63.                 moveUp()
  64.                 currentPosition = vector.new(gps.locate(2 , false))
  65.             end
  66.         end
  67.  
  68.         turtle.forward()
  69.         currentPosition = vector.new(gps.locate(2 , false))
  70.  
  71.         local difference = startingPosition - currentPosition
  72.  
  73.         local moving = ""
  74.  
  75.         if difference.x == -1 then
  76.             -- East
  77.             moving = "east"
  78.         elseif difference.x == 1 then
  79.             -- West
  80.             moving = "west"
  81.         end
  82.  
  83.         if difference.z == -1 then
  84.             -- South
  85.             moving = "south"
  86.         elseif difference.z == 1 then
  87.             -- North
  88.             moving = "north"
  89.         end
  90.  
  91.         -- X Movement
  92.         if currentPosition.x > location.x then
  93.             -- We need to move West, to let's face that direction relative to the current facting direciton.
  94.             if moving == "south" then
  95.                 turtle.turnRight()
  96.             elseif moving == "north" then
  97.                 turtle.turnLeft()
  98.             elseif moving == "east" then
  99.                 turtle.turnRight()
  100.                 turtle.turnRight()
  101.             end
  102.  
  103.             moving = "west"
  104.  
  105.             while currentPosition.x ~= location.x do
  106.                 moveForward()
  107.                 currentPosition = vector.new(gps.locate(2 , false))
  108.                 print(moving)
  109.             end
  110.         else
  111.             -- We need to move East, to let's face that direction relative to the current facting direciton.
  112.             if moving == "south" then
  113.                 turtle.turnLeft()
  114.             elseif moving == "north" then
  115.                 turtle.turnRight()
  116.             elseif moving == "west" then
  117.                 turtle.turnRight()
  118.                 turtle.turnRight()
  119.             end
  120.  
  121.             moving = "east"
  122.  
  123.             while currentPosition.x ~= location.x do
  124.                 moveForward()
  125.                 currentPosition = vector.new(gps.locate(2 , false))
  126.                 print(moving)
  127.             end
  128.         end
  129.  
  130.         -- Z Movement
  131.         if currentPosition.z > location.z then
  132.             -- We need to move North, to let's face that direction relative to the current facting direciton.
  133.             if moving == "south" then
  134.                 turtle.turnRight()
  135.                 turtle.turnRight()
  136.             elseif moving == "west" then
  137.                 turtle.turnRight()
  138.             elseif moving == "east" then
  139.                 turtle.turnLeft()
  140.             end
  141.  
  142.             moving = "north"
  143.  
  144.             while currentPosition.z ~= location.z do
  145.                 moveForward()
  146.                 currentPosition = vector.new(gps.locate(2 , false))
  147.                 print(moving)
  148.             end
  149.         else
  150.             -- We need to move South, to let's face that direction relative to the current facting direciton.
  151.             if moving == "north" then
  152.                 turtle.turnRight()
  153.                 turtle.turnRight()
  154.             elseif moving == "west" then
  155.                 turtle.turnLeft()
  156.             elseif moving == "east" then
  157.                 turtle.turnRight()
  158.             end
  159.  
  160.             moving = "south"
  161.  
  162.             while currentPosition.z ~= location.z do
  163.                 moveForward()
  164.                 currentPosition = vector.new(gps.locate(2 , false))
  165.                 print(moving)
  166.             end
  167.         end
  168.  
  169.     else
  170.         print("Could not get turtle's location.")
  171.         return
  172.     end
  173. end
  174.  
  175. -- rednet.open("right")
  176.  
  177. -- moveToLocation(vector.new(536, 64, -1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement