I wrote a little mining turtle programm by myself but can't figure out whats wrong here. I tried various things. Changing the order of the functions, what i think is the problem, because the error message goes to other lines by changing the order of the functions… I even tried to convert it into java and debug it, but java can't find anything at all.
The code itself should be self explaining, but im ready to answear questions! Hope anyone can help me.
Here is the code
xDir = 1
yDir = 0
w = 0
x = 0
y = 0
z = 0
n = 1
for i=1, 65 do
turtle.digDown()
turtle.down()
z=z-1
end
function dig()
while turtle.detect() do
turtle.dig()
sleep(1)
end
end
function changeDirR()
if xDir == 1 then
xDir = 0
yDir = -1
end
if xDir == -1 then
xDir = 0
yDir = 1
end
if yDir == 1 then
xDir = 1
yDir = 0
end
if yDir == -1 then
xDir = -1
yDir = 0
end
end
function changePos()
if xDir == 1 then
x = x+1
end
if xDir == -1 then
x = x-1
end
if yDir == 1 then
y = y+1
end
if yDir == -1 then
y = y-1
end
end
function turn()
turtle.turnRight()
turtle.turnRight()
end
function goup()
for h1=1, -z do
turtle.digup()
turtle.up()
end
unload()
charge()
returnDiggin()
end
function godown()
for h2=1, -z do
turtle.digdown()
turtle.down()
end
function gox()
for g1=1, x do
dig()
turtle.forward()
end
end
function goy()
for g2=1, y do
dig()
turtle.forward()
end
end
function gonegx()
for g3=1, -x do
dig()
turtle.forward()
end
end
function gonegy()
for g4=1, -y do
dig()
turtle.forward()
end
end
function goToCorner()
for bn=1, w do
turtle.forward()
end
turn()
routine()
end
function routine()
while turtle.getItemCount(16) < 1 do
w = 0
for p=1, 2 do
for u=1, n do
while turtle.detect() do
turtle.dig()
sleep(1)
end
turtle.forward()
changePos()
w = w+1
if enoughFuel() == true then
for s=1, 12 do
turtle.digUp()
turtle.up()
end
for t=1, 12, 1 do
turtle.digDown()
turtle.down()
end
else
returnToSurface()
end
end
turtle.turnRight()
changeDirR()
end
n=n+1
end
returnToSurface()
end
function returntosurface()
if xdir==1 then
turtle.turnRight()
goy()
if x<=0 then
turtle.turnLeft()
gonegx()
else
turtle.turnright()
gox()
turn()
end
end
if xdir==-1 then
turtle.turnRight()
gonegy()
if x<=0 then
turtle.turnRight()
gonegx()
else
turtle.turnLeft()
gox()
turn()
end
end
if ydir==1 then
turtle.turnRight()
gonegx()
if y<=0 then
turtle.turnLeft()
gonegy()
turtle.turnright()
else
turtle.turnright()
goy()
turtle.turnleft()
end
end
if ydir==-1 then
turtle.turnRight()
gox()
if y<=0 then
turtle.turnRight()
gonegy()
turtle.turnright()
else
turtle.turnleft()
goy()
turtle.turnleft()
end
end
goup()
end
function enoughFuel()
if xDir == 1 or xDir == -1 then
if x >= 0 then
if turtle.getFuelLevel() > x+y-z+30 then
return 1
end
end
if x < 0 then
if turtle.getFuelLevel() > -x+y-z+30 then
return 1
end
end
end
if yDir == 1 or yDir == -1 then
if y >= 0 then
if turtle.getFuelLevel() > x+y-z+30 then
return 1
end
end
if y < 0 then
if turtle.getFuelLevel() > x-y-z+30 then
return 1
end
end
end
return nil
end
function unload()
for q=1, 16 do
local itemCount = turtle.getItemCount(q)
if itemCount > 0 then
turtle.select(q)
turtle.drop()
end
end
turtle.select(1)
end
function returnDiggin()
godown()
if yDir ~= 0 then
if x >= 0 then
gox()
if y > 0 then
turtle.turnLeft()
goy()
end
if y <= 0 then
turtle.turnRight()
gonegy()
turn()
end
end
if x < 0 then
turn()
gonegx()
if y >= 0 then
turtle.turnRight()
goy()
turn()
end
if y < 0 then
turtle.turnLeft()
gonegy()
end
end
end
if xDir ~= 0 then
if y >= 0 then
turtle.turnLeft()
goy()
if x >= 0 then
turtle.turnRight()
gox()
turn()
end
if x < 0 then
turtle.turnLeft()
gonegx()
end
end
if y < 0 then
turtle.turnRigt()
gonegy()
if x > 0 then
turtle.turnLeft()
gox()
end
if x < 0 then
turtle.turnRight()
gonegx()
end
turn()
end
end
end
goToCorner()
end
function charge()
print ("Out of Fuel!")
end
routine()