abc123mewot

Power distribution system - ComputerCraft

Dec 11th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.29 KB | None | 0 0
  1. --================
  2. --=INITIALIZATION=
  3. --================
  4. print("Initializing configurations")
  5. os.loadAPI("utils")
  6. local mon = utils.mon(12)
  7. local glass = peripheral.wrap("openperipheral_bridge_0")
  8. local glassX, glassY = 1, 20
  9. local bankStructs = {
  10.   ["Main power"] = {bank = utils.cb(2),
  11.     showGlass = true,
  12.     maxInRF = 150000,
  13.     maxOutRF = 150000},
  14.   ["Base power"] = {bank = utils.cb(3),
  15.     showGlass = false,
  16.     maxInRF = 5000,
  17.     maxOutRF = 25000},
  18.   ["ME power"] = {bank = utils.cb(4),
  19.     showGlass = false,
  20.     maxInRF = 2500,
  21.     maxOutRF = 25000},
  22.   ["General power"] = {bank = utils.cb(1),
  23.     showGlass = false,
  24.     maxInRF = 15000,
  25.     maxOutRF = 25000},
  26.   ["IRF power"] = {bank = utils.cb(5),
  27.     showGlass = false,
  28.     maxInRF = 30000,
  29.     maxOutRF = 50000},
  30.   ["MekBase power"] = {bank = utils.cb(6),
  31.     showGlass = false,
  32.     maxInRF = 15000,
  33.     maxOutRF = 25000}
  34. }
  35. local lastEvent = {event = nil, side = nil, x = nil, y = nil}
  36. local hasNewEvent = false
  37. local debugMode = true
  38. print("Initializing capacitor banks")
  39. for k, v in pairs(bankStructs) do
  40.   if v ~= nil and v.bank ~= nil then
  41.     function v.isInputEnabled() return (v.bank.getMaxInput() ~= 0) end
  42.     function v.isOutputEnabled() return (v.bank.getMaxOutput() ~= 0) end
  43.     function v.toggleOutput()
  44.       local o = 0
  45.       if not v.isOutputEnabled() then o = v.maxOutRF end
  46.       v.bank.setMaxOutput(o)
  47.     end
  48.     function v.toggleInput()
  49.       local o = 0
  50.       if not v.isInputEnabled() then o = v.maxInRF end
  51.       v.bank.setMaxInput(o)
  52.     end
  53.   end
  54. end
  55.  
  56. --=======================
  57. --=GRAPHICAL AND UI CODE=
  58. --=======================
  59. print("Initializing graphical code")
  60. --Initialize buttons
  61. local ctrlButtons = {
  62.   outputCtrl = {
  63.     activeText = "Disable output",
  64.     inactiveText = "Enable output",
  65.     onPress = function(bankStruct) bankStruct.toggleOutput() end,
  66.     isPressed = function(bankStruct) return bankStruct.isOutputEnabled() end,
  67.     getColor = function(bankStruct)
  68.       if bankStruct.isOutputEnabled() then return colors.red else return colors.green end
  69.     end,
  70.     getBackColor = function(bankStruct) return colors.gray end
  71.   },
  72.   inputCtrl = {
  73.     activeText = "Disable input",
  74.     inactiveText = "Enable input",
  75.     onPress = function(bankStruct) bankStruct.toggleInput() end,
  76.     isPressed = function(bankStruct) return bankStruct.isInputEnabled() end,
  77.     getColor = function(bankStruct)
  78.       if bankStruct.isInputEnabled() then return colors.red else return colors.green end
  79.     end,
  80.     getBackColor = function(bankStruct) return colors.gray end
  81.   }
  82. }
  83. --Write the fill percentage
  84. function writeFillPerc(mon, perc)
  85.   local x, y = mon.getCursorPos()
  86.   local percStr = string.format("%i", perc)
  87.   local percCol = colors.red
  88.   if perc >= 95 then percCol = colors.cyan
  89.   elseif perc >= 75 then percCol = colors.green
  90.   elseif perc >= 60 then percCol = colors.yellow
  91.   elseif perc >= 30 then percCol = colors.orange end
  92.   utils.monWrt(mon, percStr .. "%", percCol)
  93.   mon.setCursorPos(x + 5, y)
  94. end
  95. --Write buttons and check thier states
  96. function doButtons(mon, bankStruct, buttons, event)
  97.   for k, btn in pairs(buttons) do
  98.     local text = btn.inactiveText
  99.     local xa, ya = mon.getCursorPos()
  100.     if btn.isPressed(bankStruct) then text = btn.activeText end
  101.     local oldCol, oldBCol = mon.getTextColor(), mon.getBackgroundColor()
  102.     mon.setBackgroundColor(btn.getBackColor(bankStruct))
  103.     utils.monWrt(mon, text, btn.getColor(bankStruct))
  104.     local xb, yb = mon.getCursorPos()
  105.     if event ~= nil then
  106.       local touched = utils.chkBds(event.x, event.y, xa, ya, xb, yb)
  107.       if touched then btn.onPress(bankStruct) end
  108.     end
  109.     mon.setTextColor(oldCol)
  110.     mon.setBackgroundColor(oldBCol)
  111.     mon.write(" ")
  112.   end
  113. end
  114. --Draws all the details about a capacitor bank
  115. --to the specified monitor
  116. function drawBank(mon, name, bankStruct, event)
  117.   --Variable initialization
  118.   local bank = bankStruct.bank
  119.   local rfI, rfO = bank.getAverageInputPerTick(), bank.getAverageOutputPerTick()
  120.   local rfIstr, rfOstr = utils.getPwrStr(rfI, true, 3), utils.getPwrStr(rfO, true, 3)
  121.   local rfDiffStr = utils.getPwrStr(rfI - rfO, true)
  122.   local rfC, rfT = bank.getEnergyStored(), bank.getMaxEnergyStored()
  123.   local perc = utils.round((rfC / rfT) * 100)
  124.   --Write the name (centered)
  125.   mon.setTextColor(colors.purple)
  126.   utils.printCentLn(mon, name)
  127.   mon.setBackgroundColor(colors.black)
  128.   utils.monCR(mon)
  129.   --Write statistics
  130.   writeFillPerc(mon, perc)
  131.   utils.monWrtFxdLen(mon, 16, "In: " .. rfIstr, colors.green)
  132.   utils.monWrtFxdLen(mon, 16, "Out: " .. rfOstr, colors.red)
  133.   --Write profit/deficit
  134.   local sym, col = rfDiffStr, colors.red
  135.   if rfI > rfO then
  136.     sym = "+" .. rfDiffStr
  137.     col = colors.green
  138.   elseif rfI == rfO then
  139.     sym = "="
  140.     col = colors.blue
  141.     if(perc >= 100) then
  142.       sym = "F"
  143.       col = colors.cyan
  144.     end
  145.   end
  146.   utils.monWrt(mon, " " .. sym, col)
  147.   utils.monNLCR(mon)
  148. end
  149. function drawGlass(g, bankStruct, bankName, x, y)
  150.   local bank = bankStruct.bank
  151.   local rfI, rfO = bank.getAverageInputPerTick(), bank.getAverageOutputPerTick()
  152.   local rfIstr, rfOstr = utils.getPwrStr(rfI, true, 3), utils.getPwrStr(rfO, true, 3)
  153.   local rfDiffStr = utils.getPwrStr(rfI - rfO, true)
  154.   local rfC, rfT = bank.getEnergyStored(), bank.getMaxEnergyStored()
  155.   local perc = utils.round((rfC / rfT) * 100)
  156.   g.clear()
  157.   local tw = 8
  158.   x, y = x + 1, y + 1
  159.   g.addText(x, y, bankName, 0xFF00FF)
  160.   y = y + tw + 1
  161.   local col = 0xFF0000
  162.   if perc >= 95 then col = 0x00AAFF
  163.   elseif perc >= 75 then col = 0x00FF00
  164.   elseif perc >= 60 then col = 0xFFFF00
  165.   elseif perc >= 30 then col = 0xFFBB00 end
  166.   g.addText(x, y, "Fill: " .. perc .. "%", col)
  167.   y = y + tw + 1
  168.   g.addText(x, y, "In: " .. rfIstr, 0x00FF00)
  169.   y = y + tw + 1
  170.   g.addText(x, y, "Out: " .. rfOstr, 0xFF0000)
  171.   g.sync()
  172. end
  173. --======================
  174. --=MAIN PROCESSING CODE=
  175. --======================
  176. --Debug method
  177. function debug(msg)
  178.   if debugMode then print(msg) end
  179. end
  180. --Main loop
  181. print("Initializing main loop and input loop")
  182. function mainLoop()
  183.   print("Main loop started")
  184.   while true do
  185.     utils.rstMon(mon)
  186.     utils.printCentLn(mon, "Power distribution system")
  187.     local e = nil
  188.     if hasNewEvent then e = lastEvent end
  189.     for bankName, bankStruct in pairs(bankStructs) do
  190.       if(bankStruct ~= nil) then
  191.         drawBank(mon, bankName, bankStruct, e)
  192.         doButtons(mon, bankStruct, ctrlButtons, e)
  193.         utils.monNLCR(mon)
  194.         if glass ~= nil and bankStruct.showGlass then drawGlass(glass, bankStruct, bankName, glassX, glassY) end
  195.       else
  196.         print("Severe error, refer to monitor!")
  197.         utils.monWrt(mon, "ERROR: Bank " .. bankName .. " is nil!", colors.red)
  198.         utils.monNLCR(mon)
  199.       end
  200.     end
  201.     hasNewEvent = false
  202.     sleep(0.1)
  203.   end
  204.   print("Main loop break")
  205. end
  206. function inputLoop()
  207.   print("Input loop started")
  208.   while true do
  209.     local e, s, x, y = os.pullEvent("monitor_touch")
  210.     lastEvent.event = e
  211.     lastEvent.side = s
  212.     lastEvent.x = x
  213.     lastEvent.y = y
  214.     hasNewEvent = true
  215.     debug("Registered touch event!")
  216.   end
  217.   print("Input loop exited")
  218. end
  219. print("Starting main loop, and input loop!")
  220. parallel.waitForAny(inputLoop, mainLoop)
  221. print("Program terminated")
Add Comment
Please, Sign In to add comment