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

Area Clearing Program: Help?

Started by Noiro, 06 April 2013 - 04:31 AM
Noiro #1
Posted 06 April 2013 - 06:31 AM
Hey guys, I am working on one of my first bigger projects (before, I was just doing simple stuff). I am trying to make a program where the turtle will clear an area either up or down based on the dimensions I give it. The code I wrote from scratch is about 219 lines long and I can't find all the random mess I've done to it.

Can someone with a bit more experience give me a bit of a guiding hand as to what I did wrong and what I need to do to fix it?


args = {...}
local direction
dirX,dirY,dirZ,facing = 0
--[[Meat of the program. Actually digs the hole.
It starts by looping X arrays, then moves back to starting position (0,0), moves
up or down, then mines that array as well.]]--
function mine()
  forward()
  local turnPat = 0

  for dirZ in height do
	for dirX in width do
	  for dirY in depth-1 do
	 forward()
   end

if dirX != width do
   if turnPat = 0 then
	 left()
  forward()
  left()
  turnPat=1
   elseif turnPat = 1 then
	 right()
  forward()
  right()
  turnPat=0
   end
end
  end
	if turnPat = 0 then
   left()
--[[turnPat or turnPattern essentially is for the even/odd scenario while mining. When he finishes a column, he will either be in the top right or bottom right.]]--
   while dirX-1 > 0 do
	 forward()
   end
   left()
   while dirY-1 > 0 do
	 forward()
  left()
  left()
  down()
   end
else
   right()
   while dirX > 0 do
	 forward()
  left()
  down()
   end
end

  end
end

--[[Conveniently clears screen]]--
function clear()
  term.clear()
  term.setCursorPos(1,1)
end
function kill()
  print("System shutting down in 2 seconds..")
  clear()
end
--[[Navigation loop. Goes back to point 0,0,0 by looping through and
first moving X to 0, then Y, then Z. ]]--
function home()
  --[[Nav to X = 0 loop]]--
  if dirX < 0 then
	while facing != 1 do
   left()
end
while dirX < 0 then
   forward()
end
  elseif dirX > 0 then
	while facing != 3 do
   left()
end
while dirX > 0 then
   forward()
end
  end

  --[[Nav to Y = 0 loop]]--
  if dirY < 0 then
	while facing != 0 do
   left()
end
while dirY < 0 then
   forward()
end
  elseif dirY > 0 then
	while facing != 2 do
   left()
end
while dirY > 0 then
   forward()
end
  end

  --[[Nav to Z = 0 loop]]--
  while dirZ < 0 do
	up()
  end

  while dirZ > 0 do
	down()
  end
end
--[[Navigation function. Fixes bearings to account for direction and shortens turn commands.]]--
function left()
  if facing > 1 then
	facing = facing-1
  else
	facing = 3
  end
  turtle.turnLeft()
end
function right()
  if facing < 3 then
	facing = facing+1
  else
	facing = 0
  end
  turtle.turnLeft()
end
function up()
  z = dirZ+1
  while turtle.detectUp() then
	turtle.digUp()
sleep(.2)
  end
  turtle.up()
end
function down()
  z = dirZ-1
  while turtle.detectDown() then
	turtle.digDown()
sleep(.2)
  end
  turtle.down()
end
function forward()
  if facing == 0 then
	dirY = dirY + 1
  elseif facing == 1 then
	dirX = dirX + 1
  elseif facing == 2 then
	dirY = dirY - 1
  else
	dirX = dirX - 1
  end
  while turtle.detect() then
	turtle.dig()
sleep(.2)
  end
  turtle.forward()
end
--[[Figures out whether turtle should mine up or down]]--
function prep()
  clear()
  print("Which way for height? (DOWN/up)")
  local response = read();

  if response == "up" or "UP" then
	direction = "up"
  elseif response == "down" or "DOWN" then
	direction = "down"
  elseif response == nil then
	direction == "down"
  else
	print("That is not a valid response, please give either 'up', 'down', or just hit ENTER")
sleep(5)
prep()
  end
end
--[[Is getting and saving user's input when running the program into proper arguments.]]--
if #args > 0 then
  if #args == 1 then
	depth, width, height = tonumber(args[1])
  elseif #args == 2 then
	depth, width = tonumber(args[1])
height = tonumber(args[2])
  else
	depth = tonumber(args[1])
width = tonumber(args[2])
height = tonumber(args[3])
  end
neededFuel = (width*depth*height)+(((width*depth*height)/192)*(width+depth+height))
  if neededFuel > turtle.getFuelLevel() then
	print("I estimate that I don't have enough fuel for this. I'd need about "..neededFuel)
print("and I only have about "..turtle.getFuelLevel().."\n\n")
print("Are you sure you want to do this? (yes/NO)")
response = read()
if response == "yes" or response == "YES" then
   mine();
else
   kill()
end
  else
	mine();
  end
else
  print("You did not provide enough arguments!\n")
  print(">clearArea <depth> <width> <height>")
end
Telokis #2
Posted 06 April 2013 - 06:36 AM
Could you be more explicit, please ? What kind of error are you currently having ?
Noiro #3
Posted 06 April 2013 - 06:54 AM
Could you be more explicit, please ? What kind of error are you currently having ?

I am currently getting a plethora of errors in term of nested and expected end's which apparently are not there. I'm not sure if I forgot one or what. Also, I'm well aware of the fact that my program is likely further bugged as it is my first project. I was hoping someone would just pop in and point out any and all errors they happen to notice and if I am keeping up with best coding practices. I'm sure there are things I could do better.

(Random note: code tags in here get rid of returns? Gah!)
Telokis #4
Posted 06 April 2013 - 08:16 AM
Here is your indented code :

Spoiler
args = {...}
local direction
dirX,dirY,dirZ,facing = 0
--[[Meat of the program. Actually digs the hole.
It starts by looping X arrays, then moves back to starting position (0,0), moves
up or down, then mines that array as well.]]--
function mine()
  forward()
  local turnPat = 0

  for dirZ in height do
    for dirX in width do
      for dirY in depth - 1 do
        forward()
      end

      if dirX != width do
        if turnPat = 0 then
          left()
          forward()
          left()
          turnPat = 1
        elseif turnPat = 1 then
          right()
          forward()
          right()
          turnPat = 0
        end
      end
    end
    if turnPat = 0 then
      left()
--[[turnPat or turnPattern essentially is for the even/odd scenario while mining. When he finishes a column, he will either be in the top right or bottom right.]]--
      while dirX - 1 > 0 do
        forward()
      end
      left()
      while dirY-1 > 0 do
        forward()
        left()
        left()
        down()
      end
    else
      right()
      while dirX > 0 do
        forward()
        left()
        down()
      end
    end
  end
end

--[[Conveniently clears screen]]--
function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function kill()
  print("System shutting down in 2 seconds..")
  clear()
end

--[[Navigation loop. Goes back to point 0,0,0 by looping through and
first moving X to 0, then Y, then Z. ]]--
function home()
  --[[Nav to X = 0 loop]]--
  if dirX < 0 then
    while facing != 1 do
      left()
    end
    while dirX < 0 then
      forward()
    end
  elseif dirX > 0 then
    while facing != 3 do
      left()
    end
    while dirX > 0 then
      forward()
    end
  end

  --[[Nav to Y = 0 loop]]--
  if dirY < 0 then
    while facing != 0 do
      left()
    end
    while dirY < 0 then
      forward()
    end
  elseif dirY > 0 then
    while facing != 2 do
      left()
    end
    while dirY > 0 then
      forward()
    end
  end

  --[[Nav to Z = 0 loop]]--
  while dirZ < 0 do
    up()
  end

  while dirZ > 0 do
    down()
  end
end

--[[Navigation function. Fixes bearings to account for direction and shortens turn commands.]]--
function left()
  if facing > 1 then
    facing = facing-1
  else
    facing = 3
  end
  turtle.turnLeft()
end

function right()
  if facing < 3 then
    facing = facing+1
  else
    facing = 0
  end
  turtle.turnLeft()
end

function up()
  z = dirZ + 1
  while turtle.detectUp() then
    turtle.digUp()
    sleep(.2)
  end
  turtle.up()
end

function down()
  z = dirZ - 1
  while turtle.detectDown() then
    turtle.digDown()
    sleep(.2)
  end
  turtle.down()
end

function forward()
  if facing == 0 then
    dirY = dirY + 1
  elseif facing == 1 then
    dirX = dirX + 1
  elseif facing == 2 then
    dirY = dirY - 1
  else
    dirX = dirX - 1
  end
  while turtle.detect() then
    turtle.dig()
    sleep(.2)
  end
  turtle.forward()
end

--[[Figures out whether turtle should mine up or down]]--
function prep()
  clear()
  print("Which way for height? (DOWN/up)")
  local response = read();

  if response == "up" or "UP" then
    direction = "up"
  elseif response == "down" or "DOWN" then
    direction = "down"
  elseif response == nil then
    direction == "down"
  else
    print("That is not a valid response, please give either 'up', 'down', or just hit ENTER")
    sleep(5)
    prep()
  end
end
--[[Is getting and saving user's input when running the program into proper arguments.]]--
if #args > 0 then
  if #args == 1 then
    depth, width, height = tonumber(args[1])
  elseif #args == 2 then
    depth, width = tonumber(args[1])
    height = tonumber(args[2])
  else
    depth = tonumber(args[1])
    width = tonumber(args[2])
    height = tonumber(args[3])
  end
  neededFuel = (width * depth * height) + (((width * depth * height) / 192) * (width + depth + height))
  if neededFuel > turtle.getFuelLevel() then
    print("I estimate that I don't have enough fuel for this. I'd need about "..neededFuel)
    print("and I only have about "..turtle.getFuelLevel().."\n\n")
    print("Are you sure you want to do this? (yes/NO)")
    response = read()
    if response == "yes" or response == "YES" then
      mine();
    else
      kill()
    end
  else
    mine();
  end
elseif
  print("You did not provide enough arguments!\n")
  print(">clearArea <depth> <width> <height>")
end

You should be able to find your errors easier, I think.
If you really don't find any error, I will help you but I would prefer you to search by yourself.
Noiro #5
Posted 06 April 2013 - 01:09 PM
I took a bigger look at it and after fixing a few minor for do instead of for then and such, I came out with the error:

bios:338: [string "clearArea"]:16:'then' expected. An obvious then is there. What did I miss?
Telokis #6
Posted 07 April 2013 - 01:41 AM
Could you show me your modified code, please ?
Left #7
Posted 07 April 2013 - 03:49 AM
I can see you have used a wrong operator at line 17. Lua's doesn't equal operator is ~= for eg. if some ~= other then

not

if some != other then

did you code PHP before lua?
Left #8
Posted 07 April 2013 - 03:52 AM
Here is your indented code :

Spoiler
args = {...}
local directiono
dirX,dirY,dirZ,facing = 0
--[[Meat of the program. Actually digs the hole.
It starts by looping X arrays, then moves back to starting position (0,0), moves
up or down, then mines that array as well.]]--
function mine()
  forward()
  local turnPat = 0

  for dirZ in height do
    for dirX in width do
      for dirY in depth - 1 do
        forward()
      end

      if dirX != width do
        if turnPat = 0 then
          left()
          forward()
          left()
          turnPat = 1
        elseif turnPat = 1 then
          right()
          forward()
          right()
          turnPat = 0
        end
      end
    end
    if turnPat = 0 then
      left()
--[[turnPat or turnPattern essentially is for the even/odd scenario while mining. When he finishes a column, he will either be in the top right or bottom right.]]--
      while dirX - 1 > 0 do
        forward()
      end
      left()
      while dirY-1 > 0 do
        forward()
        left()
        left()
        down()
      end
    else
      right()
      while dirX > 0 do
        forward()
        left()
        down()
      end
    end
  end
end

--[[Conveniently clears screen]]--
function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function kill()
  print("System shutting down in 2 seconds..")
  clear()
end

--[[Navigation loop. Goes back to point 0,0,0 by looping through and
first moving X to 0, then Y, then Z. ]]--
function home()
  --[[Nav to X = 0 loop]]--
  if dirX < 0 then
    while facing != 1 do
      left()
    end
    while dirX < 0 then
      forward()
    end
  elseif dirX > 0 then
    while facing != 3 do
      left()
    end
    while dirX > 0 then
      forward()
    end
  end

  --[[Nav to Y = 0 loop]]--
  if dirY < 0 then
    while facing != 0 do
      left()
    end
    while dirY < 0 then
      forward()
    end
  elseif dirY > 0 then
    while facing != 2 do
      left()
    end
    while dirY > 0 then
      forward()
    end
  end

  --[[Nav to Z = 0 loop]]--
  while dirZ < 0 do
    up()
  end

  while dirZ > 0 do
    down()
  end
end

--[[Navigation function. Fixes bearings to account for direction and shortens turn commands.]]--
function left()
  if facing > 1 then
    facing = facing-1
  else
    facing = 3
  end
  turtle.turnLeft()
end

function right()
  if facing < 3 then
    facing = facing+1
  else
    facing = 0
  end
  turtle.turnLeft()
end

function up()
  z = dirZ + 1
  while turtle.detectUp() then
    turtle.digUp()
    sleep(.2)
  end
  turtle.up()
end

function down()
  z = dirZ - 1
  while turtle.detectDown() then
    turtle.digDown()
    sleep(.2)
  end
  turtle.down()
end

function forward()
  if facing == 0 then
    dirY = dirY + 1
  elseif facing == 1 then
    dirX = dirX + 1
  elseif facing == 2 then
    dirY = dirY - 1
  else
    dirX = dirX - 1
  end
  while turtle.detect() then
    turtle.dig()
    sleep(.2)
  end
  turtle.forward()
end

--[[Figures out whether turtle should mine up or down]]--
function prep()
  clear()
  print("Which way for height? (DOWN/up)")
  local response = read();

  if response == "up" or "UP" then
    direction = "up"
  elseif response == "down" or "DOWN" then
    direction = "down"
  elseif response == nil then
    direction == "down"
  else
    print("That is not a valid response, please give either 'up', 'down', or just hit ENTER")
    sleep(5)
    prep()
  end
end
--[[Is getting and saving user's input when running the program into proper arguments.]]--
if #args > 0 then
  if #args == 1 then
    depth, width, height = tonumber(args[1])
  elseif #args == 2 then
    depth, width = tonumber(args[1])
    height = tonumber(args[2])
  else
    depth = tonumber(args[1])
    width = tonumber(args[2])
    height = tonumber(args[3])
  end
  neededFuel = (width * depth * height) + (((width * depth * height) / 192) * (width + depth + height))
  if neededFuel > turtle.getFuelLevel() then
    print("I estimate that I don't have enough fuel for this. I'd need about "..neededFuel)
    print("and I only have about "..turtle.getFuelLevel().."\n\n")
    print("Are you sure you want to do this? (yes/NO)")
    response = read()
    if response == "yes" or response == "YES" then
      mine();
    else
      kill()
    end
  else
    mine();
  end
elseif
  print("You did not provide enough arguments!\n")
  print(">clearArea <depth> <width> <height>")
end

You should be able to find your errors easier, I think.
If you really don't find any error, I will help you but I would prefer you to search by yourself.

I dont think he asked for indented code.
Telokis #9
Posted 07 April 2013 - 04:05 AM
Here is your indented code :

Spoiler
args = {...}
local directiono
dirX,dirY,dirZ,facing = 0
--[[Meat of the program. Actually digs the hole.
It starts by looping X arrays, then moves back to starting position (0,0), moves
up or down, then mines that array as well.]]--
function mine()
  forward()
  local turnPat = 0

  for dirZ in height do
	for dirX in width do
	  for dirY in depth - 1 do
		forward()
	  end

	  if dirX != width do
		if turnPat = 0 then
		  left()
		  forward()
		  left()
		  turnPat = 1
		elseif turnPat = 1 then
		  right()
		  forward()
		  right()
		  turnPat = 0
		end
	  end
	end
	if turnPat = 0 then
	  left()
--[[turnPat or turnPattern essentially is for the even/odd scenario while mining. When he finishes a column, he will either be in the top right or bottom right.]]--
	  while dirX - 1 > 0 do
		forward()
	  end
	  left()
	  while dirY-1 > 0 do
		forward()
		left()
		left()
		down()
	  end
	else
	  right()
	  while dirX > 0 do
		forward()
		left()
		down()
	  end
	end
  end
end

--[[Conveniently clears screen]]--
function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function kill()
  print("System shutting down in 2 seconds..")
  clear()
end

--[[Navigation loop. Goes back to point 0,0,0 by looping through and
first moving X to 0, then Y, then Z. ]]--
function home()
  --[[Nav to X = 0 loop]]--
  if dirX < 0 then
	while facing != 1 do
	  left()
	end
	while dirX < 0 then
	  forward()
	end
  elseif dirX > 0 then
	while facing != 3 do
	  left()
	end
	while dirX > 0 then
	  forward()
	end
  end

  --[[Nav to Y = 0 loop]]--
  if dirY < 0 then
	while facing != 0 do
	  left()
	end
	while dirY < 0 then
	  forward()
	end
  elseif dirY > 0 then
	while facing != 2 do
	  left()
	end
	while dirY > 0 then
	  forward()
	end
  end

  --[[Nav to Z = 0 loop]]--
  while dirZ < 0 do
	up()
  end

  while dirZ > 0 do
	down()
  end
end

--[[Navigation function. Fixes bearings to account for direction and shortens turn commands.]]--
function left()
  if facing > 1 then
	facing = facing-1
  else
	facing = 3
  end
  turtle.turnLeft()
end

function right()
  if facing < 3 then
	facing = facing+1
  else
	facing = 0
  end
  turtle.turnLeft()
end

function up()
  z = dirZ + 1
  while turtle.detectUp() then
	turtle.digUp()
	sleep(.2)
  end
  turtle.up()
end

function down()
  z = dirZ - 1
  while turtle.detectDown() then
	turtle.digDown()
	sleep(.2)
  end
  turtle.down()
end

function forward()
  if facing == 0 then
	dirY = dirY + 1
  elseif facing == 1 then
	dirX = dirX + 1
  elseif facing == 2 then
	dirY = dirY - 1
  else
	dirX = dirX - 1
  end
  while turtle.detect() then
	turtle.dig()
	sleep(.2)
  end
  turtle.forward()
end

--[[Figures out whether turtle should mine up or down]]--
function prep()
  clear()
  print("Which way for height? (DOWN/up)")
  local response = read();

  if response == "up" or "UP" then
	direction = "up"
  elseif response == "down" or "DOWN" then
	direction = "down"
  elseif response == nil then
	direction == "down"
  else
	print("That is not a valid response, please give either 'up', 'down', or just hit ENTER")
	sleep(5)
	prep()
  end
end
--[[Is getting and saving user's input when running the program into proper arguments.]]--
if #args > 0 then
  if #args == 1 then
	depth, width, height = tonumber(args[1])
  elseif #args == 2 then
	depth, width = tonumber(args[1])
	height = tonumber(args[2])
  else
	depth = tonumber(args[1])
	width = tonumber(args[2])
	height = tonumber(args[3])
  end
  neededFuel = (width * depth * height) + (((width * depth * height) / 192) * (width + depth + height))
  if neededFuel > turtle.getFuelLevel() then
	print("I estimate that I don't have enough fuel for this. I'd need about "..neededFuel)
	print("and I only have about "..turtle.getFuelLevel().."\n\n")
	print("Are you sure you want to do this? (yes/NO)")
	response = read()
	if response == "yes" or response == "YES" then
	  mine();
	else
	  kill()
	end
  else
	mine();
  end
elseif
  print("You did not provide enough arguments!\n")
  print(">clearArea <depth> <width> <height>")
end

You should be able to find your errors easier, I think.
If you really don't find any error, I will help you but I would prefer you to search by yourself.

I dont think he asked for indented code.

I know but there are several attention errors in his code. With an indented code, it's easier to read and, so, to correct.
Left #10
Posted 07 April 2013 - 04:10 AM
I would help with your code more but I'm on my phone. Sorry
Noiro #11
Posted 10 April 2013 - 06:58 AM
I can see you have used a wrong operator at line 17. Lua's doesn't equal operator is ~= for eg. if some ~= other then

not

if some != other then

did you code PHP before lua?

I do a lot more Javascript and Java than I do PHP. I keep meaning to get into it though.

Here is the modified code. I'm still getting used to the whole if xxxx then and while xxxx do. I went through and changed all the places I had
while xxxx then to while xxxx do.
Here is a pastebin because pasting into here does not indent correctly.
Berge #12
Posted 10 April 2013 - 08:54 AM
I'm not an expert but on line 20, you may need to change the

do
to

then
Noiro #13
Posted 10 April 2013 - 05:31 PM
Yeah, that's a minor change. I still haven't gotten everything worked out yet. :/ Note to self: test each individual part of the program as you go.