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

From ComputerCraft Wiki
Jump to: navigation, search
(refactored code for simplicity)
Line 29: Line 29:
 
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]
 
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>
 +
-- advanced turtle lumberjack
 
  shell.run('clear')
 
  shell.run('clear')
C=1
+
  print("Please put Dirt in Slot 1, Saplings in Slot 2, and a log in Slot 3.")
X=0
+
  print("Number of Trees To Plant?")
X1=0
+
  local x = io.read()
  print("Place Dirt in Slot 1 and Saplings in Slot 2 and 1 log in slot 3 Thankyou :) ")
+
  local dirt, sapling, log = 1, 2, 3
  write("How many trees do you want me to plant X direction?:")
+
  X=io.read()
+
   
+
X=X+0
+
X1=X
+
X1=X1+0
+
 
   
 
   
 +
-- get into position
 
  turtle.up()
 
  turtle.up()
 
  turtle.turnLeft()
 
  turtle.turnLeft()
 
  turtle.turnLeft()
 
  turtle.turnLeft()
 
 
  turtle.back()
 
  turtle.back()
 
  turtle.back()
 
  turtle.back()
 
  turtle.back()
 
  turtle.back()
 
   
 
   
  while C>0 do
+
  -- initial planting
while X>0 do
+
for i = 0, x, 1 do
turtle.digDown()
+
  turtle.digDown()
turtle.select(1)
+
  turtle.select(dirt)
turtle.placeDown()
+
  turtle.placeDown()
turtle.back()
+
  turtle.back()
turtle.select(2)
+
  turtle.select(sapling)
turtle.place()
+
  turtle.place()
turtle.back()
+
  turtle.back()
turtle.back()
+
  turtle.back()
turtle.back()
+
  turtle.back()
X=X-1
+
  end
end
+
C=0
+
  end
+
print("Planted Trees!")
+
 
+
 
  turtle.turnLeft()
 
  turtle.turnLeft()
 
  turtle.forward()
 
  turtle.forward()
 
  turtle.turnRight()
 
  turtle.turnRight()
 
+
 
+
  --chop trees
 
+
  while true do
  --/chop trees
+
  -- sleep for 30 seconds
X=X1
+
  os.sleep(30)
C=1
+
  for i = 0, x, 1 do
C=C+0
+
    turtle.forward()
  while C>0 do
+
    turtle.forward()
while X>0 do
+
    turtle.forward()
print("X= "..X.."")
+
    turtle.forward()
X=X+0
+
    turtle.turnRight()
turtle.forward()
+
    turtle.select(log)
turtle.forward()
+
    -- if the tree has grown
turtle.forward()
+
    if turtle.compare() then
turtle.forward()
+
      turtle.dig()
turtle.turnRight()
+
      turtle.forward()
turtle.select(3)
+
      -- harvest the tree
if turtle.compare()==true then
+
      while turtle.detectUp() do
turtle.dig()
+
        turtle.up()
turtle.forward()
+
        turtle.digUp()
+
      end
while turtle.detectUp() do
+
      -- return to the ground
turtle.up()
+
      while not turtle.detectDown() do
turtle.digUp()
+
        turtle.down()
turtle.up()
+
      end
end
+
      -- plant a new sapling
+
      turtle.back()
while turtle.detectDown()==false do
+
      turtle.select(sapling)
turtle.down()
+
      turtle.place()
end
+
    end
turtle.back()
+
    turtle.turnLeft()
turtle.select(2)
+
  end
turtle.place()
+
  -- round the corner
end
+
  turtle.forward()
+
  turtle.turnRight()
turtle.turnLeft()
+
  turtle.forward()
X=X-1
+
  turtle.forward()
print("X after = "..X.."")
+
  turtle.turnRight()
end
+
  turtle.forward()
turtle.forward()
+
turtle.turnRight()
+
turtle.forward()
+
turtle.forward()
+
turtle.turnRight()
+
turtle.forward()
+
X=X1
+
W=turtle.getItemCount(3)
+
print("I've cut down "..T.." Trees")
+
print("I've collected "..W.." Logs")
+
 
  end
 
  end
 
</code>
 
</code>

Revision as of 06:52, 10 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, 1 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)
  for i = 0, x, 1 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.up()
        turtle.digUp()
      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.)