Mining turtle excavate issue - turn right forever loop

Started by faintvirus, Dec 14, 2020, 11:49 PM

Previous topic - Next topic

faintvirus

Hello!

I am new to Computercraft and Lua, but I have read a lot on "How to Lua", read several posts on different websites, forums, and guides. I have also looked into the in-built excavate function and looked at other people's codes to understand different parts and concepts.

I have been trying since friday to create a program that can:

- Assign x, z, y
- Store as xPos, zPos, yPos for returning to working position
- return to origin for fueling purposes (in case I cannot find it...)
- read position and direction to know when to go up, down, turn left/right...
- Use an ender chest (ender storage) to store items (which in turn is entered into an ME system)
- Condensing the inventory if all slots are taken (not currently in use, but is done)

This is the result: https://pastebin.com/jYvvBHUr

Separate functions have been tested and returned the correct output/expected result. The problem occurs when initiating the "MAIN" program. Upon entering a value (ex. mine 10 10 10) the turtle initiates a lovely pirouette forever-loop...

Here is what confuses me the most:

==MAIN==

while not done and isStuck() do -- stuck = false

- calculate current fuel level - 1
- calculate necessary fuel amount

-- if current < necessary, enter while loop for refueling
=> even when the turtle has 0 fuel, it never enters this loop.

if I assign xDir = 0 originally, the turtle spins. If I assign xDir = 1 it skips the entire "MAIN" section, runs the storeItems() function and prints the final message:
     "Mining process - completed."

I assume there is a while loop that is too strict, telling the robot to turn right when direction xDir == x and then never updating the xDir to another value, but I am struggling to find where it happens..

I am also new to the forum, so I would also love some help on how I should structure my questions to make this as efficient as possible for all of us :)

Best Regards
Faintvirus


Lupus590

Line 243 you have:

for x = n or 1, -1 do
Unless n is less than or equal to -1, this loop never ends.

You have the same on line 256.

If you want your loop to count down you need to specify the third parameter of the for loop and that parameter must be a negative number.

faintvirus

Hi!

Thank you for your reply. That makes sense -- there is technically no reason for me to have a for loop in the left and right function as the function is called each time the direction does not correspond to the path it will take. I have therefore changed this to

local function left()
        turtle.turnLeft()
        xDir, zDir = -zDir, xDir
end --function

local function right()
        turtle.turnRight()
        xDir, zDir = zDir, -xDir
end --function

I have fixed the issue where it never enters the main part of the loop. I forgot to add not in front of "While not done and *not* isStuck() do". Now the turtle enters the first part of the code.

Now, what I struggle to see is how xDir and zDir is affected by my code... If I write
    local xDir, zDir = 0,1

the turtle moves forwards forever.

    local xDir, zDir = 1,1

the turtle moves forwards two steps, digs down, moves forwards two steps....

I will spend a few hours on this to understand what is going on, but any help or tips would be greatly appreciated :)

Best Regards
Faintvirus




faintvirus

UPDATE

the code has been updated. Each individual part now works except for the final main part of the code. Upon having fuel for moving, it runs in a straight line forever. Not sure what to do about it, but will check it out as soon as I have time.

Best regards
Faintvirus

faintvirus

UPDATE 2

I have updated the code again. Did some changes with the fuel part - I have one ender chest for storing items and one for grabbing lava buckets/fuel. The first and second slot will now be occupied by two chests.

One issue that I should have discovered before was a lack of directional change. I never asserted when the turtle was supposed to change direction. I have added a function for this based on 360 degree calculations:

local function dirTurn(r)
    local r = math.floor(r/90)*90
    while r >= 360 do r = r-360 end
    while r < 0 do r = r + 360 end

    local x = r-direction -- x negative of r -- 90dg from north is -90x => turn left
    while x < 0 do x = x + 360 end
    if x == 90 then right()
    elseif x == 180 then
        if r == 0 then right(2)
        else left(2) end
    elseif x == 270 then left()
    end --if/else
end --function

the left and right function now update the direction by -90/+90, and the main function calls this function with the value dirTurn(0) and dirTurn(180) when the zDir == 1/-1.

I am still lacking the part where the xDir and xPos is being interpreted. Will be working on this now.

Best Regards
Faintvirus