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

[Question][Lua] Help with loop re-iteration

Started by Scripp, 28 June 2012 - 04:47 PM
Scripp #1
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:

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.
MysticT #2
Posted 28 June 2012 - 07:54 PM
Formated and commented code (I also replaced all the else if with elseif, just to make it look better):

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
    elseif turtle.detect() then
        turtle.dig()
        turtle.forward()
        return y
    elseif detectLeft() then
        turtle.turnLeft()
        turtle.dig()
        turtle.forward()
        return y
    elseif not turtle.detect() then
        return n
    end
end

chisel() -- call the function (1)

-- you set y to true, and never change it
-- so it will always be true
if y then
    chisel() -- call the function (2)
elseif n then -- this will never happen, n is always false
    turtle.turnLeft()
    turtle.turnLeft()
    while turtle.detectDown() and not turtle.detect() do
        turtle.forward()
        if not turtle.detectDown() then
            break
        elseif turtle.detect() then
            chisel()
        end
    end
end
So, you never run the loop.
Scripp #3
Posted 28 June 2012 - 09:08 PM
Hrmm, so in order to enter the loop I have to set y to true or false within chisel()? Should the previous definition of the two be changed at all?
Lyqyd #4
Posted 28 June 2012 - 09:45 PM
It's probably best to completely get rid of y and n, then return either true or false from the function and use "if chisel() then" to determine what to do next.