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

first program, tunnel empty space

Started by blackbetty, 16 February 2015 - 03:33 PM
blackbetty #1
Posted 16 February 2015 - 04:33 PM
this is my first program. i have very little programming experience, mostly html. i want it to dig a 3x3 tunnel. so far it works pretty great. until it encounters an empty block, then it keeps going in that direction messing up its pattern. any suggestions?


local run = 0
term.write("Distance? ")
run = read() / 2
function digAll()
   while turtle.detect() do
	  turtle.dig()
	  sleep(0.5)
   end
end
function digForward()
   digAll()
   turtle.forward()
   turtle.digUp()
   turtle.digDown()
end
function layerA()
   digForward()
   turtle.turnLeft()
   digForward()
   turtle.forward()
   digForward()
   turtle.turnRight()
end
function layerB()
   digForward()
   turtle.turnRight()
   digForward()
   turtle.forward()
   digForward()
   turtle.turnLeft()
end
for i = 1, run do
   layerA()
   layerB()
end

digAll() is for gravel/sand
distance is divided in half because it makes two passes. left to right, then right to left. this is to minimize movement as much as possible to conserve fuel.
Hawthorne #2
Posted 19 February 2015 - 07:26 PM
I am by no means a professional and most of my programming experience has come from computercraft. However, I do have some experience.


Judging from your code, the turtle should start right to left, then left to right. That being said, your turtle. goes extra far in the one direction because you have two sections of your code that tell the turtle to move forward. digForward() has the turtle move forward after digging. When this function returns, you immediatly try to tell the turtle to go forward. Since he cant when there are blocks in the way, this code does nothing until you hit an open space. Thus, function layerA() should read:

digForward()
turtle.turnLeft()
digForward()
digForward()
turtle.turnRight()

layerB() has the same issue.

I hope this helps.
Lyqyd #3
Posted 19 February 2015 - 07:54 PM
Moved to Ask a Pro.