CCCPaste

mcdonaldsBlack

-- McDonald's V9 with redstone button press
local mon = peripheral.wrap("monitor_14")
local chest = peripheral.wrap("minecraft:chest_0")
local redstoneSide = "back" -- Change this to the side your redstone is connected to

mon.setTextScale(0.5)
mon.setBackgroundColor(colors.black)
mon.clear()

local w, h = mon.getSize()
local ordersPreparing = {}
local ordersReady = {}
local nextOrderId = 1
local activeOrders = {}
local prepTime = 2

local maxBurgers = 64 * 27 * 2 -- 27 single chest, *2 for double

-- Function to check if the depot is empty
local function isDepotEmpty()
    return next(peripheral.wrap("create:depot_0").list()) == nil
end

-- Get hamburger stock
local function getStock()
    local total = 0
    for _, item in pairs(chest.list()) do
        total = total + item.count
    end
    return total
end

local function drawStockBar()
    local stock = getStock()
    local barWidth = w
    local filledWidth = math.floor((stock / maxBurgers) * barWidth)
    local barY = h

    mon.setBackgroundColor(colors.black)
    mon.setTextColor(colors.white)
    mon.setCursorPos(1, barY)
    mon.write(string.rep(" ", barWidth))

    if filledWidth > 0 then
        mon.setBackgroundColor(colors.green)
        mon.setCursorPos(1, barY)
        mon.write(string.rep(" ", filledWidth))
    end

    local text = stock .. " burgers"
    local textX = 1 + math.floor((barWidth - #text) / 2)

    for i = 1, #text do
        local charX = textX + i - 1
        local char = text:sub(i, i)
        if charX <= filledWidth then
            mon.setBackgroundColor(colors.green)
        else
            mon.setBackgroundColor(colors.black)
        end
        mon.setCursorPos(charX, barY)
        mon.setTextColor(colors.white)
        mon.write(char)
    end

    mon.setBackgroundColor(colors.black)
end

local function drawBox(x1, y1, x2, y2, color)
    mon.setBackgroundColor(color)
    for y = y1, y2 do
        mon.setCursorPos(x1, y)
        mon.write(string.rep(" ", x2 - x1 + 1))
    end
    mon.setBackgroundColor(colors.black)
end

local function centerText(y, text, color)
    mon.setTextColor(color or colors.white)
    local x = math.floor((w - #text) / 2) + 1
    mon.setCursorPos(x, y)
    mon.write(text)
end

local lastStock = getStock()

local function updateStock()
    local currentStock = getStock()
    if currentStock ~= lastStock then
        lastStock = currentStock
        drawStockBar()
    end
end

local stockTimer = os.startTimer(1)

local function drawStaticUI()
    mon.clear()
    local logo = {
        "  __  __      _____                    _     _ _     ",
        " |  \\/  |    |  __ \\                  | |   | ( )    ",
        " | \\  / | ___| |  | | ___  _ __   __ _| | __| |/ ___ ",
        " | |\\/| |/ __| |  | |/ _ \\| '_ \\ / _` | |/ _` | / __|",
        " | |  | | (__| |__| | (_) | | | | (_| | | (_| | \\__ \\",
        " |_|  |_|\\___|_____/ \\___/|_| |_|\\__,_|_|\\__,_| |___/","                                                      "
    }
    local yOffset = 1
    for _, line in ipairs(logo) do
        centerText(yOffset, line, colors.yellow)
        yOffset = yOffset + 1
    end

    drawBox(2, 9, math.floor(w/2)-2, h-5, colors.red)
    drawBox(math.floor(w/2)+2, 9, w-2, h-5, colors.lime)

    local btnText = " Place Order "
    local btnX = math.floor((w - #btnText) / 2)
    local btnY = h - 3

    mon.setBackgroundColor(colors.orange)
    mon.setTextColor(colors.white)
    for y = btnY - 1, btnY + 1 do
        mon.setCursorPos(btnX - 1, y)
        mon.write(string.rep(" ", #btnText + 2))
    end

    mon.setCursorPos(btnX, btnY)
    mon.write(btnText)

    drawStockBar()
end

local function drawTable(x1, y1, x2, y2, color, title)
    mon.setBackgroundColor(color)
    mon.setTextColor(colors.white)
    for y = y1, y2 do
        mon.setCursorPos(x1, y)
        mon.write(string.rep(" ", x2 - x1 + 1))
    end
    local tableWidth = x2 - x1 + 1
    local titleX = x1 + math.floor((tableWidth - #title) / 2)
    mon.setCursorPos(titleX, y1)
    mon.write(title)
end

local function updateOrdersUI()
    local leftX1, leftY1 = 3, 10
    local leftX2, leftY2 = math.floor(w/2)-3, h-6
    local rightX1, rightY1 = math.floor(w/2)+3, 10
    local rightX2, rightY2 = w-3, h-6

    drawTable(leftX1, leftY1, leftX2, leftY2, colors.red, "Preparing")
    drawTable(rightX1, rightY1, rightX2, rightY2, colors.lime, "Collect")

    for i, order in ipairs(ordersPreparing) do
        mon.setBackgroundColor(colors.red)
        mon.setTextColor(colors.white)
        mon.setCursorPos(leftX1 + 1, leftY1 + i)
        local text = "Order #" .. order
        mon.write(text .. string.rep(" ", leftX2 - leftX1 - #text))
    end

    for i, order in ipairs(ordersReady) do
        mon.setBackgroundColor(colors.lime)
        mon.setTextColor(colors.white)
        mon.setCursorPos(rightX1 + 1, rightY1 + i)
        local text = "Order #" .. order
        mon.write(text .. string.rep(" ", rightX2 - rightX1 - #text))
    end

    drawStockBar()
end

local function addOrder()
    local thisOrder = nextOrderId
    nextOrderId = nextOrderId + 1
    table.insert(ordersPreparing, thisOrder)

    local prepTimer = os.startTimer(prepTime)
    activeOrders[prepTimer] = {id = thisOrder, stage = "preparing"}

    updateOrdersUI()
end

local function isInButton(x, y)
    local btnText = " Place Order "
    local btnX = math.floor((w - #btnText) / 2)
    local btnY = h - 3
    return x >= btnX - 1 and x <= btnX + #btnText and y >= btnY - 1 and y <= btnY + 1
end

drawStaticUI()
updateOrdersUI()

-- Start stock timer
local stockTimer = os.startTimer(1)

while true do
    local event, arg1, arg2, arg3 = os.pullEvent()
    
    if event == "monitor_touch" then
        local x, y = arg2, arg3
        if isInButton(x, y) then
            addOrder()
        end

    elseif event == "timer" then
        local timerId = arg1

        -- === Stock update timer ===
        if timerId == stockTimer then
            updateStock()
            stockTimer = os.startTimer(1) -- restart stock timer

        else
            local order = activeOrders[timerId]
            if order then
                -- Handle redstone pulse off
                if order.redstoneOff then
                    redstone.setOutput(redstoneSide, false)
                    activeOrders[timerId] = nil

                -- Preparing → Collect
                elseif order.stage == "preparing" then
                    -- Remove from preparing table
                    for i, o in ipairs(ordersPreparing) do
                        if o == order.id then
                            table.remove(ordersPreparing, i)
                            break
                        end
                    end
                    -- Add to ready table
                    table.insert(ordersReady, order.id)
                    updateOrdersUI()

                    -- Trigger redstone pulse
                    redstone.setOutput(redstoneSide, true)
                    local pulseTimer = os.startTimer(1) -- must be >= 1
                    activeOrders[pulseTimer] = {redstoneOff = true}

                    -- Start "check depot" timer after 3s
                    local checkTimer = os.startTimer(3)
                    activeOrders[checkTimer] = {id = order.id, stage = "checkDepot"}

                    activeOrders[timerId] = nil

                -- Check if depot is empty
                elseif order.stage == "checkDepot" then
                    if isDepotEmpty() then
                        -- Remove from ready table
                        for i, o in ipairs(ordersReady) do
                            if o == order.id then
                                table.remove(ordersReady, i)
                                break
                            end
                        end
                        updateOrdersUI()
                        activeOrders[timerId] = nil
                    else
                        -- Recheck after 1 second
                        local retryTimer = os.startTimer(1)
                        activeOrders[retryTimer] = {id = order.id, stage = "checkDepot"}
                        activeOrders[timerId] = nil
                    end
                end
            end
        end
    end
end