BlissSilence

Untitled

Jan 12th, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. -- Find the stressometer and monitor
  2. local stressometer = peripheral.find("Create_Stressometer")
  3. local monitor = peripheral.find("monitor")
  4.  
  5. if not stressometer or not monitor then
  6. error("Stressometer or Monitor not found!")
  7. end
  8.  
  9. -- Redirect term to monitor
  10. term.redirect(monitor)
  11.  
  12. -- Set the monitor size and clear it
  13. monitor.setTextScale(0.5) -- Adjust text scale for better resolution
  14. monitor.clear()
  15. monitor.setCursorPos(1, 1)
  16.  
  17. -- Monitor dimensions
  18. local monitorWidth, monitorHeight = monitor.getSize()
  19.  
  20. -- Gauge parameters
  21. local gaugeWidth = monitorWidth - 4 -- Leave padding for the border
  22. local gaugeXStart = 3 -- Start slightly inside the edges
  23. local gaugeYStart = math.floor(monitorHeight / 2) -- Move down to accommodate the title
  24. local gaugeHeight = 3 -- Height of the gauge
  25.  
  26. -- Function to draw the title
  27. local function drawTitle()
  28. local title = "Current System Stress"
  29. local textX = math.floor((monitorWidth - #title) / 2)
  30. local textY = gaugeYStart - 4 -- Position above the gauge
  31.  
  32. monitor.setCursorPos(textX, textY)
  33. monitor.setBackgroundColor(colors.black)
  34. monitor.setTextColor(colors.white)
  35. monitor.write(title)
  36. end
  37.  
  38. -- Function to draw a gauge
  39. local function drawGauge(current, max)
  40. -- Check for invalid data
  41. if max <= 0 then
  42. monitor.clear()
  43. monitor.setCursorPos(1, 1)
  44. monitor.write("Invalid stress capacity!")
  45. return
  46. end
  47.  
  48. local percentage = current / max
  49. if percentage > 1 then percentage = 1 end -- Cap at 100%
  50. if percentage < 0 then percentage = 0 end -- Ensure no negative values
  51.  
  52. local filledWidth = math.floor(gaugeWidth * percentage)
  53.  
  54. -- Clear the area (gauge only)
  55. paintutils.drawBox(gaugeXStart, gaugeYStart, gaugeXStart + gaugeWidth, gaugeYStart + gaugeHeight, colors.black)
  56.  
  57. -- Draw the gauge border in light grey
  58. paintutils.drawBox(gaugeXStart, gaugeYStart, gaugeXStart + gaugeWidth, gaugeYStart + gaugeHeight, colors.lightGray)
  59.  
  60. -- Fill the gauge proportionally to the current stress
  61. if filledWidth > 0 then
  62. paintutils.drawFilledBox(
  63. gaugeXStart + 1,
  64. gaugeYStart + 1,
  65. gaugeXStart + filledWidth,
  66. gaugeYStart + gaugeHeight - 1,
  67. colors.red
  68. )
  69. end
  70.  
  71. -- Write the percentage text slightly below the gauge
  72. local percentageText = string.format("Stress: %.2f%%", percentage * 100)
  73. local textX = math.floor((monitorWidth - #percentageText) / 2)
  74. local textY = gaugeYStart + gaugeHeight + 2 -- Adjusted position for text
  75. monitor.setCursorPos(textX, textY)
  76. monitor.setBackgroundColor(colors.black)
  77. monitor.setTextColor(colors.white)
  78. monitor.write(percentageText)
  79. end
  80.  
  81. -- Main loop
  82. while true do
  83. local currentStress = stressometer.getStress()
  84. local maxStress = stressometer.getStressCapacity()
  85.  
  86. if maxStress > 0 then
  87. monitor.clear()
  88. drawTitle()
  89. drawGauge(currentStress, maxStress)
  90. else
  91. monitor.clear()
  92. monitor.setCursorPos(1, 1)
  93. monitor.write("No stress capacity detected!")
  94. end
  95.  
  96. sleep(1) -- Update every second
  97. end
  98.  
Advertisement
Add Comment
Please, Sign In to add comment