1

我想从 ComputerCraft mod 中为 Turtle 创建挖掘程序。我创建了这个:

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

但是乌龟向前走 3 个街区,转身,向前走 3 个街区,转身,向前走 3 个街区,依此类推。

我究竟做错了什么?

4

1 回答 1

1

您正在调用back函数。

back函数调用turtle.forward()了两次,第一次检查它是否为假,第二次调用它(之后)。

然后move函数正在调用它。

试试这个

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
于 2015-02-05T16:43:14.673 回答