Difference between revisions of "Advanced Turtle Lumberjack (tutorial)"

From ComputerCraft Wiki
Jump to: navigation, search
(Created page with " == Advanced Turtle Lumberjack == === Introduction === Welcome to this tutorial about Advanced Turtle Lumberjacks. See the [[Turtle_Lumberjack_(tutoria...")
 
(Another Advanced Turtle Lumberjack)
 
(17 intermediate revisions by 11 users not shown)
Line 6: Line 6:
  
 
=== What are Advanced Turtle Lumberjacks? ===
 
=== What are Advanced Turtle Lumberjacks? ===
Advanced Turtle Lumberjacks are basically [[Turtle_Lumberjack_(tutorial)|Turtle Lumberjacks]] that can dig blocks above them.
+
Advanced Turtle Lumberjacks are basically [[Turtle_Lumberjack_(tutorial)|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 ==
 
== The Code ==
First, we start with the basics.
 
 
<code>
 
<code>
while turtle.detect() do
+
  turtle.dig()
+
  turtle.forward()
end
+
  while turtle.detectUp() do
 +
    turtle.digUp()
 +
    turtle.up()
 +
  end
 +
  while not turtle.detectDown() do
 +
    turtle.down()
 +
  end
 
</code>
 
</code>
 +
This makes us check if there are no blocks to be dug, then we go down until we can't.
 +
That's it!
  
This makes us loop until there is no blocks in front of the turtle.
+
-----------------------------------------------------------------------------------------
  
 +
== 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- [http://www.youtube.com/watch?v=7rmB09b6wYk ComputerCraft-Turtle-Tree Lumberjack]
 
<code>
 
<code>
  while turtle.detect() do
+
  -- advanced turtle lumberjack
  turtle.dig()
+
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
 
  end
</code>
+
turtle.turnLeft()
 
+
turtle.forward()
This makes it dig the block. Now we make it see if there is a block ABOVE it.
+
turtle.turnRight()
 
+
<code>
+
--chop trees
  while turtle.detect() do
+
  while true do
  turtle.dig()
+
  -- sleep for 30 seconds
  if turtle.detectUp()
+
  os.sleep(30)
    turtle.digUp()
+
  -- 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
 
   end
 +
  -- round the corner
 +
  turtle.forward()
 +
  turtle.turnRight()
 +
  turtle.forward()
 +
  turtle.forward()
 +
  turtle.turnRight()
 +
  turtle.forward()
 
  end
 
  end
 
</code>
 
</code>
  
This makes us dig the block above if there is one.
 
Let's make us move up.
 
  
<code>
+
('''Helpful tip:''' Save this to your computercraft/lua/rom/programs folder, then it will be on all turtles.)
while turtle.detect() do
+
  turtle.dig()
+
  if turtle.detectUp()
+
    turtle.digUp()
+
  turtle.up()
+
  end
+
end
+
</code>
+
  
Now, let's make it come back down when there is no blocks to be digged.
+
== 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 [https://github.com/noctolater/Turtles this github repository] for this and other automatic turtle related programs.
  
 
<code>
 
<code>
  while turtle.detect() do
+
--[[ Simple lumberjack program for the wiki.  Assumes the turtle is in the
  turtle.dig()
+
bottom left corner of a 10 ahead and 3 to the right area of clear dirt or
  if turtle.detectUp()
+
grass.  Also, only works for oak saplings, since they can grow next to each
    turtle.digUp()
+
other. ]]--
  turtle.up()
+
local saplings = 1 -- the slot where the saplings are
  end
+
turtle.select(saplings) -- select the saplings
end
+
  while turtle.getItemCount(saplings) > 0 do -- while more saplings in inventory
while not turtle.detect() and not turtle.detectUp() and turtle.detectDown() do
+
for i = 1, 8 do -- plant 8 saplings in a row
  turtle.down()
+
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
 
  end
 
</code>
 
</code>
 
This makes us check if there are no blocks to be digged, then we go down until we can't.
 
That's it!
 
 
('''Helpful tip:''' Save this to your computercraft/lua/programs folder, then it will be on all turtles.)
 
 
== Category & Author ==
 
A tutorial by [[User:TheVarmari|TheVarmari]]. Feel free to correct any mistakes!
 
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Latest revision as of 19:19, 23 September 2012

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