1

I'd like to create mining program for Turtle from ComputerCraft mod. I've created this:

function initVariables()
    stone = 0
    cobblestone = 0
    coal = 0
    iron = 0
    gold = 0
    redstone = 0
    diamond = 0
    lapis = 0
    dirt = 0
    gravel = 0
    sand = 0
    emerald = 0
    mossy = 0
end

function count()
    local success, data = turtle.inspect()

    if success then
        if data.name == "minecraft:stone" then stone = stone + 1 end
        if data.name == "minecraft:cobblestone" then cobblestone = cobblestone + 1 end
        if data.name == "minecraft:coal_ore" then coal = coal + 1 end
        if data.name == "minecraft:iron_ore" then iron = iron + 1 end
        if data.name == "minecraft:gold_ore" then gold = gold + 1 end
        if data.name == "minecraft:redstone_ore" then redstone = redstone + 1 end
        if data.name == "minecraft:diamond_ore" then diamond = diamond + 1 end
        if data.name == "minecraft:lapis_ore" then lapis = lapis + 1 end
        if data.name == "minecraft:dirt" then dirt = dirt + 1 end
        if data.name == "minecraft:gravel" then gravel = gravel + 1 end
        if data.name == "minecraft:sand" then sand = sand + 1 end
        if data.name == "minecraft:emerald_ore" then emerald = emerald + 1 end
        if data.name == "minecraft:mossy_cobblestone" then mossy = mossy + 1 end
    end
end

function display()
    print("I have mined:")
        if stone > 0 then
            print(stone .. " blocks of stone")
        end

        if cobblestone > 0 then
            print(cobblestone .. " blocks of cobblestone")
        end

        if coal > 0 then
            print(coal .. " coal ores")
        end

        if iron > 0 then
            print(iron .. " iron ores")
        end

        if gold > 0 then
            print(gold .. " gold ores")
        end

        if redstone > 0 then
            print(redstone .. " redstone ores")
        end

        if diamond > 0 then
            print(diamond .. " diamond ores")
        end

        if lapis > 0 then
            print(lapis .. " lapis ores")
        end

        if dirt > 0 then
            print(dirt .. " blocks of dirt")
        end

        if gravel > 0 then
            print(gravel .. " blocks of gravel")
        end

        if sand > 0 then
            print(sand .. " blocks of sand")
        end

        if emerald > 0 then
            print(emerald .. " emerald ores")
        end

        if mossy > 0 then
            print(mossy .. " blocks of mossy cobblestone")
        end
end

function refuel()
    turtle.select(1)
    turtle.refuel(1)
end

function checkIfBack()
    fuelCount = turtle.getItemCount(1)
    if fuelCount < 5 then
        return true
    else
        return false
    end
end

function back()
    if checkIfBack() == true then
        turtle.turnLeft()
        turtle.turnLeft()

        local stopblock, data = turtle.inspect()
        if data.name ~= "minecraft:redstone_block" then
            if turtle.forward() == false then
                turtle.attack()
            else
                turtle.forward()
            end
            return false
        else
            return true
        end
    end
end

function move()
    if back() == false then
        fuel = turtle.getTurtleLevel()
        if fuel < 10 then refuel() end
        if turtle.inspect() == false then
            turtle.forward()
        else
            count()
            turtle.dig()
            turtle.attack()
        end
    else
        print("END OF PROGRAM")
    end
end

initVariables()
while true do move() end

But the turtle goes 3 blocks forward, turns around, goes 3 blocks forward, turns around, goes 3 blocks forward etc.

What am I doing wrong?

Universal Electricity
  • 775
  • 1
  • 12
  • 26
mieciu0077
  • 13
  • 1
  • 5

1 Answers1

1

You are calling back function.

the back function is calling turtle.forward() twice, first time it checks if its false, and second time it calls it (afterwards).

Then move function is calling it.

Try this

function back()
    if checkIfBack() == true then
        -- turtle.turnLeft() -- this makes it turtle
        -- turtle.turnLeft() -- also this..

        local stopblock, data = turtle.inspect()
        if data.name ~= "minecraft:redstone_block" then
            if turtle.forward() == false then
                turtle.attack()
            -- else -- if we move forward == false then u want him to move? no need
                -- turtle.forward() -- 2nd forward move
            end
            return false
        else
            return true
        end
    end
end

function move()
    if back() == false then
        fuel = turtle.getTurtleLevel()
        if fuel < 10 then refuel() end
        if turtle.inspect() == false then
            -- turtle.forward() -- 3rd call to move forward..
        else
            count()
            turtle.dig()
            turtle.attack()
        end
    else
        print("END OF PROGRAM")
    end
end
das
  • 547
  • 3
  • 12
  • Okey, I've fixed it but turtle still just goes forward, turns around etc. :\ – mieciu0077 Feb 05 '15 at 16:59
  • Replace the 2 functions – das Feb 05 '15 at 17:32
  • I don't know why you want me to delete rotation, back() should rotate its and go back to home when it hasn't fuel. And after that it displays "END OF THE PROGRAM" many many times untill error apears "Too long without yielding" ;| – mieciu0077 Feb 05 '15 at 18:23
  • I'm not using computercraft or such, trying to help you by logic. If you have less than 5 fuel, then `checkIfBack` function returns true, which makes calls `turtle.turnLeft` twice, and if you're not standing near redstone block, it'll call `turtle.forward` which I assume moves you (I'm not sure if it returns boolean, so it probably ignores `turtle.attack` and executes `turtle.forward` again) and then returns false. – das Feb 05 '15 at 18:29
  • Yes, you're thinking good, but turtle.forward() should return false if turtle isn't able to move. – mieciu0077 Feb 05 '15 at 18:46