Advertisement
Morthaden

colony-maintainer

Jul 24th, 2022 (edited)
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.32 KB | None | 0 0
  1. shell.run("update-utils")
  2.  
  3. monitor = morthutils.setPeripForCache("monitor", peripheral.wrap("top"))
  4. colony = peripheral.wrap("right")
  5. me = morthutils.getPeripByName("rsBridge")
  6. warehouse = morthutils.getPeripByName("minecolonies:warehouse")
  7. PER_PAGE = 15
  8. currentPage = 1
  9. maxPages = 1
  10.  
  11. monitor.setTextScale(0.5)
  12. monWidth, monHeight = monitor.getSize()
  13.  
  14. buttons = {
  15.     ["nextPage"] = {
  16.         ["func"] = function() currentPage = currentPage + 1 end,
  17.         ["shouldShow"] = function() return currentPage < maxPages end,
  18.         ["text"] = "Next Page",
  19.         ["x"] = 11,
  20.         ["y"] = monHeight-1
  21.     },
  22.     ["prevPage"] = {
  23.         ["func"] = function() currentPage = currentPage + 1 end,
  24.         ["shouldShow"] = function() return currentPage > 1 end,
  25.         ["text"] = "Prev Page",
  26.         ["x"] = 1,
  27.         ["y"] = monHeight-1
  28.     }
  29. }
  30.  
  31. morthutils.clear()
  32. morthutils.clearMonitor(monitor)
  33.  
  34. local function getNeededTools()
  35.     local wip = {}
  36.     pcall(function()
  37.         for i,req in ipairs(colony.getRequests()) do
  38.             if req.count > 0 then
  39.                 table.insert(wip, req.name.." for "..req.target)
  40.             end
  41.         end
  42.     end)
  43.     return wip
  44. end
  45.  
  46. local function isValidMonClick(mx, my)
  47.     for _, btn in pairs(buttons) do
  48.         if mx >= btn.x and mx <= (btn.x + #btn.text) and my == btn.y then
  49.           return btn
  50.         end
  51.     end
  52.     return nil
  53. end
  54.  
  55. local function amtNeededForInv(item_id, inv, amtNeeded)
  56.     local amtInInv = 0
  57.     for k,stack in pairs(inv.list()) do
  58.         if stack.name == item_id then
  59.             amtInInv = amtInInv + stack.count
  60.         end
  61.     end
  62.     return math.max(amtNeeded - amtInInv, 0), amtInInv -- Return both how much we still need to get and how much is already in there
  63. end
  64.  
  65. local function getNeededResources()
  66.     local wip = {}
  67.     for i,order in ipairs(colony.getWorkOrders()) do
  68.         if order.isClaimed then
  69.             for j,matl in ipairs(colony.getWorkOrderResources(order.id)) do
  70.                 if matl.needed and matl.needed > matl.available and matl.status ~= "NOT_NEEDED" then
  71.                 local quant = matl.needed - matl.available
  72.                     local forInv, inInv = amtNeededForInv(matl.item, warehouse, quant)
  73.                     table.insert(wip, {
  74.                         ["displayName"] = matl.displayName,
  75.                         ["item"] = matl.item,
  76.                         ["quantity"] = quant,
  77.                         ["forInv"] = forInv,
  78.                         ["inInv"] = inInv
  79.                     })
  80.                 end
  81.             end
  82.         end
  83.     end
  84.     return wip
  85. end
  86.  
  87. local function listener() -- TODO: Other events if we end up doing that
  88.     while true do
  89.         local event, side, x, y = os.pullEvent("monitor_touch")
  90.         local button = isValidMonClick(x,y)
  91.         if event == "monitor_touch" and button ~= nil then
  92.             button.func(button.args)
  93.         end
  94.     end
  95. end
  96.  
  97. TO_STORAGE_QUEUE = {}
  98.  
  99. local function enQueue(msg)
  100.     table.insert(TO_STORAGE_QUEUE, msg)
  101. end
  102.  
  103. local function nextOfQueue()
  104.     local ret = TO_STORAGE_QUEUE[1]
  105.     table.remove(TO_STORAGE_QUEUE, 1)
  106.     return ret
  107. end
  108.  
  109. local function depositor()
  110.     while true do
  111.         if #TO_STORAGE_QUEUE > 0 then
  112.             me.exportItem(nextOfQueue(), "top")
  113.         end
  114.         sleep(0.1)
  115.     end
  116. end
  117.  
  118.  
  119.  
  120. local function main()
  121.     while true do
  122.         neededResources = getNeededResources()
  123.         table.sort(neededResources, function(a,b) return a.forInv - b.forInv end)
  124.         neededTools = getNeededTools()
  125.         --morthutils.clearMonitor(monitor)
  126.         maxPages = #neededResources / PER_PAGE
  127.        
  128.         morthutils.clearMonitor()
  129.         morthutils.writeLine("Building materials needed:", false, monitor)
  130.         morthutils.writeLine("", false, monitor)
  131.         for i,matl in ipairs(neededResources) do
  132.             if matl.forInv > 0 then -- Try to fill buffer chest with anything we need
  133.                 --print("Exporting "..matl.forInv.." "..matl.item)
  134.                 enQueue({name=matl.item, count=matl.forInv})
  135.             end
  136.             if i >= currentPage and i < currentPage+PER_PAGE then
  137.                 local color = (matl.inInv < matl.quantity and colors.red) or colors.white
  138.                 monitor.setTextColor(color)
  139.                 morthutils.writeLine(matl.displayName.."\t("..matl.item..")\t"..matl.inInv.."/"..matl.quantity.."("..matl.forInv..")", false, monitor)
  140.             end
  141.         end
  142.        
  143.         monitor.setCursorPos(1, PER_PAGE + 5)
  144.         morthutils.writeLine("", false, monitor)
  145.         morthutils.writeLine("Tools needed:", false, monitor)
  146.         for i,tool in ipairs(neededTools) do
  147.             if not tool:match(".*Blacksmith") then -- Ignore blacksmith requests, arm the guards manually
  148.                 morthutils.writeLine(tool, false, monitor)
  149.             end
  150.         end
  151.        
  152.         for _,btn in pairs(buttons) do
  153.             if btn.shouldShow() then
  154.                 monitor.setCursorPos(btn.x, btn.y)
  155.                 monitor.setTextColor(colors.white)
  156.                 monitor.setBackgroundColor(colors.lightGray)
  157.                 monitor.write(btn.text)
  158.                 monitor.setBackgroundColor(colors.black)
  159.             end
  160.         end
  161.         sleep(5)
  162.     end
  163. end
  164.  
  165. parallel.waitForAll(listener, main, depositor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement