Thor_s_Crafter

energyDisplay

Jun 28th, 2016
2,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.31 KB | None | 0 0
  1. -- Energy display --
  2. -- by Thor_s_Crafter --
  3. -- Version 1.2 --
  4.  
  5. --Global variables
  6. local mon
  7. local c
  8. local en
  9. local enMax
  10. local enPer
  11. local inOut
  12. local ioCol
  13. local ioPre
  14. local x,y
  15. local x2
  16. local timerVar = 0
  17. local ioTmp1
  18. local ioTmp2
  19. local inOut5 = 0
  20. local ioPre5
  21. local ioCol5
  22.  
  23. --Finds Monitor & energy storage
  24. function initPeripherals()
  25.  
  26.   --Table for all peripherals
  27.   local per = peripheral.getNames()
  28.  
  29.   --Checks all attached peripherals
  30.   for i=1,#per do
  31.  
  32.     --Monitor
  33.     if peripheral.getType(per[i]) == "monitor" then
  34.       mon = peripheral.wrap(per[i])
  35.  
  36.       --Some global settings for the monitor
  37.       x,y = mon.getSize()
  38.       x2 = x/2
  39.       mon.setBackgroundColor(colors.black)
  40.      
  41.     --Energy storage
  42.     elseif peripheral.getType(per[i]) == "draconic_rf_storage" then
  43.       c = peripheral.wrap(per[i])
  44.     elseif peripheral.getType(per[i]) == "tile_blockcapacitorbank_name" then
  45.       c = peripheral.wrap(per[i])
  46.     elseif peripheral.getType(per[i]) == "capacitor_bank" then
  47.       c = peripheral.wrap(per[i])
  48.     end
  49.   end
  50. end
  51.  
  52. --Clears the entire screen and the terminal
  53. function clearAll()
  54.   term.clear()
  55.   term.setCursorPos(1,1)
  56.   mon.clear()
  57. end
  58.  
  59. --Formats big numbers into a string (e.g. 1000 -> 1.000)
  60. function format(value)
  61.  
  62.   --Values smaller 1000 doesn't need to be formatted
  63.   if value < 1000 then return value end
  64.  
  65.   --Some calculation variables
  66.   local array = {}
  67.   local vStr = tostring(value)
  68.   local len = string.len(vStr)
  69.   local modulo = math.fmod(len,3)
  70.  
  71.   --Saves digits into a table (array)
  72.   for i=1,len do array[i] = string.sub(vStr,i,i) end
  73.  
  74.   --Moves additional digits into a second table (array)
  75.   local array2 = {}
  76.   if modulo ~= 0 then
  77.     for i=1,modulo do
  78.       array2[i] = array[i]
  79.       table.remove(array,i)
  80.     end
  81.   end
  82.  
  83.   --Adds the dots into the first array
  84.   for i=1,#array+1,4 do
  85.     table.insert(array,i,".")
  86.   end
  87.  
  88.   --Merges both arrays
  89.   for i=#array2,1,-1 do table.insert(array,1,array2[i]) end
  90.   if modulo == 0 then table.remove(array,1) end --Removes front dots
  91.  
  92.   --Converts everything into a string and returns it
  93.   local final = ""
  94.   for k,v in pairs(array) do final = final..v end
  95.   return final
  96. end
  97.  
  98. --Getts the current energy values
  99. function getEnergy()
  100.   en = c.getEnergyStored()
  101.   enMax = c.getMaxEnergyStored()
  102.   enPer = math.floor(en/enMax*100)
  103.  
  104.   --Debug prints
  105.   term.setCursorPos(1,1)
  106.   print("En: "..en)
  107.   print("EnMax: "..enMax)
  108.   print("EnPer: "..enPer)
  109. end
  110.  
  111. --Calculates energy changes (interval: 1s)
  112. function getInOut()
  113.  
  114.   --First value
  115.   getEnergy()
  116.   local tmp1 = en
  117.   sleep(0.5)
  118.  
  119.   --Second value
  120.   getEnergy()
  121.   local tmp2 = en
  122.  
  123.   --Calculates the difference of both values
  124.   local inOutTmp = math.floor((tmp2-tmp1)/10)
  125.  
  126.   --Sets inOut (finally)
  127.   inOut = inOutTmp
  128.  
  129.   --Debug prints
  130.   term.setCursorPos(1,4)
  131.   print("tmp1: "..tmp1)
  132.   print("tmp2: "..tmp2)
  133.   print("inOutTmp: "..inOutTmp)
  134.   print("inOut: "..inOut)
  135.  
  136.   --Sets the text color & the prefix
  137.   if inOut > 0 then ioCol = colors.green ioPre = "+"
  138.   elseif inOutTmp < 0 then ioCol = colors.red ioPre = "-"
  139.   elseif inOutTmp == 0 then ioCol = colors.white ioPre = "" end
  140. end
  141.  
  142. --Calculates energy changes (interval: 5s)
  143. function getInOut5()
  144.   getEnergy() --Gets the current energy
  145.  
  146.   --Gets the first value at time code 0
  147.   if timerVar == 0 then
  148.     ioTmp1 = en
  149.     timerVar = timerVar + 1
  150.  
  151.   --Gets the second value at time code 10 (= 5s)
  152.   elseif timerVar == 10 then
  153.     timerVar = 0
  154.     ioTmp2 = en
  155.  
  156.     --Sets inOut5
  157.     inOut5 = math.floor((ioTmp2 - ioTmp1)/100)
  158.  
  159.   --Increments timer
  160.   else
  161.     timerVar = timerVar + 1
  162.   end
  163.  
  164.   --Debug print
  165.   term.setCursorPos(1,8)
  166.   print("inOut5: "..inOut5)
  167.  
  168.   --Sets the text color and the prefix
  169.   if inOut5 > 0 then ioCol5 = colors.green ioPre5 = "+"
  170.   elseif inOut5 < 0 then ioCol5 = colors.red ioPre5 = "-"
  171.   elseif inOut5 == 0 then ioCol5 = colors.white ioPre5 = "" end
  172. end
  173.  
  174.  
  175. --Draws the energy bar
  176. function printBar()
  177.   local part1 = enPer/5
  178.   mon.setCursorPos(x2-10,5)
  179.   mon.setTextColor(colors.white)
  180.   mon.write("|--------------------|")
  181.   mon.setTextColor(colors.green)
  182.   mon.setCursorPos(x2-9,5)
  183.   for i=1,part1 do
  184.     mon.write("=")
  185.   end
  186.   mon.setTextColor(colors.white)
  187. end
  188.  
  189. --Prints all data onto the screen
  190. function printStats()
  191.  
  192.   --Caption
  193.   mon.setCursorPos(1,1)
  194.   mon.write("Energy Display")
  195.  
  196.   mon.setCursorPos(1,2)
  197.   for i=1,x do
  198.     mon.write("-")
  199.   end
  200.  
  201.   --Energy (in % + the energy bar)
  202.   mon.setCursorPos(x2-2,4)
  203.   mon.write(enPer.."%  ")
  204.   printBar()
  205.  
  206.   --Energy (current)
  207.   mon.setCursorPos(1,7)
  208.   mon.write("Total: "..format(en).." RF")
  209.  
  210.   --Energy (max)
  211.   mon.setCursorPos(1,8)
  212.   mon.write("Capacity: "..format(enMax).." RF")
  213.  
  214.   --Energy (In/Out)
  215.   mon.setCursorPos(1,10)
  216.   mon.write("In/Out: ")
  217.   mon.setTextColor(ioCol)
  218.   mon.write(ioPre..format(math.abs(inOut)).." RF/t          ")
  219.   mon.setTextColor(colors.white)
  220.  
  221.    --Energy (In/Out)(5s)
  222.   mon.setCursorPos(1,11)
  223.   mon.write("In/Out (5s): ")
  224.   mon.setTextColor(ioCol5)
  225.   mon.write(ioPre5..format(math.abs(inOut5)).." RF/t          ")
  226.   mon.setTextColor(colors.white)
  227. end
  228.  
  229. --Start
  230. initPeripherals()
  231. clearAll()
  232.  
  233. --Main loop
  234. while true do
  235.   getEnergy()
  236.   getInOut()
  237.   getInOut5()
  238.   printStats()
  239. end
Add Comment
Please, Sign In to add comment