Advertisement
Guest User

pathfinder

a guest
Jul 4th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. --Load Current Coordinates
  2. local file = fs.open("coord", "r")
  3. local cCoord = file.readAll()
  4. file.close()
  5. cCoord = textutils.unserialize(cCoord)
  6.  
  7. function cS()
  8.   term.clear()
  9.   term.setCursorPos(1,1)
  10. end
  11.  
  12. --Get Desired Coordinates
  13. local flag
  14. local nCoord = {x, y, z, dir}
  15. repeat
  16.   print("Enter X Coords: ")
  17.   nCoord.x = io.read()
  18.   print("")
  19.   print("Enter Z Coords: ")
  20.   nCoord.z = io.read()
  21.   print("")
  22.   print("What Direction Is Turtle Facing?: ")
  23.   nCoord.dir = io.read()
  24.   print("")
  25.   print("Are These Correct? Y/N: ")
  26.   flag = io.read()
  27.   string.lower(flag)
  28.   cS()
  29. until flag == "y"
  30.  
  31. --Compare coords
  32. local pCoord = {x, z}
  33. pCoord.x = cCoord.x - nCoord.x
  34. pCoord.z = cCoord.z - nCoord.z
  35.  
  36. --Travel Y
  37. for i = 1,30 do
  38.   turtle.up()
  39. end
  40. cCoord.y = cCoord.y + 30
  41.  
  42. --Travel X
  43. if nCoord.dir == "east" then
  44.   if pCoord.x < 0 then
  45.     pCoord.x = pCoord.x * -1
  46.     for i = 1,pCoord.x do
  47.       turtle.forward()
  48.     end
  49.   else
  50.     for i = 1,pCoord.x do
  51.       turtle.back()
  52.     end
  53.   end
  54. elseif nCoord.dir == "south" then
  55.   if pCoord.x < 0 then
  56.     pCoord.x = pCoord.x * -1
  57.     turtle.turnLeft()
  58.     for i = 1,pCoord.x do
  59.       turtle.forward()
  60.     end
  61.     turtle.turnRight()
  62.   else
  63.     turtle.turnRight()
  64.     for i = 1,pCoord.x do
  65.       turtle.forward()
  66.     end
  67.     turtle.turnLeft()
  68.   end
  69. elseif nCoord.dir == "west" then
  70.   if pCoord.x < 0 then
  71.     pCoord.x = pCoord.x * -1
  72.     for i = 1,pCoord.x do
  73.       turtle.back()
  74.     end
  75.   else
  76.     for i = 1,pCoord.x do
  77.       turtle.forward()
  78.     end
  79.   end
  80. elseif nCoord.dir == "north" then
  81.   if pCoord.x < 0 then
  82.     pCoord.x = pCoord.x * -1
  83.     turtle.turnRight()
  84.     for i = 1,pCoord.x do
  85.       turtle.forward()
  86.     end
  87.     turtle.turnLeft()
  88.   else
  89.     turtle.turnLeft()
  90.     for i = 1,pCoord.x do
  91.       turtle.forward()
  92.     end
  93.   end
  94. end
  95.  
  96. --Travel Z
  97.   if nCoord.dir == "east" then
  98.     if pCoord.z < 0 then
  99.       pCoord.z = pCoord.z * -1
  100.       turtle.turnRight()
  101.       for i = 1,pCoord.z do
  102.         turtle.forward()
  103.       end
  104.       turtle.turnLeft()
  105.     else
  106.       turtle.turnLeft()
  107.       for i = 1,pCoord.z do
  108.         turtle.forward()
  109.       end
  110.       turtle.turnRight()
  111.     end
  112.   elseif nCoord.dir == "south" then
  113.     if pCoord.z < 0 then
  114.       pCoord.z = pCoord.z * -1
  115.       for i = 1,pCoord.z do
  116.         turtle.forward()
  117.       end
  118.     else
  119.       for i = 1,pCoord.z do
  120.         turtle.back()
  121.       end
  122.     end
  123.   elseif nCoord.dir == "west" then
  124.     if pCoord.z < 0 then
  125.       pCoord.z = pCoord.z * -1
  126.       turtle.turnLeft()
  127.       for i = 1,pCoord.z do
  128.         turtle.forward()
  129.       end
  130.       turtle.turnRight()
  131.     else
  132.       turtle.turnRight()
  133.       for i = 1,pCoord.z do
  134.         turtle.forward()
  135.       end
  136.       turtle.turnLeft()
  137.     end
  138.   elseif nCoord.dir == "north" then
  139.     if pCoord.z < 0 then
  140.       pCoord.z = pCoord.z * -1
  141.       for i = 1,pCoord.z do
  142.         turtle.back()
  143.       end
  144.     else
  145.       for i = 1,pCoord.z do
  146.         turtle.forward()
  147.       end
  148.     end
  149.   end
  150.  
  151. --Down Y
  152. local yFlag = 0
  153. repeat
  154.   turtle.down()
  155.   yFlag = yFlag + 1
  156. until turtle.detectDown()
  157. cCoord.y = cCoord.y - yFlag
  158.  
  159. --Save New Coords
  160. cCoord.x = nCoord.x
  161. cCoord.z = nCoord.z
  162. fs.delete("coord")
  163. local file = fs.open("coord", "w")
  164. file.write(textutils.serialize(cCoord))
  165. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement