Posted 09 April 2013 - 05:45 AM
I made a little program tasking a turtle with harvesting my carrot fields, unfortunately i seem to have created a loop which crashes the turtle.
The exact error
The program
It would also be great if u could help me shorten this line:
Thanks for your time.
The exact error
turtle:13: vm error: java.lang.ArrayIndexOutOfBoundsException: 256
The program
x = 1
y = 1
dir = 1
function forward()
if (dir == 3) then
turtle.forward()
x = x - 1
elseif (dir == 1) then
turtle.forward()
x = x + 1
elseif (dir == 2) then
turtle.forward()
y = y + 1
elseif (dir == 4) then
turtle.forward()
y = y - 1
end
harvest()
end
function harvest()
turtle.digDown()
turtle.select(1)
turtle.placeDown()
check()
end
function turnleft()
turtle.turnLeft()
if dir == 4 then
dir = dir - 3
else
dir = dir + 1
end
end
function turn(orientation)
if dir ~= orientation then
repeat
turnleft()
until dir == orientation
end
end
function check()
if ((y == 4) or (y == 8) or (y == 12) or (y == 16)) and (dir == 2) then
forward()
elseif (x == 19) and (dir == 1) then
turn(2)
forward()
elseif (x == 19) and (dir == 2) then
turn(3)
harvest()
elseif (x == 1) and (dir == 3) then
turn(2)
forward()
elseif (x == 1) and (dir == 2) then
turn(1)
harvest()
elseif (y == 20) then
finish()
elseif turtle.getItemCount(15) == 64 then
deload()
else
forward()
end
end
function finish()
print("done")
turn(3)
repeat
turtle.forward()
x = x - 1
until x == -2
turn(4)
repeat
turtle.forward()
y = y - 1
until y == 1
drop()
turn(1)
repeat
turtle.forward()
x = x + 1
until x == 1
os.reboot()
end
function drop()
i = 1
for i = 1,16 do
turtle.select(i)
turtle.dropDown()
end
turtle.select(1)
end
function deload()
ysave = y
xsave = x
dirsave = dir
turn(3)
repeat
turtle.forward()
x = x - 1
until x == -2
turn(4)
repeat
turtle.forward()
y = y - 1
until y == 1
drop()
turn(2)
repeat
turtle.forward()
y = y + 1
until y == ysave
turn(1)
repeat
turtle.forward()
x = x + 1
until x == xsave
turn(dirsave)
forward()
end
harvest()
It would also be great if u could help me shorten this line:
if ((y == 4) or (y == 8) or (y == 12) or (y == 16)) and (dir == 2) then
Thanks for your time.