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).
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.
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.
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.
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:
And now for the icing on the cake, the execution:
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