タートルだけで、工業化! Part02 を投稿しました。

MOD 一覧などのシリーズ通しての情報はこちらです。

金曜日夜に投稿する予定を立てていたのですが、Part01 のコメントなどを見て、思わず舞い上がって気合入れすぎました。

今回紹介したコード

tofu

-- cube house building program
-- usage: tofu <witdh> <depth> <height>

function selectNonEmptySlot()
    for i = 1, 16 do
        if 0 < turtle.getItemCount(i) then
            turtle.select(i)
        end
    end
end

function floor(width, depth)
    -- build floor
    for w = 1, width do
        for d = 1, depth do
            selectNonEmptySlot()
            turtle.placeDown()
            if d < depth then
                turtle.forward()
            else
                if w % 2 == 1 then
                    turtle.turnRight()
                    turtle.forward()
                    turtle.turnRight()
                else
                    turtle.turnLeft()
                    turtle.forward()
                    turtle.turnLeft()
                end
            end
        end
    end
    -- come back
    if width % 2 == 0 then
        turtle.turnLeft()
    else
        for i = 1, depth - 1 do
            turtle.forward()
        end
        turtle.turnRight()
    end
    for i = 1, width do
        turtle.forward()
    end
    turtle.turnRight()
end

function rectangle(width, depth)
    for i = 1, depth - 1 do
        selectNonEmptySlot()
        turtle.placeDown()
        turtle.forward()
    end
    turtle.turnRight()
    for i = 1, width - 1 do
        selectNonEmptySlot()
        turtle.placeDown()
        turtle.forward()
    end
    turtle.turnRight()
    for i = 1, depth - 1 do
        selectNonEmptySlot()
        turtle.placeDown()
        turtle.forward()
    end
    turtle.turnRight()
    for i = 1, width - 1 do
        selectNonEmptySlot()
        turtle.placeDown()
        turtle.forward()
    end
    turtle.turnRight()
end


local args = { ... }
local tofuWidth = tonumber(args[1])
local tofuDepth = tonumber(args[2])
local tofuHeight = tonumber(args[3])

turtle.up()
turtle.forward()
floor(tofuWidth, tofuDepth)
for h = 1, tofuHeight - 2 do
    turtle.up()
    rectangle(tofuWidth, tofuDepth)
end
turtle.up()
floor(tofuWidth, tofuDepth)

matrix

length = 10


termWidth, termHeight = term.getSize()

function getRandomChar()
    local r = math.random(33, 126)
    return string.char(r)
end

function generateChars()
    local chars = {}
    for i = 1, termWidth*termHeight do
        chars[i] = getRandomChar()
    end
    return chars
end

function showAt(col, row)
    if 1 <= row and row <= termHeight then
        term.setCursorPos(col, row)
        write(random_chars[(row-1)*termWidth + col])
    end
end

function generate()
    for col = 1, termWidth do
        if math.random() < 0.05 then
            table.insert(lines, { col=col, row=0 })
        end
    end
end

function update()
    for k, v in pairs(lines) do
        v["row"] = v["row"] + 1
        if v["row"] - length >= termHeight then
            table.remove(lines, k)
        end
    end
end

function show()
    for k, v in pairs(lines) do
        for i = 1, length do
            showAt(v["col"], v["row"] - length + i)
        end
    end
end


random_chars = generateChars()
lines = {}
while true do
    term.clear()
    generate()
    update()
    show()
    sleep(0.1)
end