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

[LUA] turtle.compare() malfunction (tree farming)

Started by FF0084, 20 January 2013 - 05:32 AM
FF0084 #1
Posted 20 January 2013 - 06:32 AM
The idea is simple: I have a tree farm, forming a rectangle. The turtle follows the perimeter and encounters either saplings or wood blocks ( generally speaking ).
* When it's the wood log it encounters, it chops down the entire tree, plants a sapling and carries on - to the next tree.
* When it encounters a sapling, it waits until it grows. Then a wood log should appear and the cycle would begin from the beginning ( a tree's getting chopped down, the sapling is left in place and so on ).

The problem is with my waiting bit. It's something like this:


turtle.select(1) --the slot with saplings
while turtle.compare() do
	sleep(10)
end

Simple, eh? The sad thing is, that it doesn't work. After waiting some time ( Minecraft days for instance ), the turtle.compare() function eventually returns false even though the sapling is still there. The outcome is far from expected: after some time, every tree is gone and no saplings are deployed.

I presume the malfunction takes place right when a tree is about to grow, but it's only an assumption (because I have to wait, it doesn't happen right away). However, even if I wait few seconds after the malfunction, the sapling stays. I've even tried resetting the slot selected and moving the turtle back and forth hoping the turtle will get set right afterwards. It doesn't.

Maybe the sapling changes its block ID and it's no longer the same? Unfortunately I'm not able to check that with the turtles themselves. It either fails or not, it doesn't tell me why it failed.

Any ideas how to debug this script? I'm lacking some knowledge on turtles; maybe there are some magic functions that would give me some more details on why turtle is not seeing my sapling as it should. Cheers, Tom
ChunLing #2
Posted 20 January 2013 - 09:46 AM
Hmm…I always just use while not turtle.compare() do sleep(20) end with a log selected.
FF0084 #3
Posted 21 January 2013 - 04:05 AM
I've run the new script for few hours and to my surprise, the succession rate of my tree farm has risen to 100% - the farm was intact and the wooden logs are now getting farmed correctly.

But the question still remains … what was actually wrong? Can/should we consider it a ComputerCraft bug?
Eric #4
Posted 21 January 2013 - 04:42 AM
Known problem (not necessarily bug) with using .compare: it checks not only block ID, but block metadata. When a sapling is planted, its metadata changes over time. This also affects:
  • Natural vs harvested leaves
  • Rotations of stairs
  • Wool colors
I'd add that to the turtle.compare wiki page, but I've had my wiki access temporarily revoked.
FF0084 #5
Posted 21 January 2013 - 05:46 AM
That would explain pretty much everything! It's hard to tell though whether it's good or bad news … It poses a threat to mess up everything pretty badly but on the other hand it allows a turtle to tell apart wool blocks with different dyes applied and so on.

This problem really needs some attention. I suggest making a rigid list of vanilla block IDs for which metadata should not be concerned ( saplings, crops, furnaces, crafting tables etc. ). This list then can be hard-coded into ComputerCraft and the problem would be gone.
Eric #6
Posted 21 January 2013 - 05:57 AM
That would explain pretty much everything! It's hard to tell though whether it's good or bad news … It poses a threat to mess up everything pretty badly but on the other hand it allows a turtle to tell apart wool blocks with different dyes applied and so on.

This problem really needs some attention. I suggest making a rigid list of vanilla block IDs for which metadata should not be concerned ( saplings, crops, furnaces, crafting tables etc. ). This list then can be hard-coded into ComputerCraft and the problem would be gone.

The rule should probably be "If I were to middle click this in creative mode, would the item I get match the one in the turtle's selected slot", else there'd be exactly the same problem with mods.
ChunLing #7
Posted 21 January 2013 - 01:38 PM
They are planning to update the compare to use the pick block logic at some point, but it just hasn't been done yet.
Cloudy #8
Posted 21 January 2013 - 09:17 PM
It isn't in yet since I need the forge hook to be updated and no pull request that I've made has been accepted - then a new Minecraft version always came out.

This would be so much easier if Mojang handled the pick block logic server side instead of client side.
theoriginalbit #9
Posted 21 January 2013 - 11:53 PM
This would be so much easier if Mojang handled the pick block logic server side instead of client side.
A lot of things would be easier if Mojang did stuff better!
RedSnt #10
Posted 20 March 2013 - 08:55 AM
Sorry for Resurrecting an old thread, but I was having some very weird bugs with my tree farm script as well, and this definitely explains why it was behaving so oddly.

This really needs fixing.