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

Branch mining program

Started by GamerNebulae, 30 December 2012 - 06:02 AM
GamerNebulae #1
Posted 30 December 2012 - 07:02 AM
So I was thinking of a branch mining program, but I had no clue how for-loops worked. I went to my dad and we came up with this solution. You can copy the code at the bottom of this post!
First of all: the functions. Don't DRY yourself (Don't Repeat Yourself).

function forward()
if not turtle.forward() then
   turtle.dig()
   turtle.digDown()
   sleep(1)
   turtle.forward()
  end
end

function dig()
  turtle.dig()
  sleep (1)
  turtle.digDown()
  sleep (1)
  turtle.digUp()
  sleep (1)
end

function Hall(len)
  for x = 1, len do
	dig()
	if x % 6 == 0 then
	  turtle.placeDown()
	end
   forward()
  end --Einde for-loop.
end

Okay, now let's explain every paragraph of code. If you just want to copy the code, there is a file attached to this topic.

function forward()
if not turtle.forward() then
   turtle.dig()
   turtle.digDown()
   sleep(1)
   turtle.forward()
  end
end

This paragraph prevents the turtle from being stuck. What it will do is look if the turtle can move forward. If it can, it will skip this code. But if it can't move forward it will do this:
- Check if there is something in front of it.
- If there is it will dig and dig down, to prevent gravel from begin stuck in your tunnels.
- It will sleep for a second, to wait for the gravel to come falling down,
- The turtle will move forward.

function dig()
  turtle.dig()
  sleep (1)
  turtle.digDown()
  sleep (1)
  turtle.digUp()
  sleep (1)
end

This paragraph is obviously to dig the hallway. What it will do is:
- Dig forward and sleep for a second.
- Dig down and sleep for a second.
- Dig up and sleep for a second.

function Hall(len)
  for x = 1, len do
	dig()
	if x % 6 == 0 then
	  turtle.placeDown()
	end
   forward()
  end
end

This paragraph is the for-loop. What it will do is:
- Checks how long the tunnel is gonna be.
- It will dig.
- After 6 blocks, it will place a torch.
- Moves forward.

Okay, now let's create some local variables:


local tArgs = { ... }
local minFuelLevel = 150
local minTorchLevel = 10
steps = tonumber(tArgs[1])
steps = steps or 3

And now for the icing on the cake, the execution:


if turtle.getItemCount(1) < minTorchLevel then
  print ("Not enough Torches!")
elseif turtle.getFuelLevel() < minFuelLevel then
  print ("Not enough fuel!")
else
  print ("Fuel: " .. turtle.getFuelLevel())
  Hall(steps)
  turtle.turnRight()
  Hall(3)
  turtle.turnRight()
  Hall(steps)
  print ("Honey, I'm home!")
end



function forward()
if not turtle.forward() then
   turtle.dig()
   turtle.digDown()
   sleep(1)
   turtle.forward()
  end
end

function dig()
  turtle.dig()
  sleep (1)
  turtle.digDown()
  sleep (1)
  turtle.digUp()
  sleep (1)
end

function Hall(len)
  for x = 1, len do
	dig()
	if x % 6 == 0 then
	  turtle.placeDown()
	end
   forward()
  end
end

local tArgs = { ... }
local minFuelLevel = 150
local minTorchLevel = 10
steps = tonumber(tArgs[1])
steps = steps or 3

if turtle.getItemCount(1) < minTorchLevel then
  print ("Not enough Torches!")
elseif turtle.getFuelLevel() < minFuelLevel then
  print ("Not enough fuel!")
else
  print ("Fuel: " .. turtle.getFuelLevel())
  Hall(steps)
  turtle.turnRight()
  Hall(3)
  turtle.turnRight()
  Hall(steps)
  print ("Honey, I'm home!")
end
ChunLing #2
Posted 30 December 2012 - 07:21 PM
For the forward function, something like:
while not turtle.forward() do turtle.dig() end
turtle.digDown()
would be a pretty standard way of accomplishing basically the same thing, but a tad more robustly.

You don't really need all those sleep calls, unless you're using the turtle.native functions somehow. The turtle functions that take time and have returns pretty much all yield while they operate, once the lua turtle api is loaded. If gravel is falling into the space you're trying to move into, then the move will fail, so the only time you really need the sleep is when you are digging gravel that will not fall into the space you're moving to (such as clearing a gravel fall from above the turtle when it's moving forward).

But overall, pretty solid looking code. If you get into running programs from within other programs using shell, it's good to keep your functions local along with your other variables to avoid scoping issues, but that's probably not an immediate issue with this.