Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- https://pastebin.com/QXEhJ0h8
- -- pastebin get QXEhJ0h8 treefarm
- -- tree farm for straight trees up to 7 blocks high
- -- written with MC 1.14.4 Forge 28.1.26-74, CCTweaked 1.84.1
- -- 2019 Oct 1 by Konitor
- -- Updates:
- -- 2019-11-01: Check for chest on top when restarting and get that
- -- things back.
- -- refueling strategy taken from aTreeFarm by Kaikaku
- -- http://pastebin.com/JgRgRK5q
- -- setup:
- -- * rectangle of cobble ( 1 block high, approx 11*15 ) to catch turtle
- -- walkway for turtle on inside must be non-plantable eg cobble
- -- because it would plant a sapling in the corner and get stuck
- -- oak tree farm: put a ceiling for the trees at height 8
- -- to prevent oak trees from growing weird
- -- put one chest somewhere under the turtle's walkway where
- -- the turtle can unload the harvested stuff when it walks over it
- -- turtle needs to be felling AND crafting
- -- slot 1 = saplings (a lot of them)
- -- slot 2 = blocks from that trees (to have one stack for sure when crafting)
- -- slot 16 = 1 chest (for putting things away when crafting planks)
- -- first time start helper:
- -- give it a stack of logs in slot 2 for fueling
- -- help the turtle setup the ring of saplings and bonemeal some of them
- -- bugs:
- -- May run out of saplings if leaves give nothing
- -- behaviour:
- -- turtle walks along the cobble wall and places saplings on the inside
- -- turtle tries to refuel from inventory when at bottom
- -- turtle tries to get new saplings by digging a lot of leaves
- -- max working height = 7 blocks
- -- self sustain: no new setup needed after chunk unload (eg visit the nether)
- -- switch on/off: put a lever on the outside of any wall block of the farm.
- -- turtle stops at that block when the lever is "on"
- -- global variable expectedBlock for movement direction
- -- good values: "stone" (counter) or "ite" (clockwise)
- -- this is the block from which the wall for the walkway is made
- expectedBlock = "stone"
- function GoUp()
- -- get the tree, go straight up
- -- clears the situation where the bottom has a sapling and above it
- -- are logs. The sapling could never grow up if we checked for it.
- -- trees over saplings or even air happen rather often in tree farms.
- print( "Going up")
- for i=1, 7 do
- if Mineable() then
- turtle.dig()
- end
- if i < 7 then
- turtle.digUp()
- end
- turtle.up()
- end
- end
- function askRedstone()
- local signal = false
- -- prevent "Too long without yielding error" by sleeping
- sleep( 0.5 )
- if signal == false then signal = rs.getInput("top") end
- if signal == false then signal = rs.getInput("front") end
- if signal == false then signal = rs.getInput("left") end
- if signal == false then signal = rs.getInput("right") end
- if signal == false then signal = rs.getInput("back") end
- if signal == false then signal = rs.getInput("bottom") end
- return signal
- end
- function StopOnRedstone()
- local signal = false
- local saidstop = false
- signal = askRedstone()
- -- stop on redstone
- while signal == true do
- if not saidstop then
- print("Stopped because of Redstone Signal")
- saidstop = true
- end
- signal = askRedstone()
- end
- end
- function Walk()
- StopOnRedstone()
- -- walk counterclock wise with anything that contains "stone"
- -- stone, cobblestone, stonebrick
- -- walks around left corners "expectedBlock" is global
- if expectedBlock == "stone" then
- local count = 0
- while Check("stone") == false and count < 4 do
- count = count + 1
- turtle.turnRight()
- end
- if count < 4 then
- while Check("stone") == true do
- turtle.turnLeft()
- end
- else
- print ("changed expected wall block to *ite*")
- expectedBlock = "ite"
- return
- end
- else
- local count = 0
- while Check("ite") == false and count < 4 do
- count = count + 1
- turtle.turnLeft()
- end
- if count < 4 then
- while Check("ite") == true do
- turtle.turnRight()
- end
- else
- print ("changed expected wall block to *stone*")
- expectedBlock = "stone"
- return
- end
- end
- print("Walking")
- turtle.select(1) -- sapling slot
- turtle.forward()
- StopOnRedstone()
- end
- function stringhas( s, pattern )
- local found = false
- local anfang, ende = string.find( s, pattern, 1, true )
- if ( anfang ~= nil ) then
- found = true
- end
- return found
- end
- function Mineable()
- local found = false
- local detect, what = turtle.inspect()
- if detect then
- if stringhas( what.name, "_leaves" ) then
- found = true
- end
- if stringhas( what.name, "_log" ) then
- found = true
- end
- end
- return found
- end
- function Check( pattern )
- local found = false
- local detect, what = turtle.inspect()
- if detect then
- if stringhas( what.name, pattern ) then
- found = true
- end
- end
- return found
- end
- function CheckUp( pattern )
- local found = false
- local detect, what = turtle.inspectUp()
- if detect then
- if stringhas( what.name, pattern ) then
- found = true
- end
- end
- return found
- end
- -- FuelCheck() is a function to be able to
- -- stop execution with a return when no fuel is needed.
- function FuelCheck( maxlevel )
- local fl = turtle.getFuelLevel()
- local oldfl = fl
- local stop = false
- if fl == "unlimited" then return end
- print( "Checking Fuellevel")
- if turtle.getItemCount(2) < 3 then
- print("Not enough logs in slot 2. Continue with "..fl.." fuel.")
- return
- end
- if fl < maxlevel then
- print( "trying to refuel. Fuellevel = ".. fl.. "/"..maxlevel )
- repeat
- sleep( 0.1 )
- stop = false
- -- new
- if turtle.getItemCount(2) > 1 then
- turtle.select(16)
- while not turtle.placeUp() do turtle.digUp() end
- for i=1, 16 do
- turtle.select(i)
- if i ~= 2 then
- turtle.dropUp()
- else
- turtle.dropUp( turtle.getItemCount(2) - 2 )
- end
- end
- turtle.craft()
- -- we don't know where the 8 planks are now
- -- so we look in every slot and refuel max
- for i=1, 16 do
- turtle.select(i)
- turtle.refuel()
- sleep(0.1)
- end
- -- sleep() hopefully updates the inventory and helps
- -- against the deleting bug
- turtle.select(1)
- -- get everything back, hopefully saplings to slot 1
- while turtle.suckUp() do sleep(0.1) end
- -- put the chest back to slot 16
- turtle.select(16)
- turtle.digUp()
- sleep( 0.1 )
- -- select sapling slot
- turtle.select(1)
- if turtle.getItemCount(2) < 3 then stop = true end
- fl = turtle.getFuelLevel()
- end
- -- assuming there is not enough logs in slot 2 atm
- if oldfl == fl then stop = true end
- -- just enough fuel
- if fl > maxlevel then stop = true end
- -- last check "ran out of fuel?"
- -- 1 step = 6 up + 6 down + 1 walk and that amount of fuel
- if fl < 13 then
- -- stay in the loop until someone brought fuel
- -- i cannot move another full step anyways
- stop = false
- print("FUEL NEEDED!")
- end
- print( " fl="..fl )
- until( stop )
- end
- print( "continuing with Fuellevel = "..fl.."/"..maxlevel)
- end
- function GoDown()
- local detect = false
- local what = ""
- local iamdown = false
- local chest = false
- -- going down to surface
- -- leaves are ok and get digged
- print( "Going down" )
- turtle.select(1)
- while iamdown == false do
- for i = 1, 4 do
- turtle.turnLeft()
- if Mineable() then
- turtle.dig()
- end
- -- try to get extra saplings
- turtle.suck()
- turtle.suckDown()
- end
- detect, what = turtle.inspectDown()
- if detect then
- print( "down block= ".. what.name )
- if( what.name == "minecraft:chest" ) then
- chest = true
- -- before unloading try to refuel a bit
- FuelCheck( 1000 )
- -- everything ok
- iamdown = true
- -- unload to chest
- print( "unloading to chest")
- -- no saplings, no templates, not the crafting chest in slot 16
- for i = 3, 15 do
- turtle.select(i)
- turtle.dropDown()
- sleep( 0.1 )
- end
- -- template slot 2
- -- not everything, please
- local c2 = turtle.getItemCount(2)
- if( c2 > 1 ) then
- turtle.select(2)
- turtle.dropDown( c2 - 1 )
- end
- turtle.select(1)
- end
- if stringhas(what.name, "_leaves") then
- turtle.digDown()
- else
- iamdown = true
- end
- end
- turtle.down()
- end
- -- check for fuellevel
- if not chest then FuelCheck( 1000 ) end
- end
- -- Check to see if I'm a felling or mining turtle
- if type( turtle.dig ) ~= "function" then
- print( "I need a pickaxe or an axe to be mining or felling turtle.")
- error("Craft me together with a diamond tool!")
- end
- -- Check to see if I'm alright and ready for crafting
- bCrafting = (peripheral.getType("left") == "workbench") or (peripheral.getType("right") == "workbench")
- if not bCrafting then
- error("I want to be a crafty turtle! Craft me together with a crafting table.")
- end
- -- self sustain: a) label the turtle
- if os.getComputerLabel() == nil then
- os.setComputerLabel("Lumberjack")
- end
- -- self sustain: b) make it autostart
- -- copy this program to startup when its not "startup"
- -- to make it resistant against chunk reloads (ie return from nether)
- me = fs.getName( shell.getRunningProgram() )
- if( me ~= "startup") then
- print( "Overwriting startup with "..me )
- if( fs.exists("startup") ) then
- fs.delete("startup")
- end
- fs.copy( me, "startup" )
- end
- -- self sustain: c) find the wall to get orientation
- -- not too rare situation: player left while chest was on top (refueling)
- -- check for chest on top and if yes then get everything back
- -- using the 2nd half of the refueling function
- if CheckUp( "chest" ) then
- turtle.select(1)
- -- get everything back, hopefully saplings to slot 1
- while turtle.suckUp() do sleep(0.1) end
- -- put the chest back to slot 16
- turtle.select(16)
- turtle.digUp()
- sleep( 0.1 )
- -- select sapling slot
- turtle.select(1)
- end
- GoDown()
- -- main loop
- while true do
- Walk()
- -- turnto the inside
- if expectedBlock == "stone" then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- GoUp()
- GoDown()
- detect, what = turtle.inspect()
- if not detect then
- turtle.select(1)
- -- don't run out of samples!!!
- if turtle.getItemCount(1) > 1 then
- if turtle.place() then
- print("planted a sapling")
- else
- -- no dirt after a corner!!
- -- we want to see this message
- -- else it gets stuck in the corner
- print("failed to plant a sapling")
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement