Advertisement
Inksaver

Computercraft Recursive Tree Harvesting

Mar 5th, 2015
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.77 KB | None | 0 0
  1. -- Example of recursion to dig branched or large trees
  2. -- ensure there is a log of the tree  type in slot 1
  3.  
  4. function isTree(direction)
  5.     result = false
  6.    
  7.     turtle.select(1) --select slot 1 which contains wood
  8.     if direction == "up" then -- check block above
  9.         if turtle.compareUp() then -- wood block above
  10.             result = true
  11.         end
  12.     elseif direction == "down" then -- check block down
  13.         if turtle.compareDown() then -- wood block below
  14.             result = true
  15.         end
  16.     elseif direction == "forward" then -- check block ahead
  17.         if turtle.compare() then -- wood block ahead
  18.             result = true
  19.         end
  20.     end
  21.    
  22.     return result -- true/false
  23. end
  24.  
  25. function mineTree(direction)   
  26.     --RECURSIVE FUNCTION - BEWARE!
  27.  
  28.     -- check if block in front is valuable. If so dig it
  29.     -- direction only set to up/down if already called recursively
  30.     if direction == "up" then
  31.         if isTree("up") then
  32.             turtle.digUp()
  33.         end
  34.         -- move up into space dug out
  35.         turtle.up()
  36.         -- check if item in front is valuable
  37.         if isTree("forward") then
  38.             mineTree("forward")
  39.         end
  40.         --check right side
  41.         turtle.turnRight()
  42.         if isTree("forward") then
  43.             mineTree("forward")
  44.         end
  45.         --check behind
  46.         turtle.turnRight()
  47.         if isTree("forward") then
  48.             mineTree("forward")
  49.         end
  50.         --check left side
  51.         turtle.turnRight()
  52.         if isTree("forward") then
  53.             mineTree("forward")
  54.         end
  55.         --return to front
  56.         turtle.turnRight()
  57.         -- end of recursion so return to previous position
  58.         turtle.down()
  59.     end
  60.     if direction == "down" then
  61.         if isTree("down") then
  62.             turtle.digDown()
  63.         end
  64.         -- move down into space dug out
  65.         turtle.down()
  66.         -- check if item in front is valuable
  67.         if isTree("forward") then
  68.             mineTree("forward")
  69.         end
  70.         --check right side
  71.         turtle.turnRight()
  72.         if isTree("forward") then
  73.             mineTree("forward")
  74.         end
  75.         --check behind
  76.         turtle.turnRight()
  77.         if isTree("forward") then
  78.             mineTree("forward")
  79.         end
  80.         --check left side
  81.         turtle.turnRight()
  82.         if isTree("forward") then
  83.             mineTree("forward")
  84.         end
  85.         --return to front
  86.         turtle.turnRight()
  87.         -- end of recursion so return to previous position
  88.         turtle.up()
  89.     end
  90.     if direction == "forward" then
  91.         if isTree("forward") then
  92.             turtle.dig()
  93.             turtle.forward()
  94.             if isTree("up") then
  95.                 mineTree("up")
  96.             end
  97.             if isTree("down") then
  98.                 mineTree("down")
  99.             end
  100.             -- check if item in front is valuable
  101.             if isTree("forward") then
  102.                 mineTree("forward")
  103.             end
  104.             --check left side
  105.             turtle.turnLeft()
  106.             if isTree("forward") then
  107.                 mineTree("forward")
  108.             end
  109.             -- check right side
  110.             turtle.turnRight()
  111.             turtle.turnRight()
  112.             if isTree("forward") then
  113.                 mineTree("forward")
  114.             end
  115.             turtle.turnLeft()
  116.             -- end of recursion so return to previous position
  117.             turtle.back()
  118.         end
  119.     end
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement