Advertisement
gelatine87

Mekanism Boiler Saftyvalve

May 25th, 2024 (edited)
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | Gaming | 0 0
  1. -- Function to ensure the presence of required peripherals (monitor and teleporter).
  2. local function checkPeripheral(name, errorMessage)
  3.     local peripheral = peripheral.find(name)
  4.     if not peripheral then
  5.         error(errorMessage)
  6.     end
  7.     return peripheral
  8. end
  9.  
  10. -- Retrieve peripherals
  11. local boiler = checkPeripheral("boilerValve", "Boiler peripheral not found.")
  12.  
  13. local function getThresholds(maxCap)
  14.     return maxCap * 0.9, maxCap * 0.7
  15. end
  16.  
  17. local function drawBar(amount, maxCap)
  18.     local width, height = term.getSize()
  19.     local barLength = math.floor(width * 0.8)
  20.     local fillPercentage = amount / maxCap
  21.     local fillLength = math.floor(fillPercentage * barLength)
  22.     local upperThreshold, lowerThreshold = getThresholds(maxCap)
  23.     local upperMark = math.floor(upperThreshold / maxCap * barLength)
  24.     local lowerMark = math.floor(lowerThreshold / maxCap * barLength)
  25.     local startX = math.floor((width - barLength) / 2)
  26.     local bar = string.char(143)
  27.  
  28.     term.clear()
  29.     term.setCursorPos(startX, 1)
  30.     term.write("0%")
  31.     term.setCursorPos(startX + lowerMark - 1, 1)
  32.     term.write("70%")
  33.     term.setCursorPos(startX + upperMark - 1, 1)
  34.     term.write("90%")
  35.     term.setCursorPos(startX + barLength, 1)
  36.     term.write("100%")
  37.  
  38.     term.setCursorPos(startX, 2)
  39.     term.write("[")
  40.     for i = 1, barLength do
  41.         if i == upperMark then
  42.             term.setTextColor(colors.red)
  43.             term.write("|")
  44.         elseif i == lowerMark then
  45.             term.setTextColor(colors.green)
  46.             term.write("|")
  47.         elseif i <= fillLength then
  48.             if fillPercentage >= 0.9 then
  49.                 term.setTextColor(colors.red)  -- Füllstand über 90% -> Rot
  50.             elseif fillPercentage <= 0.7 then
  51.                 term.setTextColor(colors.white)  -- Füllstand unter 70% -> Grün
  52.             elseif fillPercentage >= 0.7 and fillPercentage <= 0.9 then
  53.                 term.setTextColor(colors.green)  -- Füllstand zwischen 70% und 90% -> Grün
  54.             else
  55.                 term.setTextColor(colors.white)  -- Normaler Bereich -> Weiß
  56.             end
  57.             term.write(bar)
  58.         else
  59.             term.write(" ")
  60.         end
  61.     end
  62.     term.setTextColor(colors.white)
  63.     term.write("]")
  64.  
  65.     term.setCursorPos(math.floor((width - 40) / 2), 4)
  66.     print(string.format("Füllstand: %.2f%%", fillPercentage * 100))
  67.     term.setCursorPos(math.floor((width - 40) / 2), 5)
  68.     print(string.format("Sodium: %d / %d", amount, maxCap))
  69. end
  70.  
  71. local function updateRedstone(exceedsUpper, fallsBelowLower)
  72.     local newState = exceedsUpper and 15 or (fallsBelowLower and 0 or nil)
  73.     if newState then
  74.         rs.setAnalogOutput("right", newState)
  75.     end
  76. end
  77.  
  78. while true do
  79.     local maxCap = boiler.getHeatedCoolantCapacity()
  80.     local amount = boiler.getHeatedCoolant().amount
  81.  
  82.     if type(maxCap) ~= "number" or type(amount) ~= "number" then
  83.         print("Error: maxCap oder amount sind keine Zahlen")
  84.     else
  85.         local upperThreshold, lowerThreshold = getThresholds(maxCap)
  86.         local exceedsUpper = amount >= upperThreshold
  87.         local fallsBelowLower = amount <= lowerThreshold
  88.  
  89.         updateRedstone(exceedsUpper, fallsBelowLower)
  90.         drawBar(amount, maxCap)
  91.     end
  92.  
  93.     sleep(1)
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement