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

[Lua] [Question] "Forward" program

Started by Finnsmyth, 31 December 2012 - 03:38 AM
Finnsmyth #1
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.


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.")
zekesonxx #2
Posted 31 December 2012 - 05:22 AM
y, a, b, and c are not constantly updated. Instead of calling y/a/b/c call what you're defining them as. So replace any use of the 'a' variable with 'turtle.detect()'.
remiX #3
Posted 31 December 2012 - 05:54 AM
Yeah, put

y = turtle.getFuelLevel()
a = turtle.detect()
b = turtle.detectUp()
c = turtle.detectDown()

Into the while loop


while x > 0 do
  y = turtle.getFuelLevel()
  a = turtle.detect()
  b = turtle.detectUp()
  c = turtle.detectDown()
  First()
  if check==1 then 
    Up() 
    Forward() 
    x = x - 1
    Forward()  
    x = x - 1  
    Down()  
  else  
	 x = x - 1
  end
end 
Finnsmyth #4
Posted 31 December 2012 - 10:45 AM
y, a, b, and c are not constantly updated. Instead of calling y/a/b/c call what you're defining them as. So replace any use of the 'a' variable with 'turtle.detect()'.

Thanks, that worked. Any futher debugging I can do myself. Thanks, again. :)/>