Advertisement
PrinceOfCookies

Untitled

Feb 15th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. local function formatPower(power)
  2. local powerString = tostring(math.floor(power))
  3. local units = { "", "k", "M", "G", "T" }
  4. local unitIndex = 1
  5.  
  6. while #powerString >= 4 do
  7. powerString = tostring(math.floor(power / 1000))
  8. power = power / 1000
  9. unitIndex = unitIndex + 1
  10. end
  11.  
  12. return powerString .. units[unitIndex]
  13. end
  14.  
  15. local function rebootIfTimeIsSixAM()
  16. local currTime = os.time()
  17. if currTime == 6.25 then
  18. local file
  19. local mode = fs.exists("reboot.log") and "a" or "w"
  20. file = fs.open("reboot.log", mode)
  21. file.write("OS Reboot: " .. os.date() .. "\n")
  22. file.close()
  23. os.reboot()
  24. end
  25. end
  26.  
  27. local reactorTypes = {
  28. "powah:reactor_starter",
  29. "powah:reactor_basic",
  30. "powah:reactor_hardened",
  31. "powah:reactor_blazing",
  32. "powah:reactor_niotic",
  33. "powah:reactor_spirited",
  34. "powah:reactor_nitro",
  35. }
  36.  
  37. local reactorTypeString = table.concat(reactorTypes, ",")
  38.  
  39. -- Get the list of all attached peripherals
  40. local periphList = peripheral.getNames()
  41. local monitor = peripheral.find("monitor")
  42.  
  43. if not monitor then
  44. error("Monitor not found")
  45. end
  46.  
  47. while true do
  48. local start = os.clock()
  49. rebootIfTimeIsSixAM()
  50.  
  51. local totalCapacity = 0
  52. local totalEnergy = 0
  53. local totalSolarPanels = 0
  54. local totalReactors = 0
  55. local reactorCapacity = 0
  56. local reactorEnergy = 0
  57. local reactorGeneration = 0
  58. local solarGenerator = 0
  59. local solarEnergyAfter = 0
  60. local elapsedTime = 0
  61. local realTime = 0
  62. local iterationTimer = os.clock()
  63. -- Iterate over all attached peripherals
  64.  
  65. for i, periphName in ipairs(periphList) do
  66. local periphType = peripheral.getType(periphName)
  67. if periphType == "ad_astra:solar_panel" then
  68. local panel = peripheral.wrap(periphName)
  69. local panelEnergy = panel.getEnergy() or 0
  70. local panelCapacity = panel.getEnergyCapacity() or 0
  71. totalEnergy = totalEnergy + panelEnergy
  72. totalCapacity = totalCapacity + panelCapacity
  73. totalSolarPanels = totalSolarPanels + 1
  74. local solarEnergyAfter = panel.getEnergy() or 0
  75. solarGenerator = 0
  76. elseif string.match(reactorTypeString, periphType) then
  77. local reactor = peripheral.wrap(periphName)
  78. reactorEnergy = reactorEnergy + reactor.getEnergy() or 0
  79. reactorCapacity = reactorCapacity + reactor.getEnergyCapacity() or 0
  80. totalReactors = totalReactors + 1
  81. local reactorEnergyAfter = reactor.getEnergy() or 0
  82. reactorGeneration = 0
  83. end
  84. end
  85.  
  86. local solarPercentage = math.floor((totalEnergy / totalCapacity) * 100)
  87. local reactorPercentage = math.floor((reactorEnergy / reactorCapacity) * 100)
  88.  
  89. print("Solar Power: " .. totalEnergy .. "\nSolar Capacity: " .. totalCapacity .. "\n" .. solarPercentage .. "\nTotal Panels: " .. totalSolarPanels .. "\nSolar Generation: " .. solarGenerator)
  90. print("Reactor Power: " .. reactorEnergy .. "\nReactor Capacity: " .. reactorCapacity .. "\n" .. reactorPercentage .. "\nTotal Reactors: " .. totalReactors .. "\nReactor Generation: " .. reactorGeneration)
  91.  
  92. monitor.clear()
  93.  
  94. local x, y = monitor.getSize()
  95.  
  96. for i = 1, x do
  97. monitor.setCursorPos(i, y - 1)
  98. monitor.write(string.rep("-", x))
  99. end
  100.  
  101. for i = 1, y do
  102. monitor.setCursorPos(x / 2 + 1, i)
  103. monitor.write("|")
  104. end
  105.  
  106. monitor.setCursorPos(1, y)
  107. monitor.write(formatPower(totalEnergy) ..
  108. "/" ..
  109. formatPower(totalCapacity) .. " | " .. formatPower(solarGenerator) .. "/s" .. " | " .. totalSolarPanels .. " panels")
  110.  
  111. if totalReactors == nil or reactorCapacity == nil then
  112. reactorGeneration = 0
  113. totalReactors = 0
  114. reactorCapacity = 0
  115. end
  116.  
  117. local reactorString = totalReactors ..
  118. " reactors | " ..
  119. formatPower(reactorGeneration) ..
  120. "/s" .. " | " .. formatPower(reactorEnergy) .. "/" .. formatPower(reactorCapacity)
  121. local reactorStringLength = string.len(reactorString)
  122. monitor.setCursorPos(x - reactorStringLength + 1, y)
  123. monitor.write(reactorString)
  124.  
  125.  
  126. sleep(0.5)
  127. local duration = os.clock() - start
  128. print("While loop (48-119): " .. duration)
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement