Advertisement
orblazer

EnergyManager

May 16th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.33 KB | None | 0 0
  1. local max = 90 -- pourcentage maximum de stockage d'energie (stop la production d'energie)
  2. local min = 10 -- pourcentage minimum de stockage d'energie (lance la production d'energie)
  3. local conversion = {
  4.     J = 4, -- Mekanism: RF multiplier to Joules
  5.     MJ = 40 -- Mekanism: RF multiplier to Mega Joules
  6. }
  7.  
  8. -- utils functions
  9. function round(val, decimal)
  10.     local exp = decimal and 10^decimal or 1
  11.     return math.ceil(val * exp - 0.5) / exp
  12. end
  13.  
  14. function convertPower(power)
  15.     if power >= 100000000000000 then
  16.         return round(power/1000000000000, 3).." TRF"
  17.     elseif power >= 100000000000 then
  18.         return round(power/1000000000, 3).." GRF"
  19.     elseif power >= 100000000 then
  20.         return round(power/10000000, 3).." MRF"
  21.     elseif power >= 100000 then
  22.         return round(power/1000, 3).." kRF"
  23.     else
  24.         return round(power, 3).." RF"
  25.     end
  26. end
  27.  
  28. function getUnit(type, maxEnergy)
  29.     if type == "cube" then
  30.         if maxEnergy >= 1000000 then
  31.             return "MJ"
  32.         else
  33.             return "J"
  34.         end
  35.     end
  36.  
  37.     return "RF"
  38. end
  39.  
  40.  
  41. function convertPowerType(power, type)
  42.     if type == "J" then
  43.         power = power * conversion.J
  44.     elseif type == "MJ" then
  45.         power = power * conversion.MJ
  46.     end
  47.  
  48.     return power
  49. end
  50.  
  51. function detectDevice(deviceName)
  52.     deviceSide = "none"
  53.     for k,v in pairs(redstone.getSides()) do
  54.         if peripheral.getType(v) == deviceName then
  55.             deviceSide = v
  56.             break
  57.         end
  58.     end
  59.  
  60.     return(deviceSide)
  61. end
  62.  
  63. function centerText(text, yVal)
  64.     length = string.len(text)
  65.     minus = math.floor(36-length)
  66.     x = math.floor(minus/2)
  67.     monitor.setCursorPos(x+1,yVal)
  68.     monitor.write(text)
  69. end
  70.  
  71. --Sets the BackGround to Gray   (Messy!  I know!)
  72. function clearScreen(color)
  73.     monitor.setBackgroundColour(color)
  74.     monitor.clear()
  75.     monitor.setCursorPos(1,1)
  76.     monitor.write(string.rep(" ",36))
  77.     monitor.setCursorPos(1,2)
  78.     monitor.write(string.rep(" ",36))
  79.     monitor.setCursorPos(1,3)
  80.     monitor.write(string.rep(" ",36))
  81.     monitor.setCursorPos(1,4)
  82.     monitor.write(string.rep(" ",36))
  83.     monitor.setCursorPos(1,5)
  84.     monitor.write(string.rep(" ",36))
  85.     monitor.setCursorPos(1,6)
  86.     monitor.write(string.rep(" ",36))
  87.     monitor.setCursorPos(1,7)
  88.     monitor.write(string.rep(" ",36))
  89.     monitor.setCursorPos(1,8)
  90.     monitor.write(string.rep(" ",36))
  91.     monitor.setCursorPos(1,9)
  92.     monitor.write(string.rep(" ",36))
  93.     monitor.setCursorPos(1,10)
  94.     monitor.write(string.rep(" ",36))
  95. end
  96.  
  97.  
  98. storage = "none"
  99. storageType = "none"
  100. storageUnit = "none"
  101. monitor = "none"
  102. local peripheralList = peripheral.getNames()
  103. monitorSize = "0x0"
  104.  
  105. ----------------------
  106. -- Find peripheral
  107. ----------------------
  108. capacitor = detectDevice("Elite Energy Cube")
  109.  
  110. -- capacitor bank
  111. if capacitor ~= "none" then
  112.     storage = peripheral.wrap(capacitor)
  113.     storageType = "capacitor"
  114.     print("Capacitor bank on the "..capacitor.." connected.")
  115. else
  116.     for index=1, #peripheralList do
  117.         -- energy cell
  118.         if string.find(peripheral.getType(peripheralList[index]), "tile_thermalexpansion_cell") then
  119.             storage = peripheral.wrap(peripheralList[index])
  120.             storageType = "cell"
  121.             storageUnit = getUnit("cell")
  122.  
  123.             print(peripheralList[index].." connected.")
  124.         -- energy cube
  125.         elseif string.find(peripheral.getType(peripheralList[index]), "Energy Cube") then
  126.             storage = peripheral.wrap(peripheralList[index])
  127.             storageType = "cube"
  128.             storageUnit = getUnit("cube", storage.getMaxEnergy())
  129.  
  130.             print(peripheralList[index].." connected.")
  131.         -- capacitor bank
  132.         elseif string.find(peripheral.getType(peripheralList[index]), "capacitor_bank") then
  133.             storage = peripheral.wrap(peripheralList[index])
  134.             storageType = "capacitor"
  135.             storageUnit = getUnit("capacitor")
  136.  
  137.             print(peripheralList[index].." connected.")
  138.         end
  139.     end
  140.  
  141.     if storage == "none" then
  142.         print("No Energy storage found. Halting script!")
  143.         return
  144.     end
  145. end
  146.  
  147. -------------------
  148. -- Find monitor
  149. -------------------
  150. monitorSide = detectDevice("monitor")
  151.  
  152. if monitor ~= "none" then
  153.     monitor = peripheral.wrap(monitorSide)
  154.     local w, h = monitor.getSize()
  155.     monitorSize = w.."x"..h
  156.     print("monitor on the "..monitorSide.." connected.")
  157. else
  158.     for index=1, #peripheralList do
  159.         if peripheral.getType(peripheralList[index]) == "monitor" then
  160.             monitor = peripheral.wrap(monitorSide)
  161.             local w, h = monitor.getSize()
  162.             monitorSize = w.."x"..h
  163.             print("monitor on the "..monitorSide.." connected.")
  164.         end
  165.     end
  166.  
  167.     if monitor == "none" then
  168.         print("Warning - No monitor attached, continuing without.")
  169.     end
  170. end
  171.  
  172. ----------------
  173. -- Main code
  174. ----------------
  175. redstone.setOutput("back", false)
  176.  
  177. -- if monitor is attached, write data on monitor
  178. if monitor ~= "none" then
  179.     if monitorSize == "36x10" or monitorSize == "18x5" then
  180.         clearScreen(colors.gray)
  181.  
  182.         monitor.setTextScale(0.5)
  183.         monitor.setCursorPos(1, 1)
  184.     else
  185.         monitor.clear()
  186.  
  187.         monitor.setBackgroundColour(colors.gray)
  188.         monitor.setCursorPos(1, 4)
  189.         monitor.write(" ON ")
  190.  
  191.         monitor.setBackgroundColour(colors.green)
  192.         monitor.setCursorPos(5, 4)
  193.         monitor.write(" OFF ")
  194.  
  195.         monitor.setBackgroundColour(colors.black)
  196.     end
  197. end
  198.  
  199. -- Main loop
  200. lastEnergy = 0
  201. while true do
  202.     -- get Storage value
  203.     if storageType == "cube" then
  204.         local current = convertPowerType(storage.getEnergy(), storageUnit)
  205.         info = {
  206.             energy = current,
  207.             maxEnergy = convertPowerType(storage.getMaxEnergy(), storageUnit),
  208.             usage = convertPowerType(storage.getOutput(), storageUnit),
  209.             averageChange = math.floor(current - lastEnergy)
  210.         }
  211.     elseif storageType == "capacitor" then
  212.         info = {
  213.             energy = storage.getEnergyStored("unknown"),
  214.             maxEnergy = storage.getMaxEnergyStored("unknown"),
  215.             usage = 0, -- TODO: Make that
  216.             averageChange = math.floor(storage.getAverageChangePerTick())
  217.         }
  218.     else
  219.         print("NOT SUPPORTED "..storageType)
  220.         return
  221.     end
  222.  
  223.     -- Get the percentage
  224.     local rawPerc = info.energy / info.maxEnergy
  225.     local percentFull = round(rawPerc * 100, 1)
  226.     local barPerc = math.floor((rawPerc * 34) + 0.5)
  227.  
  228.     -- if monitor is attached, write data on monitor
  229.     if monitor ~= "none" then
  230.         -- update monitor size
  231.         local w, h = monitor.getSize()
  232.         monitorSize = w.."x"..h
  233.  
  234.         if monitorSize == "36x10" or monitorSize == "18x5" then
  235.             clearScreen(colors.gray)
  236.  
  237.             -- Draw the current energy and max energy
  238.             monitor.setCursorPos(1,2)
  239.             monitor.write(string.rep(" ",36))
  240.             monitor.setTextColour(colors.blue)
  241.             centerText("Current Energy: "..convertPower(info.energy), 1)
  242.             monitor.setCursorPos(1,3)
  243.             monitor.write(string.rep(" ",36))
  244.             monitor.setTextColour(colors.white)
  245.             centerText("Of : "..convertPower(info.maxEnergy), 2)
  246.  
  247.             -- Draw the average change
  248.             monitor.setCursorPos(1,3)
  249.             monitor.write(string.rep(" ",36))
  250.             local avgText = ""
  251.             if info.averageChange > 0 then
  252.                 avgText = "+"..convertPower(info.averageChange)
  253.                 monitor.setTextColour(colors.green)
  254.             elseif info.averageChange == 0 then
  255.                 avgText = "0 RF"
  256.                 monitor.setTextColour(colors.black)
  257.             else
  258.                 avgText = "-"..convertPower(info.averageChange * (-1))
  259.                 monitor.setTextColour(colors.red)
  260.             end
  261.             centerText(avgText.."/t", 3)
  262.  
  263.             -- Draw the bar
  264.             monitor.setCursorPos(1, 7)
  265.             monitor.write(string.rep(" ", 36))
  266.             if percentFull <= 20 then
  267.                 monitor.setTextColour(colors.red)
  268.             elseif percentFull <= 60 then
  269.                 monitor.setTextColour(colors.orange)
  270.             else
  271.                 monitor.setTextColour(colors.green)
  272.             end
  273.             centerText(percentFull.."% Full", 7)
  274.  
  275.             --Loading Bar Code Re-Written Much MUCH simpler than before
  276.             monitor.setCursorPos(2, 8)
  277.             monitor.setBackgroundColour(colors.white)
  278.             monitor.write(string.rep(" ", 34))
  279.             monitor.setCursorPos(2, 8)
  280.             monitor.setBackgroundColour(colors.lime)
  281.             monitor.write(string.rep(" ", barPerc))
  282.  
  283.             monitor.setCursorPos(2, 9)
  284.             monitor.setBackgroundColour(colors.white)
  285.             monitor.write(string.rep(" ", 34))
  286.             monitor.setCursorPos(2, 9)
  287.             monitor.setBackgroundColour(colors.lime)
  288.             monitor.write(string.rep(" ", barPerc))
  289.         else
  290.             clearScreen(colors.gray)
  291.  
  292.             -- Draw the current energy and max energy
  293.             monitor.setCursorPos(1,2)
  294.             monitor.write(string.rep(" ",36))
  295.             monitor.setTextColour(colors.blue)
  296.             centerText("Current Energy: "..convertPower(info.energy), 1)
  297.             monitor.setCursorPos(1,3)
  298.             monitor.write(string.rep(" ",36))
  299.             monitor.setTextColour(colors.white)
  300.             centerText("Of : "..convertPower(info.maxEnergy), 2)
  301.         end
  302.     end
  303.  
  304.     -- Toggle engine
  305.     if percentFull > max then
  306.         -- energy level is over max level, turning redstone signal off
  307.         redstone.setOutput("back", false)
  308.  
  309.         if monitor ~= "none" then
  310.             if monitorSize ~= "36x10" and monitorSize ~= "18x5" then
  311.                 monitor.setBackgroundColour(colors.gray)
  312.                 monitor.setCursorPos(1, 4)
  313.                 monitor.write(" ON ")
  314.  
  315.                 monitor.setBackgroundColour(colors.green)
  316.                 monitor.setCursorPos(5, 4)
  317.                 monitor.write(" OFF ")
  318.  
  319.                 monitor.setBackgroundColour(colors.black)
  320.             end
  321.         end
  322.     elseif percentFull < min then
  323.         -- energy level is below max level, turning redstone signal on
  324.         redstone.setOutput("back", true)
  325.  
  326.         if monitor ~= "none" then
  327.             if monitorSize ~= "36x10" and monitorSize ~= "18x5" then
  328.                 monitor.setBackgroundColour(colors.green)
  329.                 monitor.setCursorPos(1, 4)
  330.                 monitor.write(" ON ")
  331.  
  332.                 monitor.setBackgroundColour(colors.gray)
  333.                 monitor.setCursorPos(5, 4)
  334.                 monitor.write(" OFF ")
  335.  
  336.                 monitor.setBackgroundColour(colors.black)
  337.             end
  338.         end
  339.     end
  340.  
  341.     -- Update lastEnergy
  342.     lastEnergy = info.energy
  343.  
  344.     sleep(1)
  345. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement