Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shell.run("update-utils")
- monitor = morthutils.setPeripForCache("monitor", peripheral.wrap("top"))
- colony = peripheral.wrap("right")
- me = morthutils.getPeripByName("rsBridge")
- warehouse = morthutils.getPeripByName("minecolonies:warehouse")
- PER_PAGE = 15
- currentPage = 1
- maxPages = 1
- monitor.setTextScale(0.5)
- monWidth, monHeight = monitor.getSize()
- buttons = {
- ["nextPage"] = {
- ["func"] = function() currentPage = currentPage + 1 end,
- ["shouldShow"] = function() return currentPage < maxPages end,
- ["text"] = "Next Page",
- ["x"] = 11,
- ["y"] = monHeight-1
- },
- ["prevPage"] = {
- ["func"] = function() currentPage = currentPage + 1 end,
- ["shouldShow"] = function() return currentPage > 1 end,
- ["text"] = "Prev Page",
- ["x"] = 1,
- ["y"] = monHeight-1
- }
- }
- morthutils.clear()
- morthutils.clearMonitor(monitor)
- local function getNeededTools()
- local wip = {}
- pcall(function()
- for i,req in ipairs(colony.getRequests()) do
- if req.count > 0 then
- table.insert(wip, req.name.." for "..req.target)
- end
- end
- end)
- return wip
- end
- local function isValidMonClick(mx, my)
- for _, btn in pairs(buttons) do
- if mx >= btn.x and mx <= (btn.x + #btn.text) and my == btn.y then
- return btn
- end
- end
- return nil
- end
- local function amtNeededForInv(item_id, inv, amtNeeded)
- local amtInInv = 0
- for k,stack in pairs(inv.list()) do
- if stack.name == item_id then
- amtInInv = amtInInv + stack.count
- end
- end
- return math.max(amtNeeded - amtInInv, 0), amtInInv -- Return both how much we still need to get and how much is already in there
- end
- local function getNeededResources()
- local wip = {}
- for i,order in ipairs(colony.getWorkOrders()) do
- if order.isClaimed then
- for j,matl in ipairs(colony.getWorkOrderResources(order.id)) do
- if matl.needed and matl.needed > matl.available and matl.status ~= "NOT_NEEDED" then
- local quant = matl.needed - matl.available
- local forInv, inInv = amtNeededForInv(matl.item, warehouse, quant)
- table.insert(wip, {
- ["displayName"] = matl.displayName,
- ["item"] = matl.item,
- ["quantity"] = quant,
- ["forInv"] = forInv,
- ["inInv"] = inInv
- })
- end
- end
- end
- end
- return wip
- end
- local function listener() -- TODO: Other events if we end up doing that
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- local button = isValidMonClick(x,y)
- if event == "monitor_touch" and button ~= nil then
- button.func(button.args)
- end
- end
- end
- TO_STORAGE_QUEUE = {}
- local function enQueue(msg)
- table.insert(TO_STORAGE_QUEUE, msg)
- end
- local function nextOfQueue()
- local ret = TO_STORAGE_QUEUE[1]
- table.remove(TO_STORAGE_QUEUE, 1)
- return ret
- end
- local function depositor()
- while true do
- if #TO_STORAGE_QUEUE > 0 then
- me.exportItem(nextOfQueue(), "top")
- end
- sleep(0.1)
- end
- end
- local function main()
- while true do
- neededResources = getNeededResources()
- table.sort(neededResources, function(a,b) return a.forInv - b.forInv end)
- neededTools = getNeededTools()
- --morthutils.clearMonitor(monitor)
- maxPages = #neededResources / PER_PAGE
- morthutils.clearMonitor()
- morthutils.writeLine("Building materials needed:", false, monitor)
- morthutils.writeLine("", false, monitor)
- for i,matl in ipairs(neededResources) do
- if matl.forInv > 0 then -- Try to fill buffer chest with anything we need
- --print("Exporting "..matl.forInv.." "..matl.item)
- enQueue({name=matl.item, count=matl.forInv})
- end
- if i >= currentPage and i < currentPage+PER_PAGE then
- local color = (matl.inInv < matl.quantity and colors.red) or colors.white
- monitor.setTextColor(color)
- morthutils.writeLine(matl.displayName.."\t("..matl.item..")\t"..matl.inInv.."/"..matl.quantity.."("..matl.forInv..")", false, monitor)
- end
- end
- monitor.setCursorPos(1, PER_PAGE + 5)
- morthutils.writeLine("", false, monitor)
- morthutils.writeLine("Tools needed:", false, monitor)
- for i,tool in ipairs(neededTools) do
- if not tool:match(".*Blacksmith") then -- Ignore blacksmith requests, arm the guards manually
- morthutils.writeLine(tool, false, monitor)
- end
- end
- for _,btn in pairs(buttons) do
- if btn.shouldShow() then
- monitor.setCursorPos(btn.x, btn.y)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.lightGray)
- monitor.write(btn.text)
- monitor.setBackgroundColor(colors.black)
- end
- end
- sleep(5)
- end
- end
- parallel.waitForAll(listener, main, depositor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement