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

Tunnel help [Gravel]

Started by Mikev619619, 29 December 2012 - 11:20 AM
Mikev619619 #1
Posted 29 December 2012 - 12:20 PM
So i have my code working the way i want it but if i run into gravel it gets funny. I just cant find a way to make it so if i cant move forward dig until it can..
Any way to solve?
Thanks Mike


function Torch()
  turtle.turnRight();
  turtle.dig();
  turtle.select(3);
  turtle.place();
  turtle.turnLeft();
end
function Dig()
  turtle.dig();
  turtle.forward();
  turtle.digUp();
  if turtle.getFuelLevel() < 2000 then
  turtle.refuel(1)
  end
end
function Turn()
  turtle.turnRight();
  turtle.dig();
  turtle.select(2);
  turtle.place();
  for i = 4,16 do
  turtle.select(i)
  turtle.drop()
  end
  turtle.select(2);
  turtle.dig();
  turtle.select(1)
  turtle.forward();
  turtle.digUp();
  turtle.dig();
  turtle.forward();
  turtle.digUp();
  turtle.turnRight();
end
function TurnLeft()
  turtle.turnLeft();
  turtle.dig();
  turtle.select(2);
  turtle.place();
  for i = 4,16 do
  turtle.select(i)
  turtle.drop()
  end
  turtle.select(2);
  turtle.dig();
  turtle.select(1)
  turtle.forward();
  turtle.digUp();
  turtle.dig();
  turtle.forward();
  turtle.digUp();
  turtle.turnLeft();
end
function Go()
for i=1,8 do
  Dig(i)
end
Torch()
end
function Go2()
for i=1,8 do
Go(i)
end
end
function Program()
Go2()
Turn()
Go2()
TurnLeft()
end
function LoopProgram()
local a = 1
repeat
  Program(a)
  a = a + 1
until a == 5
end
LoopProgram()

inside the turtle the first slot is fuel, second enderchest, third torch
OmegaVest #2
Posted 29 December 2012 - 12:28 PM
I'm gonna give you a pice of advice and let you work out how you want this to work in your program.

turtle.forward and turtle.dig both return true or false when they fire. If they move or dig, they return true.

This can allow you to use them in conditionals, like so:
Spoiler

while not turtle.forward() do
   turtle.dig()
   sleep(1.0)
end
Mikev619619 #3
Posted 29 December 2012 - 01:40 PM
ive have tried that.. Unless i did it wrong. and still cant seem to get it to work i was thinking of using


if turtle.compare() == true then
sleep(1.00)
turtle.dig()
end

but that dosen't work either.
ChunLing #4
Posted 29 December 2012 - 04:30 PM
You did it wrong. Put the code in your function Dig, replacing turtle.dig() and turtle.forward(). Like so:
function Dig()
    while not turtle.forward() do
        turtle.dig()
    end
    turtle.digUp()
    if turtle.getFuelLevel() < 2000 then
        turtle.refuel(1)
    end
end
The "while not turtle.forward() do" means that the turtle will call the turtle.forward function to get the result, then invert that to control the loop. If turtle.forward returns false, then the loop is executed and turtle.forward is called again to decide whether the loop needs to execute again. The loop will continue until turtle.forward returns true, which means that the turtle moved forward, meaning that there was nothing in it's way. You do not need the sleep in this case because the turtle isn't fast enough to beat falling gravel anyway.
Mikev619619 #5
Posted 29 December 2012 - 05:16 PM
Thank you so much! :)/> , i actually tried that before but… I forgot to get rid of the turtle.forward after the end above the turtle.dig().