如何在ComputerCraft中保存Lua程序创建的表

时间:2018-12-23 18:21:57

标签: lua computercraft

我正在尝试使用计算机乌龟创建一个仓库存储系统。我希望乌龟能够检测出它具有的块,找到该物品的箱子坐标,然后转到该箱子并放下物品。但是,我不想输入项目和游戏中每个项目的坐标。我希望乌龟能够检测到没有物品,将其放在未分配的箱子中,并将该物品和新箱子放在其物品和坐标表中。在检测到新项目后重新启动后,我无法弄清楚如何获取乌龟创建的表。该代码仍设置用于调试目的。如果有人可以帮助我制作该程序,使其能够区分橡木和桦木之间的区别,我也希望获得帮助。任何帮助将不胜感激!该装置包括一只乌龟,前面有一个投入箱,右边有一个加油箱,后面有一个仓库。

blockAmount = 3
highestVal = {2,0,3}
blockTypes = {
    cobblestone = {2,0,1};
    dirt = {2,0,2};
    log = {2,0,3};
}
pos = {0,0,0}
looking = 0
function fuel()
    if turtle.getFuelLevel() < 20 then
        turtle.select(16)
        turtle.refuel(1)
    end
end
function left()
    turtle.turnLeft()
    looking = looking - 1
    if looking < 0 then
        looking = 3
    end
end
function right()
    turtle.turnRight()
    looking = looking + 1
    if looking > 3 then
        looking = 0
    end
end
function forward()
    fuel()
        if turtle.forward() then
            if looking == 0 then
                pos[1] = pos[1] - 1
            elseif looking == 1 then
                pos[3] = pos[3] - 1 
            elseif looking == 2 then
                pos[1] = pos[1] + 1
            elseif looking == 3 then
                pos[3] = pos[3] + 1
            else
            end
        end

end
function up()
    fuel()
    turtle.up()
    pos[2] = pos[2] + 1
end
function down()
    fuel()
    turtle.down()
    pos[2] = pos[2] - 1
end
function goHome()
    while pos[3] > 0 do
        while  looking > 1 do
            left()
        end
        forward()
    end
    while  pos[2] > 0 do
        down()
    end
    while  pos[1] > 0 do
        while  looking > 0 do
            left()
        end
        forward()
    end
end
while true do
    turtle.select(1)
    while not turtle.suck() do
        sleep(1)
    end
    itemDetails = turtle.getItemDetail()
    name = itemDetails.name:match("^minecraft:(.+)$")
    local elem = blockTypes[name]
    right()
    turtle.select(16)
    turtle.suck(5)
    turtle.select(1)
    if elem then
        while looking < 2 or looking > 2 do
            right()
        end
        for i = pos[1],elem[1]-1 do
            forward()
        end
        while looking > 3 or looking < 3 do
            right()
        end
        for i = pos[3],elem[3]-1 do
            forward()
        end
        for i = pos[2],elem[2]-1 do
            up()
        end
        while looking < 2 or looking > 2 do
            left()
        end
        turtle.select(1)
        turtle.drop()
        goHome()
        right()
        turtle.select(16)
        turtle.drop()
        turtle.select(1)
        left()
    else
        blockAmount = blockAmount + 1
        highestVal[3] = highestVal[3] + 1
        if highestVal[3] > 5 then
            highestVal[3] = 1
            highestVal[2] = highestVal[2] + 1
        end
        if highestVal[2] > 4 then
            highestVal[2] = 0
            highestVal[1] = highestVal[1] + 2
        end
        print(name)
        blockTypes[blockAmount] = name
        blockTypes[name] = {highestVal[1],highestVal[2],highestVal[3]}
        print(blockTypes[blockAmount])
        print(blockTypes[name][1],blockTypes[name][2],blockTypes[name][3])
        local elem = blockTypes[name]
        left()
        turtle.drop()
    end
end

我希望乌龟放入一个物品,例如sandstone,请注意,它没有该物品,将其放置在数据库中并为其放置新的箱子坐标,然后将其放置箱子里的东西。重新启动后,该数据库也应保持更新,因此我正在考虑对其进行读写的文件,但我一直无法弄清楚如何成功进行此操作。 编辑:我想出了如何让乌龟为新物品创建子表并为它们分配一个箱子。但是,它无法区分诸如蓝色和绿色羊毛之类的不同羊毛,桦木和云杉之类的不同原木之类的东西之间的区别。我也不知道如何在重新启动后或何时重新放置子表重新加入世界。再次,任何帮助将不胜感激!

0 个答案:

没有答案