Advertisement
Guest User

pathfinder

a guest
Apr 9th, 2020
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1.  
  2. print("Input turtle X coord")
  3. xCoord = read()
  4. xCoord = tonumber(xCoord)
  5. print("Input turtle Y coord")
  6. yCoord = read()
  7. yCoord = tonumber(yCoord)
  8. print("Input turtle Z coord")
  9. zCoord = read()
  10. zCoord = tonumber(zCoord)
  11. print("Input turtle direction NSEW")
  12. facing = read()
  13.  
  14. function stepForwards(int)
  15.     local i = 0
  16.     repeat
  17.         if turtle.forward() then
  18.             i = i + 1
  19.             if facing == "N" then
  20.                 zCoord = zCoord - 1            
  21.             elseif facing == "E" then
  22.                 xCoord = xCoord + 1
  23.             elseif facing == "S" then
  24.                 zCoord = zCoord + 1
  25.             elseif facing == "W" then
  26.                 xCoord = xCoord - 1
  27.             end
  28.         else
  29.             turtle.attack()
  30.             turtle.dig()
  31.         end
  32.     until i == int
  33.    
  34. end
  35.  
  36. function stepDown(int)
  37.     local i = 0
  38.     repeat
  39.         if turtle.down() then
  40.             i = i + 1
  41.             yCoord = yCoord - 1
  42.         else
  43.             turtle.attackDown()
  44.             turtle.digDown()
  45.         end
  46.     until i == int
  47. end
  48.  
  49. function stepUp(int)
  50.     local i = 0
  51.     repeat
  52.         if turtle.up() then
  53.             i = i + 1
  54.             yCoord = yCoord + 1
  55.         else
  56.             turtle.attackUp()
  57.             turtle.digUp()
  58.         end
  59.     until i == int
  60. end
  61.  
  62. function rotateNorth()
  63.     if facing == "N" then
  64.         facing = "N"
  65.         return true
  66.     elseif facing == "E" then
  67.         facing = "N"
  68.         turtle.turnLeft()
  69.         return true    
  70.     elseif facing == "S" then
  71.         facing = "N"
  72.         turtle.turnLeft()
  73.         turtle.turnLeft()
  74.         return true
  75.     elseif facing == "W" then
  76.         facing = "N"
  77.         turtle.turnRight()
  78.         return true
  79.     end
  80. end
  81.  
  82. function rotateEast()
  83.     if facing == "N" then
  84.         facing = "E"
  85.         turtle.turnRight()
  86.         return true
  87.     elseif facing == "E" then
  88.         facing = "E"
  89.         return true    
  90.     elseif facing == "S" then
  91.         facing = "E"
  92.         turtle.turnLeft()
  93.         return true
  94.     elseif facing == "W" then
  95.         facing = "E"
  96.         turtle.turnRight()
  97.         turtle.turnRight()
  98.         return true
  99.     end
  100. end
  101.  
  102. function rotateSouth()
  103.     if facing == "N" then
  104.         facing = "S"
  105.         turtle.turnLeft()
  106.         turtle.turnLeft()
  107.         return true
  108.     elseif facing == "E" then
  109.         facing = "S"
  110.         turtle.turnRight()
  111.         return true    
  112.     elseif facing == "S" then
  113.         facing = "S"
  114.         return true
  115.     elseif facing == "W" then
  116.         facing = "S"
  117.         turtle.turnLeft()
  118.         return true
  119.     end
  120. end
  121. function rotateWest()
  122.     if facing == "N" then
  123.         facing = "W"
  124.         turtle.turnLeft()
  125.         return true
  126.     elseif facing == "E" then
  127.         facing = "W"
  128.         turtle.turnRight()
  129.         turtle.turnRight()
  130.         return true    
  131.     elseif facing == "S" then
  132.     facing = "W"
  133.         turtle.turnRight()
  134.         return true
  135.     elseif facing == "W" then
  136.         facing = "W"
  137.         return true
  138.     end
  139. end
  140.  
  141. while true do
  142.     print("input targetX")
  143.     targetX = read()
  144.     targetX = tonumber(targetX)
  145.     print("input targetY")
  146.     targetY = read()
  147.     targetY = tonumber(targetY)
  148.     print("input targetZ")
  149.     targetZ = read()
  150.     targetZ = tonumber(targetZ)
  151.    
  152.     if xCoord < targetX then
  153.         rotateEast()
  154.         stepForwards(targetX - xCoord)
  155.     elseif xCoord > targetX then
  156.         rotateWest()
  157.         stepForwards(xCoord - targetX)
  158.     end
  159.     if yCoord > targetY then
  160.         stepDown(yCoord - targetY)
  161.     elseif yCoord < targetY then
  162.         stepUp(targetY - yCoord)
  163.     end
  164.     if zCoord > targetZ then
  165.         rotateNorth()
  166.         stepForwards(zCoord - targetZ)
  167.     elseif zCoord < targetZ then
  168.         rotateSouth()
  169.         stepForwards(targetZ - zCoord)
  170.     end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement