Advertisement
Stekeblad

MatrixPower.lua

Mar 3rd, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. -- This is a program for Computercraft used to display
  2. -- the power inside a Mekanism Induction Matrix.
  3.  
  4. -- It looks best on a 4 wide by 3 high monitor
  5.  
  6. -- By: Stekeblad
  7. -- License: MIT
  8.  
  9. -----------------------------------------------------------
  10. -- config options
  11.  
  12. -- digits of precision when displaying numbers
  13. local precision = 3
  14.  
  15. -- variables
  16. local maxIO = 0
  17. local maxPow = 0
  18.  
  19. local matrix = nil
  20. local mon = nil
  21.  
  22. local powNow
  23. local inNow
  24. local outNow
  25.  
  26.  
  27. ------ Functions ------
  28. -- progress bars
  29. local function drawInputBar(inputRate)
  30.     progbar.drawVerticalBar(2, 2, 3, 13,
  31.             inputRate/maxIO,
  32.             colors.lime, true, colors.gray)
  33. end
  34.  
  35. local function drawOutputBar(outputRate)
  36.     progbar.drawVerticalBar(14, 2, 3, 13,
  37.             outputRate/maxIO,
  38.             colors.red, true, colors.gray)
  39. end
  40.  
  41. local function drawMainBar(stored)
  42.     progbar.drawVerticalBar(30, 2, 8, 13,
  43.             stored/maxPow, colors.blue,
  44.             true, colors.gray)
  45. end
  46.  
  47. -- conversion and formatting
  48. local function scaleNumber(number)
  49.     local numLog = math.floor(math.log(number))
  50.     if (numLog < precision) then
  51.         return number
  52.     end
  53.    
  54.     number = math.floor(number)
  55.     local asText = tostring(number)
  56.     local revText = ""
  57.     local counter = 0
  58.     local magnitude = 0
  59.     for chara in string.gmatch(string.reverse(asText), ".") do
  60.         if counter % 3 == 0 and counter ~= 0 then
  61.             revText = revText .. ","
  62.             magnitude = magnitude + 1
  63.         end
  64.         revText = revText .. chara
  65.         counter = counter + 1
  66.     end
  67.    
  68.     asText = string.reverse(revText)
  69.     local commaIndex = string.find(asText, ",")
  70.     if commaIndex == nil then
  71.         if counter <= precision then
  72.             return asText -- no comma and fits in precision
  73.         end
  74.     elseif commaIndex == precision + 1 then
  75.         asText = string.sub(asText, precision) -- missing suffix at this point
  76.     else
  77.         -- adjust asText to precision
  78.         local i = 0
  79.         local newText = ""
  80.         for chara in asText:gmatch(".") do
  81.             newText = newText .. chara
  82.             if chara ~= "," then
  83.                 i = i + 1
  84.             end
  85.             if i == precision then break end
  86.         end
  87.         asText = newText
  88.     end
  89.    
  90.     if magnitude == 0 then
  91.         return asText
  92.     elseif magnitude == 1 then
  93.         return asText .. "k"
  94.     elseif magnitude == 2 then
  95.         return asText .. "M"
  96.     elseif magnitude == 3 then
  97.         return asText .. "G"
  98.     elseif magnitude == 4 then
  99.         return asText .. "T"
  100.     elseif magnitude == 5 then
  101.         return asText .. "P"
  102.     elseif magnitude == 6 then
  103.         return asText .. "E"
  104.     end
  105. end
  106.  
  107. ------ init --------
  108.  
  109. if not progbar then
  110.     os.loadAPI("progbar.lua")
  111.     if not progbar then
  112.         error("Could not load progbar.lua API, get it with this pastebin code: gVWb750w")
  113.     end
  114. end
  115.  
  116. matrix = peripheral.find("Induction Matrix")
  117. if matrix == nil then
  118.     error("Could not find a Mekanism Induction Matrix")
  119. end
  120.  
  121. mon = peripheral.find("monitor")
  122. if mon == nil then
  123.     error("Could not find a monitor, please connect one")
  124. end
  125.  
  126. mon.setTextScale(1)
  127. local oldterm = term.redirect(mon)
  128.  
  129.  
  130. maxPow = matrix.getMaxEnergy() /2.5 --J --> RF
  131. maxIO = matrix.getTransferCap() /2.5 -- J --> RF
  132.  
  133. ------ Main loop -----
  134.  
  135. while true do
  136.     powNow = matrix.getEnergy() /2.5 -- J --> RF
  137.     inNow = matrix.getInput() /2.5 -- J --> RF
  138.     outNow = matrix.getOutput() /2.5 -- J --> RF
  139.    
  140.     term.clear()
  141.    
  142.     drawInputBar(inNow)
  143.     drawOutputBar(outNow)
  144.     drawMainBar(powNow)
  145.    
  146.     term.setBackgroundColor(colors.black)
  147.     term.setTextColor(colors.white)
  148.     term.setCursorPos(7, 3)
  149.     print(scaleNumber(maxIO))
  150.    
  151.     term.setCursorPos(31, 17)
  152.     print(scaleNumber(powNow))
  153.    
  154.     term.setCursorPos(7, 7)
  155.     term.setTextColor(colors.green)
  156.     print(scaleNumber(inNow))
  157.    
  158.     term.setCursorPos(7, 11)
  159.     term.setTextColor(colors.red)
  160.     print(scaleNumber(outNow))
  161.    
  162.     sleep(3)
  163. end
  164. --term.redirect(oldterm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement