Posted 31 December 2012 - 04:38 AM
Now, from the title, you may be thinking, "Why does he need help with a program to make his turtle go forward?"
Answer:
1) New to LUA, semi-new to coding (beginner to C++, completely new to LUA)
2) This program is designed to make the turtle go x distance in a straight line, trying to avoid mining the blocks directly in front of it and keep track of d items mined.
My problem is that with this code I am getting errors not in the code itself but in its execution. You see, I am trying to make the turtle go over an obstacle, check if it has another obstacle, and if it doesn't, to go down. But when it encounters an obstacle, it repeats the loop of avoiding the obstacle even when the turtle doe snot, in fact, have an obstacle in front of it anymore. Without the obstacle, it runs fine.
I have tried fixing this part of the code to no avail, so here is my code pre-screwing it up to see what works and what doesn't.
Because I'm new both to coding and to Lua, any tips on cleaning up the code would also be appreciated.
Answer:
1) New to LUA, semi-new to coding (beginner to C++, completely new to LUA)
2) This program is designed to make the turtle go x distance in a straight line, trying to avoid mining the blocks directly in front of it and keep track of d items mined.
My problem is that with this code I am getting errors not in the code itself but in its execution. You see, I am trying to make the turtle go over an obstacle, check if it has another obstacle, and if it doesn't, to go down. But when it encounters an obstacle, it repeats the loop of avoiding the obstacle even when the turtle doe snot, in fact, have an obstacle in front of it anymore. Without the obstacle, it runs fine.
I have tried fixing this part of the code to no avail, so here is my code pre-screwing it up to see what works and what doesn't.
Because I'm new both to coding and to Lua, any tips on cleaning up the code would also be appreciated.
x = tonumber(read())
y = turtle.getFuelLevel()
a = turtle.detect()
b = turtle.detectUp()
c = turtle.detectDown()
d = 0
check = 0
function Refuel()
if y > 100 then
print("No fuel required.")
else
print("Refueling...")
z = turtle.refuel()
if z==true then
print("Fueled and ready.")
else
print("Unable to refuel. Check system. ErrF")
end
end
end
function First()
if a==false then
turtle.forward()
check = 0
else
print("Way blocked. Alternate route. Err0")
check = 1
end
end
function Up()
if b==false then
turtle.up()
else
turtle.digUp()
turtle.suckUp()
turtle.up()
d = d + 1
end
end
function Forward()
if a==false then
turtle.forward()
else
turtle.dig()
turtle.suck()
turtle.forward()
end
end
function Down()
if c==false then
turtle.down()
else
if a==false then
turtle.forward()
x = x - 1
if c==false then
turtle.down()
else
print("Obstruction ahead. Taking new path. Err1")
end
else
print("Obstruction ahead. Taking new path. Err2")
end
end
end
Refuel()
while x > 0 do
First()
if check==1 then
Up()
Forward()
x = x - 1
Forward()
x = x - 1
Down()
else
x = x - 1
end
end
print("I have mined " .. d .. " items.")