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

[Question][Lua] Efficient digging through gravel

Started by seanbrockest, 28 May 2013 - 10:32 PM
seanbrockest #1
Posted 29 May 2013 - 12:32 AM
Suggested SubForum "Ask A Pro"
Title [Question][Lua] Efficient digging through gravel

I'm trying to write a new excavation program. I really like the current one as a strip miner, but one thing bugs me. It's really inefficient digging 1 block at a time. Turtles can dig Up, Front, and Down, then move.

If anyone is interested, here's what i have so far

Sorry, the forum seems to have removed my line spacing


function digmove(times)
for i=1,times do
while not turtle.forward() do
turtle.dig()
sleep (0.2)
end
turtle.forward()
turtle.digUp()
turtle.digDown()
end
end
function dopath()
end
function gotolevel()
turtle.digDown()
turtle.down()
turtle.digDown()
turtle.down()
turtle.digDown()
segmentdone=0
end
function segment(segments)
digmove(17)
turtle.turnRight()
moveone()
turtle.turnRight()
digmove(17)
segmentdone=segmentdone+1
if segments>segmentdone then
leftturn()
end
end
function leftturn()
turtle.turnLeft()
moveone()
turtle.turnLeft()
end
function moveone()
while not turtle.forward() do
turtle.dig()
sleep (0.2)
end
turtle.forward()
turtle.digUp()
turtle.digDown()
end
gotolevel()
dotimes=1
while dotimes>segmentdone do
segment(dotimes-segmentdone)
end

(Dont worry, i dont expect anyone to dig through that to find my problem. A lot of the functions are still being debugged)

The problem here is that it's possible for gravel to fall from the top/front to the front slot. If this happens, the turtle skips a point in the digmove loop, so he ends up turning early, and ruining his pattern. To avoid this, i changed his dig/move routine to the following


while not turtle.forward() do
turtle.dig()
sleep (0.2)
end

Then once this while loop ends, he moves. If i've got it right (and i dont think i do), he should be only digging when there's something diggable in front of him, but continuing to dig until there is not.

Have I done this right? Is there a better way?
KaoS #2
Posted 29 May 2013 - 11:51 AM
you have it perfectly, I'm just not so sure about that sleep, is it really necessary?
Exerro #3
Posted 29 May 2013 - 11:52 AM
I usually use something like

while turtle.detect( ) do
    turtle.dig( )
    sleep( 0.5 ) -- you may have to change this according to how long it takes gravel to fall
end
but your way works equally as well
KaoS #4
Posted 29 May 2013 - 11:56 AM
the thing is you have to wait for the gravel to fall before turtle.detect works, as soon as it is falling into the block turtle.forward returns false
Pyro_ #5
Posted 29 May 2013 - 11:59 AM
*snip*

EDIT: Upon rereading, my response was very similar to KaoS
GopherAtl #6
Posted 29 May 2013 - 12:36 PM
awsumben's way requires the sleep, the op's way does not. And if you don't need to sleep, you shouldn't sleep.