Here's the code:
-- Functions --
local function treeCut()
turtle.select(1)
turtle.place()
turtle.select(2)
turtle.place()
turtle.dig()
turtle.forward()
turtle.select(1)
while turtle.detectUp() do
turtle.digUp()
turtle.up()
end
local function resetPos()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
end
-- Code --
print("How many trees?")
local amount = tonumber read()
for i = 1, amount do
treeCut()
turtle.down()
while not turtle.detectDown() do
turtle.down()
end
resetPos()
end
Now, how and why does it work?!
Firstly, lets look at those functions. What is a function?!
A function is a bunch of code that you might want to re-use without continuously typing out, it also helps make the code neater.
local function cake()
print("Would you like a cake?")
cake = read()
if cake == yes then
print("Have a cake")
else
print("Fine then...")
end
In this, we firstly state what it is (a function) and then we state the function name [cake()]. To use the code, all we have to do is use the name "cake()" and it will carry out the instructions in the code itself. Remember to "end" the function, or else all you type after will be used as part of the function too!
And when stating the name of the function, note that it IS case sensitive, so Cake() is completely different to cake(). Also note the brackets, they ARE needed.
So, as previously stated, all we need to do to now use this function, is to call it using its name: cake(). Simple right?
Now lets look at the functions in the program. Function 1 [treeCut()] is the first function stated. Its the main part of the program, the planting, bone mealing, and cutting of the tree!!
Lets see how it works..
Firstly, we state its a local function, and its named treeCut(). Then we tell it to [turtle.select(1)] this tells the turtle to selece position 1 in the inventory, next we tell it to place that item (should be saplings!!). Then we tell the turtle to [turtle.select(2)] and [turtle.place()] this selects inventory slot 2 and places it (should be bone meal). This will grow the tree!!.
Now we tell it to[turtle.dig()] and then [turtle.forward()].Simple, it cuts the first log down, and moves forward. Now the rest of the code comes into play.
[while turtle.detectUp do] tells the turtle, while it detects a block above it, to do the following instructions. These instructions are:
turtle.digUp()
turtle.up()
So turtle digs up, and moves up. Unless your tree is special, there should now be another log above it, and [while turtle.detectUp do] makes it dig up and move up again.
When the turtle has finished this, digging up the whole log of the tree, it will now continue to take out the following instructions:
turtle.down()
while turtle.down() do
turtle.down()
end
WHOA!! Inception! Yes, this is self explanatory, while turtle.down() do the following:
turtle.down()
So, it makes the turtle go down, and when it goes down, go down. When it reaches the bottom, it wont continuously try to go down, because it hasn't gone down.. Confused? Give it a go!
ResetPos()
What it is? All it does is turn the turtle right twice, move once forward and turn around again.
That's it!! Not too confusing is it now?
I hope you enjoyed the tutorial. Download for the program file is here:
DOWNLOAD