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

[Lua][Error] Need help with Mining Turtle Program

Started by drawingkid1313, 13 December 2012 - 04:28 PM
drawingkid1313 #1
Posted 13 December 2012 - 05:28 PM
The program I'm trying to write is a quarry-type program. I'm trying to make it with as few lines as possible. This is my first program.
I think the problem is because I'm trying to use a while statement inside of an if-then statement, but I'm not exactly sure.
This is a quick video I made to show what happens when the program runs that way you don't have to try it yourself: [media]http://www.youtube.com/watch?v=3FN99-zfmpw[/media]
(I apologize for the ads on it, I tried to disable it, but YouTube put it on there automatically.
This is the pastebin code, if you want to fool around with it in game: http://pastebin.com/UN18uSna

and finally, here's the code:

local x, y, z
local width, length, depth = 0, 0, 0
local xPos, yPos, zPos = 0, 0, 0
function digLevel(w, l, d)
x=0
y=0
z=0
turtle.down()
while z < tonumber(d) do
  while x < tonumber(w) do
   if y > 0 then
	 while y< tonumber(l) do
	  digOn()
	  y = y + 1
	 end
   else
	 while y>0 do
	   digOn()
	   y=y-1
	 end
   end
	 newRow(y)
	 x=x+1
	end
	goDown(x,y)
	x=0
	y=0
   end
   end
function newRow(yy)
turtle.digDown()
if yy>1 then
  turtle.turnRight()
else
  turtle.turnLeft()
end
turtle.forward()
turtle.digDown()
if yy>1 then
  turtle.turnRight()
else
turtle.turnLeft()
end
end
function digOn()
turtle.digDown()
turtle.forward()
end
function goDown(xx,yy)
if yy>1 then
  turtle.turnRight()
  while yy > 0 do
   turtle.forward()
   yy=yy-1
  end
  turtle.turnLeft()
else
  turtle.turnRight()
end

while xx > 0 do
  turtle.forward()
  xx = xx - 1
end
turtle.down()
turtle.digDown()
turtle.turnRight()
end
term.write("What is the width of your quarry? ")
width = read()
term.write("What is the length of your quarry? ")
length = read()
term.write("How deep? ")
depth = read()
digLevel(width, length, depth)
Dlcruz129 #2
Posted 13 December 2012 - 06:48 PM
Well 1 error I saw was you defined digOn after you called it. Lua runs code in order. Please also post the error you are getting.
Luanub #3
Posted 13 December 2012 - 10:27 PM
Please also post the error you are getting.

Its a logic issue not a Lua error so he will not have an error to post. As you can see in the video the script runs, its just doesn't do what he wants it to.

To the OP:
You've got if statements that are using the wrong logic as well as some extra moves in there somewhere. You need to do some serious debugging on this script. I suggest adding print statements to check the value's of the variables and correct the errors as you go.

Some things that I have found with just a short time playing with the script.
1. Do what Dlcruz129 says and move digLevel() to where it is below the other functions in the script.

2. The first if in digLevel() you do

	if y > 0 then --change me to if y == 0 then
	  while y< tonumber(l) do
		digOn()
		y = y + 1
	  end
	else
	  while y>0 do
		digOn()
		y=y-1
	  end
	end
y starts out at 0 and with this statement setup this way it will never run any of the code in the block due to the fact that y must first be greater then 0 in order to increase its value. Make the change in the comment above to correct this issue, by checking if it is 0 you will then be able to add to it and this portion of the code will semi work as expected(there are other issues with the amount of moves he makes).

3. You never increment z in digLevel() so the "while z &amp;lt; tonumber(d) do" loop is basically an infinite loop since z will always be 0 and &amp;lt; d.

Once you correct these errors you should be able to start printing the vars and working on the movements of the turtle.