Advertisement
Renjestoo

Pathfinding for turtles

Jan 30th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.48 KB | None | 0 0
  1. -- Variables
  2. xPos, yPos, zPos = nil
  3. XLocation, YLocation, ZLocation = nil
  4. hx = homeX
  5. hy = homeY
  6. hz = homeZ
  7. face = 1
  8. side = nil
  9. cal = false
  10. -- end Variables
  11.  
  12. --locations
  13. function homeXLoc()
  14. local Xhome = fs.open("homeX", "w")
  15. Xhome.writeLine(XLocation)
  16. Xhome.close()
  17. end
  18.  
  19. function homeYLoc()
  20. local Yhome = fs.open("homeY", "w")
  21. Yhome.writeLine(YLocation)
  22. Yhome.close()
  23. end
  24.  
  25. function homeZLoc()
  26. local Zhome = fs.open("homeZ", "w")
  27. Zhome.writeLine(ZLocation)
  28. Zhome.close()
  29. end
  30.  
  31. function readXLoc()
  32. XLoc = fs.open("homeX", "r")
  33. homeX = tonumber (XLoc.readLine())
  34. XLoc.close()
  35. end
  36.  
  37. function readYLoc()
  38. YLoc = fs.open("homeY", "r")
  39. homeY = tonumber(YLoc.readLine())
  40. YLoc.close()
  41. end
  42.  
  43. function readZLoc()
  44. ZLoc = fs.open("homeZ", "r")
  45. homeZ = tonumber(ZLoc.readLine())
  46. ZLoc.close()
  47. end
  48. --end Locations
  49.  
  50. --functions
  51. function setLocation() -- get gps using other computers
  52.  xPos, yPos, zPos = gps.locate()
  53.  cal = true
  54. end
  55.  
  56. function setHome()
  57.     setLocation()
  58.     XLocation = xPos
  59.     YLocation = yPos
  60.     ZLocation = zPos
  61.     homeXLoc()
  62.     homeYLoc()
  63.     homeZLoc()
  64. end
  65.  
  66. function getSide()
  67.     if face == 1 then
  68.         side = "West"
  69.     elseif face == 2 then
  70.         side = "South"
  71.     elseif face == 3 then
  72.         side = "East"
  73.     elseif face == 0 then
  74.         side = "North"
  75.     end
  76. end
  77.  
  78. function getLocation() -- return the location
  79.  if xPos ~= nil then
  80.   print(xPos, yPos, zPos)
  81.  else
  82.   return nil
  83.  end
  84. end
  85.  
  86. function turnLeft() -- turn left
  87. turtle.turnLeft()
  88.  if face == 0 then
  89.   face = 1
  90.  elseif face == 1 then
  91.   face = 2
  92.  elseif face == 2 then
  93.   face = 3
  94.  elseif face == 3 then
  95.   face = 0
  96.  end
  97. end
  98.  
  99. function turnRight() -- turn right
  100. turtle.turnRight()
  101.  if face == 0 then
  102.   face = 3
  103.  elseif face == 1 then
  104.   face = 0
  105.  elseif face == 2 then
  106.   face = 1
  107.  elseif face == 3 then
  108.   face = 2
  109.  end
  110. end
  111.  
  112. function forward() -- go forward
  113.  turtle.forward()
  114.  if cal == true then
  115.   if face == 0 then
  116.    zPos = zPos - 1
  117.   elseif face == 1 then
  118.    xPos = xPos - 1
  119.   elseif face == 2 then
  120.    zPos = zPos + 1
  121.   elseif face == 3 then
  122.    xPos = xPos + 1
  123.   end
  124.  else
  125.   print("Not Calibrated.")
  126.  end
  127. end
  128.  
  129. function back() -- go back
  130.  turtle.back()
  131.  if cal == true then
  132.   if face == 0 then
  133.    zPos = zPos + 1
  134.   elseif face == 1 then
  135.    xPos = xPos + 1
  136.   elseif face == 2 then
  137.    zPos = zPos - 1
  138.   elseif face == 2 then
  139.    xPos = xPos - 1
  140.   end
  141.  else
  142.   print("Not Calibrated.")
  143.  end
  144. end
  145.  
  146. function up() -- go up
  147.  turtle.up()
  148.  if cal == true then
  149.   yPos = yPos + 1
  150.  else
  151.   print("Not Calibrated.")
  152.  end
  153. end
  154.  
  155. function down() -- go down
  156. turtle.down()
  157.     if cal == true then
  158.         yPos = yPos - 1
  159.     else
  160.         print("Not Calibrated.")
  161.     end
  162. end
  163.  
  164. function sort()
  165.     for i = 1,16 do
  166.         turtle.select(i)
  167.         turtle.suck()
  168.     end
  169.     for i = 1,16 do
  170.         turtle.select(i)
  171.         if turtle.getItemCount() == 0 then
  172.             break
  173.         else
  174.             table.insert(items, i, turtle.getItemDetail().name)
  175.             table.insert(amount, i, turtle.getItemCount())
  176.         end    
  177.     end
  178.  
  179.     for k,v in pairs(items) do
  180.         print(v)
  181.     end
  182. end
  183.  
  184. function goHome()
  185. --setLocation()
  186. --getLocation()
  187. readXLoc()
  188. readYLoc()
  189. readZLoc()
  190.     while zPos ~= homeZ do
  191.         if zPos < homeZ then
  192.             if side ~= "South" then
  193.                 repeat
  194.                 turnRight()
  195.                 getSide()
  196.                 until side == "South"
  197.             end
  198.             repeat forward() until zPos == homeZ
  199.             turnRight()
  200.         else
  201.             if side ~= "North" then
  202.                 repeat
  203.                 turnLeft()
  204.                 getSide()
  205.                 until side == "North"
  206.             end
  207.             repeat forward() until zPos == homeZ
  208.             turnLeft()
  209.         end
  210.     end
  211.     while xPos ~= homeX do
  212.         if xPos > homeX then
  213.             repeat forward() until xPos == homeX
  214.         else
  215.             repeat back() until xPos == homeX
  216.         end
  217.     end
  218.     while yPos ~= homeY do
  219.         if yPos < homeY then
  220.             repeat up() until yPos == homeY
  221.         else
  222.             repeat down() until yPos == homeY
  223.         end
  224.     end
  225. end
  226.  
  227. function errorMes()
  228.         print("Don't understand the command.")
  229.         print("Press enter to continue")
  230.             if io.read() == "" then
  231.                 term.clear()
  232.                 term.setCursorPos(1,1)
  233.                 write("Enter the Command: ")
  234.             end
  235. end
  236.  
  237. function help()
  238. print("The following commands can be used: ")
  239. print("- sethome or home")
  240. print("- sort")
  241. print("- go home")
  242. print("- help OR ?")
  243. print("- exit or quit")
  244. print("")
  245. end
  246. -- end functions
  247. setLocation()
  248. term.clear()
  249. term.setCursorPos(1,1)
  250. write("Enter the Command: ")
  251. while true do
  252. local input = read()
  253.     if input:lower() == "set home" or input:lower() == "sethome" or input:lower() == "home" then
  254.         setHome()
  255.         print("Home is Set")
  256.         print("Press enter to continue")
  257.             if io.read() == "" then
  258.                 term.clear()
  259.                 term.setCursorPos(1,1)
  260.                 write("Enter the Command: ")
  261.             end
  262.     elseif input:lower() == "help" or input:lower() == "?" then
  263.         help()
  264.         print("Press enter to continue")
  265.             if io.read() == "" then
  266.                 term.clear()
  267.                 term.setCursorPos(1,1)
  268.                 write("Enter the Command: ")
  269.             end
  270.     elseif input:lower() == "exit" or input:lower() == "quit" then
  271.         os.reboot()
  272.     elseif input:lower() == "go home" then
  273.         while xPos ~= homeX do
  274.             repeat
  275.             term.clear()
  276.             term.setCursorPos(1,1)
  277.             print("Turtle Location: ")
  278.             print(xPos..", "..yPos..", "..zPos)
  279.             term.setCursorPos(1,3)
  280.             print("Home Location: ")
  281.             print(hx..", "..hy..", "..hz)
  282.             until xPos == homeX and yPos == homeY and zPos == homeZ
  283.         end
  284.         goHome()
  285.         print("Press enter to continue")
  286.         if io.read() == "" then
  287.             term.clear()
  288.             term.setCursorPos(1,1)
  289.             write("Enter the Command: ")
  290.         end
  291.     else
  292.         errorMes()
  293.     end
  294. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement