Jump to content




[Question][Lua] Efficient digging through gravel


  • You cannot reply to this topic
5 replies to this topic

#1 seanbrockest

  • New Members
  • 1 posts

Posted 28 May 2013 - 11:32 PM

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?

#2 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 29 May 2013 - 10:51 AM

you have it perfectly, I'm just not so sure about that sleep, is it really necessary?

#3 Exerro

  • Members
  • 801 posts

Posted 29 May 2013 - 10: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

#4 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 29 May 2013 - 10: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

#5 Pyro_

  • Members
  • 39 posts
  • LocationAustralia

Posted 29 May 2013 - 10:59 AM

*snip*

EDIT: Upon rereading, my response was very similar to KaoS

#6 GopherAtl

  • Members
  • 888 posts

Posted 29 May 2013 - 11:36 AM

awsumben's way requires the sleep, the op's way does not. And if you don't need to sleep, you shouldn't sleep.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users