Advertisement
Knito

treefarm

Oct 1st, 2019
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.43 KB | None | 0 0
  1. -- https://pastebin.com/QXEhJ0h8
  2. -- pastebin get QXEhJ0h8 treefarm
  3.  
  4. -- tree farm for straight trees up to 7 blocks high
  5. -- written with MC 1.14.4 Forge 28.1.26-74,  CCTweaked 1.84.1
  6. -- 2019 Oct 1 by Konitor
  7.  
  8. -- Updates:
  9. -- 2019-11-01: Check for chest on top when restarting and get that
  10. -- things back.
  11.  
  12. -- refueling strategy taken from aTreeFarm by Kaikaku
  13. -- http://pastebin.com/JgRgRK5q
  14.  
  15.  
  16. -- setup:
  17. -- * rectangle of cobble ( 1 block high, approx 11*15 ) to catch turtle
  18. -- walkway for turtle on inside must be non-plantable eg cobble
  19. -- because it would plant a sapling in the corner and get stuck
  20. -- oak tree farm: put a ceiling for the trees at height 8
  21. -- to prevent oak trees from growing weird
  22. -- put one chest somewhere under the turtle's walkway where
  23. -- the turtle can unload the harvested stuff when it walks over it
  24. -- turtle needs to be felling AND crafting
  25.  
  26. -- slot 1 = saplings (a lot of them)
  27. -- slot 2 = blocks from that trees (to have one stack for sure when crafting)
  28. -- slot 16 = 1 chest (for putting things away when crafting planks)
  29.  
  30. -- first time start helper:
  31. -- give it a stack of logs in slot 2 for fueling
  32. -- help the turtle setup the ring of saplings and bonemeal some of them
  33.  
  34. -- bugs:
  35. -- May run out of saplings if leaves give nothing
  36.  
  37. -- behaviour:
  38. -- turtle walks along the cobble wall and places saplings on the inside
  39. -- turtle tries to refuel from inventory when at bottom
  40. -- turtle tries to get new saplings by digging a lot of leaves
  41. -- max working height = 7 blocks
  42. -- self sustain: no new setup needed after chunk unload (eg visit the nether)
  43. -- switch on/off: put a lever on the outside of any wall block of the farm.
  44. -- turtle stops at that block when the lever is "on"
  45.  
  46. -- global variable expectedBlock for movement direction
  47. -- good values: "stone" (counter) or "ite" (clockwise)
  48. -- this is the block from which the wall for the walkway is made
  49.  
  50. expectedBlock = "stone"
  51.  
  52. function GoUp()
  53.  
  54. -- get the tree, go straight up
  55. -- clears the situation where the bottom has a sapling and above it
  56. -- are logs. The sapling could never grow up if we checked for it.
  57. -- trees over saplings or even air happen rather often in tree farms.
  58. print( "Going up")
  59. for i=1, 7 do
  60.   if Mineable() then
  61.     turtle.dig()
  62.   end
  63.   if i < 7 then
  64.     turtle.digUp()
  65.   end
  66.   turtle.up()
  67. end
  68.  
  69. end
  70.  
  71. function askRedstone()
  72.  
  73. local signal = false
  74.  
  75. -- prevent "Too long without yielding error" by sleeping
  76. sleep( 0.5 )
  77.  
  78. if signal == false then signal = rs.getInput("top") end
  79. if signal == false then signal = rs.getInput("front") end
  80. if signal == false then signal = rs.getInput("left") end
  81. if signal == false then signal = rs.getInput("right") end
  82. if signal == false then signal = rs.getInput("back") end
  83. if signal == false then signal = rs.getInput("bottom") end
  84.  
  85. return signal
  86. end
  87.  
  88. function StopOnRedstone()
  89. local signal = false
  90. local saidstop = false
  91.  
  92. signal = askRedstone()
  93.  
  94. -- stop on redstone
  95. while signal == true do
  96.  
  97.     if not saidstop then
  98.         print("Stopped because of Redstone Signal")
  99.         saidstop = true
  100.     end
  101.  
  102.     signal = askRedstone()
  103.    
  104. end
  105.    
  106. end
  107.  
  108.  
  109. function Walk()
  110.  
  111.     StopOnRedstone()
  112.  
  113.     -- walk counterclock wise with anything that contains "stone"
  114.     -- stone, cobblestone, stonebrick
  115.     -- walks around left corners "expectedBlock" is global
  116.     if expectedBlock == "stone" then
  117.    
  118.         local count = 0
  119.         while Check("stone") == false and count < 4 do
  120.             count = count + 1
  121.             turtle.turnRight()
  122.         end
  123.         if count < 4 then
  124.             while Check("stone") == true do
  125.                 turtle.turnLeft()
  126.             end
  127.         else
  128.             print ("changed expected wall block to *ite*")     
  129.             expectedBlock = "ite"
  130.             return
  131.         end
  132.        
  133.     else
  134.    
  135.         local count = 0
  136.         while Check("ite") == false and count < 4 do
  137.             count = count + 1
  138.             turtle.turnLeft()
  139.         end
  140.         if count < 4 then
  141.             while Check("ite") == true do
  142.                 turtle.turnRight()
  143.             end
  144.         else
  145.             print ("changed expected wall block to *stone*")
  146.             expectedBlock = "stone"
  147.             return
  148.         end
  149.    
  150.     end
  151.    
  152.    
  153.     print("Walking")
  154.     turtle.select(1) -- sapling slot
  155.     turtle.forward()
  156.  
  157.     StopOnRedstone()
  158.  
  159. end
  160.  
  161. function stringhas( s, pattern )
  162. local found = false
  163.     local anfang, ende = string.find( s, pattern, 1, true )
  164.     if  ( anfang ~= nil ) then
  165.         found = true
  166.     end
  167. return found
  168. end
  169.  
  170. function Mineable()
  171. local found = false
  172. local detect, what = turtle.inspect()
  173. if detect then
  174.     if stringhas( what.name, "_leaves" ) then
  175.         found = true
  176.     end
  177.     if stringhas( what.name, "_log" ) then
  178.         found = true
  179.     end
  180. end
  181.  
  182. return found
  183. end
  184.  
  185. function Check( pattern )
  186. local found = false
  187. local detect, what = turtle.inspect()
  188. if detect then
  189.     if stringhas( what.name, pattern ) then
  190.         found = true
  191.     end
  192. end
  193. return found
  194. end
  195.  
  196. function CheckUp( pattern )
  197. local found = false
  198. local detect, what = turtle.inspectUp()
  199. if detect then
  200.     if stringhas( what.name, pattern ) then
  201.         found = true
  202.     end
  203. end
  204. return found
  205. end
  206.  
  207.  
  208.  
  209. -- FuelCheck() is a function to be able to
  210. -- stop execution with a return when no fuel is needed.
  211.  
  212. function FuelCheck( maxlevel )
  213.  
  214. local fl = turtle.getFuelLevel()
  215. local oldfl = fl
  216. local stop = false
  217.  
  218. if fl == "unlimited" then return end
  219.  
  220. print( "Checking Fuellevel")
  221. if turtle.getItemCount(2) < 3 then
  222.     print("Not enough logs in slot 2. Continue with "..fl.." fuel.")
  223.     return
  224. end
  225.  
  226. if fl < maxlevel then
  227. print( "trying to refuel. Fuellevel = ".. fl.. "/"..maxlevel )
  228.  
  229. repeat
  230.  sleep( 0.1 )
  231.  stop = false
  232.  -- new
  233.  if turtle.getItemCount(2) > 1 then
  234.  turtle.select(16)
  235.  while not turtle.placeUp() do turtle.digUp() end
  236.  for i=1, 16 do
  237.     turtle.select(i)
  238.     if i ~= 2 then
  239.         turtle.dropUp()
  240.     else
  241.         turtle.dropUp( turtle.getItemCount(2) - 2 )
  242.     end
  243.  end
  244.  
  245.  turtle.craft()
  246.  
  247.  -- we don't know where the 8 planks are now
  248.  -- so we look in every slot and refuel max
  249.  for i=1, 16 do
  250.     turtle.select(i)
  251.     turtle.refuel()
  252.     sleep(0.1)
  253.  end
  254.  
  255.  -- sleep() hopefully updates the inventory and helps
  256.  -- against the deleting bug
  257.  
  258.  turtle.select(1)
  259.  -- get everything back, hopefully saplings to slot 1
  260.  while turtle.suckUp() do sleep(0.1) end
  261.  -- put the chest back to slot 16
  262.  turtle.select(16)
  263.  turtle.digUp()
  264.  sleep( 0.1 )
  265.  -- select sapling slot
  266.  turtle.select(1)
  267.  
  268.  if turtle.getItemCount(2) < 3 then stop = true end
  269.  
  270.  fl = turtle.getFuelLevel()
  271.  end
  272.  
  273.  
  274.  -- assuming there is not enough logs in slot 2 atm
  275.  
  276.  if oldfl == fl then stop = true end
  277.  
  278.  -- just enough fuel
  279.  
  280.  if fl > maxlevel then stop = true end
  281.  
  282.  -- last check "ran out of fuel?"
  283.  -- 1 step = 6 up + 6 down + 1 walk and that amount of fuel
  284.  if fl < 13 then
  285.     -- stay in the loop until someone brought fuel
  286.     -- i cannot move another full step anyways
  287.     stop = false
  288.     print("FUEL NEEDED!")
  289.  end
  290.  print( " fl="..fl )
  291.  
  292. until( stop )
  293.  
  294. end
  295. print( "continuing with Fuellevel = "..fl.."/"..maxlevel)
  296.  
  297.  
  298. end
  299.  
  300. function GoDown()
  301.  
  302. local detect = false
  303. local what = ""
  304. local iamdown = false
  305. local chest = false
  306.  
  307. -- going down to surface
  308. -- leaves are ok and get digged
  309.  
  310. print( "Going down" )
  311. turtle.select(1)
  312.  
  313. while iamdown == false do
  314.  
  315.   for i = 1, 4 do
  316.     turtle.turnLeft()
  317.     if Mineable() then
  318.         turtle.dig()
  319.     end
  320.    
  321.     -- try to get extra saplings
  322.     turtle.suck()
  323.     turtle.suckDown()
  324.    
  325.   end
  326.  
  327.  detect, what = turtle.inspectDown()
  328.  
  329.  if detect then
  330.  
  331.      
  332.   print( "down block= ".. what.name )
  333.  
  334.   if( what.name == "minecraft:chest" ) then
  335.    chest = true
  336.    -- before unloading try to refuel a bit
  337.    FuelCheck( 1000 )
  338.    -- everything ok
  339.    iamdown = true
  340.    -- unload to chest
  341.    print( "unloading to chest")
  342.    -- no saplings, no templates, not the crafting chest in slot 16
  343.    for i = 3, 15 do
  344.     turtle.select(i)
  345.     turtle.dropDown()
  346.     sleep( 0.1 )
  347.    end
  348.    -- template slot 2
  349.    -- not everything, please
  350.    local c2 = turtle.getItemCount(2)
  351.    if( c2 > 1 ) then
  352.      turtle.select(2)
  353.      turtle.dropDown( c2 - 1 )
  354.    end
  355.    turtle.select(1)
  356.    
  357.   end
  358.  
  359.  
  360.  
  361.    if stringhas(what.name, "_leaves") then
  362.    
  363.     turtle.digDown()
  364.    
  365.    else
  366.    
  367.     iamdown = true
  368.    
  369.    end
  370.  
  371.   end
  372.  
  373.     turtle.down()  
  374.  
  375. end
  376. -- check for fuellevel
  377. if not chest then FuelCheck( 1000 ) end
  378. end
  379.  
  380.  
  381. -- Check to see if I'm a felling or mining turtle
  382. if type( turtle.dig ) ~= "function" then
  383.     print( "I need a pickaxe or an axe to be mining or felling turtle.")
  384.     error("Craft me together with a diamond tool!")
  385. end
  386.  
  387. -- Check to see if I'm alright and ready for crafting
  388.  
  389. bCrafting = (peripheral.getType("left") == "workbench") or (peripheral.getType("right") == "workbench")
  390. if not bCrafting then
  391.   error("I want to be a crafty turtle! Craft me together with a crafting table.")
  392. end
  393.  
  394. -- self sustain: a) label the turtle
  395.  
  396. if os.getComputerLabel() == nil then
  397.   os.setComputerLabel("Lumberjack")
  398. end
  399.  
  400. -- self sustain: b) make it autostart
  401. -- copy this program to startup when its not "startup"
  402. -- to make it resistant against chunk reloads (ie return from nether)
  403.  
  404. me = fs.getName( shell.getRunningProgram() )
  405.  
  406. if( me ~=  "startup") then
  407.     print( "Overwriting startup with "..me )
  408.     if( fs.exists("startup") ) then
  409.         fs.delete("startup")
  410.     end
  411.     fs.copy( me, "startup" )
  412. end
  413.  
  414.  
  415. -- self sustain: c) find the wall to get orientation
  416.  
  417. -- not too rare situation: player left while chest was on top (refueling)
  418. -- check for chest on top and if yes then get everything back
  419. -- using the 2nd half of the refueling function
  420.  
  421. if CheckUp( "chest" ) then
  422.  
  423.  turtle.select(1)
  424.  -- get everything back, hopefully saplings to slot 1
  425.  while turtle.suckUp() do sleep(0.1) end
  426.  -- put the chest back to slot 16
  427.  turtle.select(16)
  428.  turtle.digUp()
  429.  sleep( 0.1 )
  430.  -- select sapling slot
  431.  turtle.select(1)
  432.  
  433. end
  434.  
  435. GoDown()
  436.  
  437. -- main loop
  438. while true do
  439.  
  440. Walk()
  441. -- turnto the inside
  442. if expectedBlock == "stone" then
  443. turtle.turnLeft()
  444. else
  445. turtle.turnRight()
  446. end
  447.  
  448. GoUp()
  449. GoDown()
  450.  
  451. detect, what = turtle.inspect()
  452. if not detect then
  453.     turtle.select(1)
  454.     -- don't run out of samples!!!
  455.     if turtle.getItemCount(1) > 1 then
  456.         if turtle.place() then
  457.             print("planted a sapling")
  458.         else
  459.             -- no dirt after a corner!!
  460.             -- we want to see this message
  461.             -- else it gets stuck in the corner
  462.             print("failed to plant a sapling")
  463.         end
  464.     end
  465. end
  466. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement