mast3rillusion

power.lua

Jul 27th, 2021 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. os.loadAPI("settings.lua")
  2. os.loadAPI("graphics.lua")
  3. os.loadAPI("sliders.lua")
  4. os.loadAPI("buttons.lua")
  5. os.loadAPI("notifications.lua")
  6. os.loadAPI("redstone.lua")
  7.  
  8. controlBtn = nil
  9. autoPilotBtn = nil
  10. redstoneOn = false
  11. oldPowerLevel = 0
  12.  
  13. energyPercentage = 0
  14. maxEnergy = 0
  15. totalEnergy = 0
  16. energyNeeded = 0
  17.  
  18. cycles = 0
  19.  
  20. local powerCells = {}
  21. found = false
  22.  
  23. for _, name in pairs(peripheral.getNames()) do
  24.     if peripheral.getType(name) == "peripheralProxy:mekanismMachine" then --# change "monitor" to your peripheral type.
  25.         periph = peripheral.wrap(name)
  26.         table.insert(powerCells, periph)
  27.         found = true
  28.     end
  29. end
  30.  
  31. if found == false then
  32.     error("There must be at least one PeripheralProxy attached to a Mekanism Energy Cube hooked up to this computer via wired modem!")
  33. end
  34.  
  35. function getPowerCellsInfo()
  36.     maxEnergy = 0
  37.     totalEnergy = 0
  38.     energyNeeded = 0
  39.     energyPercentage = 0
  40.     for k,v in pairs(powerCells) do
  41.         if v ~= nill then
  42.         maxEnergy = maxEnergy + v.getTotalMaxEnergy()
  43.         totalEnergy = totalEnergy + v.getTotalEnergy()
  44.         energyNeeded = energyNeeded + v.getTotalEnergyNeeded()
  45.         end
  46.     end
  47.     energyPercentage = (totalEnergy / maxEnergy) * 100
  48. end
  49.  
  50. function setupPowerBars()
  51.     w, h = term.getSize()
  52.     sliders.createSlider("powerSlider", 2, 8, w-2, 1, colors.lime, colors.red)
  53. end
  54.  
  55. function activateRedstone(txt, bkgColor, txtColor)
  56.     notifications.addNotification("generator_switch", 4, txt, bkgColor, txtColor)
  57.     redstone.toggleRedstone(settings.redstonePowerSide, true)
  58.     redstoneOn = true
  59.     controlBtn.text = "Stop"
  60.     controlBtn.bkgColor = colors.red
  61. end
  62.  
  63. function deactivateRedstone(txt, bkgColor, txtColor)
  64.     notifications.addNotification("generator_switch", 4, txt, bkgColor, txtColor)
  65.     redstone.toggleRedstone(settings.redstonePowerSide, false)
  66.     redstoneOn = false
  67.     controlBtn.text = "Start"
  68.     controlBtn.bkgColor = colors.lime
  69. end
  70.  
  71. function toggleMachine()
  72.     checked = autoPilotBtn.checked
  73.     if checked == true  and redstoneOn == false then
  74.         notifications.addNotification("generator_switch", 4, "Cannot activate, auto pilot enabled!", colors.red, colors.black)
  75.         return
  76.     elseif checked == true  and redstoneOn == true then
  77.         notifications.addNotification("generator_switch", 4, "Cannot deactivate, auto pilot enabled!", colors.red, colors.black)
  78.         return
  79.     elseif checked == false then
  80.         if energyNeeded == 0 then
  81.             notifications.addNotification("generator_switch", 4, "Cannot activate, power is full!", colors.red, colors.black)
  82.             return
  83.         elseif redstoneOn == false then
  84.             activateRedstone("Power gen activated", colors.lime, colors.white)
  85.             return
  86.         elseif redstoneOn == true then
  87.             deactivateRedstone("Power gen deactivated", colors.red, colors.white)
  88.             return
  89.         end
  90.     end
  91. end
  92.  
  93. function autoPilot()
  94.     checked = autoPilotBtn.checked
  95.     if checked == true then
  96.         notifications.addNotification("autopilot", 4, "Auto pilot enabled", colors.lime, colors.white)
  97.  
  98.         if energyPercentage < settings.lowPowerThreshold then
  99.             activateRedstone("Power gen auto activated", colors.lime, colors.white)
  100.         else
  101.             deactivateRedstone("Power gen auto deactivated", colors.lime, colors.white)
  102.         end
  103.     else
  104.         notifications.addNotification("autopilot", 4, "Auto pilot disabled", colors.red, colors.white)
  105.         deactivateRedstone("Auto pilot disabled", colors.red, colors.white)
  106.     end
  107. end
  108.  
  109. function setupControlBtns()
  110.     controlBtn = buttons.createBtn("Start",colors.lime,3,7, w - 9, 2)
  111.     controlBtn.trigger = function()
  112.         toggleMachine()
  113.     end
  114.     buttons.addButton(controlBtn)
  115.  
  116.     -- Autopilot checkbox/ button
  117.     autoPilotBtn = buttons.createCheckbox("Auto Pilot",w - 22,3,settings.autoPilot,settings.backgroundColor)
  118.     autoPilotBtn.trigger = function()
  119.         autoPilot()
  120.     end
  121.     buttons.addButton(autoPilotBtn)
  122.     if settings.autoPilot == true then
  123.         autoPilot()
  124.     end
  125. end
  126.  
  127. function displayPowerInfo()
  128.     local w, h = term.getSize()
  129.     powerPercentage = energyPercentage
  130.     powerLevel = totalEnergy
  131.     maxPowerLevel = maxEnergy
  132.  
  133.     if powerLevel ~= oldPowerLevel then
  134.         inputPower = (powerLevel - oldPowerLevel) / 20
  135.     else
  136.         inputPower = 0
  137.     end
  138.  
  139.     oldPowerLevel = powerLevel
  140.  
  141.     term.setCursorPos(2,6)
  142.     term.setBackgroundColor( settings.backgroundColor )
  143.     term.clearLine()
  144.     notification = graphics.label:new()
  145.     notification.text = "Power: " .. powerLevel .. "/" .. maxPowerLevel
  146.     lenght = string.len(notification.text)
  147.     notification.x = (w/2) - (lenght/2) + 1
  148.     notification.y = 6
  149.     notification.bkgColor = settings.backgroundColor
  150.     notification.draw(notification)
  151.  
  152.     term.setCursorPos(2,7)
  153.     term.setBackgroundColor( settings.backgroundColor )
  154.     term.clearLine()
  155.     notification = graphics.label:new()
  156.     notification.text = "I/O: " .. inputPower .. " rf/t"
  157.     lenght = string.len(notification.text)
  158.     notification.x = (w/2) - (lenght/2) + 1
  159.     notification.y = 7
  160.     notification.bkgColor = settings.backgroundColor
  161.     notification.draw(notification)
  162.  
  163.     sliders.updateSlider("powerSlider", powerPercentage)
  164.     sliders.draw()
  165.  
  166.     term.setCursorPos(2,h - 1)
  167.     term.setBackgroundColor( settings.backgroundColor )
  168.     term.clearLine()
  169.     notification = graphics.label:new()
  170.     notification.text = "Cycle Count: " .. cycles
  171.     lenght = string.len(notification.text)
  172.     notification.x = (w/2) - (lenght/2) + 1
  173.     notification.y = h - 1
  174.     notification.bkgColor = settings.backgroundColor
  175.     notification.draw(notification)
  176. end
  177.  
  178. function initPower()
  179.     getPowerCellsInfo()
  180.     setupPowerBars()
  181.     setupControlBtns()
  182. end
  183.  
  184. function update()
  185.     getPowerCellsInfo()
  186.     displayPowerInfo()
  187.     powerPercentage = energyPercentage
  188.     -- manual update
  189.     if redstoneOn == true and autoPilotBtn.checked == false then
  190.         if powerPercentage >= 100 then
  191.             deactivateRedstone("Power gen auto deactivated", colors.lime, colors.white)
  192.             cycles = cycles + 1
  193.         end
  194.  
  195.     -- autopilot update
  196.     elseif redstoneOn == true and autoPilotBtn.checked == true then
  197.         if powerPercentage >= settings.highPowerThreshold then
  198.             deactivateRedstone("Power gen auto deactivated", colors.lime, colors.white)
  199.             cycles = cycles + 1
  200.         end
  201.     elseif redstoneOn == false and autoPilotBtn.checked == true then
  202.         if powerPercentage < settings.lowPowerThreshold then
  203.             activateRedstone("Power gen auto activated", colors.lime, colors.white)
  204.         end
  205.     end
  206. end
Add Comment
Please, Sign In to add comment