Advertisement
MoJoCreatior

Power Monitor Program V:30b

Feb 13th, 2016
7,816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.37 KB | None | 0 0
  1. --[[  Power Monitoring Program Written By: 0_Mr_Redstone_0  AKA  MoJoCreatior
  2. This code is considered free for use both commercial and private and can be redistributed so long as it meets following criteria:
  3. 1: Player_Athena and MoJoCreatior are credited as the original authors
  4. 2: You do not try to take full ownership of the code/written program
  5. 3: If you modify the code for re-upload you must provide it under the same conditions. as in- Open Source, and credited to me as original author]]
  6.  
  7. program = "Power Monitoring Program V:30"
  8.  
  9. --Function
  10.   function intro()
  11.     term.setBackgroundColour(colors.white)
  12.     term.clear()
  13.     term.setCursorPos(1,1)
  14.     term.setTextColour(colors.green)
  15.     print(program.." -Written By: 0_Mr_Redstone_0  AKA  MoJoCreatior")
  16.     print(" ")
  17.   end
  18.  
  19.   function centerText(text,yVal)
  20.     length = string.len(text)
  21.     minus = math.floor(monX-length)
  22.     x = math.floor(minus/2)
  23.     mon.setCursorPos(x+1,yVal)
  24.     mon.write(text)
  25.   end
  26.  
  27.   function centerTextC(text,line,colorB,colorT)
  28.     mon.setBackgroundColour(colorB)
  29.     mon.setTextColour(colorT)
  30.     length = string.len(text)
  31.     minus = math.floor(monX-length)
  32.     x = math.floor(minus/2)
  33.     mon.setCursorPos(x+1,line)
  34.     mon.write(text)
  35.   end
  36.  
  37.   function clearScreen()
  38.     mon.setBackgroundColour(convert(colorMB))
  39.     mon.clear()
  40.   end
  41.  
  42.   function config(name)
  43.     if fs.exists("config/"..name) then
  44.       cfg = fs.open("config/"..name,"r")
  45.       name = cfg.readAll()
  46.       cfg.close()
  47.       return name
  48.     else
  49.       fs.delete("startup")
  50.       startup = fs.open("startup","w")
  51.       startup.write('shell.run("setup.lua")')
  52.       startup.close()
  53.       term.setTextColour(colors.red)
  54.       print("ERROR! 404")
  55.       print(name.." config not found")
  56.       term.setTextColour(colors.blue)
  57.       print("rebooting into setup")
  58.       print("3") sleep(1)
  59.       print("2") sleep(1)
  60.       print("1") sleep(1)
  61.       os.reboot()
  62.     end
  63.   end
  64.  
  65.   function textScale()
  66.     mon.setTextScale(0.5)
  67.     monX,monY = mon.getSize()
  68.     if monX <= 16 or monY <= 11 then mon.setTextScale(0.5) elseif
  69.       monX <= 37 or monY <= 25 then mon.setTextScale(1) elseif
  70.       monX <= 58 or monY <= 39 then mon.setTextScale(1.5) elseif
  71.       monX <= 80 or monY <= 53 then mon.setTextScale(2) elseif
  72.       monX <= 101 or monY <= 68 then mon.setTextScale(2.5) else
  73.       mon.setTextScale(3)
  74.     end  
  75.   end
  76.  
  77.   function IC2Check()
  78.     IC2 = false
  79.     if peripheral.find("mfsu") == nil then
  80.       else cube = peripheral.find("mfsu")
  81.       IC2 = true
  82.     end
  83.     if peripheral.find("mfe") == nil then
  84.       else cube = peripheral.find("mfe")
  85.       IC2 = true
  86.     end
  87.     if peripheral.find("cesu") == nil then
  88.       else cube = peripheral.find("cesu")
  89.       IC2 = true
  90.     end
  91.     if peripheral.find("batbox") == nil then
  92.       else cube = peripheral.find("batbox")
  93.       IC2 = true
  94.     end
  95.   end
  96.  
  97.   function mekanismCheck()
  98.     mekanism = false
  99.     if peripheral.find("Basic Energy Cube") == nil then
  100.       else cube = peripheral.find("Basic Energy Cube")
  101.       mekanism = true
  102.     end
  103.     if peripheral.find("Advanced Energy Cube") == nil then
  104.       else cube = peripheral.find("Advanced Energy Cube")
  105.       mekanism = true
  106.     end
  107.     if peripheral.find("Elite Energy Cube") == nil then
  108.       else cube = peripheral.find("Elite Energy Cube")
  109.       mekanism = true
  110.     end
  111.     if peripheral.find("Ultimate Energy Cube") == nil then
  112.       else cube = peripheral.find("Ultimate Energy Cube")
  113.       mekanism = true
  114.     end
  115.     if peripheral.find("Induction Matrix") == nil then
  116.       else cube = peripheral.find("Induction Matrix")
  117.       mekanism = true
  118.     end
  119.   end
  120.  
  121.   function RFAPI()
  122.     rfAPI = false
  123.     if peripheral.find("tile_thermalexpansion_cell_resonant_name") == nil then
  124.       else cube = peripheral.find("tile_thermalexpansion_cell_resonant_name")
  125.       rfAPI = true
  126.     end
  127.     if peripheral.find("tile_blockcapacitorbank_name") == nil then
  128.       else cube = peripheral.find("tile_blockcapacitorbank_name")
  129.       rfAPI = true
  130.     end
  131.     if peripheral.find("extrautils_generatorsolar") == nil then
  132.       else cube = peripheral.find("extrautils_generatorsolar")
  133.       rfAPI = true
  134.     end
  135.   end
  136.  
  137.   function convert(color)
  138.     if color == "white" then color = colors.white end
  139.     if color == "orange" then color = colors.orange end
  140.     if color == "magenta" then color = colors.magenta end
  141.     if color == "lightBlue" then color = colors.lightBlue end
  142.     if color == "yellow" then color = colors.yellow end
  143.     if color == "lime" then color = colors.lime end
  144.     if color == "pinkgray" then color = colors.pinkgray end
  145.     if color == "gray" then color = colors.gray end
  146.     if color == "lightGray" then color = colors.lightGray end
  147.     if color == "cyan" then color = colors.cyan end
  148.     if color == "purple" then color = colors.purple end
  149.     if color == "blue" then color = colors.blue end
  150.     if color == "brown" then color = colors.brown end
  151.     if color == "green" then color = colors.green end
  152.     if color == "red" then color = colors.red end
  153.     if color == "black" then color = colors.black end
  154.     return color
  155.   end
  156.  
  157.   function eChange()
  158.     newE = curE
  159.     local difference = (newE-oldE)
  160.     oldE = curE
  161.     if rate == "f" then dividend = 20 elseif rate == "s" then dividend = 100 end
  162.     local change = math.floor(difference/dividend)
  163.     return(change)
  164.   end
  165.  
  166. --PreFace
  167.   --Color Coneverter
  168.   --Load Settings
  169.     title = config("title")
  170.     rate = config("rate")
  171.     dynamic = config("dynamic")
  172.     colorTB = config("colorTB")
  173.     colorTT = config("colorTT")
  174.     colorMB = config("colorMB")
  175.     colorST = config("colorST")
  176.     colorBT = config("colorBT")
  177.     colorBE = config("colorBE")
  178.     colorBF = config("colorBF")
  179.     colorPR = config("colorPR")
  180.     colorPos = config("colorPos")
  181.     colorNeg = config("colorNeg")
  182.     redstone = config("redstone")
  183.     side = config("side")
  184.     bundled = config("bundled")
  185.     options = config("options")
  186.     IC2Check()
  187.     mekanismCheck()
  188.     RFAPI()
  189.    
  190.   --Shows Settings inside control computer
  191.     intro()
  192.     term.setTextColour(colors.red)
  193.     print("Final Settings")
  194.     term.setTextColour(colors.blue)
  195.     print(" Title: "..title)
  196.     if rate == "f" then print(" Refresh Rate: fast") else print(" Refresh Rate: slow") end
  197.     if mekanism == true then print(" Mekanism: true") else print(" Mekanism: false") end
  198.     if IC2 == true then print(" IC2: true") else print(" IC2: false") end
  199.     if dynamic == "y" then print(" Dynamic Scaling: true") else print(" Dynamic Scaling: false") end
  200.     if options == "y" then
  201.       term.setTextColour(colors.red)
  202.       print("\nAdvanced Options")
  203.       term.setTextColour(colors.blue)
  204.       print(" Title Text Color: "..colorTT)
  205.       print(" Title Background: "..colorTB)
  206.       print(" Background Color: "..colorMB)
  207.       print(" Statistics Color: "..colorST)
  208.       print(" Bar Percent Text: "..colorBT)
  209.       print(" Bars Empty Color: "..colorBE)
  210.       print(" Bar Filled Color: "..colorBF)
  211.       print(" Positive Change: "..colorPos)
  212.       print(" Negative Change: "..colorNeg)
  213.     end
  214.     term.setTextColour(colors.gray)
  215.     print("Million < Billion < Trillion < Quadrillion")
  216.     term.setTextColour(colors.red)
  217.     print("\nIf settings are incorrect. Run setup.lua")
  218.     print("Hold CTRL+T to stop program")
  219.  
  220.   --RF/EU label
  221.     if IC2 == true then energyAPI = " EU" else energyAPI = " RF" end
  222.     if IC2 == true then energyAPI_ = "EU" else energyAPI_ = "RF" end
  223.    
  224.   --Wraps the Monitor
  225.     mon = peripheral.find("monitor")
  226.     monX,monY = mon.getSize()
  227.     textScale()
  228.    
  229.     monX,monY = mon.getSize()
  230.     oldE,newE,curE,maxE = 0,0,0,0
  231.    
  232.   while true do    
  233.       if dynamic == "y" then textScale() end
  234.  
  235.   --Main Program
  236.     --Variables for Calculations
  237.       if mekanism == true then maxE = cube.getMaxEnergy() or 1
  238.         elseif IC2 == true then maxE = cube.getEUCapacity() or 1
  239.         else maxE = cube.getMaxEnergyStored() or 1
  240.       end
  241.       if mekanism == true then curE = cube.getEnergy() or 1
  242.         elseif IC2 == true then curE = cube.getEUStored() or 1
  243.         else curE = cube.getEnergyStored() or 1
  244.       end
  245.       if mekanism == true then
  246.         maxE = math.floor(maxE/2.5)
  247.         curE = math.floor(curE/2.5)
  248.       end
  249.  
  250.       percent = math.floor(((curE/maxE)*100)+0.5)
  251.       bar = math.floor(((curE/maxE)*(monX-2))+0.5)
  252.       stats = curE
  253.      
  254.     --Redstone Output
  255.       if redstone == "y" then
  256.         if bundled == "1" then
  257.           if percent >= 99 then
  258.             rs.setBundledOutput(side,convert(colorPR))
  259.             else rs.setBundledOutput(side,0)
  260.           end
  261.         else
  262.           if percent >= 99 then
  263.             rs.setOutput(side,true)
  264.             else rs.setOutput(side,false)
  265.           end
  266.         end
  267.       end
  268.  
  269.     --Centers and Displays Title On Monitor
  270.  
  271.       clearScreen()
  272.       mon.setBackgroundColour(colors.black)
  273.       mon.setTextColour(colors.lightGray)
  274.       mon.setCursorPos(1,1)
  275.       centerTextC(string.rep(" ",string.len(title)+2),1,convert(colorTB),convert(colorTT))
  276.       centerTextC(title,1,convert(colorTB),convert(colorTT))
  277.  
  278.     --Monitor Statistics
  279.       --[[if curE >= 1000000 and curE <= 1000000000 then
  280.         stats = math.floor(stats/1000)
  281.         stats = (stats/1000)
  282.         mbtq = " M"
  283.       else]]if curE >= 1000000000 and curE <= 1000000000000 then
  284.         stats = math.floor(stats/1000000)
  285.         stats = (stats/1000)
  286.         mbtq = " B"
  287.       elseif curE >= 1000000000000 and curE <= 1000000000000000 then
  288.         stats = math.floor(stats/1000000000)
  289.         stats = (stats/1000)
  290.         mbtq = " T"
  291.       elseif curE >= 1000000000000000 then
  292.         stats = math.floor(stats/1000000000000)
  293.         stats = (stats/1000)
  294.         mbtq = " Q"
  295.       elseif curE <= 1000000 then mbtq = " "
  296.       end
  297.       mon.setBackgroundColour(convert(colorMB))
  298.       mon.setTextColour(convert(colorST))
  299.       if curE >= 1000000 then eAPI = energyAPI_ else eAPI = energyAPI end
  300.       centerText(stats..mbtq..eAPI,3)
  301.       --[[
  302.       if monX >= math.floor(string.len(stats)+9)
  303.         then
  304.           centerText("Total:"..stats..mbtq..eAPI,3)
  305.         else
  306.           centerText("Total:",3)
  307.           centerText(stats..mbtq..eAPI,4)
  308.       end
  309.       ]]
  310.      
  311.     --Change Per Tick Calculator and Display
  312.       e_Change = eChange()
  313.       if e_Change >= 0 then sign = "+" elseif e_Change <= 0 then sign = "-" end
  314.       if e_Change >= 0 then signC = colorPos elseif e_Change <= 0 then signC = colorNeg end
  315.       centerTextC(sign..e_Change..energyAPI.."/t",5,convert(colorMB),convert(signC))
  316.      
  317.       centerTextC(percent.."% Full",monY-3,convert(colorMB),convert(colorBT))
  318.    
  319.     --Loading Bar Code Re-Written Much MUCH simpler than before
  320.       mon.setCursorPos(2,monY-2)
  321.       mon.setBackgroundColour(convert(colorBE))
  322.       mon.write(string.rep(" ",monX-2))
  323.       mon.setCursorPos(2,monY-2)
  324.       mon.setBackgroundColour(convert(colorBF))
  325.       mon.write(string.rep(" ",bar))
  326.    
  327.       mon.setCursorPos(2,monY-1)
  328.       mon.setBackgroundColour(convert(colorBE))
  329.       mon.write(string.rep(" ",monX-2))
  330.       mon.setCursorPos(2,monY-1)
  331.       mon.setBackgroundColour(convert(colorBF))
  332.       mon.write(string.rep(" ",bar))
  333.    
  334.       if rate == "f" then os.sleep(1)
  335.       elseif rate == "s" then os.sleep(5)
  336.       else os.sleep(5) end
  337.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement