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. :)/>
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