Posted 28 June 2012 - 06:47 PM
Hi, I've only been doing any sort of programming for a few days tops, and I've been trying to write a LUA turtle program that will have a tulrtle 'chisel' away layers of mountain from the outside in. The goal of this project was to be able to remove underwater mountains / hills for better underwater construction, while preventing unsightly water flows.
The problem with my code is that the turtle will only do two iterations of the local chisel(), whereas it should perform chisel in an infinite loop until told otherwise. I have chisel return 'local y = true' for a successful iteration of chisel, and 'local n = false' for an unsuccessful iteration. If the turtle returns n, it's supposed to about face until it finds a suitable surface, and resumes chiseling. This was to prevent it from getting hung up on single-outyling blocks. I use the condition 'not turtle.detectDown()' to stop operation entirely, lesst the turtle fly off into the sky and wontonly chisel that one z-layer out from the world entirely.
Here is the code:
Any help with how to proceed would be wonderful.
The problem with my code is that the turtle will only do two iterations of the local chisel(), whereas it should perform chisel in an infinite loop until told otherwise. I have chisel return 'local y = true' for a successful iteration of chisel, and 'local n = false' for an unsuccessful iteration. If the turtle returns n, it's supposed to about face until it finds a suitable surface, and resumes chiseling. This was to prevent it from getting hung up on single-outyling blocks. I use the condition 'not turtle.detectDown()' to stop operation entirely, lesst the turtle fly off into the sky and wontonly chisel that one z-layer out from the world entirely.
Here is the code:
local function detectRight()
local r
turtle.turnRight()
r = turtle.detect()
turtle.turnLeft()
return r
end
local function detectLeft()
local l
turtle.turnLeft()
l = turtle.detect()
turtle.turnRight()
return l
end
local y = true
local n = false
local function chisel()
if detectRight() then
turtle.turnRight()
turtle.dig()
turtle.forward()
return y
else if turtle.detect() then
turtle.dig()
turtle.forward()
return y
else if detectLeft() then
turtle.turnLeft()
turtle.dig()
turtle.forward()
return y
else if not turtle.detect() then
return n
end
end
end
end
end
chisel()
if y then
chisel()
else if n then
turtle.turnLeft()
turtle.turnLeft()
while turtle.detectDown() and not turtle.detect() do
turtle.forward()
if not turtle.detectDown() then
break
else if turtle.detect() then
chisel()
end
end
end
end
end
Any help with how to proceed would be wonderful.