Advertisement
montana_1

Go to location

Oct 20th, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function setHeading(heading,newHeading)
  2.      if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
  3.         if(turtle.getFuelLevel() < 2) then
  4.             error("Insufficient fuel")
  5.         end
  6.                 local start = vector.new(gps.locate())
  7.                 while(turtle.detect()) do
  8.                         turtle.dig()
  9.                 end
  10.                 turtle.forward()
  11.                 local current = vector.new(gps.locate())
  12.                 turtle.back()
  13.                 if(start.z - current.z > 0) then
  14.                         heading = 'N'
  15.                 elseif (start.z - current.z < 0) then
  16.                         heading = 'S'
  17.                 end
  18.                 if(start.x - current.x > 0) then
  19.                         heading = 'W'
  20.                 elseif (start.x - current.x < 0) then
  21.                         heading = 'E'
  22.                 end
  23.         end
  24.  
  25.     if(heading ~= newHeading) then
  26.         if(newHeading == 'N' or newHeading == 'n') then
  27.             if(heading == 'S' or heading == 's') then
  28.                 turtle.turnLeft()
  29.                 turtle.turnLeft()
  30.             end
  31.             if(heading == 'E' or heading == 'e') then
  32.                 turtle.turnLeft()
  33.             end
  34.             if(heading == 'W' or heading == 'w') then
  35.                 turtle.turnRight()
  36.             end
  37.         end
  38.         if(newHeading == 'E' or newHeading == 'e') then
  39.             if(heading == 'S' or heading == 's') then
  40.                 turtle.turnLeft()
  41.             end
  42.             if(heading == 'N' or heading == 'n') then
  43.                 turtle.turnRight()
  44.             end
  45.             if(heading == 'W' or heading == 'w') then
  46.                 turtle.turnRight()
  47.                 turtle.turnRight()
  48.             end
  49.         end
  50.         if(newHeading == 'S' or newHeading == 's') then
  51.             if(heading == 'N' or heading == 'n') then
  52.                 turtle.turnLeft()
  53.                 turtle.turnLeft()
  54.             end
  55.             if(heading == 'E' or heading == 'e') then
  56.                 turtle.turnRight()
  57.             end
  58.             if(heading == 'W' or heading == 'w') then
  59.                 turtle.turnLeft()
  60.             end
  61.         end
  62.         if(newHeading == 'W' or newHeading == 'w') then
  63.             if(heading == 'S' or heading == 's') then
  64.                 turtle.turnRight()
  65.             end
  66.             if(heading == 'E' or heading == 'e') then
  67.                 turtle.turnLeft()
  68.                 turtle.turnLeft()
  69.             end
  70.             if(heading == 'N' or heading == 'n') then
  71.                 turtle.turnLeft()
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. function getHeading()
  78.     if(turtle.getFuelLevel() < 2) then
  79.         error("Insufficient fuel")
  80.     end
  81.     local start = vector.new(gps.locate())
  82.     while(turtle.detect()) do
  83.         turtle.dig()
  84.     end
  85.     turtle.forward()
  86.     local current = vector.new(gps.locate())
  87.     turtle.back()
  88.     if(start.z - current.z > 0) then
  89.         heading = 'N'
  90.     elseif (start.z - current.z < 0) then
  91.         heading = 'S'
  92.     end
  93.     if(start.x - current.x > 0) then
  94.         heading = 'W'
  95.     elseif (start.x - current.x < 0) then
  96.         heading = 'E'
  97.     end
  98.         return heading
  99. end
  100.  
  101. function goToLocation(location,heading)
  102.     if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
  103.         heading = getHeading()
  104.     end
  105.    
  106.     local current = vector.new(gps.locate())
  107.     if(location.x ~= current.x or location.y ~= current.y or location.z ~= current.z) then
  108.         if(turtle.getFuelLevel() < 2) then
  109.             error("Insufficient fuel")
  110.         end
  111.     end
  112.     while(location.y ~= current.y) do
  113.         if(location.y > current.y) then
  114.             while(turtle.detectUp()) do
  115.                 turtle.digUp()
  116.             end
  117.             turtle.up()
  118.         else
  119.             while(turtle.detectDown()) do
  120.                 turtle.digDown()
  121.             end
  122.             turtle.down()
  123.         end
  124.         current = vector.new(gps.locate())
  125.     end
  126.    
  127.     while(location.z ~= current.z) do
  128.         if(location.z > current.z) then
  129.             setHeading(heading,'S')
  130.             heading = 'S'
  131.         elseif(location.z < current.z) then
  132.             setHeading(heading,'N')
  133.             heading = 'N'
  134.         end
  135.         while(turtle.detect()) do
  136.             turtle.dig()
  137.         end
  138.         turtle.forward()
  139.         current = vector.new(gps.locate())
  140.     end
  141.  
  142.     while(location.x ~= current.x) do
  143.         if(location.x > current.x) then
  144.             setHeading(heading,'E')
  145.             heading = 'E'
  146.         elseif(location.x < current.x) then
  147.             setHeading(heading,'W')
  148.             heading = 'W'
  149.         end
  150.         while(turtle.detect()) do
  151.             turtle.dig()
  152.         end
  153.         turtle.forward()
  154.         current = vector.new(gps.locate())
  155.     end
  156.     return heading
  157. end
  158.  
  159. print("X: ")
  160. x = tonumber(read())
  161. print("Y: ")
  162. y = tonumber(read())
  163. print("Z: ")
  164. z = tonumber(read())
  165. print("Heading: ")
  166. heading = read()
  167.  
  168. location = vector.new(x,y,z)
  169.  
  170.  
  171. print(goToLocation(location,heading))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement