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

Odd turtle behavior

Started by lecrouch, 10 March 2016 - 01:52 AM
lecrouch #1
Posted 10 March 2016 - 02:52 AM
Hey guys,
So I just found a bug in my mining program and I am trying to figure it out!

In a nutshell, the program takes on parameter, a number, and it mines in a straight line for that distance one row at a time until it detects bedrock. However for some reason apparently at random it seems that my turtles lose track of where they are and turn around unexpectedly and continue on their row in the wrong direction.

I recently changed some of the location updates in my move forward/up/down functions in hopes of a fix, but that didn't seem to help. Since I don't think it can be an environmental problem, I'm assuming my location system has a bug.

If anyone has thoughts, let me know!

obviously feel free to run the program use it etc..

http://pastebin.com/XQaq8neb
Edited on 10 March 2016 - 01:54 AM
subzero22 #2
Posted 10 March 2016 - 03:57 AM
I'm not an expert to lua but I'm wondering what the point of newRow = true/false is as it seems to do nothing and the only time you use it is in the same while true loop at the end so it would just be constantly turning on/off. I just can't seem to see where its used anywhere else.

While testing your script to get the program to do what yours did you might also want it to check for bedrock in front of it also as during one of the lines there wasn't any bedrock under it but got stuck by a peice of bedrock in front of it. Remember not all worlds have flat bedrock.

Sorry I couldnt't help you with your problem though but I'll still test more to see if it does it.
lecrouch #3
Posted 10 March 2016 - 05:13 AM
Hah, good catch on both accounts. I've actually not run across the bedrock problem, but that was probably just luck on my behalf. I'll add another check for bedrock in the moveForward() function. As for the newRow, I have no idea what I was thinking. I probably had plans to use it in some way, but obviously I didn't because it does nothing but flip itself over and over.

I revised the code with newRow commented out, and added a new check for bedrock in front of the turtle. I didn't have time to run it and make sure I didn't break it, but it should be fine.

Thanks!
Bomb Bloke #4
Posted 10 March 2016 - 05:57 AM
Just eyeballing the code, I'm noticing that although you set a flag when going off to wait for fuel, you don't do it when emptying your inventory. Hence if the turtle runs into any walls / mobs while doing the latter it'll notice its inventory is full again and start recursively calling returnAndUnload(); for each time this happens, it'll redundantly travel to base and back from the position where the collision occurred. If it happens too often it may even crash due to a stack overflow.

There are a few bits of redundant code floating around, but this is the one that bugs me the most:

    for i = 1, tonumber(programParam[1]) do
        if i  < tonumber(programParam[1]) then
            moveForward()
        end
    end

It could be written as:

    for i = 1, tonumber(programParam[1]) - 1 do moveForward() end
lecrouch #5
Posted 10 March 2016 - 09:18 PM
Thanks BB, you are right, I wrote a large amount of this code awhile back and didn't do a great job with some bits of it.

I'll make some changes and update it soon.
Edited on 10 March 2016 - 08:19 PM
lecrouch #6
Posted 11 March 2016 - 07:38 PM
So, since by all accounts that original code had been changed and updated so many times that it was sloppy as $%&amp;.

Because of it's crappyness I decided it was high time that I do a revision, so I rewrote/copied it last night, I haven't tested it yet, but I think it'll be much smoother. To that end, I would like some advice. BB mentioned that (my interpretation was) that while the turtle is going back to the start for fuel or because of a full inventory it should not be able to re-test it's inventory. My thought to fix this issue would be to create a new bool value, and when flipped (for above reasons) make it so that the checkInvSpace() function just returns false and not be able to constantly re-check anything.

Would this take care of the issue? If not, what am I missing?

Thanks fellas!

http://pastebin.com/aWUF8zxJ
Edited on 11 March 2016 - 06:48 PM
lecrouch #7
Posted 13 March 2016 - 09:12 PM
Update, with the revisions from BB, turtles don't appear to be going a-wall anymore.

However, A new issue has arrived.
getting this error:
…:23:vm error:java.lang.ArrayIndexOutOfBoundsException

Question is, isn't that normally from a recursive call?
Not seeing anything like that on line 23, and on top of that, it has only happened a couple of times, and is by no means consistent.

Code has been updated and is now back to the original file:
http://pastebin.com/XQaq8neb
Bomb Bloke #8
Posted 13 March 2016 - 09:54 PM
Question is, isn't that normally from a recursive call?

Sort of; but any function call increases the number of functions in the stack (which then decreases as functions return). The stack naturally grows and shrinks, so if you make a few non-recursive calls in the middle of a recursive loop, then the stack may overflow at a point where the recursion isn't obvious.

It seems that you've gone and removed the flag you were setting to prevent the turtle from noticing it's low on fuel and calling goWaitForFuel() when it's already in the process of going back to get fuel.
lecrouch #9
Posted 13 March 2016 - 10:03 PM
Hah, so simple and so completely under my nose. I bet that was it. I'll update and test soon. Cheers