This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
De Tronazox's profile picture

Tree Cutting Tool (With Amount of Trees Input) For n00bs

Started by De Tronazox, 22 December 2012 - 09:39 PM
De Tronazox #1
Posted 22 December 2012 - 10:39 PM
A simple program for those newer to computercraft.

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
ChunLing #2
Posted 22 December 2012 - 11:30 PM
Hmm…I think that:
while turtle.down() do
  turtle.down()
end
Is a little more self-explanatory.
Doyle3694 #3
Posted 22 December 2012 - 11:42 PM
Same imaginary useage that (read()) will somehow create a number, just as with your last program…
De Tronazox #4
Posted 23 December 2012 - 12:24 AM
Hmm…I think that:
while turtle.down() do
  turtle.down()
end
Is a little more self-explanatory.
Haha, fair enough

Same imaginary useage that (read()) will somehow create a number, just as with your last program…

Yeah, seeing what you mean. I'll not bother next time, haha
ChunLing #5
Posted 23 December 2012 - 01:23 AM
Just tonumber it. But yeah, you should really test programs that you are going to advertise as suitable for noobs, cause they're not going to be well equipped to debug them if they don't work.
De Tronazox #6
Posted 23 December 2012 - 01:59 AM
Just tonumber it. But yeah, you should really test programs that you are going to advertise as suitable for noobs, cause they're not going to be well equipped to debug them if they don't work.

I will, haha. And it's more of a tutorial then anything else.
De Tronazox #7
Posted 23 December 2012 - 01:15 PM
Fixed up the code to make it easier to read/understand.
De Tronazox #8
Posted 25 December 2012 - 09:49 PM
No one likes this tool? I think it's pretty cool
Mikeemoo #9
Posted 25 December 2012 - 10:59 PM
while turtle.down() do
turtle.down()
end

This makes little sense. For every loop you're calling down() twice, once in the conditional and once in the body.
De Tronazox #10
Posted 25 December 2012 - 11:02 PM
while turtle.down() do
turtle.down()
end

This makes little sense. For every loop you're calling down() twice, once in the conditional and once in the body.
While it goes down, go down. When it stops going down (hits floor) it stops.
Mikeemoo #11
Posted 25 December 2012 - 11:09 PM
while turtle.down() do
turtle.down()
end

This makes little sense. For every loop you're calling down() twice, once in the conditional and once in the body.
While it goes down, go down. When it stops going down (hits floor) it stops.

I fully understand WHAT it does, but it doesn't make sense to do that. This is the kind of thing that leads to buggy, hard to understand code.

"While it goes down, go down." <– makes little sense. You're not saying "While i CAN go down, move down", you're saying "go down and if that worked go down again". Sure, the code still works, but it's non-nonsensical code. Your action is based on an irrelevant condition.


This makes far more sense:


while not turtle.detectDown() do
  turtle.down()
end
De Tronazox #12
Posted 25 December 2012 - 11:13 PM
while turtle.down() do
turtle.down()
end

This makes little sense. For every loop you're calling down() twice, once in the conditional and once in the body.
While it goes down, go down. When it stops going down (hits floor) it stops.

I fully understand WHAT it does, but it doesn't make sense to do that. This is the kind of thing that leads to buggy, hard to understand code.

"While it goes down, go down." <– makes little sense. You're not saying "While i CAN go down, move down", you're saying "go down and if that worked go down again". Sure, the code still works, but it's non-nonsensical code. Your action is based on an irrelevant condition.


This makes far more sense:


while not turtle.detectDown() do
  turtle.down()
end

I had that before, and for some reason it would get stuck on the ground.. I think i actually fixed it though when I changed some of the other code. I just had that in place. I completely see what you mean, and I will change it now. :D/>
ChunLing #13
Posted 26 December 2012 - 02:13 PM
You can use "while turtle.down() do end", if you really need to avoid the second turtle.down call.

But the point was something that was self-explanatory, and "while turtle.down do end" might be a bit confusing to novice programmers.

There is nothing buggy about it in use, it works perfectly until the turtle can't go down anymore, at which point the loop ends.
De Tronazox #14
Posted 26 December 2012 - 08:45 PM
You can use "while turtle.down() do end", if you really need to avoid the second turtle.down call.

But the point was something that was self-explanatory, and "while turtle.down do end" might be a bit confusing to novice programmers.

There is nothing buggy about it in use, it works perfectly until the turtle can't go down anymore, at which point the loop ends.
Thats what I thought, it really souldn't be buggy.. But ahh well, trying to make everyone happy. :)/>
ChunLing #15
Posted 27 December 2012 - 12:01 AM
Meh. It's actually the detect dependent loop that's potentially buggy, since there are things other than blocks that can stop movement (going down, the only likely candidate is an entity, but that's not really unlikely to happen while tree farming). Some stupid chicken gets stuck under your turtle, and you'll need to terminate out (or dig out and kill the chicken). Not very likely, but possible. That's why I set my turtles to kill anything that gets in the way.
De Tronazox #16
Posted 01 January 2013 - 08:11 PM
Meh. It's actually the detect dependent loop that's potentially buggy, since there are things other than blocks that can stop movement (going down, the only likely candidate is an entity, but that's not really unlikely to happen while tree farming). Some stupid chicken gets stuck under your turtle, and you'll need to terminate out (or dig out and kill the chicken). Not very likely, but possible. That's why I set my turtles to kill anything that gets in the way.
That makes sense. Thanks
ChunLing #17
Posted 02 January 2013 - 02:11 AM
Just don't stand in the way and try any "I am your creator" nonsense. One time I had several turtles digging, and I sorta…I was using my PDA to send commands to another turtle (I was also in a boat, cause the area was flooded).
De Tronazox #18
Posted 02 January 2013 - 05:19 PM
Just don't stand in the way and try any "I am your creator" nonsense. One time I had several turtles digging, and I sorta…I was using my PDA to send commands to another turtle (I was also in a boat, cause the area was flooded).

That was a very confusing sentence to understand. So, what? Haha
ChunLing #19
Posted 03 January 2013 - 03:03 AM
AND…it was a Hardcore game.
De Tronazox #20
Posted 03 January 2013 - 06:50 PM
AND…it was a Hardcore game.
Righto,aha
De Tronazox #21
Posted 04 January 2013 - 05:38 PM
Made for any treeeeeee
xInDiGo #22
Posted 10 January 2013 - 11:47 AM
I got two errors when trying this one out. The first one being the Local Function was missing an END and i was able to fix that error, but the next one is on line 30 or so. the error is 'for' limit must be a number. any ideas?

::edit:: alright after messing around and reading other posts i figure out the errors and solved them both. The first one i listed above, the second one should have been this:
local amount = tonumber(read())

should i post the whole thing?
Kangarooian #23
Posted 25 February 2013 - 02:01 AM
hey im a noob to codes. i coppied it just as it is but i keep "getting attempt to call nil" on the line for the tree cut local function. someone help?
Kangarooian #24
Posted 25 February 2013 - 02:23 AM
just plaied with it more and got this: bios:338:[string "treeCut()"]:12:'end' expected (to close 'function' at line 1)

heres what i did:
>edit treeCut()

function treeCut()
turtle.select(1)
turtle.place()
turtle.select(2)
turtle.place()
turtle.dig()
turtle.foward()
turtle.select(1)
while turtle.detectUp () do
turtle.digUp()
turtle.up()
end


Im really new to this so all help would be Awsome! thanks!
unobtanium #25
Posted 25 February 2013 - 05:37 AM
It simply says that you are missing an "end".
You closed the while-loop but not the function.
Add an end and it should work. Maybe look over turtle.detectUp() and not turtle.detectUp ()
Kangarooian #26
Posted 25 February 2013 - 07:43 AM
ok thats fixed issue now is: treeCut() :4: attempt to index ?(a nil value)
unobtanium #27
Posted 25 February 2013 - 09:56 AM
turtle.forward() instead of turtle.foward()