This is my first computercraft program that I have written. Although I have experience writing C++ and C# applications, this is also my first attempt at a LUA program. If you have any constructive suggestions or notice any issues with my program, please share them with me.

This program digs a tunnel allowing the user to customize the width, height and length of the tunnel. It will also place torches at an interval of your choosing. The couple of tunneling programs that I downloaded were slightly inefficient. The programs I used would use the center of the tunnel as the origin and would take unnecessary movements back tracking in areas that were already cleared away.

This mining tunnel program uses the top left block as its origin. Meaning, it will begin at the top left and dig down and to the right. The mining program assumes that torches are placed in item slot 1. Fuel may be placed in any item slot.

I currently do not detect for falling blocks (such as sand or gravel) and I do not check to see if the turtle was able to move forward (unbreakable block or player in the way).

I also noticed Advanced Turtle this morning -AFTER- I wrote this program. So, I am probably going to give it a try as it does exactly what I was looking for. But, it was a good learning experience for LUA syntax.

Spoiler

sizeX = 1
sizeY = 1
sizeZ = 1
posX = 1
posY = 1
posZ = 0
torchSpace = 0
torchCount = 0
reverse = false
finished = false
function refreshDisplay()
shell.run("clear")
print("")
print("Tunnel Size:   " .. sizeX .. "x" .. sizeY)
print("Target Depth:  " .. sizeZ)
if torchSpace > 0 then
  print("Torch Spacing: " .. torchSpace)
else
  print("Torch Spacing: n/a")
end
print("")
print("Current Depth: " .. posZ)
print("Fuel Level:	" .. turtle.getFuelLevel())
print("Torch Count:   " .. turtle.getItemCount(1))
print("")
print("Progress:	  " .. getProgress() .. "%")
end
function round(n)
if n >= 0 then
  return math.floor(n + .5)
else
  return math.ceil(ns - .5)
end
end
function getProgress()
if finished then
  return 100
elseif posZ <= 0 then
  return 0
else
  return round(((posZ - 1) / sizeZ) * 100)
end
end
function init()
shell.run("clear")
-- Width
print("")
io.write("How wide do you want the tunnel? ")
sizeX = io.read()
if tonumber(sizeX) ~= nil then
  sizeX = tonumber(sizeX)
else
  print("")
  print("You have entered an invalid width.")
  print("Terminating")
  return false
end
-- Height
print("")
io.write("How tall do you want the tunnel? ")
sizeY = io.read()
if tonumber(sizeY) ~= nil then
  sizeY = tonumber(sizeY)
else
  print("")
  print("You have entered an invalid height.")
  print("Terminating")
  return false
end
-- Depth
print("")
io.write("How deep do you want the tunnel? ")
sizeZ = io.read()
if tonumber(sizeZ) ~= nil then
  sizeZ = tonumber(sizeZ)
else
  print("")
  print("You have entered an invalid depth.")
  print("Terminating")
  return false
end
-- Torch Spacing
print("")
io.write("How often would you like to place torches? ")
torchSpace = io.read()
if tonumber(torchSpace) ~= nil then
  torchSpace = tonumber(torchSpace)
  torchCount = 1
else
  torchSpace = 0
end
return true
end
function refuel(turns)
if turtle.getFuelLevel() < turns then
  for p = 1, 16, 1 do
   if turtle.getItemCount(p) > 0 then
	turtle.select(p)
	if turtle.refuel(1) then
	 break
	end
   end
  end
end
end
function returnHome()
if posY > 1 then
  refuel(posY - 1)
  for p = posY - 1, 1, -1 do
   if turtle.detectUp() then
	turtle.digUp()
   end
   turtle.up()
  end
end
if posX > 1 then
  refuel(posX - 1)
  turtle.turnLeft()
  for p = posX - 1, 1, -1 do
   if turtle.detect() then
	turtle.dig()
   end
   turtle.forward()
  end
  turtle.turnRight()
end
for p = posZ, 1, -1 do
  turtle.back()
end
end
function shiftPos()
refuel(1)
if posX == 1 and sizeX > 1 then
  turtle.turnRight()
elseif posX == sizeX and sizeX > 1 then
  turtle.turnLeft()
end
if posZ < sizeZ or (posX > 1 and posX < sizeX) or (posX == 1 and not reverse and sizeX > 1) or (posX == sizeX and reverse and sizeX > 1) then
  if turtle.detect() then
   turtle.dig()
  end
  turtle.forward()
  if reverse then
   posX = posX - 1
   if posX == 0 then
	posX = 1
	posZ = posZ + 1
	if torchSpace > 0 and torchCount == torchSpace then
	 torchCount = 1
	elseif torchSpace > 0 then
	 torchCount = torchCount + 1
	end
	reverse = false
   end
  else
   posX = posX + 1
   if posX > sizeX then
	posX = sizeX
	posZ = posZ + 1
	if torchSpace > 0 and torchCount == torchSpace then
	 torchCount = 1
	elseif torchSpace > 0 then
	 torchCount = torchCount + 1
	end
	reverse = true
   end
  end
end
end
function sliver()
if posY == 1 then
  refuel(sizeY - posY)
  while posY < sizeY do
   if turtle.detectDown() then
	turtle.digDown()
   end
   turtle.down()
   posY = posY + 1
  end
  shiftPos()
elseif posY == sizeY then
  refuel(posY - 1)
  while posY > 1 do
   if turtle.detectUp() then
	turtle.digUp()
   end
   turtle.up()
   posY = posY - 1
   if torchCount == 1 then
	if posY == sizeY - 1 and ((not reverse and posX == 2) or (reverse and posX == 1)) then
	 turtle.select(1)
	 turtle.placeDown()
	end
   end
  end

  shiftPos()
else
  print("Unexpected position (X=" .. posX .. ", Y=" .. posY .. ", Z=" .. posZ .. ")")
  returnHome()
end
end
function slice()
for p = 1, sizeX, 1 do
  sliver()
  refreshDisplay()
end
end
if init() then
refuel(1)
refreshDisplay()
if turtle.detect() then
  turtle.dig()
end
turtle.forward()
posZ = 1
refreshDisplay()
for p = 1, sizeZ, 1 do
  slice()
end
finished = true
refreshDisplay()
returnHome()
end