Difference between revisions of "Turtle Lumberjack (tutorial)"

From ComputerCraft Wiki
Jump to: navigation, search
(Introduction)
m (Fix code blocks being badly formatted)
 
(5 intermediate revisions by 4 users not shown)
Line 6: Line 6:
 
=== What are Turtle Lumberjacks? ===
 
=== What are Turtle Lumberjacks? ===
 
Turtle Lumberjacks are mining turtles that are programmed to chop down a tree.
 
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!
+
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.
 
This can be also used to destroy pillars of sandstone, sand etc.
  
 
== The Code ==
 
== The Code ==
 
First, we start with the basics.
 
First, we start with the basics.
<code>
+
 
 
  while turtle.detect() do
 
  while turtle.detect() do
 
   
 
   
 
  end
 
  end
</code>
+
 
 
This makes it loop while there is a block in front of it.
 
This makes it loop while there is a block in front of it.
 
Let's add the digging part to it.
 
Let's add the digging part to it.
<code>
+
 
 
  while turtle.detect() do
 
  while turtle.detect() do
 
   turtle.dig()
 
   turtle.dig()
 
   turtle.up()
 
   turtle.up()
 
  end
 
  end
</code>
+
 
 
This makes it dig the block if it is detected, then move up.
 
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.
 
Let's add the print function to print what the turtle is doing.
<code>
+
 
 
  while turtle.detect() do
 
  while turtle.detect() do
 
   turtle.dig()
 
   turtle.dig()
Line 33: Line 33:
 
   print("Moving up")
 
   print("Moving up")
 
  end
 
  end
</code>
+
 
 
There we go!
 
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!
 
Let's make it come back down when there is no more blocks to be chopped, or there is a roof above it!
<code>
+
 
 
  while turtle.detect() do
 
  while turtle.detect() do
 
   turtle.dig()
 
   turtle.dig()
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()
 
   print("Moving down")
 
   print("Moving down")
 
  end
 
  end
</code>
+
 
 
This makes it check that while there are no blocks to be chopped and it can go down, it goes down.
 
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.
 
Let's add a notification about the job being done.
<code>
+
 
 
  while turtle.detect() do
 
  while turtle.detect() do
 
   turtle.dig()
 
   turtle.dig()
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>
+
 
  
 
There you go! Now it is done!
 
There you go! Now it is done!
Your turtle will dig the tree down and then come back down.
+
Your turtle will dig the tree up and then come back down.
  
('''Helpful tip:''' Save this to your computercraft/lua/programs folder, then it will be on all turtles.)
+
('''Helpful tip:''' Save this to your ComputerCraft/lua/rom/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 21:58, 12 September 2017

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.)