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

Lua Question Turtle Code

Started by Fireshocksaga, 27 June 2012 - 12:49 AM
Fireshocksaga #1
Posted 27 June 2012 - 02:49 AM
I'm new to this so bare with me.

I'm trying to write a strip mining code so that the turtle digs a 1x2 tunnel 10 blocks deep and then turns around and comes back and dumps the items it mined. It works fine until there's a gap in the surface I'm mining. Now if the gap is an even number of blocks, the code works fine but if it's an odd numbered gap, then my turtle goes extra spaces for some reason. When it breaks the block before the gap, it'll skip breaking the block above and travel two spaces before taking variables into account again. I'm trying to test with 10 blocks but it goes 20 instead if there's nothing in front of it.

Any help is appreciated.


x,y,z=rednet.receive()
print(y)
if y=="go" then
b=0
c=0
if b<11 then
   repeat
   if turtle.detect()==false then
	  b=b+1
		 end
turtle.detect()
turtle.dig()
   if turtle.forward()==true then
	  b=b+1
		 end
turtle.forward()
turtle.digUp()
   sleep(.5)
   if turtle.detectUp()==true then
	  repeat
		 turtle.digUp()
		 sleep(.5)
		    until turtle.detectUp==false()
			   end
   until b>9
turtle.turnRight
turtle.turnRight
if c<10 then
   repeat
	  if turtle.detect()==false then
		 c=c+1
		    end
turtle.forward()
   until c>8
	  end
turtle.drop()
   end
end
Lyqyd #2
Posted 27 June 2012 - 04:50 AM
I haven't any idea what you were originally trying to do with your code. Try this out; let me know if it does what you were trying to accomplish:


x,y,z=rednet.receive()
print(y)
if y=="go" then
    b=0
    c=0
    --removed ifs that will always be true.
    repeat
        --detection stuff removed since it wasn't used for anything.
        turtle.dig()
        if turtle.forward() then
            --We should only increment our movement counters on successful move.
            b=b+1
        end
        turtle.digUp()
        sleep(.5)
        if turtle.detectUp() then
            repeat
                turtle.digUp()
                sleep(.5)
            until not turtle.detectUp()
            --simplified this loop.
        end
    until b>9
    turtle.turnRight
    turtle.turnRight
    repeat
        --the turtle.detect() wasn't doing anything here, so I removed it.
        if turtle.forward() then
            --Again, we should only increment our movement counters on a successful move.
            c=c+1
            --You may wish to add an else case to handle unsuccessful movement.  Otherwise, it will infinite loop here.
        end
    until c>8
    turtle.drop()
end
Fireshocksaga #3
Posted 27 June 2012 - 06:08 AM
Works perfectly. Thank you kind sir.