Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ==================== INIT ====================
- local integrator = peripheral.find("colonyIntegrator")
- local bridge = peripheral.find("meBridge")
- local monitors = { peripheral.find("monitor") }
- local monitorOrders -- für Work Orders
- local monitorRequests -- für Requests
- if #monitors == 0 then
- error("Kein Monitor gefunden!")
- elseif #monitors == 1 then
- monitorOrders = monitors[1]
- monitorRequests = monitors[1]
- print("Nur 1 Monitor gefunden → alles auf einen Monitor")
- else
- monitorOrders = monitors[1] -- Erster Monitor = Work Orders
- monitorRequests = monitors[2] -- Zweiter Monitor = Requests
- print("2 Monitore gefunden → SPLIT_MODE aktiviert")
- end
- if not integrator then error("No colonyIntegrator!") end
- if not bridge then error("No ME Bridge!") end
- -- ==================== SETTINGS ====================
- local CONFIG = {
- EXPORT_DIRECTION = "down",
- LOOP_INTERVAL = 300,
- AUTO_EXPORT = true,
- AUTO_CRAFT = true,
- CLEAR_MONITOR = true,
- SPLIT_MODE = (#monitors >= 2) -- automatisch aktiv, wenn 2 Monitore da sind
- }
- -- ==================== DEBUG ====================
- local function dbg(msg)
- print("[DEBUG] " .. tostring(msg))
- end
- -- ==================== UI ====================
- local UI = {}
- function UI.init(monitor)
- if CONFIG.CLEAR_MONITOR then monitor.clear() end
- monitor.setCursorPos(1, 1)
- monitor.setTextScale(0.5) -- Gut für die meisten Monitore
- return { monitor = monitor, line = 1 }
- end
- function UI.write(ui, text, color)
- if not ui or not ui.monitor then return end
- ui.monitor.setCursorPos(1, ui.line)
- ui.monitor.setTextColor(color or colors.white)
- ui.monitor.write(tostring(text or ""):sub(1, 60))
- ui.line = ui.line + 1
- end
- function UI.hr(ui)
- UI.write(ui, string.rep("-", 52), colors.gray)
- end
- -- ==================== ME SYSTEM ====================
- local ME = {}
- function ME.getCount(r)
- if not r then return 0 end
- local item = r.item or {}
- if item.name then
- local res = bridge.getItem({ name = item.name })
- return res and res.amount or 0
- end
- return 0
- end
- function ME.export(r, amount)
- if not CONFIG.AUTO_EXPORT then return false end
- local item = r.item
- if not item or not item.name then return false end
- dbg("Export: " .. item.name .. " x" .. amount)
- local exported = 0
- local retries = 0
- while exported < amount and retries < 15 do
- local ok, result = pcall(function()
- return bridge.exportItem({ name = item.name, count = 1 }, CONFIG.EXPORT_DIRECTION)
- end)
- if ok and result and result > 0 then
- exported = exported + 1
- retries = 0
- else
- retries = retries + 1
- sleep(0.5)
- end
- sleep(0.1)
- end
- return exported > 0
- end
- function ME.isCraftable(r)
- if not r then return false end
- local item = r.item
- if not item or not item.name then return false end
- local ok, result = pcall(function()
- return bridge.isItemCraftable({ name = item.name })
- end)
- return ok and result == true
- end
- function ME.craft(r, amount)
- if not CONFIG.AUTO_CRAFT then return false end
- local item = r.item
- if not item or not item.name then return false end
- local ok = pcall(function()
- bridge.craftItem({ name = item.name, count = amount })
- end)
- if ok then
- dbg("Craft started: " .. item.name)
- end
- return ok
- end
- -- ==================== COLONY ====================
- local Colony = {}
- function Colony.getOrders()
- local orders = integrator.getWorkOrders() or {}
- dbg("Orders: " .. #orders)
- return orders
- end
- function Colony.getRequests()
- local requests = integrator.getRequests() or {}
- dbg("Requests: " .. #requests)
- return requests
- end
- function Colony.getRequestInfo(req)
- if not req then return "Unknown Request" end
- local name = req.name or req.desc or "Unbekannter Request"
- local count = req.count or 1
- local state = req.state or "UNKNOWN"
- -- Wenn es Items gibt, den ersten Item-Namen anzeigen
- local itemName = "?"
- if req.items and req.items[1] then
- local item = req.items[1]
- itemName = (item.displayName or item.name or "?"):gsub("^.*:", ""):gsub("_", " ")
- end
- return string.format("%s x%d [%s] → %s", name, count, state, itemName)
- end
- function Colony.getResources(order)
- local builder = order.builder
- local ok, res = pcall(function()
- return integrator.getBuilderResources(builder)
- end)
- if ok then
- return res or {}
- end
- return {}
- end
- function Colony.getRequestItem(req)
- if not req or not req.items or not req.items[1] then
- return nil
- end
- return req.items[1] -- das eigentliche Item-Objekt
- end
- function Colony.getBuilderName(builder)
- if type(builder) == "string" then return builder end
- if type(builder) == "table" then return builder.name or "Builder" end
- return "Unknown"
- end
- -- ==================== LOGIC ====================
- local Logic = {}
- function Logic.handleResource(r, ui)
- local name = (r.displayName or "?"):gsub("^.*:", ""):gsub("_"," ")
- local need = r.needed or 0
- local delivered = r.delivered or 0
- local status = r.status or "UNKNOWN"
- local missing = need - delivered
- if status ~= "DONT_HAVE" then
- UI.write(ui, " " .. name .. " [" .. status .. "]", colors.gray)
- return 0, 0
- end
- local haveME = ME.getCount(r)
- if haveME >= missing then
- UI.write(ui, " " .. name .. " missing " .. missing, colors.red)
- if ME.export(r, missing) then
- UI.write(ui, " -> Export OK", colors.lime)
- return 1, 0
- else
- UI.write(ui, " -> Export FAIL", colors.orange)
- end
- else
- UI.write(ui, " " .. name .. " missing " .. missing .. " (ME:" .. haveME .. ")", colors.orange)
- local craftable = ME.isCraftable(r)
- if craftable then
- UI.write(ui, " [CRAFTABLE]", colors.cyan)
- if ME.craft(r, missing) then
- UI.write(ui, " -> Craft started", colors.yellow)
- return 0, 1
- else
- UI.write(ui, " -> Craft FAIL", colors.red)
- end
- else
- UI.write(ui, " [NOT CRAFTABLE]", colors.red)
- end
- end
- return 0, 0
- end
- function Logic.handleWorkOrder(order, ui)
- local builderName = Colony.getBuilderName(order.builder)
- UI.write(ui, builderName, colors.white)
- local resources = Colony.getResources(order)
- if #resources == 0 then
- UI.write(ui, " (keine Resourcen-Daten)", colors.gray)
- return 0, 0
- end
- local exp, craft = 0, 0
- for _, r in ipairs(resources) do
- local e, c = Logic.handleResource(r, ui)
- exp = exp + e
- craft = craft + c
- end
- UI.write(ui, " ")
- return exp, craft
- end
- function Logic.handleRequest(req, ui)
- if not req then return 0, 0 end
- local state = req.state or "UNKNOWN"
- local reqName = req.name or "Request"
- local count = req.count or 1
- -- Zeile 1: Allgemeine Request-Info
- UI.write(ui, reqName .. " x" .. count .. " [" .. state .. "]", colors.lightBlue)
- if state ~= "IN_PROGRESS" and state ~= "OPEN" then
- UI.write(ui, " (Status übersprungen)", colors.gray)
- UI.write(ui, " ")
- return 0, 0
- end
- local possibleItems = req.items or {}
- if #possibleItems == 0 then
- UI.write(ui, " Keine Items im Request", colors.red)
- UI.write(ui, " ")
- return 0, 0
- end
- local exp, craft = 0, 0
- local fulfilled = false
- -- Wir gehen alle Alternativen durch und nehmen die erste, die im ME verfügbar oder craftbar ist
- for i, item in ipairs(possibleItems) do
- if not item or not item.name then goto continue end
- local displayName = (item.displayName or item.name):gsub("^.*:", ""):gsub("_", " ")
- local haveME = ME.getCount({item = item})
- UI.write(ui, " Variante " .. i .. ": " .. displayName .. " (ME: " .. haveME .. ")", colors.white)
- if haveME >= count then
- UI.write(ui, " -> Genug im ME! Exportiere...", colors.red)
- if ME.export({item = item}, count) then
- UI.write(ui, " -> Export ERFOLGREICH", colors.lime)
- exp = exp + 1
- fulfilled = true
- break
- else
- UI.write(ui, " -> Export fehlgeschlagen", colors.orange)
- end
- elseif ME.isCraftable({item = item}) then
- UI.write(ui, " -> Craftbar! Starte Craft...", colors.cyan)
- if ME.craft({item = item}, count) then
- UI.write(ui, " -> Craft gestartet", colors.yellow)
- craft = craft + 1
- fulfilled = true
- break
- else
- UI.write(ui, " -> Craft fehlgeschlagen", colors.red)
- end
- else
- UI.write(ui, " -> Weder vorhanden noch craftbar", colors.gray)
- end
- ::continue::
- end
- if not fulfilled then
- UI.write(ui, " Keine Variante konnte erfüllt werden", colors.orange)
- end
- UI.write(ui, " ")
- return exp, craft
- end
- -- ==================== MAIN UPDATE ====================
- local function update()
- local uiOrders = UI.init(monitorOrders)
- local uiRequests = CONFIG.SPLIT_MODE and UI.init(monitorRequests) or uiOrders
- -- === WORK ORDERS ===
- UI.write(uiOrders, "=== WORK ORDERS ===", colors.cyan)
- UI.hr(uiOrders)
- local orders = Colony.getOrders()
- local totalExp = 0
- local totalCraft = 0
- if #orders == 0 then
- UI.write(uiOrders, "Keine Work Orders", colors.lime)
- else
- UI.write(uiOrders, "WorkOrders: " .. #orders, colors.cyan)
- for _, order in ipairs(orders) do
- local e, c = Logic.handleWorkOrder(order, uiOrders)
- totalExp = totalExp + e
- totalCraft = totalCraft + c
- end
- end
- UI.hr(uiOrders)
- -- === REQUESTS ===
- if CONFIG.SPLIT_MODE then
- UI.write(uiRequests, "=== REQUESTS ===", colors.cyan)
- UI.hr(uiRequests)
- else
- UI.write(uiOrders, "=== REQUESTS ===", colors.cyan)
- UI.hr(uiOrders)
- end
- local requests = Colony.getRequests()
- if #requests == 0 then
- UI.write(CONFIG.SPLIT_MODE and uiRequests or uiOrders, "Keine Requests", colors.lime)
- else
- UI.write(CONFIG.SPLIT_MODE and uiRequests or uiOrders, "Requests: " .. #requests, colors.cyan)
- for _, req in ipairs(requests) do
- Logic.handleRequest(req, CONFIG.SPLIT_MODE and uiRequests or uiOrders)
- end
- end
- -- Summary
- UI.hr(uiOrders)
- UI.write(uiOrders, "Export: " .. totalExp .. " | Craft: " .. totalCraft, colors.lime)
- end
- -- ==================== LOOP ====================
- while true do
- local ok, err = pcall(update)
- if not ok then
- print("ERROR: " .. tostring(err))
- if monitorOrders then
- monitorOrders.clear()
- monitorOrders.setCursorPos(1,1)
- monitorOrders.setTextColor(colors.red)
- monitorOrders.write("ERROR!")
- monitorOrders.setTextColor(colors.white)
- end
- sleep(5)
- end
- sleep(CONFIG.LOOP_INTERVAL)
- end
Advertisement
Add Comment
Please, Sign In to add comment