Jump to content




first program, tunnel empty space



2 replies to this topic

#1 blackbetty

  • Members
  • 4 posts

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.

#2 Hawthorne

  • New Members
  • 1 posts

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.

#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 February 2015 - 07:54 PM

Moved to Ask a Pro.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users