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

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

ネタで画像を挟むために、初めて MMD を触りました。 今回の苦労は大体そいつのせい。

今回紹介したコード

shaft

-- make a shaft for mining
-- usage: shaft <Current Y Axis>
--        Ladders must be placed at 14th or lower slot,
--        a watter backet must be placed at 16th slot, and a sign 15th.
--        Blocks at 1st or higher slots are used for wall.

local WATTERBUCKET_SLOT = 16
local SIGN_SLOT = 15
local MINING_HEIGHT = 11


local yAxis = tonumber(...)
local depthCount = 0

function shaftDown()
    selectWallSlot()

    turtle.digDown()
    turtle.down()
    for i = 1, 4 do
        if i ~= 1 then
            turtle.place()
        end
        turtle.turnRight()
        suckAll()
    end
    turtle.dig()
    turtle.forward()
    for i = 1, 4 do
        if i ~= 3 then
            turtle.place()
        end
        turtle.turnRight()
        suckAll()
    end
    turtle.back()

    selectLadderSlot()
    turtle.place()
    suckAll()

    depthCount = depthCount + 1
end

function selectWallSlot()
    for i = 1, 14 do
        if 0 < turtle.getItemCount(i) then
            turtle.select(i)
            return
        end
    end
end

function selectLadderSlot()
    for i = 14, 1, -1 do
        if 0 < turtle.getItemCount(i) then
            turtle.select(i)
            return
        end
    end
end

function suckAll()
    turtle.suck()
    turtle.suckUp()
    turtle.suckDown()
end

function waterCushion()
    turtle.up()
    turtle.dig()
    turtle.forward()
    turtle.turnRight()
    turtle.turnRight()
    turtle.select(SIGN_SLOT)
    turtle.place("")
    selectLadderSlot()
    turtle.digUp()
    turtle.up()
    turtle.placeDown()
    turtle.digUp()
    turtle.up()
    turtle.placeDown()
    turtle.select(WATTERBUCKET_SLOT)
    turtle.place()
    turtle.digUp()
    turtle.up()
    selectLadderSlot()
    turtle.placeDown()
    turtle.forward()
    turtle.turnRight()
    turtle.turnRight()
    turtle.place()

    depthCount = depthCount - 4
end

function comeBack()
    for i = 1, depthCount do
        turtle.up()
    end
end


for i = yAxis, MINING_HEIGHT + 1, -1 do
    shaftDown()
end
waterCushion()
comeBack()

mining

local SLOT_GLASS = 14
local SLOT_TORCH = 15
local SLOT_FUEL = 16

local lightLevel = 8


function isValid()
    if turtle.getItemCount(SLOT_GLASS) <= 1
        or turtle.getItemCount(SLOT_TORCH) <= 1
        or turtle.getItemCount(SLOT_FUEL) <= 1 then
        return false
    end
    for i = 1, 16 do
        if turtle.getItemCount(i) == 0 then
            return true
        end
    end
    return false
end


function refuel()
    if turtle.getFuelLevel() < 10 then
        turtle.select(SLOT_FUEL)
        turtle.refuel(1)
    end
end

function onestep()
    local function fillSide()
        turtle.select(SLOT_GLASS)
        turtle.turnRight()
        turtle.place()
        turtle.turnLeft()
        turtle.turnLeft()
        turtle.place()
        turtle.turnRight()
    end

    turtle.up()
    fillSide()
    turtle.placeUp()
    turtle.dig()
    turtle.down()
    fillSide()
    turtle.placeDown()
    repeat
        turtle.dig()
    until turtle.forward()
    lightLevel = lightLevel - 1
end

function suckAll()
    turtle.suck()
    turtle.suckUp()
    turtle.suckDown()
end

function torch()
    if lightLevel < 8 then
        turtle.turnRight()
        turtle.turnRight()
        turtle.select(SLOT_TORCH)
        if turtle.place() then
            lightLevel = 13
        end
        turtle.turnRight()
        turtle.turnRight()
    end
end


while isValid() do
    refuel()
    onestep()
    suckAll()
    torch()
end