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

Help needed with a strip mining turtle program

Started by ExCreationS, 29 December 2012 - 01:20 PM
ExCreationS #1
Posted 29 December 2012 - 02:20 PM
Hello,

I am posting because I have written a script which a turtle mines a 2x1 strip mine for a user defined length, places torches every 10 blocks and returns when he is finished. All is well apart from I have one problem. When the turtle comes up against some gravel, he mines it fine, however when it comes to placing the next torch he places it prematurely and doesnt move forward the correct number of blocks after her comes up against gravel. When it comes to the turtle moving back down the mine to his starting position, he will stop further back than where he started because he didnt move forward enough after he encountered gravel. I have included my code below, if you know a way to improve it and solve this problem, I will be eternally frateful.

Thanks for any help,

Luke.


-- Strip mining turtle program.
-- Luke *********, Ryszard ********. 29/12/12
--[[fuel must be placed in slot 15,
torches must be placed in slot 16.]]
-- Create the function for refueling
function checkFuel()
  if turtle.getFuelLevel() <= 10 then
	if turtle.getItemCount(15) == 0 then
   print("Out of fuel.")
   exit()
else
   turtle.select(15)
	  turtle.refuel(1)
	  turtle.select(1)
end --if
  end --if
end --checkFuel()
-- Create the turnAround function
function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end --turnAround()
-- Digs the tunnel for the given length
function tunnel(givenLength)
  local torchDistance = 0
  for index = 1,givenLength do
	if turtle.detect() then
   repeat
		turtle.dig()
  sleep(0.25)
	  until turtle.detect() == false
else
   turtle.forward()
end --if
torchDistance = torchDistance + 1

if turtle.detectUp() then
   repeat
	 turtle.digUp()
  sleep(0.25)
   until turtle.detectUp() == false
end --if

-- attempts to place a block below
	turtle.select(1)
	turtle.placeDown()
-- Places a torch every 10 blocks
	if torchDistance == 10 then
	  print("Placing torch...")
   turtle.select(16)
	  turnAround()
	  turtle.place()
	  turnAround()
	  torchDistance = 0
	  checkFuel()
	end --if
  end --for
-- Sends the turtle back to the start
	turtle.up()
turnAround()
	for index = 1,givenLength do
	  checkFuel()
   if not turtle.detect() then
  turtle.forward()
	  else
	 repeat
	turtle.dig()
	   turtle.sleep(0.25)
  until turtle.detect() == false
  turtle.forward()
	  end --if
	end --for
	turtle.down()
turnAround()
end --tunnel()
-- Main script
print("Input tunnel length:")
local length = read()
print("starting excavation...")
checkFuel()
tunnel(length)
print("The tunnel has been excavated!")
Vogon_Jeltz #2
Posted 08 January 2013 - 09:26 AM
Your indentation is a little odd in places, if you fix that, it'll be much easier to read.

The bit of code that I think is causing your gravel problems is

if turtle.detect() then
   repeat
			    turtle.dig()
  sleep(0.25)
		  until turtle.detect() == false
else
   turtle.forward()
end --if

Try this instead


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

And then try changing this

if turtle.detectUp() then
   repeat
		 turtle.digUp()
  sleep(0.25)
   until turtle.detectUp() == false
end --if

to this

while turtle.detectUp() do
    turtle.digUp()
    sleep(0.25)
end

That should do the same thing. If the turtle is unable to move forward, it will dig, and it will continue to dig until it can move forward. This is a nicer solution for gravel as you don't have to waste time using sleep(0.25). Using while loops is better than a repeat nested inside an if for this.
RunasSudo-AWOLindefinitely #3
Posted 09 January 2013 - 02:24 PM
This belongs in the Ask a Pro section. I'll ask a mod to move it.
AfterLifeLochie #4
Posted 09 January 2013 - 02:27 PM
Moved to Ask A Pro.