Posted 06 January 2013 - 06:00 PM
I am a fairly new computercraft user, but I know how to program different languages. I attempted to write a program that would make a strip-mine for me. I got the majority of it to work, but whenever the turtle runs out of space (which I attempted to account for) it freaks out and either giver me a java.lang.arrayIndexOutOfBoundsException 256 error, or the turtle will just begin to spin uncontrollably.
The console also gives out a line number, but it changes every time with seemingly no pattern so I don't know what to to about that.
So, the setup is: an iron chest in the middle of a 5x5x3 room underground with the turtle on top of the chest facing the direction I would like it to go. The program is supposed to mine out a 3x3 tunnel of any length I choose placing torches every 6 blocks. I have coal in slot 16 and torches in slot 15. In the refuel chest, the bottom row is dedicated to coal while the row above that is dedicated to torches.
When the turtle either runs out of torches, or fills up on inventory space, the turtle is supposed to return to the chest and drop off its stuff and fill up on supplies.
Most of the print() code is attempted debugging. I'm not sure if I am just missing something blatantly obvious for some people or if I''m just getting tunnel vision.
If anyone can help me, it would be much appreciated and also I welcome any suggestions to changes in the code.
The console also gives out a line number, but it changes every time with seemingly no pattern so I don't know what to to about that.
So, the setup is: an iron chest in the middle of a 5x5x3 room underground with the turtle on top of the chest facing the direction I would like it to go. The program is supposed to mine out a 3x3 tunnel of any length I choose placing torches every 6 blocks. I have coal in slot 16 and torches in slot 15. In the refuel chest, the bottom row is dedicated to coal while the row above that is dedicated to torches.
When the turtle either runs out of torches, or fills up on inventory space, the turtle is supposed to return to the chest and drop off its stuff and fill up on supplies.
Most of the print() code is attempted debugging. I'm not sure if I am just missing something blatantly obvious for some people or if I''m just getting tunnel vision.
xpos=0
zpos=2
dir=3
length=read()
function wait()
bRead=true
while(bRead) do
local sEvent, param = os.pullEvent("key")
if (sEvent=="Key") then
break
end
end
end
function forward(x)
print("Moving forwards")
if debug1~=1 then
refuel()
checkAll()
end
if dir==3 then
for i=1,x,1 do
turtle.forward()
xpos=xpos+1
print("X Position is: "..xpos)
end
end
if dir==2 then
for i=1,x,1 do
turtle.forward()
zpos=zpos+1
print("Z Position is: "..zpos)
end
end
if dir==4 then
for i=1,x,1 do
turtle.forward(x)
zpos=zpos-1
print("Z Position is: "..zpos)
end
end
end
function turnLeft(x)
for i=1,x,1 do
print("Turning left")
turtle.turnLeft()
if dir>1 then
dir=dir-1
else
dir=4
end
end
end
function turnRight(x)
for i=1,x,1 do
print("Turning right")
turtle.turnRight()
if dir<4 then
dir=dir+1
else
dir=1
end
end
end
function pannel()
while turtle.detectUp() do
turtle.digUp()
sleep(.5)
end
turtle.digDown()
while turtle.detect() do
turtle.dig()
sleep(.5)
end
forward(1)
while turtle.detectUp() do
turtle.digUp()
sleep(.5)
end
turtle.digDown()
if xpos%6==0 then
turtle.select(15)
turtle.placeDown()
end
while turtle.detect() do
turtle.dig()
sleep(.5)
end
forward(1)
while turtle.detectUp() do
turtle.digUp()
sleep(.5)
end
turtle.digDown()
end
function refuel()
coal = turtle.getFuelLevel()
if coal<=(xpos+(math.abs(zpos))) then
turtle.select(16)
num = turtle.getItemCount(16)-1
if not turtle.refuel(num) then
homeBase(1)
end
end
end
function homeBase(v)
goX=xpos
goZ=zpos
goDir=dir
turn=(dir-1)
turnLeft(turn)
debug1=1
forward(xpos)
debug1=0
if zpos~=2 then
turnRight(zpos)
forward()
turnLeft(zpos)
end
for i=1, 14, 1 do
turtle.select(i)
turtle.drop()
end
if v==1 then
turtle.select(16)
for i=46,54,1 do
if turtle.suckDown(i) then
turtle.refuel()
if turtle.getFuelLevel>(goX+goZ) then
break
end
end
if i==54 then
print("Not enough coal")
wait()
turtle.suckDown(46)
turtle.refuel()
end
end
if v==2 then
turtle.select(15)
for i=37,45,1 do
if turtle.suckDown(i) then
break
end
if i==45 then
print("No torches")
wait()
turtle.suck(37)
end
end
end
if v==3 then
print("Tunnel done!")
shell.exit()
end
turnLeft(2)
forward(goX)
turnLeft(goDir-1)
forward(math.abs(zpos))
end
end
function checkAll()
slot14 = turtle.getItemCount(14)
if slot14>=1 then
homeBase(0)
end
slot15=turtle.getItemCount(15)
if slot15<1 then
homeBase(2)
end
end
function tunnel(x)
forward(2)
turnRight(1)
forward(1)
turnLeft(1)
turtle.dig()
forward(1)
turnLeft(1)
for i=1,x,1 do
pannel()
turnLeft(zpos)
while turtle.detect() do
turtle.dig()
sleep(.5)
end
forward(1)
turnLeft(zpos)
end
homeBase(3)
end
tunnel(length)
If anyone can help me, it would be much appreciated and also I welcome any suggestions to changes in the code.