Posted 27 June 2013 - 08:27 PM
So, I'm very new to ComputerCraft, but I really wanted to write my own mining program, rather than copying someone else's, to learn how it all works. I based it off this one here: http://www.computerc...-mining-turtle/ but with a few changes.
I'm more than happy to manually refuel the turtle, but basically what I want is for it to keep going down its tunnel, placing torches every 10 blocks, placing cobble underneath it (so that I can easily walk down and collect stuff later), placing chests behind it and dumping everything in them when its inventory gets full. I want it to return to its starting point when one of the following things happens:
1) it runs out of chests
2) it runs out of torches
3) the fuel level drops below an arbitrary number (I went for 100, but open to suggestions!)
4) there is no block to mine
I'm getting an attempt to call nil at line 77 (calling the digforward() function inside the run() function), have tried a few different things but can't for the life of me work out why! Anyone? The code is here:
Thanks in advance!
I'm more than happy to manually refuel the turtle, but basically what I want is for it to keep going down its tunnel, placing torches every 10 blocks, placing cobble underneath it (so that I can easily walk down and collect stuff later), placing chests behind it and dumping everything in them when its inventory gets full. I want it to return to its starting point when one of the following things happens:
1) it runs out of chests
2) it runs out of torches
3) the fuel level drops below an arbitrary number (I went for 100, but open to suggestions!)
4) there is no block to mine
I'm getting an attempt to call nil at line 77 (calling the digforward() function inside the run() function), have tried a few different things but can't for the life of me work out why! Anyone? The code is here:
Spoiler
local chests = turtle.getItemCount(1)
local torches = turtle.getItemCount(2)
local cobble = turtle.getItemCount(3)
local length = 0
local fuel = turtle.getFuelLevel()
function dump()
if turtle.getItemCount(16) > 0 then
turtle.turnLeft()
turtle.turnLeft()
turtle.select(1)
turtle.place()
for i = 4,16 do
turtle.select(i)
turtle.drop()
turtle.turnLeft()
turtle.turnLeft()
chests = chests - 1
end
function cobble()
if turtle.detectDown() == false then
turtle.select(3)
turtle.place()
end
end
function home()
dump()
turtle.turnLeft()
turtle.turnLeft()
turtle.up()
for i = 1,length,1 do
turtle.forward(i)
end
turtle.down()
if torch == 0 then
print ("No more torches!")
elseif chests == 0 then
print ("No more chests!")
elseif fuel > 100 then
print ("Fuel level low!")
else
print ("Nothing to mine!")
end
function digforward()
if turtle.detect() then
turtle.dig()
end
cobble()
turtle.forward()
turtle.digUp()
dump()
length = length + 1
end
end
elseif turtle.detect() == false then
home()
end
end
function light()
turtle.turnLeft()
turtle.turnLeft()
turtle.select(2)
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
torches = torches - 1
end
function run()
while (torches > 0) and (chests > 0) and (cobble > 0) and (fuel > 100) do
for i = 1,10,1 do
digforward()
end
light()
end
home()
end
run()
Thanks in advance!