Advanced Turtle Lumberjack (tutorial)

From ComputerCraft Wiki
Jump to: navigation, search

Advanced Turtle Lumberjack

Introduction

Welcome to this tutorial about Advanced Turtle Lumberjacks. See the Turtle Lumberjack Tutorial first.

What are Advanced Turtle Lumberjacks?

Advanced Turtle Lumberjacks are basically Turtle Lumberjacks that can dig blocks above them. We first make the turtle dig the block in front of it and then move forward so the rest of the tree is directly above it.

The Code

 turtle.dig()
 turtle.forward()
 while turtle.detectUp() do
   turtle.digUp()
   turtle.up()
 end
 while not turtle.detectDown() do
   turtle.down()
 end

This makes us check if there are no blocks to be dug, then we go down until we can't. That's it!


Another Advanced Turtle Lumberjack

This is a lot more complex BUT this can create a row of trees and constantly monitor them to see if they have grown or not. Here is a link to a video showing what it does- ComputerCraft-Turtle-Tree Lumberjack

-- advanced turtle lumberjack
shell.run('clear')
print("Please put Dirt in Slot 1, Saplings in Slot 2, and a log in Slot 3.")
print("Number of Trees To Plant?")
local x = io.read()
local dirt, sapling, log = 1, 2, 3

-- get into position
turtle.up()
turtle.turnLeft()
turtle.turnLeft()
turtle.back()
turtle.back()
turtle.back()

-- initial planting
for i = 0, x do
  turtle.digDown()
  turtle.select(dirt)
  turtle.placeDown()
  turtle.back()
  turtle.select(sapling)
  turtle.place()
  turtle.back()
  turtle.back()
  turtle.back()
end
turtle.turnLeft()
turtle.forward()
turtle.turnRight()

--chop trees
while true do
  -- sleep for 30 seconds
  os.sleep(30)
  -- check all trees for any that have grown
  for i = 0, x do
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
    turtle.select(log)
    -- if the tree has grown
    if turtle.compare() then
      turtle.dig()
      turtle.forward()
      -- harvest the tree
      while turtle.detectUp() do
      turtle.digUp()  
      turtle.up()
      end
      -- return to the ground
      while not turtle.detectDown() do
        turtle.down()
      end
      -- plant a new sapling
      turtle.back()
      turtle.select(sapling)
      turtle.place()
    end
    turtle.turnLeft()
  end
  -- round the corner
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
end


(Helpful tip: Save this to your computercraft/lua/rom/programs folder, then it will be on all turtles.)

Simplified Advanced Turtle Lumberjack

This is a simplified version of an advanced turtle lumberjack. As the header comment states, it assumes that the turtle is in the bottom left corner of a 10x3 flat area of dirt and a stack of saplings is in slot 1. This will run until it runs out of fuel, or it has planted all the saplings. See this github repository for this and other automatic turtle related programs.

--[[ Simple lumberjack program for the wiki.  Assumes the turtle is in the 
	bottom left corner of a 10 ahead and 3 to the right area of clear dirt or
	grass.  Also, only works for oak saplings, since they can grow next to each
	other. ]]--
local saplings = 1 -- the slot where the saplings are
turtle.select(saplings) -- select the saplings
while turtle.getItemCount(saplings) > 0 do -- while more saplings in inventory
	for i = 1, 8 do -- plant 8 saplings in a row
		turtle.turnRight()
		if not turtle.compare() then -- if not a sapling, then a tree grew
			turtle.dig()
			turtle.forward()
			while turtle.detectUp() do -- dig tree out
				turtle.digUp()
				turtle.up()
			end
			while not turtle.detectDown() do -- back down to ground
				turtle.down()
			end
			turtle.back()
			turtle.place() -- put down new sapling
		end
		turtle.turnLeft()
		turtle.forward()
	end
	-- turn around and line up for next pass
	turtle.turnRight()
	turtle.forward()
	turtle.forward()
	turtle.turnRight()
	turtle.forward()
	os.sleep(30) -- sleep for 30 seconds to allow trees to grow
end