Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------
- -- Computercraft Turtle Tree Farm
- -----------------------------------------------------------------------------------
- -- Change the item IDs for the sapling, marker and storage block before use
- -- Also can configure the minimum fuel level
- --
- -- Place at least 2 saplings in the first slot and
- -- some of the desired fuel to use in the last slot
- --
- -- Build a ring of the marker block around the tree farm to keep
- -- the turtle from flying away
- -----------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------
- local sapling_name = "minecraft:spruce_sapling"
- local marker_name = "minecraft:cobblestone_wall"
- local storage_name = "minecraft:chest"
- local min_fuel_level = 500
- -----------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------
- -- Code
- -- ONLY EDIT BEYOND THIS POINT IF YOU ARE CONFIDENT PROGRAMMER
- -----------------------------------------------------------------------------------
- -----------------------------------------------------------------------------------
- local sapling_slot = 1
- local fuel_slot = 16
- -------------------
- local last_turn = 0
- function isLeaf(blockName)
- return string.sub(blockName, -6, -1) == "leaves"
- end
- function isLog(blockName)
- return string.sub(blockName, -3, -1) == "log"
- end
- function findOperatingLevel()
- print("Trying to find the base level")
- turtle.select(sapling_slot)
- while true do
- local detected, data = turtle.inspectDown()
- if detected and (isLog(data.name) or isLeaf(data.name)) then
- print("Leaf or log is below. Going down.")
- turtle.digDown()
- turtle.down()
- elseif detected and turtle.compareDown() then
- print("Sapling is below. Must be there")
- break
- elseif detected and data.name == marker_name then
- print("Marker is below. Must be there")
- return
- elseif detected then
- print("Some sort of floor is below. Planting a sapling")
- turtle.up()
- if turtle.getItemCount() > 1 then
- turtle.placeDown()
- end
- break
- else
- print("Nothing below. Going down")
- turtle.down()
- end
- end
- end
- function findBoundary()
- print("Trying to find the boundary")
- findOperatingLevel()
- turtle.select(sapling_slot)
- while true do
- local detected, data = turtle.inspectDown()
- if detected and data.name == marker_name then
- break
- end
- if not turtle.forward() then
- turtle.dig()
- turtle.forward()
- turtle.digDown()
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- end
- while turtle.down() do
- end
- turtle.up()
- if turtle.getItemCount() > 1 then
- turtle.placeDown()
- end
- end
- end
- end
- function findStorage()
- print("Trying to find the chest")
- findBoundary()
- while true do
- local detected, data = turtle.inspect()
- if detected and data.name == storage_name then
- return
- else
- if not turtle.forward() then
- turtle.dig()
- turtle.forward()
- end
- local detected, data = turtle.inspectDown()
- if detected and data.name == marker_name then
- else
- turtle.back()
- turtle.turnLeft()
- end
- end
- end
- end
- function restockRefuel()
- print("Restock and Refuel")
- findStorage()
- turtle.select(sapling_slot)
- turtle.suck(turtle.getItemSpace())
- turtle.select(fuel_slot)
- turtle.suck(turtle.getItemSpace())
- for var=1,16 do
- turtle.select(var)
- if var == sapling_slot or var == fuel_slot then
- else
- if turtle.getItemCount(var) == 0 then
- elseif not turtle.drop() then
- print("The storage is full. Stopping...")
- error()
- end
- end
- end
- if turtle.getFuelLevel() < min_fuel_level then
- turtle.select(fuel_slot)
- turtle.refuel(turtle.getItemCount()-1)
- end
- turtle.back()
- turtle.turnLeft()
- end
- function chopTree()
- turtle.dig()
- turtle.forward()
- turtle.digDown()
- turtle.select(sapling_slot)
- turtle.placeDown()
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- end
- while turtle.down() do
- turtle.select(sapling_slot)
- local detected, data = turtle.inspectDown()
- if detected and (isLeaf(data.name) or isLog(data.name)) then
- turtle.digDown()
- elseif detected and turtle.compareDown() then
- else
- turtle.up()
- turtle.placeDown()
- break
- end
- end
- end
- while true do
- print("Loop")
- sleep(3)
- if turtle.getItemCount(sapling_slot) == 0 or turtle.getItemCount(fuel_slot) == 0 then
- print("Need fuel and saplings to work")
- sleep(10)
- end
- if turtle.getItemCount(sapling_slot) == 1 or turtle.getFuelLevel() < min_fuel_level then
- print(" Low on fuel" .. tostring(turtle.getFuelLevel()))
- restockRefuel()
- end
- if not turtle.forward() then
- local detected, data = turtle.inspect()
- if detected and isLeaf(data.name) then
- turtle.dig()
- turtle.forward()
- elseif detected and isLog(data.name) then
- chopTree()
- end
- end
- local detected, data = turtle.inspectDown()
- if detected and data.name == marker_name then
- print("On Marker")
- local turn_func = nil
- if last_turn == 0 then
- turn_func = turtle.turnLeft
- last_turn = 1
- else
- turn_func = turtle.turnRight
- last_turn = 0
- end
- print("Picked turning direction")
- while true do
- local downDetected, downData = turtle.inspectDown()
- if downDetected and downData.name == marker_name then
- turn_func()
- print("Turn")
- if not turtle.forward() then
- local frontDetected, frontData = turtle.inspect()
- if frontDetected and isLeaf(frontData.name) then
- turtle.dig()
- turtle.forward()
- break
- elseif frontDetected and isLog(frontData.name) then
- chopTree()
- break
- end
- end
- else
- print("Marker not below. Resuming operation")
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement