Turtle Lumberjack (tutorial)

From ComputerCraft Wiki
Jump to: navigation, search

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 chopped 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 up and then come back down.

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