Advertisement
Jpaul999

flyto

Feb 24th, 2024 (edited)
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. -- Retrieve command line arguments
  2. local args = {...}
  3.  
  4. -- Check if there are three arguments provided
  5. if #args ~= 3 then
  6.     print("Usage: lua fly_and_return.lua <x> <y> <z>")
  7.     return
  8. end
  9.  
  10. -- Convert arguments to numbers
  11. local x, y, z = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
  12.  
  13. -- Function to fly to specific coordinates
  14. local function flyTo(x, y, z)
  15.     local startX, startY, startZ = gps.locate()
  16.     if not startX then
  17.         print("Error: Could not determine current position.")
  18.         return
  19.     end
  20.    
  21.     local deltaX, deltaY, deltaZ = x - startX, y - startY, z - startZ
  22.    
  23.     -- Move along X-axis
  24.     if deltaX > 0 then
  25.         turtle.turnRight()
  26.         for _ = 1, deltaX do
  27.             while not turtle.forward() do
  28.                 sleep(0.5)
  29.             end
  30.         end
  31.         turtle.turnLeft()
  32.     elseif deltaX < 0 then
  33.         turtle.turnLeft()
  34.         for _ = 1, math.abs(deltaX) do
  35.             while not turtle.forward() do
  36.                 sleep(0.5)
  37.             end
  38.         end
  39.         turtle.turnRight()
  40.     end
  41.    
  42.     -- Move along Y-axis
  43.     local currentY = startY
  44.     while currentY < y do
  45.         while not turtle.up() do
  46.             sleep(0.5)
  47.         end
  48.         currentY = currentY + 1
  49.     end
  50.     while currentY > y do
  51.         while not turtle.down() do
  52.             sleep(0.5)
  53.         end
  54.         currentY = currentY - 1
  55.     end
  56.    
  57.     -- Move along Z-axis
  58.     if deltaZ > 0 then
  59.         for _ = 1, deltaZ do
  60.             while not turtle.forward() do
  61.                 sleep(0.5)
  62.             end
  63.         end
  64.     elseif deltaZ < 0 then
  65.         turtle.turnAround()
  66.         for _ = 1, math.abs(deltaZ) do
  67.             while not turtle.forward() do
  68.                 sleep(0.5)
  69.             end
  70.         end
  71.     end
  72. end
  73.  
  74. -- Function to return to the start position
  75. local function returnToStart()
  76.     local startX, startY, startZ = gps.locate()
  77.     if not startX then
  78.         print("Error: Could not determine current position.")
  79.         return
  80.     end
  81.    
  82.     flyTo(startX, startY, startZ)
  83. end
  84.  
  85. -- Fly to the specified coordinates
  86. flyTo(x, y, z)
  87.  
  88. -- Perform actions at the destination
  89.  
  90. -- Return to the starting position
  91. returnToStart()
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement