Guest User

Untitled

a guest
Dec 4th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.59 KB | None | 0 0
  1. --# you may as well use numbers instead of strings
  2. local mfsu = {
  3.   peripheral.wrap("mfsu_3");
  4.   peripheral.wrap("mfsu_4");
  5.   peripheral.wrap("mfsu_5");
  6.   peripheral.wrap("mfsu_6");
  7.   peripheral.wrap("mfsu_7");
  8.   peripheral.wrap("mfsu_8");
  9.   peripheral.wrap("mfsu_9");
  10.   peripheral.wrap("mfsu_10");
  11.   peripheral.wrap("mfsu_11");
  12. }
  13.  
  14. local m = peripheral.wrap("monitor_13")
  15. m.clear()
  16. local storedEU = {}
  17. local storedPerc = {}
  18. local plotter = {}
  19. local printer = {}
  20. local energyTotals = 0
  21. local energyFlow = 0
  22.  
  23. local function shorten(num)
  24.   if num then
  25.     return string.format("%.1f", num)
  26.   end
  27.   return "0"
  28. end
  29.  
  30. local function colorPerc(num)
  31.   num = tonumber(num)
  32.   if num then --# you had ~= nil here
  33.     --# order has been switched so elseif can be used
  34.     if num > 90 then
  35.       m.setTextColor(colors.lime)
  36.     elseif num > 60 then
  37.       m.setTextColor(colors.yellow)
  38.     elseif num > 30 then
  39.       m.setTextColor(colors.orange)
  40.     else
  41.       m.setTextColor(colors.red)
  42.     end
  43.     m.write(num.."%")
  44.     space = #tostring(num) - 5 * (-1) --# string.len and # are the same on strings
  45.     for i = 1, space do
  46.       m.write(" ")
  47.     end
  48.     m.setTextColor(colors.white)
  49.   end
  50. end
  51.  
  52. local function storedTotal()
  53.   local energyTotal = 0
  54.   for i = 1,9 do
  55.     energyTotal = energyTotal + mfsu[i].getEUStored() --# removed tostring 'cause the table is now indexes instead of keys
  56.   end
  57.   energyFlow = (energyTotal - energyTotals) / 20
  58.   energyTotals = energyTotal
  59.   energyPercentage = shorten(energyTotal / 360000000 * 100) --# you had tostring on this
  60.   m.setCursorPos(3,3)
  61.   m.write("Total Energy Stored: "..energyTotal.." EU")
  62.   m.setCursorPos(9,4)
  63.   m.write("Total Capacity: ")
  64.   colorPerc(energyPercentage)
  65. end
  66.  
  67. local function storedEach()
  68.   for i = 1,9 do
  69.     storedEU[i] = mfsu[i].getEUStored() --# removed tostring 'cause the table is now indexes instead of keys
  70.     storedPerc[i] = shorten(storedEU[i] / 40000000 * 100) --# you had tostring on this
  71.   end
  72.   m.setCursorPos(1,6)
  73.   for i = 1,3 do
  74.     m.write("MFSU"..i..": ")
  75.     colorPerc(storedPerc[i])
  76.     xPos = i * 14
  77.     m.setCursorPos(xPos,6)
  78.   end
  79.   m.setCursorPos(1,7)
  80.   for i = 4,6 do
  81.     m.write("MFSU"..i..": ")
  82.     colorPerc(storedPerc[i])
  83.     xPos = (i-3) * 14
  84.     m.setCursorPos(xPos,7)
  85.   end
  86.   m.setCursorPos(1,8)
  87.   for i = 7,9 do
  88.     m.write("MFSU"..i..": ")
  89.     colorPerc(storedPerc[i])
  90.     xPos = (i-6) * 14
  91.     m.setCursorPos(xPos,8)
  92.   end
  93. end
  94.  
  95. local function flowChart()
  96.   for i = 1,15 do
  97.     m.setCursorPos(6,(i + 10))
  98.     m.write("|")
  99.   end
  100.   for i = 1,39 do
  101.     m.setCursorPos(i,26)
  102.     m.write("-")
  103.   end
  104.   for i = 1,15 do
  105.     m.setCursorPos(34,(i + 10))
  106.     m.write("|")
  107.   end
  108.   for i = 1,2 do
  109.     if i == 1 then
  110.       yP = 1
  111.     else
  112.       yP = 35
  113.     end
  114.     m.setCursorPos(yP,11)
  115.     m.write("2.1k ")
  116.     m.setCursorPos(yP,13)
  117.     m.write("1.5k ")
  118.     m.setCursorPos(yP,15)
  119.     m.write(" 900 ")
  120.     m.setCursorPos(yP,17)
  121.     m.write(" 300 ")
  122.     m.setCursorPos(yP,19)
  123.     m.write("-300 ")
  124.     m.setCursorPos(yP,21)
  125.     m.write("-900 ")
  126.     m.setCursorPos(yP,23)
  127.     m.write("-1.5k")
  128.     m.setCursorPos(yP,25)
  129.     m.write("-2.1k")
  130.   end
  131. end
  132.  
  133. local function plotPos(num)
  134.   local plotter,plotW,plotR = energyFlow / 300,0,0
  135.   if plotter < 0 then
  136.     plotW = tonumber(string.sub(tostring(plotter),2,2))
  137.     plotR = tonumber(string.sub(tostring(plotter),4,4))
  138.     negative = true
  139.   else
  140.     plotW = tonumber(string.sub(tostring(plotter),1,1))
  141.     plotR = tonumber(string.sub(tostring(plotter),3,3))
  142.     negative = false
  143.   end
  144.   if plotR then
  145.     if plotR >= 5 then
  146.       plotW = plotW + 1
  147.     end
  148.   end
  149.   if not negative then --# you had == false here
  150.     plotW = plotW * (-1)
  151.   end
  152.   return plotW
  153. end
  154.  
  155. local function drawPlots()
  156.   m.setCursorPos(12,26)
  157.   m.write("--("..energyFlow.." EU/t)--------")
  158.   yPos = plotPos(energyFlow) + 18
  159.   printer[34] = yPos
  160.   for i = 7,33 do
  161.     j = i + 1
  162.     h = i
  163.     printer[i] = printer[j]
  164.     for i = 11,25 do
  165.       m.setCursorPos(h,i)
  166.       if i == printer[h] then
  167.         if i < 18 then
  168.           m.setBackgroundColor(colors.cyan)
  169.         else
  170.           m.setBackgroundColor(colors.blue)
  171.         end
  172.       else
  173.         m.setBackgroundColor(colors.black)
  174.       end
  175.       m.write(" ")
  176.       m.setBackgroundColor(colors.black)
  177.     end
  178.   end
  179. end
  180.  
  181. local function label()
  182.   m.setCursorPos(13,1)
  183.   m.write("Energy Monitor")
  184.   m.setCursorPos(1,10)
  185.   m.write("---------- Energy Flow (EU/t)----------")
  186. end
  187.  
  188. label()
  189. flowChart()
  190.  
  191. while true do
  192.   storedTotal()
  193.   storedEach()
  194.   drawPlots()
  195.   sleep(1)
  196. end
Advertisement
Add Comment
Please, Sign In to add comment