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

Can I get some fresh eyes on this?

Started by fuzzl3, 08 September 2012 - 03:37 AM
fuzzl3 #1
Posted 08 September 2012 - 05:37 AM
I'm trying to write a program similar to the built in excavate program but more tailored to how I like to do things with my extremely limited abilities and knowledge of coding. It's late and I'm tired, and basically I can't spot why I'm getting the error: Quarry:40: attempt to call nil. Please keep in mind that the program is currently incomplete and will currently only work with odd valued widths.


term.clear()
term.setCursorPos(1,1)
print("Input length of area to be quarried:  ")
input1 = read()
print("Input width of area to be quarried:  ")
input2 = read()
print("Input current height:  ")
input3 = read()
print("Input desired stopping level:  ")
input4 = read()
length = input1
width = input2
height = input3-1
bottom = input4
x = 0
z = 0
a = 0
b = 0
i = 0
currentHeight = height
number = input2
level = height-bottom
y = currentHeight
odd = "false"
even = "false"
odd2 = "false"
even2 = "false"
aboveBottom = bottom+1
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Length will be "..length..", width will be "..width..", height is "..height..", and digging will stop at level "..bottom..".")
print("Is this correct?")
input5 = read()
if input5 == "yes" then
  term.clear()
  term.setCursorPos(1,1)
  print("Engaging quarry process")
  sleep(2)
  commenceDigging()
else
  print("Session terminated")
  sleep(1)
  term.shutdown()
end
function checkHeight(level)
  if level%2 == 0 then
	even2 = "true"
  else
	odd2 = "true"
  end
end
function check(number)
  number = input2
  if number%2 == 0 then
	even = "true"
  else
	odd = "true"
  end
end
function commenceDigging()
  check(number)
  checkHeight(level)
  if odd == "true" then
	if odd2 == "true" then
	  while currentHeight>bottom do
		if currentHeight == aboveBottom then
		  b = width
		  while b>0 do
			if b == 1 then
			  clearRowRedAlpha()
			else
			  clearRowRedAlpha()
			  nextRowLeft()
			  clearRowBlueAlpha()
			  nextRowRight()
			end
		  end
		else
		  clearLayerOddAlpha()
		  clearLayerOddBeta()
		end
	  end
	  turtle.turnRight()
	  while z>0 do
		turtle.forward()
		z = z-1
	  end
	  turtle.turnRight()
	  while x>0 do
		turtle.forward()
		x = x-1
	  end
	  while currentHeight<height do
		turtle.up()
		currentHeight = currentHeight+1
	  end
	  dumpInventory()
	end
  end
end
function clearLayerOddAlpha()
  b = width
  while b>0 do
	if b == 1 then
	  clearRowRedAlpha()
	  nextLayerOddAlpha()
	else
	  clearRowRedAlpha()
	  nextRowLeft()
	  clearRowBlueAlpha()
	  nextRowRight()
	end
  end
end
function clearLayerOddBeta()
  b = length
  while b>0 do
	if b == 1 then
	  clearRowRedBeta()
	  nextLayerOddBeta()
	else
	  clearRowRedBeta()
	  nextRowRight()
	  clearRowBlueBeta()
	  nextRowLeft()
	end
  end
end
function clearRowRedAlpha()
a = length
  while a>0 do
	turtle.dig()
	turtle.forward()
	x = x+1
	a = a-1
  end
b = b-1
z = z+1
end
function clearRowRedBeta()
a = width
  while a>0 do
	turtle.dig()
	turtle.forward()
	z = z-1
	a = a-1
  end
b = b-1
x = x-1
end
function clearRowBlueAlpha()
a = length
  while a>0 do
	turtle.dig()
	turtle.forward()
	a = a-1
	x = x-1
  end
b = b-1
z = z+1
end
function clearRowBlueBeta()
a = width
  while a>0 do
	turtle.dig()
	turtle.forward()
	a = a-1
	z = z+1
  end
b = b-1
x = x-1
end
function nextRowLeft()
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
end
function nextRowRight()
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
end
function nextLayerOddAlpha()
  turtle.digDown()
  turtle.down()
  turtle.turnRight()
  currentHeight = currentHeight-1
end
function nextLayerOddBeta()
  turtle.select(16)
  if turtle.getItemCount() then
	y = currentHeight
	while y<height do
	  turtle.up()
	  y = y+1
	end
	turtle.turnRight()
	dumpInventory()
	while y>currentHeight do
	  turtle.down()
	  y = y-1
	end
	turtle.digDown()
	turtle.down()
	currentHeight = currentHeight-1
  else
	turtle.digDown()
	turtle.down()
	turtle.turnLeft()
	currentHeight = currentHeight-1
  end
end
function dumpInventory()
  for i=1,16,1 do
	turtle.select(i)
	turtle.drop(64)
  end
  shell.run("turn","left",2)
end
Lettuce #2
Posted 08 September 2012 - 05:58 AM
Okay, that's quite a bit to go through. What line does it call nil? That would be along the order of

string:test:123:attempt to call nil

those error messages help us out a lot.
fuzzl3 #3
Posted 08 September 2012 - 06:02 AM
Sorry, edited it in. Quarry:40: Attempt to call nil just in case you haven't checked yet.
Lettuce #4
Posted 08 September 2012 - 06:10 AM
Simple. You ordered it wrong. You call commenceDigging() before you make commenceDigging() a function. You have to make the functions before you call them, as Lua reads line-by-line. You can't put all your functions at the bottom for instance. Just cut and paste so they are "in order."
Lyqyd #5
Posted 08 September 2012 - 06:10 AM
Functions should be defined before the code that calls them.
fuzzl3 #6
Posted 08 September 2012 - 06:12 AM
Thanks, this is why I asked, too tired to remember something as simple as that. Normally I write all the functions out first, but it figures the one time I don't i completely forget that bit.