Advertisement
CaptainSpaceCat

Turtle GoTo

May 16th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. local tArgs = {...}
  2. xDest, yDest, zDest = tonumber(tArgs[1]), tonumber(tArgs[2]), tonumber(tArgs[3])
  3.  
  4. direct = {}
  5. direct[0] = "south"
  6. direct[1] = "west"
  7. direct[2] = "north"
  8. direct[3] = "east"
  9.  
  10. function finalDir()
  11.   destDir = nil
  12.   if tArgs[4] then
  13.     for i, v in pairs(direct) do
  14.       if tArgs[4] == v then
  15.         destDir = i
  16.       end
  17.     end
  18.     while fDir ~= destDir do
  19.       left()
  20.     end
  21.   end
  22. end
  23.  
  24. function correctUsage()
  25.   if #tArgs < 3 or #tArgs > 4 then
  26.     print("Correct usage: goTo <X> <Y> <Z> (<ending direction>)")
  27.     print("NOTE: Requires GPS API")
  28.   end
  29. end
  30.  
  31. function direction()
  32.   xPos, yPos, zPos = gps.locate()
  33.   if not xPos or not yPos or not zPos then
  34.     print("ERROR: GPS API REQUIRED")
  35.     os.exit(goTo)
  36.   end
  37.   turtle.forward()
  38.   xPos2, yPos2, zPos2 = gps.locate()
  39.   turtle.back()
  40.   if xPos ~= xPos2 then
  41.     if xPos > xPos2 then
  42.       fDir = 1
  43.     else
  44.       fDir = 3
  45.     end
  46.   else
  47.     if zPos > zPos2 then
  48.       fDir = 2
  49.     else
  50.       fDir = 0
  51.     end
  52.   end
  53.   print("Initial direction: " .. tonumber(fDir) .. "::" .. direct[tonumber(fDir)])
  54.   if tArgs[4] then
  55.     print("Final direction: " .. tonumber(destDir) .. "::" .. direct[tonumber(destDir)])
  56.   else
  57.     print("Final direction: Undefined")
  58.   end
  59. end
  60.  
  61. function distanceFormula()
  62.   xMath = math.abs(xPos - xDest)
  63.   yMath = math.abs(yPos - yDest)
  64.   zMath = math.abs(zPos - zDest)
  65.   hyp = math.sqrt(xMath^2 + yMath^2 + zMath^2)
  66.  
  67.   print("Distance: " .. hyp .. " blocks")
  68. end
  69.  
  70. function forward()
  71.   turtle.forward()
  72.   if fDir == 0 then zPos = zPos + 1 end
  73.   if fDir == 2 then zPos = zPos - 1 end
  74.   if fDir == 1 then xPos = xPos - 1 end
  75.   if fDir == 3 then xPos = xPos + 1 end
  76. end
  77.  
  78. function left()
  79.   turtle.turnLeft()
  80.   fDir = fDir - 1
  81.   if fDir < 0 then fDir = 3 end
  82. end
  83.  
  84. function goToX()
  85.   if xDest < xPos then
  86.     while fDir ~= 1 do
  87.       left()
  88.     end
  89.   elseif xDest > xPos then
  90.     while fDir ~= 3 do
  91.       left()
  92.     end
  93.   end
  94.   while xPos ~= xDest do
  95.     turtle.dig()
  96.     forward()
  97.     print(xPos)
  98.   end
  99. end
  100.  
  101. function goToZ()
  102.   if zDest < zPos then
  103.     while fDir ~= 2 do
  104.       left()
  105.     end
  106.   elseif zDest > zPos then
  107.     while fDir ~= 0 do
  108.       left()
  109.     end
  110.   end
  111.   while zPos ~= zDest do
  112.     turtle.dig()
  113.     forward()
  114.   end
  115. end
  116.  
  117. function goToY()
  118.   if yDest < yPos then
  119.     while yDest ~= yPos do
  120.       turtle.digDown()
  121.       turtle.down()
  122.       yPos = yPos - 1
  123.     end
  124.   elseif yDest > yPos then
  125.     while yDest ~= yPos do
  126.       turtle.digUp()
  127.       turtle.up()
  128.       yPos = yPos + 1
  129.     end
  130.   end
  131. end
  132.  
  133. correctUsage()
  134. direction()
  135. distanceFormula()
  136. write("Traveling... ")
  137. goToX()
  138. goToZ()
  139. goToY()
  140. print("Complete")
  141. finalDir()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement