bobmarley12345

autopowermonitorv2

Nov 1st, 2020 (edited)
995
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- carrot xddddddddddd
  2. MonitorSide = "right"
  3. Cell = peripheral.wrap("left")
  4. Monitor = peripheral.wrap(MonitorSide)
  5.  
  6. function ClearMonitor()
  7.     Monitor.setTextColor(colours.black)
  8.     Monitor.setBackgroundColor(colours.black)
  9.     Monitor.clear()
  10.     Monitor.setCursorPos(1,1)
  11. end
  12.  
  13. function DrawText(xPos, yPos, text, textColour, backgroundColour)
  14.     Monitor.setBackgroundColor(backgroundColour)
  15.     Monitor.setTextColor(textColour)
  16.     Monitor.setCursorPos(xPos,yPos)
  17.     Monitor.write(text)
  18. end
  19.  
  20. function DrawLine(xPos, yPos, lineLength, colour)
  21.     Monitor.setBackgroundColor(colour)
  22.     Monitor.setTextColor(colour)
  23.     Monitor.setCursorPos(xPos,yPos)
  24.     Monitor.write(string.rep(" ", lineLength))
  25. end
  26.  
  27. function ProgressBar(xPos, yPos, width, value, maxValue, backgroundColour, progressColour)
  28.     DrawLine(xPos, yPos, width, backgroundColour) --backgoround bar
  29.     local barSize = math.floor((value/maxValue) * width)
  30.     DrawLine(xPos, yPos, barSize, progressColour) --progress so far
  31. end
  32.  
  33. function GroupBoxHeader(xPos, yPos, width, header, textColour, backgroundColour)
  34.     local headerLength = string.len(header)
  35.     local repeatChars = width - 4 - headerLength
  36.  
  37.     if (repeatChars < 0) then
  38.         repeatChars = 0
  39.     end
  40.  
  41.     local head = "--- " .. header .. " " .. string.rep("-", repeatChars)
  42.     DrawText(xPos, yPos, head, textColour, backgroundColour)
  43. end
  44.  
  45. function Main()
  46.     while true do
  47.         os.sleep(0.2)
  48.         Cell = peripheral.wrap("left")
  49.         Monitor = peripheral.wrap(MonitorSide)
  50.  
  51.         if (Cell ~= nil) then
  52.             local maxEnergy = Cell.getMaxEnergyStored("unknown")
  53.             local energyLastTick = Cell.getEnergyStored("unknown")
  54.             local currentTicks = os.clock() * 20
  55.             local energyNextTicks = Cell.getEnergyStored("unknown")
  56.             local nextTicks = os.clock() * 20
  57.             local tickDifference = (nextTicks - currentTicks)
  58.             local energyDifference = math.ceil(energyNextTicks - energyLastTick)
  59.             local tickEnergyDifference
  60.            
  61.             if (tickDifference > 0) then
  62.                 tickEnergyDifference = energyDifference / tickDifference
  63.             else
  64.                 tickEnergyDifference = energyDifference
  65.             end
  66.            
  67.             term.clear()
  68.             term.setCursorPos(1, 1)
  69.             print("Max Energy: " .. maxEnergy)
  70.             print("Energy stored last tick: " .. energyLastTick .. " RF/t")
  71.             print("Energy stored last tick: " .. energyLastTick / 1000  .. " KiRF/t")
  72.             print("Energy stored next tick: " .. energyNextTicks)
  73.             print("Energy stored next tick: " .. energyNextTicks / 1000  .. " KiRF/t")
  74.             print("Energy Difference: " .. energyDifference .. " RF/t")
  75.             print("Energy Difference: " .. energyDifference / 1000 .. " KiRF/t")
  76.             print("Last Tick time: " .. currentTicks)
  77.             print("Next Tick time: " .. nextTicks)
  78.             print("Tick Difference: " .. tickDifference)
  79.             print("Energy Difference over passed ticks: " .. tickEnergyDifference)
  80.  
  81.             local finalEnergyDifference = tickEnergyDifference / 1000
  82.  
  83.             print("Energy Difference over passed ticks: " .. tickEnergyDifference .. " KiRF/t")
  84.  
  85.             if (energyNextTicks ~= 0 and energyNextTicks == maxEnergy) then
  86.                 maxEnergy = 0
  87.                 energyLastTick = 0
  88.                 energyNextTicks = 0
  89.                 redstone.setOutput("top", true)
  90.                 sleep(2)
  91.                 os.reboot()
  92.             else
  93.                 redstone.setOutput("top", false)
  94.             end
  95.  
  96.             if (Monitor ~= nil) then
  97.                 local w, h = Monitor.getSize()
  98.                 ClearMonitor()
  99.                 DrawText(2, 2, "Energy Stored", colours.white, colours.black)
  100.                 ProgressBar(2, 4, w - 2, energyNextTicks, maxEnergy, colours.grey, colours.green)
  101.                 GroupBoxHeader(2, 6, w - 2, "Generated Energy", colours.cyan, colours.black)
  102.                 DrawText(2, 8, finalEnergyDifference .. " KiRF/t Generated", colours.white, colours.black)
  103.             end
  104.         else
  105.             if (Monitor ~= nil) then
  106.                 local w, h = Monitor.getSize()
  107.                 ClearMonitor()
  108.                 DrawText(2, 2, "Energy Stored", colours.white, colours.black)
  109.                 DrawText(16, 2, "(INACTIVE)", colours.red, colours.black)
  110.                 ProgressBar(2, 4, w - 2, 0, 100, colours.red, colours.red)
  111.                 GroupBoxHeader(2, 6, w - 2, "Generated Energy", colours.cyan, colours.black)
  112.                 DrawText(2, 8, "0 KiRF/t Generated", colours.white, colours.black)
  113.             end
  114.         end
  115.     end
  116. end
  117.  
  118. Main()
RAW Paste Data