Guest User

Minecraft ComputerCraft Turtle follow blocks

a guest
Sep 7th, 2012
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. -- License: Authors Right to Brag
  2. -- You may use this source without restrictions with the exception of boasting.
  3. -- Only Micha may brag about this.
  4.  
  5. print ("follow v0.1")
  6. print ("Follows a path of whatever the turtle has got in his inventory")
  7. print ("(I recommend using train tracks)")
  8. print ("All Bragging Rights Reserved - Micha 2012")
  9. print ("")
  10.  
  11. local state = "scan"
  12.  
  13. function innerscan()
  14.     local somethingDown = false
  15.     local somethingForward = false
  16.     local matchDown = false
  17.     local matchForward = false
  18.     if turtle.detectDown() then
  19.         somethingDown = true
  20.         for i = 1, 9, 1 do
  21.             turtle.select(i)
  22.             if turtle.compareDown() then
  23.                 matchDown = true
  24.             end
  25.         end
  26.     end
  27.     if turtle.detect() then
  28.         somethingForward = true
  29.         for i = 1, 9, 1 do
  30.             turtle.select(i)
  31.             if turtle.compare() then
  32.                 matchForward = true
  33.             end
  34.         end
  35.     end
  36.     return somethingDown, somethingForward, matchDown, matchForward
  37. end
  38.  
  39. function upscan()
  40.     local somethingUp = false
  41.     local matchUp = false
  42.     if turtle.detectUp() then
  43.         somethingUp = true
  44.         for i = 1, 9, 1 do
  45.             turtle.select(i)
  46.             if turtle.compareUp() then
  47.                 matchUp = true
  48.             end
  49.         end
  50.     end
  51.     return somethingUp, matchUp
  52. end
  53.  
  54.  
  55. function scan()
  56.     local somethingDown, somethingForward, matchDown, matchForward = innerscan()
  57.     if not somethingDown then
  58.         state = "toGround"
  59.    
  60.     else
  61.         if matchDown then
  62.             state = "forwardAndScan"
  63.         else
  64.             if somethingForward then
  65.                 if matchForward then
  66.                 -- train track up?
  67.                     state = "riseAndScan"
  68.                 else
  69.                     turtle.turnLeft()
  70.                 end
  71.             else
  72.                 turtle.turnLeft()
  73.             end
  74.         end
  75.     end
  76.    
  77. end
  78.  
  79. function moveDown()
  80.     local somethingDown, somethingForward, matchDown, matchForward = innerscan()
  81.     if not somethingDown then
  82.         turtle.down()
  83.         return true
  84.     else
  85.         return false
  86.     end
  87. end
  88.  
  89. function moveUp()
  90.     local somethingUp, matchUp = upscan()
  91.     if not somethingUp then
  92.         turtle.up()
  93.         return true
  94.     else
  95.         return false
  96.     end
  97. end
  98.  
  99.  
  100. function moveForward()
  101.     local somethingDown, somethingForward, matchDown, matchForward = innerscan()
  102.     if not somethingForward then
  103.         turtle.forward()
  104.         return true
  105.     else
  106.         return false
  107.     end
  108. end
  109.  
  110. function moveLeft()
  111.     turtle.turnLeft()
  112.     return moveForward()
  113. end
  114.  
  115. function moveRight()
  116.     turtle.turnRight()
  117.     return moveForward()
  118. end
  119.  
  120. function moveBackTrack()
  121.     turtle.turnRight()
  122.     turtle.turnRight()
  123.     moveForward()
  124.     turtle.turnLeft()
  125.     turtle.turnLeft()
  126. end
  127.  
  128. local lastSide="left"
  129.  
  130. function run()
  131.     if state == "toGround" then
  132.         if not moveDown() then
  133.             state = "scan"
  134.         end
  135.     elseif state == "backTrack" then
  136.         state = "scanLeft-Right"
  137.     elseif state == "scanLeft-Right" then
  138.         if lastSide == "right" then
  139.             lastSide="left"
  140.             if moveLeft() then
  141.                 local somethingDown, somethingForward, matchDown, matchForward = innerscan()
  142.                 if matchDown then
  143.                     -- right path!
  144.                     state = "forwardAndScan"
  145.                 else
  146.                     -- wrong path, double back
  147.  
  148.                     moveBackTrack()
  149.                     turtle.turnRight()
  150.                 end
  151.             else
  152.                 turtle.turnRight()
  153.             end
  154.         else
  155.             lastSide="right"
  156.             if moveRight() then
  157.                 local somethingDown, somethingForward, matchDown, matchForward = innerscan()
  158.                 if matchDown then
  159.                     -- right path!
  160.                     state = "forwardAndScan"
  161.                 else
  162.                     moveBackTrack()
  163.                     turtle.turnLeft()
  164.                 end
  165.             else
  166.                 turtle.turnLeft()
  167.             end
  168.         end
  169.  
  170.  
  171.     elseif state == "riseAndScan" then
  172.         if moveUp() then
  173.             if moveForward() then
  174.                 local somethingDown, somethingForward, matchDown, matchForward = innerscan()
  175.                
  176.                 if matchDown then
  177.                     -- one step up was a good assumption!
  178.                     state = "forwardAndScan"
  179.                 else
  180.                     state = "scanLeft-Right"
  181.                 end
  182.             else
  183.                 state = "scanLeft-Right"
  184.             end
  185.         end
  186.     elseif state == "forwardAndScan" then
  187.         if moveForward() then
  188.             local somethingDown, somethingForward, matchDown, matchForward = innerscan()
  189.             if matchDown then
  190.                 -- right path, continue
  191.                 state = "forwardAndScan"
  192.             else
  193.                 -- wrong path, is left or right
  194.                 moveBackTrack()
  195.                 state = "scanLeft-Right"
  196.             end
  197.         else
  198.             state = "scanLeft-Right"
  199.         end
  200.     elseif state == "giveUp" then
  201.         print("Giving up")
  202.         return false
  203.     elseif state == "scan" then
  204.         scan()
  205.     elseif state == "followLoop" then
  206.          
  207.     else
  208.         state = "scan"
  209.     end
  210.     return true
  211. end
  212.  
  213.  
  214. while run() do
  215.     -- nothing
  216.     print("state: " .. state)
  217. end
  218.  
  219. print("normal end")
Advertisement
Add Comment
Please, Sign In to add comment