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

[Lua][Error/Bug] Backwards Loop

Started by Emma, 10 December 2012 - 07:42 AM
Emma #1
Posted 10 December 2012 - 08:42 AM
Ok so, I have codded a quarry program and tried it out, it digs the first row fine then moves itself into position for the next one, but then it turns itself back around, so it is facing the opposite way it should be facing!

Code:

tlor=true  --True = right, False = false

function digRow(l) --Dig a row
	for i=1,(l-1) do
		if turtle.detect() then
			turtle.dig()
		end
		turtle.forward()
	end
end

function digLayer(l)
	for i=1,l do
		digRow(l)
		if tlor == true then
			turtle.turnRight()
		else
			turtle.turnLeft()
		end
		if turtle.detect() then turtle.dig() end
		turtle.forward()
		if tlor == true then
			turtle.turnRight()
		else
			turtle.turnLeft()
		end
		sleep(1)
		if tlor == true then
			tlor = false
			return true
		else
			tlor = true
			return true
		end
		print("Layer "..i.." has been dug!")
	end
end

function Quarry(len,heh)
	for d=1,(heh+1) do
		digLayer(len)
		turtle.turnLeft()
		turtle.turnLeft()
		if turtle.detectDown() then
			turtle.digDown()
		end
		turtle.down()
	end
	turtle.turnLeft()
	turtle.turnLeft()
	for l=1,(heh+1) do
		turtle.up()
	end
	return true
end

tArgs = { ... }
Arg1 = shell.resolve(tArgs[1])
Arg2 = shell.resolve(tArgs[2])
if #tArgs == 2 then
        tonumber(Arg1)
        tonumber(Arg2)
	Quarry(tArgs[1],tArgs[2])
else
	print("Usage: tQuarry <length> <height>")
end
snoble2022 #2
Posted 10 December 2012 - 09:48 AM
Add a second turnRight or turnLeft. If that doesn't work then try swithcing them
Lyqyd #3
Posted 10 December 2012 - 11:34 AM
You're returning from the digLayer function after the first row.