【问题标题】:ComputerCraft Turtle mining programComputerCraft 海龟挖矿程序
【发布时间】:2015-12-19 22:25:56
【问题描述】:

我想从 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 个街区等等。

我做错了什么?

【问题讨论】:

    标签: lua minecraft computercraft


    【解决方案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
    

    【讨论】:

    • 好吧,我已经修好了,但是乌龟还是向前走,转身等等:\
    • 替换2个函数
    • 我不知道你为什么要我删除旋转,back() 应该旋转它并在它没有燃料时回家。之后它会多次显示“程序结束”,直到出现错误“太长而没有屈服”;|
    • 我没有使用计算机技术之类的,试图通过逻辑来帮助你。如果你的燃料少于 5 个,那么checkIfBack 函数返回 true,这会调用两次turtle.turnLeft,如果你没有站在红石块附近,它会调用turtle.forward,我假设它会移动你(我'我不确定它是否返回布尔值,所以它可能会忽略turtle.attack并再次执行turtle.forward)然后返回false。
    • 是的,你的想法不错,但是如果 turtle 不能移动,turtle.forward() 应该返回 false。
    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 2015-12-20
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多