Advertisement
toxicfrazzles

Turtle Tree Farm

Feb 11th, 2021 (edited)
1,639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.17 KB | None | 0 0
  1. -----------------------------------------------------------------------------------
  2. -----------------------------------------------------------------------------------
  3. -- Computercraft Turtle Tree Farm
  4. -----------------------------------------------------------------------------------
  5. -- Change the item IDs for the sapling, marker and storage block before use
  6. -- Also can configure the minimum fuel level
  7. --
  8. -- Place at least 2 saplings in the first slot and
  9. -- some of the desired fuel to use in the last slot
  10. --
  11. -- Build a ring of the marker block around the tree farm to keep
  12. -- the turtle from flying away
  13. -----------------------------------------------------------------------------------
  14. -----------------------------------------------------------------------------------
  15.  
  16. local sapling_name = "minecraft:spruce_sapling"
  17. local marker_name = "minecraft:cobblestone_wall"
  18. local storage_name = "minecraft:chest"
  19. local min_fuel_level = 500
  20.  
  21. -----------------------------------------------------------------------------------
  22. -----------------------------------------------------------------------------------
  23. -- Code
  24. -- ONLY EDIT BEYOND THIS POINT IF YOU ARE CONFIDENT PROGRAMMER
  25. -----------------------------------------------------------------------------------
  26. -----------------------------------------------------------------------------------
  27.  
  28. local sapling_slot = 1
  29. local fuel_slot = 16
  30.  
  31. -------------------
  32.  
  33. local last_turn = 0
  34.  
  35.  
  36. function isLeaf(blockName)
  37.     return string.sub(blockName, -6, -1) == "leaves"
  38. end
  39.  
  40. function isLog(blockName)
  41.     return string.sub(blockName, -3, -1) == "log"
  42. end
  43.  
  44. function findOperatingLevel()
  45.     print("Trying to find the base level")
  46.     turtle.select(sapling_slot)
  47.     while true do
  48.         local detected, data = turtle.inspectDown()
  49.         if detected and (isLog(data.name) or isLeaf(data.name)) then
  50.             print("Leaf or log is below. Going down.")
  51.             turtle.digDown()
  52.             turtle.down()
  53.         elseif detected and turtle.compareDown() then
  54.             print("Sapling is below. Must be there")
  55.             break
  56.         elseif detected and data.name == marker_name then
  57.             print("Marker is below. Must be there")
  58.             return
  59.         elseif detected then
  60.             print("Some sort of floor is below. Planting a sapling")
  61.             turtle.up()
  62.             if turtle.getItemCount() > 1 then
  63.                 turtle.placeDown()
  64.             end
  65.             break
  66.         else
  67.             print("Nothing below. Going down")
  68.             turtle.down()
  69.         end
  70.     end
  71. end
  72.  
  73. function findBoundary()
  74.     print("Trying to find the boundary")
  75.     findOperatingLevel()
  76.     turtle.select(sapling_slot)
  77.     while true do
  78.         local detected, data = turtle.inspectDown()
  79.         if detected and data.name == marker_name then
  80.             break
  81.         end
  82.         if not turtle.forward() then
  83.             turtle.dig()
  84.             turtle.forward()
  85.             turtle.digDown()
  86.             while turtle.detectUp() do
  87.                 turtle.digUp()
  88.                 turtle.up()
  89.             end
  90.             while turtle.down() do
  91.             end
  92.             turtle.up()
  93.             if turtle.getItemCount() > 1 then
  94.                 turtle.placeDown()
  95.             end
  96.         end
  97.     end
  98. end
  99.  
  100. function findStorage()
  101.     print("Trying to find the chest")
  102.     findBoundary()
  103.     while true do
  104.         local detected, data = turtle.inspect()
  105.         if detected and data.name == storage_name then
  106.             return
  107.         else
  108.             if not turtle.forward() then
  109.                 turtle.dig()
  110.                 turtle.forward()
  111.             end
  112.             local detected, data = turtle.inspectDown()
  113.             if detected and data.name == marker_name then
  114.                
  115.             else
  116.                 turtle.back()
  117.                 turtle.turnLeft()
  118.             end
  119.         end
  120.     end
  121. end
  122.  
  123. function restockRefuel()
  124.     print("Restock and Refuel")
  125.     findStorage()
  126.     turtle.select(sapling_slot)
  127.     turtle.suck(turtle.getItemSpace())
  128.     turtle.select(fuel_slot)
  129.     turtle.suck(turtle.getItemSpace())
  130.     for var=1,16 do
  131.         turtle.select(var)
  132.         if var == sapling_slot or var == fuel_slot then
  133.         else
  134.             if turtle.getItemCount(var) == 0 then
  135.             elseif not turtle.drop() then
  136.                 print("The storage is full. Stopping...")
  137.                 error()
  138.             end
  139.         end
  140.     end
  141.     if turtle.getFuelLevel() < min_fuel_level then
  142.         turtle.select(fuel_slot)
  143.         turtle.refuel(turtle.getItemCount()-1)
  144.     end
  145.    
  146.     turtle.back()
  147.     turtle.turnLeft()
  148. end
  149.  
  150. function chopTree()
  151.     turtle.dig()
  152.     turtle.forward()
  153.     turtle.digDown()
  154.     turtle.select(sapling_slot)
  155.     turtle.placeDown()
  156.     while turtle.detectUp() do
  157.         turtle.digUp()
  158.         turtle.up()
  159.     end
  160.     while turtle.down() do
  161.         turtle.select(sapling_slot)
  162.         local detected, data = turtle.inspectDown()
  163.         if detected and (isLeaf(data.name) or isLog(data.name)) then
  164.             turtle.digDown()
  165.         elseif detected and turtle.compareDown() then
  166.         else
  167.             turtle.up()
  168.             turtle.placeDown()
  169.             break
  170.         end
  171.     end
  172. end
  173.  
  174. while true do
  175.     print("Loop")
  176.     sleep(3)
  177.     if turtle.getItemCount(sapling_slot) == 0 or turtle.getItemCount(fuel_slot) == 0 then
  178.         print("Need fuel and saplings to work")
  179.         sleep(10)
  180.     end
  181.     if turtle.getItemCount(sapling_slot) == 1 or turtle.getFuelLevel() < min_fuel_level then
  182.         print(" Low on fuel" .. tostring(turtle.getFuelLevel()))
  183.         restockRefuel()
  184.     end
  185.    
  186.     if not turtle.forward() then
  187.         local detected, data = turtle.inspect()
  188.         if detected and isLeaf(data.name) then
  189.             turtle.dig()
  190.             turtle.forward()
  191.         elseif detected and isLog(data.name) then
  192.             chopTree()
  193.         end
  194.     end
  195.    
  196.     local detected, data = turtle.inspectDown()
  197.     if detected and data.name == marker_name then
  198.         print("On Marker")
  199.         local turn_func = nil
  200.         if last_turn == 0 then
  201.             turn_func = turtle.turnLeft
  202.             last_turn = 1
  203.         else
  204.             turn_func = turtle.turnRight
  205.             last_turn = 0
  206.         end
  207.         print("Picked turning direction")
  208.         while true do
  209.             local downDetected, downData = turtle.inspectDown()
  210.             if downDetected and downData.name == marker_name then
  211.                 turn_func()
  212.                 print("Turn")
  213.                
  214.                 if not turtle.forward() then
  215.                    
  216.                     local frontDetected, frontData = turtle.inspect()
  217.                     if frontDetected and isLeaf(frontData.name) then
  218.                         turtle.dig()
  219.                         turtle.forward()
  220.                         break
  221.                     elseif frontDetected and isLog(frontData.name) then
  222.                         chopTree()
  223.                         break
  224.                     end
  225.                 end
  226.             else
  227.                 print("Marker not below. Resuming operation")
  228.                 break
  229.             end
  230.         end
  231.     end
  232. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement