Advertisement
Guest User

goTo

a guest
May 1st, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. local tArgs = {...}
  2. xDest, yDest, zDest = tArgs[1], tArgs[2], tArgs[3]
  3.  
  4. function correctUsage()
  5.   if #tArgs ~= 3 then
  6.     print("Correct usage: goTo <X> <Y> <Z>")
  7.     print("NOTE: Requires GPS API")
  8.   end
  9. end
  10.  
  11. function direction()
  12.   xPos, yPos, zPos = gps.locate()
  13.   turtle.forward()
  14.   xPos2, yPos2, zPos2 = gps.locate()
  15.   turtle.back()
  16.   if xPos ~= xPos2 then
  17.     if xPos > xPos2 then
  18.       fDir = 1
  19.     else
  20.       fDir = 3
  21.     end
  22.   else
  23.     if zPos > zPos2 then
  24.       fDir = 2
  25.     else
  26.       fDir = 0
  27.     end
  28.   end
  29.   print(fDir)
  30. end
  31.  
  32. function forward()
  33.   turtle.forward()
  34.   if fDir == 0 then zPos = zPos + 1 end
  35.   if fDir == 2 then zPos = zPos - 1 end
  36.   if fDir == 1 then xPos = xPos - 1 end
  37.   if fDir == 3 then xPos = xPos + 1 end
  38. end
  39.  
  40. function left()
  41.   turtle.turnLeft()
  42.   fDir = fDir - 1
  43.   if fDir < 0 then fDir = 3 end
  44. end
  45.  
  46. function right()
  47.   turtle.turnRight()
  48.   fDir = fDir + 1
  49.   if fDir > 3 then fDir = 0 end
  50. end
  51.  
  52. function goToX()
  53.   if xDest < xPos then
  54.     while fDir ~= 1 do
  55.       left()
  56.     end
  57.   elseif xDest > xPos then
  58.     while fDir ~= 3 do
  59.       left()
  60.     end
  61.   end
  62.   while xPos ~= xDest do
  63.     turtle.dig()
  64.     forward()
  65.   end
  66. end
  67.  
  68. function goToZ()
  69.   if zDest < zPos then
  70.     while fDir ~= 2 do
  71.       left()
  72.     end
  73.   elseif zDest > zPos then
  74.     while fDir ~= 0 do
  75.       left()
  76.     end
  77.   end
  78.   while zPos ~= zDest do
  79.     turtle.dig()
  80.     forward()
  81.   end
  82. end
  83.  
  84. function goToY()
  85.   if yDest < yPos then
  86.     while yDest ~= yPos do
  87.       turtle.digDown()
  88.       turtle.down()
  89.       yPos = yPos - 1
  90.     end
  91.   elseif yDest > yPos then
  92.     while yDest ~= yPos do
  93.       turtle.digUp()
  94.       turtle.Up()
  95.       yPos = yPos + 1
  96.     end
  97.   end
  98. end
  99.  
  100. correctUsage()
  101. direction()
  102. goToX()
  103. goToZ()
  104. goToY()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement