0

I created the following Script for a Mining Turtle:

--Strip mining turtle program test
--Based on stripmining program by Gabriel *******(Spirr0u)


function excarvateRectangle(size,looptimes)
    print("Excavating "..size .. " with looptimes "..looptimes)
    if size > 0 then
        for i=1, looptimes, 1 do
            for z=1, size, 1 do
                turtle.dig()
                sleep(1)
                turtle.forward()
            end
            turtle.turnRight()
        end
        onesmaller = size-1
        excarvateRectangle(onesmaller,2)
    end
end

function goBackToStart(size2)
    print("Going back to "..size2)

    -- go back like a stair with size as the number of turns
    if size2 % 2 == 0 then
        turnAround()
    end

    dir=0
    for z=1, size2, 1 do
        turtle.forward()
        if dir == 0 then
            turtle.turnLeft()
            dir = 1
        else 
            turtle.turnRight()
            dir = 0
        end
    end

   -- turn in the right direction
    if size2 % 2 > 0 then
        turtle.turnRight()
    end
    turtle.turnRight()
end

function digHole(origsize, depth)
    for g=1, depth, 1 do
        size = origsize
        excarvateRectangle(size,3)
        sleep(3)
        goBackToStart(origsize)
        turtle.digDown()
        turtle.down()
    end
    for g=1, depth, 1 do
        turtle.up()
    end
end

function checkFuel()
  while turtle.getFuelLevel() <= 50 do
    turtle.select(15)
    turtle.refuel(1)
    turtle.select(1)
  end
end

function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end

-- MAIN
print("Fuel in slot 15")
print("Input rectangle size:")
origsize = io.read()
origsize = origsize +0

print("Input depth:")
depth = io.read()
depth = depth +0

checkFuel()
digHole(origsize, depth)
print("Finish")

For testing put in 5 for size and 2 for depth. It should excavate 5x5 blocks 2 blocks deep.

What happens: The turtle digs a rectangular area like a spiral. Then it goes back from the center of the spiral to the starting position. Now it goes down by one and starts again 'depth' times.

But when it should return to the starting position the second time, it shows a strange behavior. Instead of going zig zag forth left forth left forth, it stops after forth left forth left, then turns around somehow and ends.

I already assumed it may be the scope and renamed the variables. I added debug output for the variables. All seems to be correct. I can't find out that is going on there. I guess it may be a race condition.

Any help would be appreciated.

Thanks, Boris

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Using local variables will help with scope and variable collision issues and is a better strategy than renaming variables. You said you added debug output to the running. What did you get from it? What do your output statements show? What do you see if you add them in the loops also? – Etan Reisner May 04 '16 at 00:43
  • Also, as a general rule you will get better results if you put the code (preferably in a runnable form) *in* the post instead of linking to somewhere else that people need to click to see. – Etan Reisner May 04 '16 at 00:44
  • `depth = depth +0` would be more clean as `depth = tonumber( depth ) ` or even better `depth = io.read( '*n' )` – DavisDude May 05 '16 at 22:30

0 Answers0