CCCPaste

stairdigger

local args = { ... }
local levels = 5
local safemode = true
if #args ~= 0 then
    if tonumber(args[1]) == nil then
        error("input must be a number")
    else
        levels = tonumber(args[1])
    end

    if tonumber(args[2]) == nil then
        safemode = false
    end
end

local BUILDINGBLOCK = { [1] = "minecraft:cobblestone", [2] = "minecraft:cobbled_deepslate" }
local LIGHTING = { [1] = "minecraft:torch" }
local TORCHDISTANCE = 7
local HOLEDISTANCE = 5

local levelCounter = 0
local holeCounter = 0
local torchCounter = 0

local function selectItem(blockGroup)
    local function isItem(itemName)
        local details = turtle.getItemDetail()
        if details ~= nil then
            if details.name == itemName then
                return true
            end
        end
        return false
    end
    local function itereateAllFields(itemName)
        if isItem(itemName) then return true end
        for i = 1, 16, 1 do
            turtle.select(i)
            if isItem(itemName) then return true end
        end
    end
    if blockGroup.lastIndex ~= nil then
        if itereateAllFields(blockGroup[blockGroup.lastIndex]) then return true end
    end
    for groupIndex = 1, #blockGroup, 1 do
        if itereateAllFields(blockGroup[groupIndex]) then return true end
    end
    error("Item not found in List: " .. textutils.serialise(blockGroup))
end

local function digHole()
    if not turtle.inspect() then return false end
    for i = 1, 4, 1 do
        while not turtle.forward() do
            turtle.dig()
        end
    end
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
end

local function digLevel()
    while not turtle.forward() do
        turtle.dig()
    end

    selectItem(BUILDINGBLOCK)
    turtle.placeDown()

    if safemode then
        turtle.turnRight()
        selectItem(BUILDINGBLOCK)
        turtle.place()
        turtle.turnLeft()
        turtle.turnLeft()
        selectItem(BUILDINGBLOCK)
        turtle.place()
        turtle.turnRight()
    end

    while not turtle.up() do
        turtle.digUp()
    end
    selectItem(BUILDINGBLOCK)
    turtle.placeUp()
end

local function digSection()
    digLevel()

    if holeCounter >= HOLEDISTANCE then
        turtle.turnLeft()
        digHole()
        turtle.turnRight()
        turtle.turnRight()
        digHole()
        turtle.turnLeft()
        holeCounter = 0
    end

    if turtle.inspectDown() then return error("not safe") end
    turtle.down()
    if turtle.inspectUp() then return error("not safe") end
    if torchCounter >= TORCHDISTANCE then
        turtle.turnRight()
        turtle.turnRight()
        selectItem(LIGHTING)
        turtle.place()
        turtle.turnLeft()
        turtle.turnLeft()
        torchCounter = 0
    end
end

while levelCounter < levels do
    digSection()
    levelCounter = levelCounter + 1
    holeCounter = holeCounter + 1
    torchCounter = torchCounter + 1
end