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

[error] [java] [lua] Mining program not working, can't find error.

Started by DHerls, 06 January 2013 - 05:00 PM
DHerls #1
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.


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.
zajrik #2
Posted 06 January 2013 - 07:40 PM
One of the things I do to help with inventory management is to have your pre-assigned slots among the first numbers of slots, like 1 - 5. This way, anything that is mined is automatically assigned to every slot after those. From there it's easy to determine when the inventory is definitely full of things you'll want to drop off. I've not done anything with maintaining position coordinates in memory and returning to certain positions based on stored positions, so I don't have my turtle return anywhere specific when its inventory is full. I have it turn right, drop a chest and fill it, then turn left and continue in the direction he was going. Since I have my pre-assigned slots started at 1 and ascending, I just use slot 16 as my check.


function checkInv()
	if turtle.getItemCount( 16 ) > 0 then
		dropInv()
	end
end

And the dropInv() code looks something like

function dropInv()
	turtle.turnRight()
	turtle.select( 2 )		 -- I keep chests in slot 2
	turtle.place()
	slot = 16
	for i=1, 11 do			 -- 11 because my first 5 slots are taken up by supplies
		turtle.select( slot )
		turtle.drop()
		slot = slot - 1
	end
	turtle.turnLeft()
end


It's not exactly how I have it in mine because I have different drop codes based on what part of the tunneling task the turtle is currently performing, but that's about the gist of it.

Don't know if this will help you or not. I'm honestly surprised at your code for someone who is new. It's certainly a lot more advanced in some parts than some parts of mine. Like I said, I've yet to tinker with storing a location and navigating back to it other than travelling x blocks straight and being able to return to the exact starting point. That's just two directions.
ChunLing #3
Posted 07 January 2013 - 10:37 PM
You've got an uncontrolled recursion. Probably more than one, but at least one that I spotted fairly quickly. Your function forward() calls checkAll() which calls homeBase() which calls forward(). Maybe you think you have that under control so that the recursion doesn't get out of hand, but clearly there are circumstances where it does (as you say, being trapped and not free to move where it wants seems a likely culprit).

Simple recursion can be a powerful tool, but it should be used only when necessary (and naturally limited). But nothing about the task you describe should require it, so my suggestion is to rewrite your program to use only local functions, which only call previously defined functions.