Advanced Turtle Lumberjack (tutorial)

From ComputerCraft Wiki
Revision as of 06:52, 10 September 2012 by Noctolater (Talk | contribs) (refactored code for simplicity)

Jump to: navigation, search

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