Advertisement
weeplay60

ComputerCraft Energy Control

Jul 12th, 2020
2,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. local batteries = {}
  2. local rednetInfo = {"EnergyControl", "MainBase"}
  3. local BatterieTypes = {"ic2:oldbatbox","ic2:oldmfe","ic2:oldmfsu"};
  4. local AutoDetect = true
  5.  
  6. if (fs.exists("EnergyConfig") and os.loadAPI("EnergyConfig")) then
  7.     if (EnergyConfig.rednetInfo ~= nil) then
  8.         rednetInfo = EnergyConfig.rednetInfo
  9.     end
  10.     if (EnergyConfig.BatterieTypes ~= nil) then
  11.         BatterieTypes = EnergyConfig.BatterieTypes
  12.     end
  13.     print("Custom Config : Load!")
  14.     os.sleep(1)
  15. end
  16.  
  17. term.clear()
  18. term.setCursorPos(1,1)
  19.  
  20. local function getNbBatteries()
  21.     local size = 0
  22.     for index, batterie in pairs(batteries) do
  23.         if (peripheral.isPresent(index)) then
  24.             size = size + 1
  25.         else
  26.             batteries[index] = nil
  27.         end
  28.     end
  29.     return size
  30. end
  31.  
  32. local function findBatteries()
  33.     for _,name in pairs(peripheral.getNames()) do
  34.         for index, type in pairs(BatterieTypes) do
  35.             if (peripheral.getType(name) == type and batteries[name] == nil) then
  36.                 batteries[name] = peripheral.wrap(name)
  37.             end
  38.         end
  39.     end
  40.     getNbBatteries()
  41. end
  42.  
  43. local function getBatterieInfo(index)
  44. if (batteries[index] == nil) then
  45.     return 0, 0
  46. end
  47. local ok, stored = pcall(batteries[index].getEUStored)
  48.     if(ok == false or stored == nil) then
  49.         stored = 0
  50.     end
  51. local ok, capacity = pcall(batteries[index].getEUCapacity)
  52.     if(ok == false or capacity == nil) then
  53.         capacity = 0
  54.     end
  55.     return stored , capacity
  56. end
  57.  
  58. local function getWirelessModemSide()
  59.     local lstSides = {"left","right","top","bottom","front","back"};
  60.    
  61.     for index, side in pairs(lstSides) do
  62.         if (peripheral.isPresent(side)) then
  63.             if (peripheral.getType(side) == string.lower("modem")) then
  64.                 if (peripheral.call(side, "isWireless")) then
  65.                     return side;
  66.                 end
  67.             end
  68.         end
  69.     end
  70.     return nil;
  71. end
  72.  
  73. findBatteries()
  74.  
  75. rednet.open(getWirelessModemSide())
  76. rednet.host(rednetInfo[1], rednetInfo[2])
  77.  
  78. print("ComputerID : " .. os.getComputerID())
  79. if (AutoDetect) then
  80.     print("AutoDetection : True")
  81. else
  82.     print("AutoDetection : False")
  83. end
  84. print("Protocol: " .. rednetInfo[1])
  85. print("Host: " .. rednetInfo[2])
  86.  
  87. scr = peripheral.find("monitor")
  88. ScrComputer = term.current()
  89. scr.setTextScale(1)
  90. term.redirect(scr)
  91. term.setCursorBlink(false)
  92. scrW, scrH = term.getSize()
  93.  
  94. function centerText(energy,yVal)
  95.     local percent =  energy[1] / energy[2] * 100
  96.     length = string.len(textutils.formatTime(os.time(), true) .. " - " .. string.format("%d%%", percent) .. " - " .. getNbBatteries() .. " Batteries")
  97.     minus = math.floor(scrW-length)
  98.     x = math.floor(minus/2)
  99.     scr.setCursorPos(x+1,yVal)
  100.     if os.time() > 18 or os.time() < 6 then
  101.         scr.setTextColor(colors.blue)
  102.     else
  103.         scr.setTextColor(colors.yellow)
  104.     end
  105.     scr.write(textutils.formatTime(os.time(), true))
  106.  
  107.     scr.setTextColor(colors.white)
  108.     scr.setBackgroundColor(colors.black)
  109.     scr.write(" - ")
  110.  
  111.     if (percent > 80) then
  112.         scr.setTextColor(colors.green)
  113.     elseif (percent > 60) then
  114.         scr.setTextColor(colors.lime)
  115.     elseif (percent > 40) then
  116.         scr.setTextColor(colors.yellow)
  117.     elseif (percent > 20) then
  118.         scr.setTextColor(colors.orange)
  119.     else
  120.         scr.setTextColor(colors.black)
  121.         scr.setBackgroundColor(colors.red)
  122.     end
  123.    
  124.     scr.write(string.format("%d%%", percent))
  125.  
  126.     scr.setTextColor(colors.white)
  127.     scr.setBackgroundColor(colors.black)
  128.     scr.write(" - ")
  129.  
  130.     if (getNbBatteries() <= 0) then
  131.         scr.setTextColor(colors.black)
  132.         scr.setBackgroundColor(colors.red)
  133.     end
  134.     scr.write(getNbBatteries() .. " Batteries")
  135.     scr.setTextColor(colors.white)
  136.     scr.setBackgroundColor(colors.black)
  137. end
  138.  
  139. local barW = scrW * 0.9
  140. local lastEnergy = 0
  141. function printStatus(energy)
  142.     term.clear()
  143.     term.setCursorPos(1,1)
  144.     centerText(energy, 1)
  145.     term.setCursorPos(1,2)
  146.     term.write("Charge: " .. string.format("%2.1f", energy[1] / 1000000) .. "M EU\n")
  147.     term.setCursorPos(1,3)
  148.     term.write("Capacité: " .. string.format("%2.1f", energy[2] / 1000000) .. "M EU\n")
  149.     printBar(energy)
  150.     term.setTextColor(colors.white)
  151.     term.setBackgroundColor(colors.black)
  152. end
  153.  
  154. function printBar(energy)
  155.     local val = math.floor((energy[1] / energy[2]) * (scrW - 2) + 0.5)
  156.     local percent =  energy[1] / energy[2] * 100
  157.  
  158.     scr.setCursorPos(2,5)
  159.     scr.setBackgroundColour(colors.gray)
  160.     scr.write(string.rep(" ", scrW-2))
  161.     scr.setCursorPos(2,5)
  162.  
  163.     if (percent > 80) then
  164.         scr.setBackgroundColour(colors.green)
  165.     elseif (percent > 60) then
  166.         scr.setBackgroundColour(colors.lime)
  167.     elseif (percent > 40) then
  168.         scr.setBackgroundColour(colors.yellow)
  169.     elseif (percent > 20) then
  170.         scr.setBackgroundColour(colors.orange)
  171.     else
  172.         scr.setBackgroundColour(colors.red)
  173.     end
  174.     scr.write(string.rep(" ", val))
  175. end
  176.  
  177. while true do
  178.     local totalEnergy = 0
  179.     local totalCapacity = 0
  180.     local energy = {}
  181.     if (AutoDetect) then
  182.         findBatteries()
  183.     end
  184.     for index, batterie in pairs(batteries) do
  185.         local currEU, currEUCap = getBatterieInfo(index)
  186.         totalEnergy = totalEnergy + currEU
  187.         totalCapacity = totalCapacity + currEUCap
  188.         table.insert(energy, { currEU, currEUCap })
  189.     end
  190.     table.insert(energy, 1, totalEnergy)
  191.     table.insert(energy, 2, totalCapacity)
  192.     printStatus(energy)
  193.     rednet.broadcast(energy, "EnergyStatus")
  194.     os.sleep(0.05)
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement