Difference between revisions of "Turtle Lumberjack (tutorial)"

From ComputerCraft Wiki
Jump to: navigation, search
(Introduction)
(The Code)
Line 43: Line 43:
 
   print("Moving up")
 
   print("Moving up")
 
  end
 
  end
 
 
  while not turtle.detect() and not turtle.detectDown() do
 
  while not turtle.detect() and not turtle.detectDown() do
 
   turtle.down()
 
   turtle.down()
Line 58: Line 57:
 
   print("Moving up")
 
   print("Moving up")
 
  end
 
  end
 
 
  while not turtle.detect() and not turtle.detectDown() do
 
  while not turtle.detect() and not turtle.detectDown() do
 
   turtle.down()
 
   turtle.down()
 
   print("Moving down")
 
   print("Moving down")
 
  end
 
  end
 
 
  print("Job done!")
 
  print("Job done!")
 
</code>
 
</code>

Revision as of 14:16, 26 February 2012

Turtle Lumberjack

Introduction

Welcome to this tutorial about Turtle Lumberjacks.

What are Turtle Lumberjacks?

Turtle Lumberjacks are mining turtles that are programmed to chop down a tree. They are placed in front of a tree and then activated, and then you watch the tree get chop down! This can be also used to destroy pillars of sandstone, sand etc.

The Code

First, we start with the basics.

while turtle.detect() do

end

This makes it loop while there is a block in front of it. Let's add the digging part to it.

while turtle.detect() do
 turtle.dig()
 turtle.up()
end

This makes it dig the block if it is detected, then move up. Let's add the print function to print what the turtle is doing.

while turtle.detect() do
 turtle.dig()
 print("Digging the block")
 turtle.up()
 print("Moving up")
end

There we go! Let's make it come back down when there is no more blocks to be chopped, or there is a roof above it!

while turtle.detect() do
 turtle.dig()
 print("Digging the block")
 turtle.up()
 print("Moving up")
end
while not turtle.detect() and not turtle.detectDown() do
 turtle.down()
 print("Moving down")
end

This makes it check that while there are no blocks to be chopped and it can go down, it goes down. Let's add a notification about the job being done.

while turtle.detect() do
 turtle.dig()
 print("Digging the block")
 turtle.up()
 print("Moving up")
end
while not turtle.detect() and not turtle.detectDown() do
 turtle.down()
 print("Moving down")
end
print("Job done!")

There you go! Now it is done! Your turtle will dig the tree down and then come back down.

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

Category & Author

A tutorial by TheVarmari. Feel free to correct any mistakes!