mrkirby153

[TURTLE] Goto

Mar 7th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. -- 0: north 1: east 2: south 3: west
  4. local direction = 0
  5.  
  6. local currentX, currentY, currentZ
  7. local targetX, targetY, targetZ
  8.  
  9. if #tArgs < 3 then
  10.     print("Usage: goto <x> <y> <z>")
  11.     return
  12. end
  13. targetX = tArgs[1]
  14. targetY = tArgs[2]
  15. targetZ = tArgs[3]
  16.  
  17. function determineLocation()
  18.     currentX, currentY, currentZ = gps.locate()
  19.     print("Current Position: "..currentX..":"..currentY..":"..currentZ)
  20. end
  21.  
  22. function determineDirection()
  23.     determineLocation()
  24.     turtle.forward()
  25.     newX, newY, newZ = gps.locate()
  26.     turtle.back()
  27.    
  28.     deltaX = newX - currentX
  29.     deltaZ = newZ - currentZ
  30.    
  31.     if deltaX <= -1 then
  32.         direction = 3
  33.     elseif deltaX >= 1 then
  34.         direction = 1
  35.     elseif deltaZ <= -1 then
  36.         direction = 0
  37.     elseif deltaZ >= 1 then
  38.         direction = 2
  39.     end
  40.     print("Delta: "..deltaX..":"..deltaY..":"..deltaZ)
  41.     print("Facing: "..direction)
  42. end
  43.  
  44. function face(newDir)
  45.     if direction < newDir then
  46.         for i=1, newDir - direction do
  47.             turtle.turnRight()
  48.         end
  49.         direction = newDir
  50.     else
  51.         for i=1, direction - newDir do
  52.             turtle.turnLeft()
  53.         end
  54.         direction = newDir
  55.     end
  56. end
  57.  
  58. function move()
  59.     print("Navigating to: "..targetX..", "..targetY..", "..targetZ)
  60.     deltaX = math.abs(currentX - targetX)
  61.     deltaY = math.abs(currentY - targetY)
  62.     deltaZ = math.abs(currentZ - targetZ)
  63.     totalFuel = deltaX + deltaY + deltaZ
  64.     print("Needed: "..totalFuel.." got "..turtle.getFuelLevel())
  65.     if turtle.getFuelLevel() < totalFuel then
  66.         print("Not enough fuel to complete the task")
  67.         return
  68.     end
  69.     print(deltaX..":"..deltaY..":"..deltaZ)
  70.     if tonumber(targetX) < tonumber(currentX) then
  71.         face(3)
  72.     else
  73.         face(1)
  74.     end
  75.     for i=1,deltaX do
  76.         if not turtle.forward() then
  77.             turtle.dig()
  78.         end
  79.     end
  80.     -- Go to the z coord
  81.     if tonumber(targetZ) < tonumber(currentZ) then
  82.         face(0)
  83.     else
  84.         face(2)
  85.     end
  86.     for i=1,deltaZ do
  87.         if not turtle.forward() then
  88.             turtle.dig()
  89.         end
  90.     end
  91.     -- Go to the y coord
  92.     for i=1,deltaY do
  93.         if tonumber(currentY) > tonumber(targetY) then
  94.             if turtle.down() then
  95.                 turtle.digDown()
  96.             end
  97.         else
  98.             if turtle.up() then
  99.                 turtle.digUp()
  100.             end
  101.         end
  102.     end
  103. end
  104. determineDirection()
  105. move()
Advertisement
Add Comment
Please, Sign In to add comment