Advertisement
markman4897

PF - Path Finder

Apr 18th, 2017
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None
  1. -- PF program - Path Finder
  2. -- Version: 1.0.3
  3. -- by @markman4897
  4.  
  5. -- Requires MPR library.
  6.  
  7. -- TODO:
  8. -- - make it use the 3 while loops so it
  9. --   first checks for x ignoring z and y
  10. --   and so on (V2.0.0)
  11.  
  12.  
  13. -- =========
  14. -- Variables
  15. -- =========
  16.  
  17. -- Load MPR library
  18.  
  19. os.loadAPI("MPR")
  20.  
  21. -- Overwrite starting coords from MPR
  22.  
  23. MPR.set("x",109)
  24. MPR.set("z",1021)
  25. MPR.set("y",93)
  26.  
  27. MPR.set("dir",1)
  28.  
  29.  
  30. -- =========
  31. -- Functions
  32. -- =========
  33.  
  34. -- Move to coordinates function
  35.  
  36. function backTrack(dir)
  37.     print(dir," STAGE 4")
  38.     MPR.turn("left")
  39.     MPR.move("forw",1,false)
  40.     --verticalSolution = false
  41. end
  42.  
  43. function verticalCheck(dir)
  44.     verticalSolution = true
  45.     print("VERTICAL SOLUTION TRUE !!!")
  46.     print("VERTICAL SOLUTION TRUE !!!")
  47.     print("VERTICAL SOLUTION TRUE !!!")
  48.     print("VERTICAL SOLUTION TRUE !!!")
  49.     if not(MPR.move("up",1,false)) then
  50.         print(dir," STAGE 2.1")
  51.         if not(MPR.move("down",1,false)) then
  52.             print(dir," STAGE 2.2")
  53.             backTrack(dir)
  54.         end
  55.     end
  56. end
  57.  
  58. function horizontalCheck(dir)
  59.     verticalSolution = false
  60.     MPR.turn("left")
  61.  
  62.     if dir == "X" then
  63.         ignoreZ = true
  64.     else
  65.         ignoreX = true
  66.     end
  67.  
  68.     if not(MPR.move("forw",1,false)) then
  69.         print(dir," STAGE 3.1")
  70.         if not(MPR.move("back",1,false)) then
  71.             print(dir," STAGE 3.2")
  72.             backTrack(dir)
  73.         end
  74.     end
  75. end
  76.  
  77. function xzMove(dir)
  78.     if not(MPR.move("forw",1,false)) then
  79.         print(dir," STAGE 1")
  80.         if not verticalSolution then
  81.             verticalCheck(dir)
  82.         else
  83.             horizontalCheck(dir)
  84.         end
  85.     end
  86. end
  87.  
  88. function moveTo(x,z,y)
  89.     print("Going to ",x,",",z,",",y)
  90.  
  91.     ignoreX = false
  92.     ignoreZ = false
  93.     ignoreY = false
  94.  
  95.     verticalSolution = false
  96.  
  97.     while (x~=MPR.get("x") or z~=MPR.get("z") or y~=MPR.get("y")) do
  98.         moved = false
  99.  
  100.         if (x ~= MPR.get("x")) and not(ignoreX) then
  101.             print("in X X X X X X cycle")
  102.             ignoreZ = false
  103.             if x > MPR.get("x") then
  104.                 MPR.turn("east")
  105.             else
  106.                 MPR.turn("west")
  107.             end
  108.             xzMove("X")
  109.  
  110.         elseif (z ~= MPR.get("z")) and not(ignoreZ) then
  111.             print("in Z Z Z Z Z Z cycle")
  112.             ignoreX = false
  113.             if z > MPR.get("z") then
  114.                 MPR.turn("south")
  115.             else
  116.                 MPR.turn("north")
  117.             end
  118.             xzMove("Z")
  119.  
  120.         elseif (y ~= MPR.get("y")) and not(ignoreY) then
  121.             print("in Y Y Y Y Y Y cycle")
  122.             ignoreY = false
  123.             if y > MPR.get("y") then
  124.                 where = "up"
  125.                 whereNot = "down"
  126.             else
  127.                 where = "down"
  128.                 whereNot = "up"
  129.             end
  130.  
  131.             if not(move(where,1,false)) then
  132.                 print("STAGE 1")
  133.                 if not(MPR.move("forw",1,false)) then
  134.                     print("STAGE 2")
  135.                     if not(MPR.move("back",1,false)) then
  136.                         print("STAGE 3")
  137.                         MPR.turn("left")
  138.                         if (dir==1 or dir==3) then
  139.                             ignoreZ = true
  140.                         else
  141.                             ignoreX = true
  142.                         end
  143.                         if not(MPR.move("forw",1,false)) then
  144.                             print("STAGE 4")
  145.                             if not(MPR.move("back",1,false)) then
  146.                                 print("STAGE 5")
  147.                                 move(whereNot)
  148.                             end
  149.                         end
  150.                     end
  151.                 end
  152.             end
  153.  
  154.         end
  155.  
  156.     end
  157.  
  158.     print("Ended at ",MPR.get("x"),",",MPR.get("z"),",",MPR.get("y"))
  159.  
  160.     return true
  161.  
  162. end
  163.  
  164.  
  165. -- ============
  166. -- Main program
  167. -- ============
  168.  
  169. print("= Executing main program.")
  170. moveTo(121,1009,93) --the hard location
  171. --[[
  172. while true do
  173.     moveTo(119,1025,85)
  174.     print("DONE - DONE - DONE")
  175.     moveTo(99,1016,92)
  176.     print("DONE - DONE - DONE")
  177. end
  178. --]]
  179.  
  180. -- Extra EXTRA!
  181. -- try:
  182. -- turtle.native.forward()
  183. -- turtle.native.left()
  184. -- and see if it rotates while moving forward
  185. -- or does it just not rotate
Advertisement
RAW Paste Data Copied
Advertisement