EnterYourName

MatrixDisplay

Jan 26th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local matrix = peripheral.find("Induction Matrix")
  3.  
  4. monitor.setTextScale(1)
  5.  
  6. --print("Start in 3")
  7. --sleep(3)
  8.  
  9. local w,h = monitor.getSize()
  10.  
  11. local energywidth = w/3
  12.  
  13. monitor.setBackgroundColor(colors.black)
  14. monitor.setTextColor(colors.white)
  15.  
  16. function printEnergyBar()
  17.    
  18.     local energystart = w-1-energywidth
  19.     local startbgcolor = monitor.getBackgroundColor()
  20.     local startfgcolor = monitor.getTextColor()
  21.  
  22.     local level = matrix.getEnergy()/matrix.getMaxEnergy()
  23.     local fieldheight = h-2
  24.  
  25.     local energylinefrom = fieldheight-fieldheight*level
  26.  
  27.     for i=2,h-1 do
  28.  
  29.         monitor.setCursorPos(energystart,i)
  30.         for j=0,energywidth do
  31.             if i >= energylinefrom then
  32.                 monitor.setBackgroundColor(colors.lime)
  33.             else
  34.                 monitor.setBackgroundColor(colors.gray)
  35.             end
  36.  
  37.             monitor.write(" ")
  38.         end
  39.  
  40.     end
  41.  
  42.     monitor.setBackgroundColor(startbgcolor)
  43.     monitor.setTextColor(startfgcolor)
  44.  
  45. end
  46.  
  47. local suffixes = {"","k","M","G","T"}
  48.  
  49. function formatNumber(num)
  50.  
  51.     local idx = 1
  52.     local prefix = ""
  53.     if num < 0 then
  54.         num = -num
  55.         prefix = "-"
  56.     end
  57.  
  58.     while num > 1000 do
  59.  
  60.         num = num/1000
  61.         idx = idx+1
  62.  
  63.     end
  64.  
  65.     if idx > 1 then
  66.         num = math.ceil(num*100)/100
  67.     else
  68.         num = math.ceil(num)
  69.     end
  70.    
  71.     if not string.match(num,"%d+%.%d%d+") then
  72.         if string.match(num,"%d+%.%d") then
  73.             num = num.."0"
  74.         elseif string.match(num,"%d+") then
  75.             num = num..".00"
  76.         end
  77.     end
  78.     return prefix..num..suffixes[idx]
  79.  
  80. end
  81.  
  82. function jToRF(joule)
  83.     return joule/2.5
  84. end
  85.  
  86. function printStats()
  87.  
  88.     local energyLine = 1/5*h
  89.     local outputLine = 2/5*h
  90.     local inputLine = 3/5*h
  91.     local diffLine = 4/5*h
  92.     local startbgcolor = monitor.getBackgroundColor()
  93.     local startfgcolor = monitor.getTextColor()
  94.  
  95.     local energypercent = math.ceil(100*matrix.getEnergy()/matrix.getMaxEnergy())
  96.     local outputPercent = math.ceil(100*matrix.getOutput()/matrix.getTransferCap())
  97.     local inputPercent = math.ceil(100*matrix.getInput()/matrix.getTransferCap())
  98.     local diff = jToRF(matrix.getInput()-matrix.getOutput())
  99.  
  100.     monitor.setCursorPos(2,energyLine)
  101.     monitor.setTextColor(colors.lightGray)
  102.     monitor.write(formatNumber(jToRF(matrix.getEnergy())).."RF ("..energypercent .."%)")
  103.  
  104.     monitor.setCursorPos(2,outputLine)
  105.     monitor.setTextColor(colors.red)
  106.     monitor.write("-"..formatNumber(jToRF(matrix.getOutput())).."RF/t ("..outputPercent .."%)")
  107.  
  108.     monitor.setCursorPos(2,inputLine)
  109.     monitor.setTextColor(colors.green)
  110.     monitor.write("+"..formatNumber(jToRF(matrix.getInput())).."RF/t ("..inputPercent .."%)")
  111.  
  112.     monitor.setCursorPos(2,diffLine)
  113.     local prefix = ""
  114.     if diff > 0 then
  115.         monitor.setTextColor(colors.green)
  116.         prefix = "+"
  117.     elseif diff < 0.09 or diff > -0.09 then
  118.         monitor.setTextColor(colors.gray)
  119.         prefix = "±"
  120.         diff = 0;
  121.     else
  122.         monitor.setTextColor(colors.red)
  123.     end
  124.     monitor.write(prefix..formatNumber(diff).."RF/t")
  125.  
  126.     monitor.setBackgroundColor(startbgcolor)
  127.     monitor.setTextColor(startfgcolor)
  128. end
  129.  
  130. while true do
  131.  
  132.     if type(matrix.getEnergy()) == "number" then
  133.         monitor.clear()
  134.         printEnergyBar()
  135.         printStats()
  136.         sleep(0.1)
  137.     else
  138.         sleep(1)
  139.     end
  140. end
Add Comment
Please, Sign In to add comment