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

Turtle anti-gravel loop not working

Started by shiskebab, 26 April 2014 - 07:03 PM
shiskebab #1
Posted 26 April 2014 - 09:03 PM
Hi! i have a question for someone more well versed in CC than i am. I created a short turtle program to branch by itself.
It Works perfectly except for when it encounters gravel, so i searched the interwebs for answers and got to this page:

http://www.computerc...mining-program/

no matter what i do, the moment the program finds it way down to the loop it loops forever! The turtle endlessly mines forward.

tiny chunk responsible:

[ for i=1,10 do
turtle.dig()
turtle.forward()
if turtle.forward() == false then
	   repeat
	   turtle.dig()
	   sleep(0.25)  
	   until turtle.forward() == true
	 end  
turtle.digUp()
end

the entire code:

local tArgs = {...}


local function tunnel()
	 turtle.refuel(1)
	 turtle.forward()
turtle.turnLeft()
for i=1,10 do
turtle.dig()
turtle.forward()
if turtle.forward() == false then
	   repeat
	   turtle.dig()
	   sleep(0.25)  
	   until turtle.forward() == true
	 end  
turtle.digUp()
end
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnRight()
for i=1,4 do
turtle.dig()
turtle.forward()
if turtle.forward() == false then
	   repeat
	   turtle.dig()
	   sleep(0.25)  
	   until turtle.forward() == true
	 end  
turtle.digUp()
end
turtle.turnRight()
turtle.select(2)
	 turtle.place()
turtle.turnLeft()
for i=1,5 do
turtle.dig()
turtle.forward()
if turtle.forward() == false then
	   repeat
	   turtle.dig()
	   sleep(0.25)  
	   until turtle.forward() == true
	 end  
turtle.digUp()
end
turtle.refuel(1)
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
end

for i=1,tArgs[1] do
   tunnel()
end

any sort of constructive critisism is appreciated!
KingofGamesYami #2
Posted 27 April 2014 - 04:14 AM
You can use boolean statements directly, like this

if not turtle.forward() then --#if turtle.forward ~= true
 --#do this
end
also, it is nice if you indent your code and add comments

if (statement) then
 repeat
  print("hi") --# this is a comment.  The "--" will tell computercraft that this is not code, the "#" is for the text highlighting on the forums.
  x = x + 1
 until x > 2
end
This is another way of doing the loop, I don't know if it will fix the problem though

while true do
 if not turtle.forward() then
  turtle.dig()
  sleep(0.25)
 else
  break
 end
end
Bomb Bloke #3
Posted 27 April 2014 - 08:05 AM
This is another way of doing the loop, I don't know if it will fix the problem though

That should indeed work, so long as you don't attempt to have the turtle try to go forward before running it. It could be condensed a bit though:

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

The reason for the original problem is that calling "turtle.forward()" attempts to have the turtle move forward, and it returns whether that result was successful or not. The original code block hence operates like this:

for i=1,10 do
turtle.dig()
turtle.forward()                            -- Attempt to move forward, ignore the result. (This is the line to delete!)
if turtle.forward() == false then           -- Attempt to move forward again, and if it fails...
           repeat                           -- Do this stuff...
           turtle.dig()                     -- ...
           sleep(0.25)                      -- ...
           until turtle.forward() == true   -- ... until an attempt succeeds.
         end  
turtle.digUp()
end

Hence the turtle will generally go forward twice for every one movement you want it to make. You've got the same issue throughout the rest of your script.
shiskebab #4
Posted 27 April 2014 - 08:23 AM
Thank you both so much! was actually kinda nervous posting for the first time :rolleyes:/> . im going to delete the "turtle.forward()" and see what happens :D/>