Advertisement
montana_1

Set Heading

Oct 20th, 2014
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  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.  
  78. print("Please enter heading")
  79. heading = read()
  80. newheading = read()
  81. setHeading(heading,newheading)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement