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