CCCPaste

chunkminer

-- === CHUNK MINER TURTLE (PERSISTENT) ===
-- Place on SOUTH edge, EAST corner of chunk, facing NORTH.
-- Fuel chest on EAST (right), drop chest on WEST (left).

-- CONFIG
local CHUNK_SIZE = 16
local WALL_SIZE = 18
local FUEL_SLOT = 1
local COBBLE_SLOT = 2
local MIN_FUEL = 500
local KEEP_COAL = 64
local KEEP_COBBLE = 64
local STATE_FILE = "chunkminer.state"

-- === UTILITY ===
local function saveState(state)
    local f = fs.open(STATE_FILE, "w")
    f.write(textutils.serialize(state))
    f.close()
end

local function loadState()
    if fs.exists(STATE_FILE) then
        local f = fs.open(STATE_FILE, "r")
        local data = textutils.unserialize(f.readAll())
        f.close()
        return data
    else
        return { layer = 0, row = 1, col = 1, phase = "init" }
    end
end

local function selectSlot(slot) turtle.select(slot) end

local function forward()
    while not turtle.forward() do
        if turtle.detect() then turtle.dig() end
        sleep(0.2)
    end
end

local function down()
    while not turtle.down() do
        if turtle.detectDown() then turtle.digDown() end
        sleep(0.2)
    end
end

local function up()
    while not turtle.up() do
        if turtle.detectUp() then turtle.digUp() end
        sleep(0.2)
    end
end

local function turnAround()
    turtle.turnLeft()
    turtle.turnLeft()
end

-- === INVENTORY & FUEL ===
local function refuelIfNeeded()
    if turtle.getFuelLevel() < MIN_FUEL then
        print("Refueling...")
        turtle.turnRight() -- east
        selectSlot(FUEL_SLOT)
        turtle.suck(KEEP_COAL)
        turtle.refuel()
        turtle.drop()
        turtle.turnLeft()
    end
end

local function dumpInventory()
    print("Dumping inventory...")
    turtle.turnLeft() -- west
    for i = 3, 16 do
        turtle.select(i)
        turtle.drop()
    end
    turtle.turnRight()
end

local function restock()
    print("Restocking...")
    turtle.turnRight() -- east
    -- Ensure 64 coal
    local coalCount = 0
    for i = 1, 16 do
        local d = turtle.getItemDetail(i)
        if d and d.name:find("coal") then coalCount = coalCount + d.count end
    end
    if coalCount < KEEP_COAL then
        turtle.suck(KEEP_COAL - coalCount)
    end
    -- Ensure 64 cobble
    local cobbleCount = 0
    for i = 1, 16 do
        local d = turtle.getItemDetail(i)
        if d and d.name:find("cobblestone") then cobbleCount = cobbleCount + d.count end
    end
    if cobbleCount < KEEP_COBBLE then
        turtle.suck(KEEP_COBBLE - cobbleCount)
    end
    turtle.turnLeft()
end

-- === WALL BUILDER ===
local function placeWall()
    print("Building 18x18 wall...")
    selectSlot(COBBLE_SLOT)
    for i = 1, WALL_SIZE do
        for j = 1, WALL_SIZE do
            if i == 1 or i == WALL_SIZE or j == 1 or j == WALL_SIZE then
                if not turtle.detectDown() then turtle.placeDown() end
            end
            if j < WALL_SIZE then forward() end
        end
        if i < WALL_SIZE then
            if i % 2 == 1 then
                turtle.turnRight()
                forward()
                turtle.turnRight()
            else
                turtle.turnLeft()
                forward()
                turtle.turnLeft()
            end
        end
    end
    print("Wall complete.")
end

-- === MINING ===
local function mineBlock()
    if turtle.detect() then turtle.dig() end
    if turtle.detectDown() then turtle.digDown() end
end

local function mineLayer(state)
    print("Mining layer "..state.layer)
    for r = state.row, CHUNK_SIZE do
        for c = state.col, CHUNK_SIZE do
            mineBlock()
            if not (r == CHUNK_SIZE and c == CHUNK_SIZE) then
                if c < CHUNK_SIZE then forward() end
            end
            -- Save position
            state.row, state.col = r, c
            saveState(state)
            if turtle.getItemCount(16) > 0 or turtle.getFuelLevel() < MIN_FUEL then
                print("Low fuel or full inv; pausing layer...")
                return false
            end
        end
        -- move to next row
        if r < CHUNK_SIZE then
            if r % 2 == 1 then
                turtle.turnRight()
                forward()
                turtle.turnRight()
            else
                turtle.turnLeft()
                forward()
                turtle.turnLeft()
            end
        end
        state.col = 1
    end
    -- Return to start orientation
    if CHUNK_SIZE % 2 == 1 then
        turnAround()
        for i = 1, CHUNK_SIZE - 1 do forward() end
        turtle.turnLeft()
        for i = 1, CHUNK_SIZE - 1 do forward() end
        turtle.turnLeft()
    else
        turtle.turnRight()
        for i = 1, CHUNK_SIZE - 1 do forward() end
        turtle.turnRight()
    end
    state.row = 1
    state.col = 1
    state.layer = state.layer + 1
    saveState(state)
    print("Layer complete!")
    return true
end

-- === MAIN ===
print("=== Chunk Miner Persistent ===")

local state = loadState()

if state.phase == "init" then
    refuelIfNeeded()
    restock()
    placeWall()
    state.phase = "mining"
    state.layer = 0
    saveState(state)
end

while true do
    refuelIfNeeded()
    local done = mineLayer(state)
    if not done then
        dumpInventory()
        restock()
    else
        dumpInventory()
        restock()
        down()
    end
end