CCCPaste

quarrycontroller

---Loads a locally saved table and returns it
---@param path string a valid filepath
---@return table params or empty table
local function loadParams(path)
    local file = fs.open(path, "r")
    if file ~= nil then
        local data = file.readAll()
        file.close()
        return textutils.unserialize(data)
    else
        return {}
    end
end

---Saves the table at the defined path
---@param path string a valid filepath
---@param table table paramtable to save
local function saveParams(path, table)
    local file = fs.open(path, "w")
    file.write(textutils.serialize(table))
    file.close()
end

local STEPSIZE = 5
local FIELDSIZE = 7
local SECONDSWAITINGONDRILL = 260
local PARAMPATH = "files/quarry"

local params = loadParams(PARAMPATH)

local function setRedstoneOutput(side, active)
    if active == nil or active then
        redstone.setOutput(side, true)
    else
        redstone.setOutput(side, false)
    end
end

local function stopRotation()
    setRedstoneOutput("top", true)
    setRedstoneOutput("right", true)
end

local function slowerSpeed(active)
    setRedstoneOutput("right", active)
    setRedstoneOutput("top", not active)
end

local function reverseRotation(active)
    setRedstoneOutput("bottom", not active)
end

local function activateFirstCarriage(active)
    setRedstoneOutput("left", active)
end

local function activateSecondCarriage(active)
    stopRotation()
    setRedstoneOutput("back", active)
    sleep(0.25)
end

local function skipCurrent()
    params = loadParams(PARAMPATH)
    if params.skip ~= nil then
        if params.skip == true then
            params.skip = false
            reverseRotation(false)
            saveParams(PARAMPATH, params)
            return true
        end
    end
    return false
end

local function skipRow()
    params = loadParams(PARAMPATH)
    if params.skiprow ~= nil then
        if params.skiprow == true then
            params.skiprow = false
            saveParams(PARAMPATH, params)
            return true
        end
    end
    return false
end

local function stopExecution()

end

local function drill()
    activateFirstCarriage()
    activateSecondCarriage()
    reverseRotation()
    slowerSpeed(false)
    if not skipCurrent() then
        for i = 1, SECONDSWAITINGONDRILL, 1 do
            sleep(1)
            term.clear()
            term.setCursorPos(1, 1)
            print(tostring(i) .. " seconds passed from " .. tostring(SECONDSWAITINGONDRILL))
            if skipCurrent() then
                break
            end
        end
        reverseRotation(false)
        for i = 1, math.ceil(SECONDSWAITINGONDRILL / 15), 1 do
            sleep(1)
        end
    end
end

local function moveFirstCarriage()
    activateFirstCarriage(false)
    activateSecondCarriage(false)
    slowerSpeed(true)
    reverseRotation(false)
    for i = 1, STEPSIZE, 1 do
        sleep(0.85)
    end
end

local function callbackFirstCarriage()
    activateFirstCarriage(false)
    activateSecondCarriage(false)
    slowerSpeed(false)
    sleep(0.5)
    reverseRotation(true)
    for i = 1, FIELDSIZE, 1 do
        sleep(1)
    end
end

local function moveSecondCarriage()
    activateFirstCarriage(true)
    activateSecondCarriage(false)
    slowerSpeed(true)
    sleep(0.5)
    reverseRotation(true)
    for i = 1, STEPSIZE, 1 do
        sleep(0.85)
    end
end

local function callbackSecondCarriage()
    reverseRotation(false)
    activateFirstCarriage(true)
    activateSecondCarriage(false)
    slowerSpeed(false)
    sleep(0.5)
    reverseRotation(false)
    for i = 1, FIELDSIZE, 1 do
        sleep(1)
    end
end

local function executeAll()
    callbackFirstCarriage()
    for rows = 1, FIELDSIZE, 1 do
        for columns = 1, FIELDSIZE, 1 do
            if skipRow() then
                break
            end
            if not skipCurrent() then
                drill()
            end
            moveSecondCarriage()
        end
        callbackSecondCarriage()
        moveFirstCarriage()
    end
end

local function test()
    callbackFirstCarriage()
    moveSecondCarriage()
    callbackSecondCarriage()
    moveFirstCarriage()
end

executeAll()
-- test()