Advertisement
TrueVirusTV

Induction Matrix Monitoring Software

Oct 16th, 2022 (edited)
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.99 KB | None | 0 0
  1. -- ####################################### --
  2. -- # InductionMatrix Monitoring Software # --
  3. -- #       Written by TrueVirusTV        # --
  4. -- # Minecraft Version: 1.16.5           # --
  5. -- # Script-Version: 1.3                 # --
  6. -- ####################################### --
  7. -- Scale 0.5: 3x2
  8. -- Scale 1:   5x3
  9. -- Scale 1.5:
  10.  
  11.  
  12. -- # CHANGELOG:
  13. -- # # v1.3:
  14. -- # - Fixed the charging bug
  15.  
  16. -- # # v1.2:
  17. -- # - Added Peta to the Downscaling function
  18.  
  19. -- # # v1.1:
  20. -- # - Adding Colors to some text (Status, Input, Output)
  21.  
  22. -- ## INITIATE NAMES ## --
  23.  
  24. -- # DEFINE NAMES ("monitor" and "inductionPort")
  25. local mon
  26. local matrix
  27.  
  28. local rawPower
  29. local rawPowerMax
  30. local rawInput
  31. local rawOutput
  32. local rawPercentage
  33. local formed
  34. local cells
  35. local providers
  36.  
  37. local powerFE
  38. local powerMaxFE
  39. local inputFE
  40. local outputFE
  41. local percentage
  42.  
  43. local powerTillFull
  44.  
  45. local balance
  46.  
  47. local avgAr = {}
  48.  
  49. local monX, monY
  50. local curX, curY
  51.  
  52. -- ############################# --
  53. -- # START OF FUNCTION SECTION # --
  54. -- ############################# --
  55.  
  56. -- # Getting all values
  57. function getValues()
  58.     rawPower = matrix.getEnergy()
  59.     rawPowerMax = matrix.getMaxEnergy()
  60.     formed = matrix.isFormed()
  61.     powerTillFull = matrix.getMaxEnergy() - matrix.getEnergy()
  62.     if formed == true then
  63.         rawPercentage = matrix.getEnergyFilledPercentage()
  64.         cells = matrix.getInstalledCells()
  65.         providers = matrix.getInstalledProviders()
  66.         rawInput = matrix.getLastInput()
  67.         rawOutput = matrix.getLastOutput()
  68.         else
  69.         rawPercentage = 0
  70.         cells = 0
  71.         providers = 0
  72.         rawInput = 0
  73.         rawOutput = 0
  74.     end
  75. end
  76.  
  77. -- # Calculation raw energydata to FE
  78. function calcToFE()
  79.     powerFE = (rawPower / 10)*4
  80.     powerMaxFE = (rawPowerMax / 10)*4
  81.     percentage = rawPercentage*100
  82.     inputFE = (rawInput / 10)*4
  83.     outputFE = (rawOutput / 10)*4
  84.     end
  85.  
  86. -- # Drawing Interface
  87. function drawBoxes()
  88.     monX, monY = mon.getSize()
  89.     scale = mon.getTextScale()
  90.    
  91.     --if scale == 0.5 and monX < 100
  92.    
  93.     mon.setBackgroundColor(colors.black)
  94.     mon.clear()
  95.    
  96.     -- # Left Box
  97.     paintutils.drawBox(2,2,((monX/4)*3)-1,monY-1, colors.blue)
  98.     --# Right Box
  99.     paintutils.drawBox(((monX/4)*3)+1, 2, monX-1, monY-1, colors.blue)
  100.     end
  101.  
  102. -- # Drawing Energybar
  103. function drawBar()
  104.     monX, monY = mon.getSize()
  105.     local redStartX
  106.     local redStartY
  107.     local redEndX
  108.     local redEndY
  109.    
  110.     local greenStartX
  111.     local greenStartY
  112.     local greenEndX
  113.     local greenEndY
  114.    
  115.     -- # Calculate Red Bar
  116.     redStartX = ((monX/4)*3)+3
  117.     redStartY = 4
  118.     redEndX = monX-3
  119.     redEndY = monY-5
  120.    
  121.     -- # Bar Background (RED)
  122.     paintutils.drawFilledBox(redStartX, redStartY, redEndX, redEndY, colors.red)
  123.    
  124.    
  125.    
  126.     local barHeight = redEndY-3
  127.     local linePerPercent = 100 / barHeight
  128.     local rawLineCount = percentage / linePerPercent
  129.     local lineCount = math.floor(rawLineCount)
  130.    
  131.     -- # Energy Bar (GREEN)
  132.     if lineCount ~= 0 then
  133.         if percentage == 100 then
  134.             paintutils.drawFilledBox(redStartX, (redEndY-lineCount), redEndX, redEndY, colors.green)
  135.             else
  136.             paintutils.drawFilledBox(redStartX, (redEndY-lineCount)+1, redEndX, redEndY, colors.green)
  137.             end
  138.         end
  139.  
  140.     mon.setCursorPos(((monX/4)*3)+3, monY-3)
  141.     pertemp = string.format("%02.02f",percentage)
  142.     mon.setBackgroundColor(colors.black)
  143.     mon.write(pertemp.." %")
  144.    
  145.     end
  146.  
  147.  
  148. -- # Down-Scaling the loooong energynumbers
  149. function energy_calc(val)
  150.     local pre = ''
  151.     local suf = ''
  152.    
  153.     if val < 0 then
  154.         pre = '-'
  155.         val = -val
  156.         end
  157.    
  158.     if val > 1000 then
  159.         suf = 'k'
  160.         val = val / 1000
  161.     end
  162.  
  163.     if val > 1000 then
  164.         suf = 'M'
  165.         val = val / 1000
  166.     end
  167.  
  168.     if val > 1000 then
  169.         suf = 'G'
  170.         val = val / 1000
  171.     end
  172.  
  173.     if val > 1000 then
  174.         suf = 'T'
  175.         val = val / 1000
  176.     end
  177.    
  178.     if val > 1000 then
  179.         suf = 'P'
  180.         val = val / 1000
  181.     end
  182.    
  183.     return string.format('%s%0.2f %sFE', pre, val, suf)
  184.     end
  185.  
  186. -- # Calculation the time left
  187. function time(secs)
  188.        
  189.           secs = math.floor(secs)
  190.  
  191.           -- Days
  192.           local weeks = math.floor(secs / 604800)
  193.           secs = secs - (604800 * weeks)
  194.  
  195.           -- Days
  196.           local days = math.floor(secs / 86400)
  197.           secs = secs - (86400 * days)
  198.  
  199.           -- Hours
  200.           local hours = math.floor(secs / 3600)
  201.           secs = secs - (3600 * hours)
  202.  
  203.           -- Minutes
  204.           local mins = math.floor(secs / 60)
  205.           secs = secs - (60 * mins)
  206.        
  207.           -- If we have more than 72h worth of storage, switch to week, day, hour format
  208.           if weeks > 0 then
  209.             return string.format('%dwk %dd %dh', weeks, days, hours)
  210.           elseif days >= 3 then
  211.             return string.format('%dd %dh', days, hours)
  212.           end
  213.  
  214.           -- Formatting to have trailing zeros on H:MM:SS
  215.           return string.format('%d:%02d:%02d', hours, mins, secs)
  216.        
  217.         end
  218.  
  219. -- ############################## --
  220. -- # ENDING OF FUNCTION SECTION # --
  221. -- ############################## --
  222.  
  223. -- # Check of peripheral "monitor"
  224. if nil == peripheral.find("monitor") then
  225.     term.clear()
  226.     term.setCursorPos(1,1)
  227.     term.setTextColor(colors.orange)
  228.     print("")
  229.     print("Trying to find monitor...")
  230.     print("")
  231.    
  232.     local counter = 0
  233.    
  234.     -- # Start loop of monitor check
  235.     while nil == peripheral.find("monitor") do
  236.         if counter==10 then
  237.             term.setTextColor(colors.red)
  238.             print("No monitor could be found. Make sure a monitor is directly, or via a modem connected. The modem should be red if connected!")
  239.             print("Continue search...")
  240.             print("")
  241.         end
  242.         counter = counter+1
  243.         os.sleep(1)
  244.     end
  245.     term.setTextColor(colors.lime)
  246.     print("Monitor has been found! Connecting...")
  247.     term.setTextColor(colors.white)
  248.     os.sleep(2)
  249.     mon = peripheral.find("monitor")
  250.     else
  251.     mon = peripheral.find("monitor")
  252. end
  253.  
  254. -- # Check of peripheral "inductionPort"
  255. if nil == peripheral.find("inductionPort") then
  256.     term.clear()
  257.     term.setCursorPos(1,1)
  258.     term.setTextColor(colors.orange)
  259.     print("")
  260.     print("Trying to find Induction Port...")
  261.     print("")
  262.    
  263.     local counter = 0
  264.    
  265.     -- # Start loop of inductionPort check
  266.     while nil == peripheral.find("inductionPort") do
  267.         if counter==10 then
  268.             term.setTextColor(colors.red)
  269.             print("No Induction Port could be found. Make sure a Port is directly, or via a modem connected. The modem should be red if connected!")
  270.             print("Continue search...")
  271.             print("")
  272.             end
  273.         counter = counter+1
  274.         os.sleep(1)
  275.     end
  276.     term.setTextColor(colors.lime)
  277.     print("Induction Port has been found! Connecting...")
  278.     os.sleep(2)
  279.     matrix = peripheral.find("inductionPort")
  280.     else
  281.     matrix = peripheral.find("inductionPort")
  282. end
  283.  
  284.  
  285. -- ##################### --
  286. -- # INITIALISE SCREEN # --
  287. -- #   STARTING LOOP   # --
  288. -- ##################### --
  289. balance = matrix.getEnergy()
  290. while true do
  291.     local status, err = pcall(function ()
  292.             --local termEvent = os.pullEventRaw("terminate")
  293.             term.redirect(mon)
  294.             term.setBackgroundColor(colors.black)
  295.             term.setTextColor(colors.white)
  296.             -- #
  297.             -- # ONLY WHILE WIP
  298.             mon.setTextScale(1)
  299.             -- #
  300.            
  301.             monX, monY = mon.getSize()
  302.  
  303.             -- # Recheck of peripheral "monitor"
  304.             if nil == peripheral.find("monitor") then
  305.                 term.setBackgroundColor(colors.black)
  306.                 term.setTextColor(colors.white)
  307.                 term.clear()
  308.                 term.setCursorPos(1,1)
  309.                 term.setTextColor(colors.orange)
  310.                 print("")
  311.                 print("Error trying to find monitor!")
  312.                 print("")
  313.  
  314.                 local counter = 0
  315.  
  316.                 -- # Start loop of monitor check
  317.                 while nil == peripheral.find("monitor") do
  318.                     if counter==10 then
  319.                         term.setTextColor(colors.red)
  320.                         print("No monitor could be found. Make sure a monitor is directly, or via a modem connected. The modem should be red if connected!")
  321.                         print("Continue search...")
  322.                         print("")
  323.                     end
  324.                     counter = counter+1
  325.                     os.sleep(1)
  326.                 end
  327.                 term.setTextColor(colors.lime)
  328.                 print("Monitor has been found! Connecting...")
  329.                 term.setTextColor(colors.white)
  330.                 os.sleep(2)
  331.                 mon = peripheral.find("monitor")
  332.                 else
  333.                 mon = peripheral.find("monitor")
  334.             end
  335.  
  336.  
  337.             -- # Recheck of peripheral "inductionPort"
  338.             if nil == peripheral.find("inductionPort") then
  339.                 term.setBackgroundColor(colors.black)
  340.                 term.clear()
  341.                 term.setCursorPos(1,1)
  342.                 term.setTextColor(colors.orange)
  343.                 print("")
  344.                 print("Error trying to find Induction Port!")
  345.                 print("")
  346.  
  347.                 local counter = 0
  348.  
  349.                 -- # Start loop of inductionPort check
  350.                 while nil == peripheral.find("inductionPort") do
  351.                     if counter==10 then
  352.                         term.setTextColor(colors.red)
  353.                         print("No Induction Port could be found. Make sure a Port is directly, or via a modem connected. The modem should be red if connected!")
  354.                         print("Continue search...")
  355.                         print("")
  356.                         end
  357.                     counter = counter+1
  358.                     os.sleep(1)
  359.                 end
  360.                 term.setTextColor(colors.lime)
  361.                 print("Induction Port has been found! Connecting...")
  362.                 os.sleep(2)
  363.                 matrix = peripheral.find("inductionPort")
  364.                 else
  365.                 matrix = peripheral.find("inductionPort")
  366.             end
  367.  
  368.             getValues()
  369.             calcToFE()
  370.             --setMonitorSize()
  371.             drawBoxes()
  372.             drawBar()
  373.             mon.setBackgroundColor(colors.black)
  374.             monX, monY = mon.getSize()
  375.  
  376.             -- # SET TITLE OF SCREEN
  377.             local title = ' Induction Matrix '
  378.             local len = string.len(title)
  379.             mon.setCursorPos(((monX-1)-(monX/4)-len+2)/2, 2)
  380.             mon.write(title)
  381.  
  382. --#####################################################################
  383.  
  384.             -- # START OF STATS
  385.             mon.setCursorPos(4,4)
  386.             mon.write("Status: ")
  387.             if formed == true then
  388.                 mon.setTextColor(colors.lime)
  389.                 mon.write("Online")
  390.                 else
  391.                 mon.setTextColor(colors.red)
  392.                 mon.write("Offline")
  393.             end
  394.             mon.setTextColor(colors.white)
  395.            
  396.             curX, curY = mon.getCursorPos()
  397.  
  398.             mon.setCursorPos(4,curY+1)
  399.             if formed == true then
  400.                 mon.write("Cells: "..cells.." | Providers: "..providers)
  401.                 else
  402.                 mon.write("Cells: n/a | Providers: n/a")
  403.                 end
  404.            
  405.             curX, curY = mon.getCursorPos()
  406.  
  407.             mon.setCursorPos(4,curY+1)
  408.             mon.write("")
  409.            
  410.             curX, curY = mon.getCursorPos()
  411.  
  412.             mon.setCursorPos(4,curY+1)
  413.             mon.write("Current: "..energy_calc(powerFE).." / "..energy_calc(powerMaxFE))
  414.            
  415.             curX, curY = mon.getCursorPos()
  416.            
  417.             mon.setCursorPos(4,curY+1)
  418.             mon.write("")
  419.            
  420.             curX, curY = mon.getCursorPos()
  421.            
  422.             mon.setCursorPos(4,curY+1)
  423.             mon.setTextColor(colors.lime)
  424.             mon.write("Input  : "..energy_calc(inputFE).."/t")
  425.            
  426.             curX, curY = mon.getCursorPos()
  427.            
  428.             mon.setCursorPos(4,curY+1)
  429.             mon.write("         "..energy_calc(inputFE*20).."/s")
  430.            
  431.             curX, curY = mon.getCursorPos()
  432.            
  433.             mon.setCursorPos(4,curY+1)
  434.             mon.write("")
  435.            
  436.             curX, curY = mon.getCursorPos()
  437.            
  438.             mon.setCursorPos(4,curY+1)
  439.             mon.setTextColor(colors.red)
  440.             mon.write("Output : "..energy_calc(outputFE).."/t")
  441.            
  442.             curX, curY = mon.getCursorPos()
  443.            
  444.             mon.setCursorPos(4,curY+1)
  445.             mon.write("         "..energy_calc(outputFE*20).."/s")
  446.             mon.setTextColor(colors.white)
  447.             curX, curY = mon.getCursorPos()
  448.  
  449.             mon.setCursorPos(4,curY+1)
  450.             mon.write("")
  451.            
  452.             curX, curY = mon.getCursorPos()
  453.            
  454.             -- # Calculating balance
  455.             local balanceChangePerTick = inputFE - outputFE
  456.             local balanceChange = balanceChangePerTick*20
  457.            
  458.  
  459.             mon.setCursorPos(4,curY+1)
  460.             mon.write("Change: "..energy_calc(balanceChange).."/s")
  461.            
  462.             curX, curY = mon.getCursorPos()
  463.            
  464.             if table.getn(avgAr) >= 20 then
  465.                 table.remove(avgAr,1)
  466.                 table.insert(avgAr,balanceChange)
  467.                 else
  468.                     table.insert(avgAr,balanceChange)
  469.                 end
  470.  
  471.             local avg = 0
  472.             for i=1,#avgAr do
  473.                 avg = avg + avgAr[i]
  474.                 end
  475.            
  476.             if balanceChange > 0 then
  477.                 -- # Matrix is charging
  478.                 local seconds = powerTillFull / (avg/20)
  479.                 mon.setCursorPos(4,curY+1)
  480.                 mon.write("Charging: "..time(seconds))
  481.                 elseif balanceChange < 0 then
  482.                 -- # Matrix is discharging
  483.                 local seconds = powerFE / -(avg/20)
  484.                 mon.setCursorPos(4,curY+1)
  485.                 mon.write("Depleting: "..time(seconds))
  486.                 else
  487.                 -- # No Balance
  488.                 mon.setCursorPos(4,curY+1)
  489.                 mon.write("No Change. Idle.")
  490.                 end
  491.         end)
  492.    
  493.     if not status then
  494.         --if event == "terminate" then
  495.         --    os.pullEvent()
  496.         --  end
  497.         term.setBackgroundColor(colors.black)
  498.         term.clear()
  499.         term.setCursorPos(2,2)
  500.         term.setTextColor(colors.red)
  501.         term.write("Errors where detected during runtime.")
  502.         term.setCursorPos(2,3)
  503.         term.write("Trying to continue...")
  504.         term.setCursorPos(2,4)
  505.         term.write("-------------------------------------")
  506.         term.setCursorPos(2,5)
  507.         term.write(err)
  508.         os.sleep(5)
  509.         end
  510.  
  511.         os.sleep(0.5)
  512. end
  513.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement