Advertisement
Gampis

Untitled

Apr 20th, 2024 (edited)
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.83 KB | None | 0 0
  1. local turtleX = 15
  2. local turtleY = 65
  3. local turtleZ = 15
  4.  
  5. local Compass = {"W", "N", "E", "S"}
  6.  
  7. -- Direction = Compass index
  8. local Direction = "N"
  9.  
  10. -- -1 = RIGHT
  11.  
  12.  
  13.  
  14. local coords = {["X"] = turtleX, ["Y"] = turtleY, ["Z"] = turtleZ}
  15. local targetCoords = {["X"] = 0, ["Y"] = 70, ["Z"] = 0}
  16.  
  17. function RotateToDirection(targetDirection)
  18.     local CurrentDirectionIndex = 0
  19.     local TargetDirectionIndex = 0
  20.  
  21.     for index, dir in pairs(Compass) do
  22.         if dir == Direction then
  23.             CurrentDirectionIndex = index
  24.         end
  25.     end
  26.  
  27.     for index, dir in pairs(Compass) do
  28.         if dir == targetDirection then
  29.             TargetDirectionIndex = index
  30.         end
  31.     end
  32.  
  33.     local diff = TargetDirectionIndex - CurrentDirectionIndex
  34.     print("Current Direction: " .. Direction .. ". \n Current Direction Index: " .. tostring(CurrentDirectionIndex))
  35.     print("Current Target Direction: " .. targetDirection .. ". \n Current Target Direction Index: " .. tostring(TargetDirectionIndex))
  36.     print("Current diff: " .. tostring(diff))
  37.  
  38.     if diff == 0 then
  39.         -- Already facing direction.
  40.     elseif diff == 3 or diff == -3 then
  41.         turtle.turnRight()
  42.     elseif diff == -1 or diff == 1 then
  43.         turtle.turnLeft()
  44.     else
  45.         turtle.turnRight()
  46.         turtle.turnRight()
  47.     end
  48.  
  49.     Direction = Compass[TargetDirectionIndex]
  50.    
  51. end
  52.  
  53. -- Move to correct Y
  54. for y = coords.Y + 1, targetCoords.Y, 1 do
  55.     local checkUp = turtle.inspectUp()
  56.     if checkUp == false then
  57.         turtle.up()
  58.     else
  59.         turtle.digUp()
  60.         turtle.up()
  61.     end
  62.     coords["Y"] = coords["Y"] + 1
  63. end
  64.  
  65. -- Move to correct X
  66. if coords.X < targetCoords.X then
  67.     RotateToDirection("E")
  68.     for x = coords.X + 1, targetCoords.X, 1 do
  69.         local check = turtle.inspect()
  70.         if check == false then
  71.             turtle.forward()
  72.         else
  73.             turtle.dig()
  74.             turtle.forward()
  75.         end
  76.     end
  77. else
  78.     RotateToDirection("W")
  79.     for x = coords.X - 1, targetCoords.X, -1 do
  80.         local check = turtle.inspect()
  81.         if check == false then
  82.             turtle.forward()
  83.         else
  84.             turtle.dig()
  85.             turtle.forward()
  86.         end
  87.     end
  88.    
  89. end
  90. -- Move to correct Z
  91. if coords.Z < targetCoords.Z then
  92.     RotateToDirection("S")
  93.     for z = coords.Z + 1, targetCoords.Z, 1 do
  94.         local check = turtle.inspect()
  95.         if check == false then
  96.             turtle.forward()
  97.         else
  98.             turtle.dig()
  99.             turtle.forward()
  100.         end
  101.     end
  102. else
  103.     RotateToDirection("N")
  104.     for z = coords.Z - 1, targetCoords.Z, -1 do
  105.         local check = turtle.inspect()
  106.         if check == false then
  107.             turtle.forward()
  108.         else
  109.             turtle.dig()
  110.             turtle.forward()
  111.         end
  112.     end
  113. end
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement