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

Program won't run

Started by Rotar, 15 July 2015 - 10:32 PM
Rotar #1
Posted 16 July 2015 - 12:32 AM
Hey guys. I'm new to CC and here is my first program. It builds a wall given the required length and height. Most of the program is cannibalized from tunnel.

When I run the program (e.g. wall 3 2) the program freezes. The cursor does not even move to a new line.

I'm sure the error is simple. Please help me find it. :)/>



local tArgs = { ... }
if #tArgs ~= 2  then
print( "Usage: wall <height> <length>" )
return
end

local height = tonumber( tArgs[1] )
local length = tonumber( tArgs[2] )
if height < 1 then
print( "Wall height must a positive number" )
return
end
if length < 1 then
print( "Wall length must a positive number" )
return
end

local function tryDig()
while turtle.detect() do
  if turtle.dig() then
   collect()
   sleep(0.5)
  else
   return false
  end
end
return true
end

local function tryDigUp()
while turtle.detectUp() do
  if turtle.digUp() then
   collect()
   sleep(0.5)
  else
   return false
  end
end
return true
end

local function refuel()
local fuelLevel = turtle.getFuelLevel()
if fuelLevel == "unlimited" or fuelLevel then
  return
end

local function tryRefuel()
  for n=1,16 do
   if turtle.getItemCount(n) > 0 then
	turtle.select(n)
	if turtle.refuel(1) then
	 turtle.select(1)
	 return true
	end
   end
  end
  turtle.select(1)
  return false
end

if not tryRefuel() then
  print( "Add more fuel to continue." )
  while not tryRefuel() do
   sleep(1)
  end
  print( "Resuming.")
end
end

local function tryUp()
refuel()
while not turtle.up() do
  if turtle.detectUp() then
   if not tryDigUp() then
	return false
   end
  elseif turtle.attackUp() then
   collect()
  else
   sleep( 0.5 )
  end
end
return true
end

local function tryForward()
refuel()
while not turtle.forward() do
  if turtle.detect() then
   if not tryDig() then
	return false
   end
  elseif turtle.attack() then
   collect()
  else
   sleep( 0.5 )
  end
end
return true
end

for n=1,height do
for m=1,length do
  turtle.placeDown()
  tryDig()

  if m<length then
   tryDig()
   if not tryForward() then
	print( "Aborting." )
	break
   end
  end
end

turtle.turnRight()
turtle.turnRight()
tryDigUp()
tryUp()
end
HPWebcamAble #2
Posted 16 July 2015 - 01:58 AM
I can't see anything wrong, other than your formating.

Make sure you indent, it makes it easier to see extra or missing 'ends' (though you didn't have any)
SpoilerNote that the forums screw up indents sometimes, but this is the idea

local tArgs = { ... }
if #tArgs ~= 2  then
  print( "Usage: wall <height> <length>" )
  return
end

local height = tonumber( tArgs[1] )
local length = tonumber( tArgs[2] )
if height < 1 then
  print( "Wall height must a positive number" )
  return
end
if length < 1 then
  print( "Wall length must a positive number" )
  return
end

local function tryDig()
  while turtle.detect() do
    if turtle.dig() then
     collect()
     sleep(0.5)
    else
     return false
    end
  end
  return true
end

local function tryDigUp()
  while turtle.detectUp() do
    if turtle.digUp() then
     collect()
     sleep(0.5)
    else
     return false
    end
  end
  return true
end

local function refuel()
  local fuelLevel = turtle.getFuelLevel()
  if fuelLevel == "unlimited" or fuelLevel then
    return
  end

  local function tryRefuel()
    for n=1,16 do
     if turtle.getItemCount(n) > 0 then
          turtle.select(n)
          if turtle.refuel(1) then
           turtle.select(1)
           return true
          end
     end
    end
    turtle.select(1)
    return false
  end

  if not tryRefuel() then
    print( "Add more fuel to continue." )
    while not tryRefuel() do
     sleep(1)
    end
    print( "Resuming.")
  end
end

local function tryUp()
  refuel()
  while not turtle.up() do
    if turtle.detectUp() then
     if not tryDigUp() then
          return false
     end
    elseif turtle.attackUp() then
     collect()
    else
     sleep( 0.5 )
    end
  end
  return true
end

local function tryForward()
  refuel()
  while not turtle.forward() do
    if turtle.detect() then
     if not tryDig() then
          return false
     end
    elseif turtle.attack() then
     collect()
    else
     sleep( 0.5 )
    end
  end
  return true
end

for n=1,height do
  for m=1,length do
    turtle.placeDown()
    tryDig()

    if m<length then
      tryDig()
      if not tryForward() then
        print( "Aborting." )
        break
      end
    end
  end

  turtle.turnRight()
  turtle.turnRight()
  tryDigUp()
  tryUp()
end

Try putting some 'print("test")' s in random spots, so you know where exactly it freezes.
Rotar #3
Posted 16 July 2015 - 04:21 AM
Fixed! In
[color=#000088][size=2]local[/size][/color][color=#000000][size=2] [/size][/color][color=#000088][size=2]function[/size][/color][color=#000000][size=2] refuel[/size][/color][color=#666600][size=2]()[/size][/color]
I tried to use
[color=#000000][size=2]fuelLevel[/size][/color]
as a boolean value. (just my ignorance of lua)