The3vilM0nk3y

RequestManager

Sep 15th, 2021 (edited)
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.27 KB | None | 0 0
  1. --todo
  2. --multi monitor support
  3. --  scaling text dependant upon screen size.
  4. function distance(pos,target)
  5.     if pos == nil then
  6.         return "P"
  7.     elseif target == nil then
  8.         return "T"
  9.     end
  10.     return math.floor(math.sqrt(math.pow(target.x - pos.x, 2) + math.pow(target.y - pos.y, 2) + math.pow(target.z - pos.z, 2)) )
  11. end
  12. function round(num,places)
  13.     mult = 10^places
  14.     return math.floor(num * mult + 0.5)/mult
  15. end
  16. function direction(pos, target)
  17.     local dx, dz = target.x - pos.x, target.z - pos.z
  18.   dir ="X"
  19.     if dx^2 + dz^2 > 0 then
  20.     local heading = math.atan2(dz, dx)
  21.     local octant = math.floor(8*heading/(2*math.pi) + 8 + 0.5)%8
  22.     dir = compass[octant + 1]
  23.   end
  24.     if pos.y > target.y then
  25.         return dir .. " \\/"
  26.     elseif pos.y < target.y then
  27.         return dir .. " /\\"
  28.     else
  29.         return dir .. " O"
  30.     end
  31. end
  32. function myErrorHandler(err)
  33.     print("Error: ", err)
  34. end
  35. function jobFromEvent()
  36.  
  37. end
  38. function saveConfig()
  39.     config.requesterList = requesterList
  40.     config.colonyInfo = monitorColonyInfo
  41.     config.builderResourceInfo = showBuilderResourceInfo
  42.     config.delay = delay
  43.     config.arShowAllResourceRequests = arShowAllResourceRequests
  44.     config.owner = owner
  45.     config.doRequests = doRequests
  46.     config.microManage = microManage
  47.     config.chestConfig = chestConfig
  48.     config.defaultSide = "minecraft:chest_0"
  49.     config.showJobInfo = showJobInfo
  50.     --for i,c in pairs(config) do
  51.     --  debugText(c)
  52.     --end
  53.     file = fs.open("managerConfig.lua","w")
  54.     file.write(textutils.serialize(config))
  55.     file.close()
  56. end
  57. function message(t,p)
  58.     if p ~= nil then
  59.         chat.sendMessageToPlayer(t,p)
  60.     else
  61.         debugText(t)
  62.     end
  63. end
  64. function clearScreen()
  65.   term.clear()
  66.   term.setCursorPos(1,1)
  67.   --m.clear()
  68.   --m.setCursorPos(1,1)
  69. end
  70. function debugText(t)
  71.     if m then
  72.         term.redirect(termInstance)
  73.         print(t)
  74.         term.redirect(m[1])
  75.     else
  76.         print(t)
  77.     end
  78. end
  79. function hasValue(tab,val,keySearch)
  80.     if tab ~= nil and val ~= nil then
  81.         for k,v in pairs(tab) do
  82.             if keySearch then
  83.                 if k == val then
  84.                     return true
  85.                 end
  86.             else
  87.                 if v == val then
  88.                     return true
  89.                 end
  90.             end
  91.         end
  92.     end
  93.     return false
  94. end
  95. --currently unused?!?!
  96. function setDisplayObjective(val)
  97.     showRequestInfo = false
  98.     showJobInfo = false
  99.     showResearchInfo = false
  100.     if val == 0 then
  101.         showRequestInfo = true
  102.     elseif val == 1 then
  103.         showJobInfo = true
  104.     elseif val == 2 then
  105.         showResearchInfo = true
  106.     end
  107. end
  108. function updateString(id, str, x, y, c, centered)
  109.     ar.clearElement(id)
  110.     if centered then
  111.         ar.drawCenteredStringWithId(id, str, x, y, c)
  112.     else
  113.         ar.drawStringWithId(id, str, x, y, c)
  114.     end
  115. end
  116. lastActiveQueueCount = 0
  117. lastQueueCount = 0
  118. lastNeededCount = 0
  119. function showAR()
  120.     spacing = 10
  121.     yMod = 60
  122.     --ar.clear()
  123.     updateString("time",formatedTime(),800,10,color, true)
  124.     if ardata.raid then
  125.         updateString("raid","RAID IN PROGRESS", 800, 100, notifyColor, true)
  126.     end
  127.     if ardata.idle > 0 then
  128.         updateString("idle","Idle Count: " .. ardata.idle, 10, yMod, notifyColor)
  129.     else
  130.         updateString("idle","Idle Count: " .. ardata.idle, 10, yMod, color)
  131.     end
  132.     yMod = yMod + spacing
  133.     updateString("pop","Population: " .. ardata.popCount .. " / " .. ardata.totalHousing, 10, yMod, color)
  134.     yMod = yMod + spacing
  135.     updateString("children","Children: " .. ardata.children, 10, yMod, color)
  136.     yMod = yMod + spacing
  137.     updateString("sleeping","Sleeping: " .. ardata.sleeping .. " / " .. ardata.sleepingMax, 10, yMod, color)
  138.     yMod = yMod + spacing
  139.     --Research Info
  140.     if ardata.research.currentAmount < ardata.research.max then
  141.         updateString("research","Research: " .. ardata.research.currentAmount .. " / " .. ardata.research.max, 10, yMod, notifyColor)
  142.     else
  143.         updateString("research","Research: " .. ardata.research.currentAmount .. " / " .. ardata.research.max, 10, yMod, color)
  144.     end
  145.     --Building Queue
  146.     yMod = yMod + spacing
  147.     if ardata.activeBuildCount < ardata.builderCount then
  148.         updateString("buildcount","Buildings In Progress: " .. ardata.activeBuildCount .. " / " .. ardata.builderCount, 10, yMod, notifyColor)
  149.     else
  150.         updateString("buildcount","Buildings In Progress: " .. ardata.activeBuildCount .. " / " .. ardata.builderCount, 10, yMod, headerColor)
  151.     end
  152.     --for i=1, lastActiveQueueCount do
  153.     --  ar.clearElement("activequeue"..i)
  154.     --end
  155.     -- r = 01
  156.     -- for i=1, #ardata.buildQueue do
  157.     --  if ardata.buildQueue[i].queued ~= true then
  158.     --      yMod = yMod + spacing
  159.     --      r = r + 1
  160.     --      updateString("activequeue"..r,"  " .. ardata.buildQueue[i].name .. ardata.buildQueue[i].toLevel, 10, yMod, color)
  161.     --  end
  162.     -- end
  163.     -- lastActiveQueueCount = r
  164.     yMod = yMod + spacing
  165.     updateString("buildqueuelabel","Buildings Queued: " .. ardata.totalQueueCount, 10, yMod, headerColor)
  166.     -- for i=1, lastQueueCount do
  167.     --  ar.clearElement("buildqueue"..i)
  168.     -- end
  169.     -- r = 0
  170.     -- for i=1, #ardata.buildQueue do
  171.     --  if ardata.buildQueue[i].queued then
  172.     --      yMod = yMod + spacing
  173.     --      r = r + 1
  174.     --      updateString("buildqueue"..r,"  " .. ardata.buildQueue[i].name, 10, yMod, color)
  175.     --  end
  176.     -- end
  177.     -- lastQueueCount = r
  178.     --Resource Queue
  179.     if doRequests then
  180.         ar.clearElement("disabledInfo")
  181.         yMod = yMod + spacing
  182.         if #ardata.neededItems > 0 then
  183.             updateString("resourcelabel","Resourses Needed: ", 10, yMod, headerColor)
  184.         else
  185.             updateString("resourcelabel","Resourses Needed: ", 10, yMod, color)
  186.         end
  187.         alertStates = {"No Recipe","Missing Items to Craft","Waiting On CPU's","Unable to Craft"}
  188.         for i=1, lastNeededCount do
  189.             ar.clearElement("needed"..i)
  190.         end
  191.         r = 0
  192.         for i=1, #ardata.neededItems do
  193.             if arShowAllResourceRequests then
  194.                 yMod = yMod + spacing
  195.                 if hasValue(alertStates, ardata.neededItems[i].status) then iColor = warnColor
  196.                 else iColor = color end
  197.                 r = r + 1
  198.                 updateString("needed" .. r,"  " .. ardata.neededItems[i].displayName .. " " ..  ardata.neededItems[i].count .. "   " .. ardata.neededItems[i].status, 10, yMod, iColor)
  199.  
  200.             else
  201.                 if hasValue(alertStates,ardata.neededItems[i].status) then
  202.                     yMod = yMod + spacing
  203.                     r = r + 1
  204.                     updateString("needed" .. r,"  " .. ardata.neededItems[i].displayName .. " " ..  ardata.neededItems[i].count .. "   " .. ardata.neededItems[i].status, 10, yMod, color)
  205.  
  206.                 end
  207.             end
  208.         end
  209.         lastNeededCount = r
  210.     else
  211.         yMod = yMod + spacing
  212.         updateString("disabledInfo","Resource Manager disabled", 10, yMod, color)
  213.     end
  214.     -- --Citizen Heath
  215.     -- pPos = playerDetector.getPlayerPos("RedStoner_Pro")
  216.     -- if pPos ~= nil then
  217.     -- --debugText(textutils.serialize(pPos))
  218.     --
  219.     -- for i=1, #ardata.health do
  220.     --  cit = ardata.health[i]
  221.     --  lColor = color
  222.     --  healthPercent = round((cit.health / cit.maxHealth)* 100,1)
  223.     --
  224.     --  if healthPercent < 80 then
  225.     --      lColor = warnColor
  226.     --  end
  227.     --  if healthPercent < 50 then
  228.     --      lColor = notifyColor
  229.     --  end
  230.     --  ar.drawRightboundString(cit.name .. " HP: " .. healthPercent .. "%" .. direction(pPos,cit.location) .. " D:" .. distance(pPos,cit.location), -10, -30 - ((i-1) * spacing), lColor)
  231.     -- end
  232.     -- --ar.drawRightboundString(tostring(direction(pPos,{x= 2382, z= 5662, y= 71})) , -10, 150, notifyColor)
  233.     --
  234.     -- --Citizens sleeping info
  235.     -- for i=1, #ardata.sleep do
  236.     --  yMod = -30 - ((i-1)*20)
  237.     --  cit = ardata.sleep[i]
  238.     --  ar.drawString(cit.name .. " " .. direction(pPos,cit.location) .. " D:" .. distance(pPos,cit.location), 100, yMod ,color)
  239.     -- end
  240. end
  241. function availableCPUs()
  242.     cpus = me.getCraftingCPUs()
  243.     available = 0
  244.     if cpus then
  245.         for _,cpu in pairs(cpus) do
  246.             if cpu.isBusy == false then
  247.                 available = available + 1
  248.             end
  249.         end
  250.     end
  251.     return available
  252. end
  253. function bootCycle()
  254.     print("Doing Boot Delay")
  255.     os.sleep(15)
  256. end
  257. function filterRequests(v)
  258.     for i=1, #requesterList do
  259.         if string.find(v.target, requesterList[i]) then
  260.             --debugText(v.target .. " is on the list")
  261.             return true
  262.         end
  263.     end
  264. end
  265. function formatedTime()
  266.     theTime = os.time()
  267.     hour = math.floor(theTime)
  268.     minutes = math.floor(60 * (theTime%1))
  269.     if minutes < 10 then
  270.         minutes = "0"..minutes
  271.     end
  272.     return hour .. ":" .. minutes
  273. end
  274. function checkConfigValues()
  275.   doSave = false
  276.   if requesterList == nil then
  277.         requesterList = {"Builder","Miner", "Library Student", "Request System", "Teacher"}
  278.         doSave = true
  279.     end
  280.     if monitorColonyInfo == nil then
  281.       monitorColonyInfo = true
  282.         doSave = true
  283.     end
  284.     if arShowAllResourceRequests == nil then
  285.         arShowAllResourceRequests = true
  286.         doSave = true
  287.     end
  288.     if showBuilderResourceInfo == nil then
  289.         showBuilderResourceInfo = false
  290.         doSave = true
  291.     end
  292.     if delay == nil then
  293.         delay = 1
  294.         doSave = true
  295.     end
  296.     if doRequest == nil then
  297.         doRequests = true
  298.         doSave = true
  299.     end
  300.     if microManage == nil then
  301.         microManage = false
  302.         doSave = true
  303.   end
  304.     if chestConfig == nil then
  305.         chestConfig = {
  306.             {{x=0,y=0,z=0}, "minecraft:chest_1"},
  307.             {{x=0,y=0,z=0}, "minecraft:chest_2"},
  308.             {{x=0,y=0,z=0}, "minecraft:chest_3"},
  309.             {{x=0,y=0,z=0}, "minecraft:chest_4"},
  310.         }
  311.         doSave = true
  312.     end
  313.     if defaultSide == nil then
  314.         defaultSide = "minecraft:chest_0"
  315.         doSave = true
  316.     end
  317.     if showJobInfo == nil then
  318.         showJobInfo = true
  319.         doSave = true
  320.     end
  321.     if doSave then
  322.         saveConfig()
  323.     end
  324. end
  325. function getPushDirection(tName, directionName)
  326.     --first find the citizens info
  327.     for k,v in pairs(colony.getCitizens()) do
  328.         --check if they are a worker
  329.         if v.work ~= nil then
  330.             --check if this is the citizen we want to check sides for
  331.             --debugText("Comparing - " .. string.lower(v.work.type .. " " .. v.name) .. " to " .. string.lower(tName))
  332.             if string.lower(v.work.type .. " " .. v.name) == string.lower(tName) then
  333.                 --check if they have an assigned direction.
  334.                     wLoc = v.work.location
  335.                 --debugText("Found Matching citizen\nComparing - " .. wLoc.x .. " " .. wLoc.y .. " " .. wLoc.z )
  336.                 for _,ent in pairs(chestConfig) do
  337.                     pos = ent[1]
  338.                     --debugText("To - " .. pos.x .. " " .. pos.y .. " " .. pos.z)
  339.                     if wLoc.x == pos.x and wLoc.y == pos.y and wLoc.z == pos.z then
  340.                         --debugText("foud matching coords on side - " .. ent[2])
  341.                         directionName = ent[2]
  342.                         break
  343.                     end
  344.                 end
  345.                 return directionName
  346.             end
  347.         end
  348.     end
  349.     --debugText("Falling back to default side")
  350.     return directionName
  351. end
  352. --monitormanager start
  353. function init()
  354.   print("Initializing monManager")
  355.   mData = {}
  356.   if fs.exists("monitorConfig.lua") then
  357.     mConfig = loadMConfig()
  358.   end
  359.   checkMConfig()
  360.   print("Config confirmed. \nNum monitors - " .. #m)
  361.   for i=1, #m do
  362.     tData = {peripheral = m[i], name = peripheral.getName(m[i]), buttons = {}}
  363.     m[i].clear()
  364.     m[i].setCursorPos(1,1)
  365.     --m[i].setTextScale(.5)
  366.     print("Adding " .. tData.name)
  367.     mData[i] = tData
  368.   end
  369. end
  370. function loadMConfig()
  371.  
  372.   file = fs.open("monitorConfig.lua","r")
  373.   content = file.readAll()
  374.   file.close()
  375.   return textutils.unserialize(content)
  376. end
  377. function saveMConfig()
  378.   file = fs.open("monitorConfig.lua","w")
  379.   file.write(textutils.serialize(mConfig))
  380.   file.close()
  381. end
  382. function checkMConfig()
  383.   for k,v in pairs(m) do
  384.     if mConfig[k] == nil then
  385.       mConfig[k] = {true, true, true, true, false}
  386.     end
  387.   end
  388.   saveMConfig()
  389. end
  390. function showButtons(dIndex)
  391.   mData[dIndex].buttons = {}
  392.   mon = mData[dIndex].peripheral
  393.   --buttons = {"Colony", "Requests", "Queue", "Research", "Citizens"}
  394.   buttons = {"Col", "Req", "Que", "Res", "Cit"}
  395.   --get size to try to space evenly
  396.   x,by = mon.getSize()
  397.   tLength = (#buttons * 4) - 1
  398.   remainingSpace = x - tLength
  399.   extraSpacing = remainingSpace / #buttons + 1
  400.   floatingSpacing = remainingSpace % #buttons + 1
  401.   --print("Available Width - " .. x .. "\nTotal Length - " .. tLength .. "\nRemaining Space - ".. remainingSpace .. "\nExtraSpacing - ".. extraSpacing .. "\nFloating Spacing - ".. floatingSpacing)
  402.   --check if we can add extra spacing
  403.   if extraSpacing >= 1 then
  404.     --Add in extra spacing
  405.   end
  406.   --_,by = mon.getCursorPos()
  407.   lastX = 1 + math.floor(floatingSpacing/2);
  408.   for k,v in pairs(buttons) do
  409.     --add an extra space if there is a remainder left
  410.     xSpace = math.floor(extraSpacing)
  411.     bx = lastX + xSpace
  412.     -- add one here to add a space between ones in the middle
  413.     lastX = bx + 1
  414.     --push button's coords to data structure
  415.     table.insert(mData[dIndex].buttons,{x1 = bx, x2 = bx+2, y=by})
  416.     --set colors based on current value
  417.     tc = colors.white
  418.     bc = colors.red
  419.     if mConfig[dIndex][k] == true then
  420.       tc = colors.white
  421.       bc = colors.green
  422.     end
  423.     --print the button to the monitor
  424.     mon.setCursorPos(bx, by)
  425.     mon.setBackgroundColor(bc)
  426.     mon.setTextColor(tc)
  427.     mon.write(v)
  428.   end
  429.   mon.setBackgroundColor(colors.black)
  430.   mon.setTextColor(colors.white)
  431.   mon.write(v)
  432. end
  433. function getButton(mon,x,y)
  434.   for k,v in pairs(mon.buttons) do
  435.     if y == v.y and x >= v.x1 and x <= v.x2 then
  436.       return k
  437.     end
  438.   end
  439.   return 0
  440. end
  441. function getMonByName(n)
  442.   for k,v in pairs(mData) do
  443.     if v.name == n then
  444.       return k
  445.     end
  446.   end
  447. end
  448. function getConfigButton(x,y)
  449.  
  450. end
  451. function printConfigButtons()
  452.    
  453. end
  454. function getButtonClick()
  455.     lx = 0
  456.     ly = 0
  457.     while true do
  458.       timerId = os.startTimer(.5)
  459.       while true do
  460.         event, p1, x, y = os.pullEvent()
  461.         if event == "monitor_touch" and (x ~= lx and y ~= ly) then
  462.           --check if there is a button at that location
  463.           mon = getMonByName(p1)
  464.           bIndex = getButton(mData[mon],x,y)
  465.           if bIndex ~= 0 then
  466.             --toggleSection for this monitor based on button clicked
  467.             mConfig[mon][bIndex] = not mConfig[mon][bIndex]
  468.             saveMConfig()
  469.             --debugText( "Set - " .. bIndex .. " to " .. tostring(mConfig[mon][bIndex]))
  470.                     showButtons(mon)
  471.                     --try to prevent double event detection
  472.                     lx = x
  473.                     ly = y
  474.             break
  475.           end
  476.         elseif event == "timer" then
  477.           --check if we have the timer event
  478.           if p1 == timerId then
  479.             --debugText("timer")
  480.                     lx = 0
  481.                     ly = 0
  482.             break
  483.           end
  484.             elseif event == "mouse_click" then
  485.                 bIndex = getConfigButton(x,y)
  486.             end
  487.       end
  488.     end
  489. end
  490. function makeDashes(mId)
  491.     x,y = mId.getSize()
  492.     t = ""
  493.     for i=1, x do
  494.         t = t .. "-"
  495.     end
  496.     return t
  497. end
  498. function addDashes(s,c)
  499.     return s:gsub("%^d", c)
  500. end
  501. --monman end
  502. function main()
  503.     while true do
  504.         ardata = {}
  505.         outputText = ""
  506.         if doRequests then
  507.             requestText = "^d\nColony Request Manager\n^d\n"
  508.             requests = colony.getRequests()
  509.             for _,v in pairs(requests) do
  510.                 --filter only wanted requests
  511.                 if (v.target ~= nil) then
  512.                     if filterRequests(v) then
  513.                         --check if this is a new request
  514.                         --v.items[1].name
  515.                         --debugText(v.target .. " " .. v.name .. " " .. v.count)
  516.                         testName = v.target .. v.name .. v.count
  517.                         if savedRequests[testName] == nil then
  518.                             --add if not
  519.                             savedRequests[testName] = {}
  520.                             savedRequests[testName].name = v.items[1].name
  521.                             savedRequests[testName].displayName = v.items[1].displayName
  522.                             savedRequests[testName].count = v.count
  523.                             savedRequests[testName].target = v.target
  524.                             savedRequests[testName].count = v.count
  525.                             savedRequests[testName].filled = false
  526.                             savedRequests[testName].crafting = false
  527.                             --dbtext = dbtext .. "Added Key- " .. testName .. "\n"
  528.                         end
  529.                     end
  530.                 end
  531.             end
  532.             rmKeys = {}
  533.             --Check for completed requests that are still saved.
  534.             for savedKey,savedVal in pairs(savedRequests) do
  535.  
  536.                 foundMatch = false
  537.                 for reqKey, reqVal in pairs(requests) do
  538.  
  539.                     if reqVal.target ~= nil then
  540.                         testName = reqVal.target .. reqVal.name ..  reqVal.count
  541.                         if testName == savedKey then
  542.                             foundMatch = true
  543.                             break
  544.                         end
  545.                     end
  546.                 end
  547.                 if foundMatch == false then
  548.                     rmKeys[#rmKeys +1] = savedKey
  549.                 end
  550.             end
  551.             --Remove completed requests
  552.             for _,v in pairs(rmKeys) do
  553.                 savedRequests[v] = nil
  554.             end
  555.             --Query the ME system for the requested item
  556.             ardata.neededItems = {}
  557.             cpuCount = availableCPUs()
  558.  
  559.  
  560.  
  561.             for _,v in pairs(savedRequests) do
  562.                 --Check if request has been handled already
  563.                 if not v.filled then
  564.  
  565.                     meItemInfo = me.getItem({name=v.name})
  566.                     --if nil we may just be out of items
  567.                     if meItemInfo == nil and me.isItemCraftable({name=v.name}) then
  568.                         meItemInfo = {}
  569.                         meItemInfo.name = v.name
  570.                         meItemInfo.amount = 0
  571.                         meItemInfo.isCraftable = true
  572.                     end
  573.                     --Check if the item requested is not nil or air
  574.                     if meItemInfo == nil and not me.isItemCraftable({name=v.name}) then
  575.                         v.filled = false
  576.                         v.status = "No Recipe"
  577.                         --debugText(v.name .. " " .. tostring(meItemInfo))
  578.                     elseif v.name == "minecraft:air" then
  579.                         v.filled = false
  580.                         v.status = "Bad Item Name"
  581.  
  582.                     else
  583.                         --Check if there is currently enough.
  584.                         if meItemInfo.amount > 0 and meItemInfo.amount >= v.count then
  585.                             --get direction to push the items use default if not micromanaging
  586.                             pushDirection = defaultSide
  587.                             pStatus, out = nil
  588.                             if microManage then
  589.                                 pushDirection = getPushDirection(v.target, defaultSide)
  590.                                 --debugText("Pushed to " .. pushDirection .. " for " .. v.target)
  591.                                 pStatus, out = pcall(me.exportItemToPeripheral,{name=v.name, count=v.count},pushDirection)
  592.                             else
  593.                                 pStatus, out = pcall(me.exportItemToPeripheral,{name=v.name, count=v.count},defaultSide)
  594.                             end
  595.                             --move the items and save the request as filled if there is
  596.                             if pStatus then
  597.                                 --debugText("Pushed " .. v.count .. " " .. v.name .. " to " .. pushDirection .. " for " .. v.target)
  598.                                 v.status = "Filled"
  599.                                 v.filled = true
  600.                             else
  601.                                 v.status = "Offline Error"
  602.                                 debugText(out)
  603.                             end
  604.                         elseif v.crafting then
  605.                             v.status = "ME Crafting"
  606.                         else
  607.                             --check for available crafting cpus
  608.                             if me.isItemCraftable({name=v.name}) then
  609.                                 if cpuCount > 0 then
  610.                                     --Check if its crafting already
  611.                                     if v.crafting == false then
  612.                                         v.craftCount = v.count - meItemInfo.amount
  613.                                         me.craftItem({name=v.name, count=v.craftCount})
  614.                                         os.sleep(.05)
  615.                                         successCraft = me.isItemCrafting({name=v.name})
  616.                                         if successCraft then
  617.                                             v.crafting = true
  618.                                             v.status = "Requesting"
  619.                                             cpuCount = cpuCount - 1
  620.                                         else
  621.                                             v.status = "Unable to Craft"
  622.                                         end
  623.                                     else
  624.                                         if me.isItemCrafting({name=v.name}) then
  625.                                             v.status = "ME Crafting"
  626.                                         else
  627.                                             if v.status == "Finalizing Craft" then
  628.                                                 if v.counter == 5 then
  629.                                                     if v.retry then
  630.                                                         v.status = "Failed to Craft!"
  631.                                                         v.crafting = false
  632.                                                     else
  633.                                                         v.crafting = false
  634.                                                         v.retry = true
  635.                                                         v.status = "Attempting Re-Crafting"
  636.                                                     end
  637.                                                 else
  638.                                                     v.counter = v.counter + 1
  639.                                                 end
  640.                                             else
  641.                                                 v.status = "Finalizing Craft"
  642.                                                 v.counter = 1
  643.                                             end
  644.                                         end
  645.                                     end
  646.                                 else
  647.                                     v.status = "Waiting On CPU's"
  648.                                 end
  649.                             else
  650.                                 v.status = "No Recipe"
  651.                             end
  652.                         end
  653.                     end
  654.                 end
  655.                 table.insert(ardata.neededItems,{count = v.count, name = v.name, displayName = v.displayName, status = v.status})
  656.                 requestText = requestText .. v.displayName .. " - " .. v.status .. "\n"
  657.             end
  658.         end
  659.         colonyText, currentJobList, queueText, researchText = colonyInfo.showColonyInfo()
  660.  
  661.         if not doRequests then
  662.             requestText = "Requests are Disabled\n"
  663.         end
  664.         jobText = "^d\nJob List\n^d\n"
  665.         if showJobInfo then
  666.             for cJob,cAmount in pairs(currentJobList)  do
  667.               jobText = jobText .. cJob .. " - " .. cAmount .. "\n"
  668.             end
  669.         else
  670.             jobText = "Job Info is Disabled\n"
  671.         end
  672.         if m then
  673.             for i,mon in pairs(m) do
  674.                 outputText = ""
  675.                 term.redirect(mon)
  676.                 clearScreen()
  677.                 d = makeDashes(mon)
  678.                 --display the buttons
  679.                 if mConfig[i][1] then outputText = outputText .. addDashes(colonyText, d) end
  680.                 if mConfig[i][3] then outputText = outputText .. addDashes(queueText, d) end
  681.                 if mConfig[i][4] then outputText = outputText .. addDashes(researchText, d) end
  682.                 if mConfig[i][5] then outputText = outputText .. addDashes(jobText, d) end
  683.                 if mConfig[i][2] then outputText = outputText .. addDashes(requestText, d) end
  684.                 print(outputText)
  685.                 showButtons(i)
  686.             end
  687.         end
  688.         if ar then
  689.             showAR()
  690.         end
  691.         if chat then
  692.             timerID = os.startTimer(delay)
  693.             while true do
  694.                 event, k1, k2, k3 = os.pullEvent()
  695.                 if event == "timer" and k1 == timerID then
  696.                     break
  697.                 elseif event == "request" then
  698.                     debugText("Chat Event Trigger")
  699.                     command = k1
  700.                     job = k2
  701.                     player = k3
  702.                     onList = hasValue(requesterList,job)
  703.                     --debugText(tostring(player))
  704.                     if command == "add" then
  705.                         if onList ~= true then
  706.                             table.insert(requesterList,job)
  707.                             message(job .. " was added to the list.", player)
  708.                             saveConfig()
  709.                         else
  710.                             message(job .. " is already on the list.", player)
  711.                         end
  712.                     elseif command == "rm" then
  713.                         if onList == true then
  714.                             for k,v in pairs(requesterList) do
  715.                                 if v == job then
  716.                                     requesterList[k] = nil
  717.                                     message(job .. " was removed from the list.", player)
  718.                                     saveConfig()
  719.                                     break
  720.                                 end
  721.                             end
  722.                         else
  723.                             message(job .. " is not on the list.", player)
  724.                         end
  725.                     elseif command == "list" then
  726.                         jobList = ""
  727.                         for k,v in pairs(requesterList) do
  728.                             jobList = jobList .. v .. ", "
  729.                         end
  730.                         message("Current List: " .. jobList  , player)
  731.                     elseif command == "arShowAll" then
  732.                         if arShowAllResourceRequests then
  733.                             arShowAllResourceRequests = false
  734.                         else
  735.                             arShowAllResourceRequests = true
  736.                         end
  737.                         message("Showing All Requests: " .. tostring(arShowAllResourceRequests)  , player)
  738.                         saveConfig()
  739.                     elseif command == "jobs" then
  740.                         s = ""
  741.                         for k,v in pairs(currentJobList) do
  742.                             s = "  " .. s .. k .. " - " .. v .. "\n"
  743.                         end
  744.                         message("Current Employment\n" .. s, player)
  745.                     elseif command == "output" then
  746.                         newOut = textutils.unserialize(k2)
  747.                         foundChestMatch = false
  748.                         --check if we already have an entry for this inventory
  749.                         for k,v in pairs(chestConfig) do
  750.                             --if we do, override it.
  751.                             if v[2] == newOut[2] then
  752.                                 chestConfig[k] = newOut
  753.                                 foundChestMatch = true
  754.                                 break
  755.                             end
  756.                         end
  757.                         if foundChestMatch == false then
  758.                             table.insert(chestConfig, newOut)
  759.                         end
  760.                         saveConfig()
  761.                     elseif command == "doRequests" then
  762.                       if doRequests then
  763.                             doRequests = false
  764.                         else
  765.                           doRequests = true
  766.                         end
  767.                         saveConfig()
  768.                     end
  769.  
  770.                     os.queueEvent("confirm",true)
  771.                 end
  772.             end
  773.         else
  774.             os.sleep(delay)
  775.         end
  776.     end
  777. end
  778.  
  779.  
  780. requesterListFull = {"Baker", "Beekeeper", "Blacksmith", "Builder", "Carpenter", "Chicken Farmer", "Composter", "Concrete Mixer", "Cook", "Assistant Cook", "Courier", "Cowhand", "Crusher", "Dyer", "Enchanter", "Farmer", "Fisher", "Fletcher", "Flourist", "Forester", "Glassblower", "Knight", "Archer", "Knight in Training", "Library Student", "Mechanic", "Miner", "Planter", "Pupil", "Rabbit Herder", "Shepherd", "Sifter", "Smelter", "Stonemason", "Stone Smelter", "Swineherd", "Teacher", "Undertaker", "Researcher"}
  781. color = 0xBBBBBB
  782. notifyColor = 0xff2222
  783. headerColor = 0x22ff22
  784. warnColor = 0xF6FF4F
  785. compass = {"E", "SE", "S", "SW","W", "NW", "N", "NE"}
  786. me = peripheral.find("meBridge")
  787. m = {peripheral.find("monitor")}
  788. ar = peripheral.find("arController")
  789. chat = peripheral.find("chatBox")
  790. colony = peripheral.find("colonyIntegrator")
  791. playerDetector = peripheral.find("playerDetector")
  792. requesterList = {}
  793. config = {}
  794. if ar then
  795.     ar.setRelativeMode(true, 1600, 900)
  796.     ar.clear()
  797. end
  798. passedChecks = true
  799. if me == nil then
  800.     print("Failed ME")
  801.     passedChecks = false
  802. end
  803. if colony == nil then
  804.     print("Colony Peripheral not attached")
  805.     passedChecks = false
  806. end
  807. if not colony.isInColony() then
  808.     print("Peripheral is not in a Colony")
  809.     passedChecks = false
  810. end
  811. colonyInfo = require("colonyInfo")
  812. if colonyInfo == nil then
  813.     print("Failed Colony Check")
  814.     passedChecks = false
  815. end
  816. mConfig = {}
  817. mData = {}
  818. init()
  819. --setup monitor scaling
  820. _tm = {}
  821. _mNames = {}
  822. for i=1, #m do
  823.     tx, ty = m[i].getSize()
  824.     if tx <=18 or ty <= 12 then
  825.         m[i].setTextScale(.5)
  826.     end
  827.     n = peripheral.getName(m[i])
  828.     fMatch = false
  829.     for k,v in pairs(_mNames) do
  830.         if v == n then
  831.             fMatch = true
  832.             break
  833.         end
  834.     end
  835.     if fMatch == false then
  836.         table.insert(_tm, m[i])
  837.         table.insert(_mNames, n)
  838.     end
  839. end
  840. m = _tm
  841. if #m >= 1 then
  842.     termInstance = term.redirect(m[1])
  843. end
  844. if fs.exists("managerConfig.lua") then
  845.     defaultRequester = {"Builder","Miner", "Library Student", "Request System", "Teacher"}
  846.     file = fs.open("managerConfig.lua", "r")
  847.     config = textutils.unserialize(file.readAll())
  848.     file.close()
  849.     requesterList = config.requesterList
  850.     monitorColonyInfo = config.colonyInfo
  851.     arShowAllResourceRequests = config.arShowAllResourceRequests
  852.     showBuilderResourceInfo = config.builderResourceInfo
  853.     delay = config.delay
  854.     doRequests = config.doRequests
  855.     microManage = config.microManage
  856.     chestConfig = config.chestConfig
  857.     defaultSide = config.defaultSide
  858.     showJobInfo = config.showJobInfo
  859.     checkConfigValues()
  860. else
  861.     requesterList = {"Builder","Miner", "Library Student", "Request System", "Teacher"}
  862.     monitorColonyInfo = true
  863.     arShowAllResourceRequests = true
  864.     showBuilderResourceInfo = false
  865.     delay = 1
  866.     doRequests = true
  867.     microManage = false
  868.     chestConfig = {
  869.         {{x=0,y=0,z=0}, "minecraft:chest_1"},
  870.         {{x=0,y=0,z=0}, "minecraft:chest_2"},
  871.         {{x=0,y=0,z=0}, "minecraft:chest_3"},
  872.         {{x=0,y=0,z=0}, "minecraft:chest_4"},
  873.     }
  874.     defaultSide = "minecraft:chest_0"
  875.     showJobInfo = true
  876. end
  877. saveConfig()
  878. debugText("Colony Manager is now running\nArgs:")
  879. debugText("monitorColonyInfo - " .. tostring(monitorColonyInfo))
  880. debugText("arShowAllResourceRequests - " .. tostring(arShowAllResourceRequests))
  881. debugText("showBuilderResourceInfo - " .. tostring(showBuilderResourceInfo))
  882. debugText("doRequests - " .. tostring(doRequests))
  883. debugText("microManage - " .. tostring(microManage))
  884. debugText("-------------\nLog\n-------------")
  885.  
  886.  
  887. savedRequests = {}
  888.  
  889.  
  890. if chat then
  891.     multishell.launch({},"chatListener.lua")
  892. end
  893. showRequestInfo = true
  894. --bootCycle()
  895. clearScreen()
  896. if passedChecks then
  897.     parallel.waitForAny(main,getButtonClick)
  898. end
  899.  
Advertisement
Add Comment
Please, Sign In to add comment