Advertisement
Sv443

ComputerCraft Container Level Display

Dec 18th, 2023 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | Gaming | 0 0
  1. -- Redstone signal input side
  2. INPUT_SIDE = "left"
  3.  
  4. -- Update interval in seconds
  5. -- Has to be a multiple of 0.05 (1 game tick)
  6. UPDATE_INTERVAL = 0.5
  7.  
  8. -- Colors for the tank level display
  9. -- The first value is the tank level in percent, above which the color (second value) is used
  10. -- Has to be sorted in descending order
  11. -- If you only want a single solid color, set the first entry to 0 and only it will be used
  12. LEVEL_COLORS = {
  13.     { 80, colors.red },
  14.     { 50, colors.yellow },
  15.     { 0,  colors.green },
  16. }
  17.  
  18. -- Text that's displayed above the tank level
  19. -- Can be 4 lines max, but up to 3 are recommended
  20. -- When using 1 or 2 lines, they will be vertically centered
  21. -- Use empty lines to offset the text
  22. -- Whitespaces will use the FG color, missing characters will use the BG color
  23. -- Each line can have 7 characters max
  24. DISPLAY_TEXT = {
  25.     " Water ",
  26.     " Level ",
  27. }
  28. -- Background color of the text display
  29. DISPLAY_TEXT_BG = colors.blue
  30. -- Foreground (text) color of the text display
  31. DISPLAY_TEXT_FG = colors.white
  32.  
  33. -- Default background color (behind the tank level bar)
  34. DEFAULT_BG = colors.gray
  35. -- Background color of the unfilled part of the tank level bar
  36. UNFILLED_BAR_BG = colors.lightGray
  37.  
  38.  
  39.  
  40. -- DON'T EDIT BELOW THIS LINE --
  41.  
  42. local lastValue = nil
  43.  
  44. function updateDisplay()
  45.     local monitor = peripheral.find("monitor")
  46.     local value = redstone.getAnalogInput(INPUT_SIDE)
  47.     if value ~= lastValue then
  48.         -- #SECTION draw text
  49.         lastValue = value
  50.  
  51.         monitor.setBackgroundColor(DEFAULT_BG)
  52.         monitor.clear()
  53.         monitor.setBackgroundColor(DISPLAY_TEXT_BG)
  54.         monitor.setTextColor(DISPLAY_TEXT_FG)
  55.  
  56.         local yPos = 1
  57.         monitor.setCursorPos(1, yPos)
  58.  
  59.         if tableLength(DISPLAY_TEXT) < 3 then
  60.             monitor.write("       ")
  61.             yPos = yPos + 1
  62.             monitor.setCursorPos(1, yPos)
  63.         end
  64.  
  65.         for _, line in pairs(DISPLAY_TEXT) do
  66.             monitor.write(line)
  67.             yPos = yPos + 1
  68.             monitor.setCursorPos(1, yPos)
  69.         end
  70.  
  71.         if tableLength(DISPLAY_TEXT) < 4 then
  72.             monitor.write("       ")
  73.             yPos = yPos + 1
  74.             monitor.setCursorPos(1, yPos)
  75.         end
  76.  
  77.         if tableLength(DISPLAY_TEXT) < 2 then
  78.             monitor.write("       ")
  79.             yPos = yPos + 1
  80.             monitor.setCursorPos(1, yPos)
  81.         end
  82.  
  83.         local level = math.floor(redstoneToPercent(value))
  84.         local spaces = level < 10 and "  " or level < 100 and " " or ""
  85.         local percText = " "..spaces..level.." % "
  86.         monitor.setCursorPos(1, 5)
  87.         monitor.write(percText)
  88.  
  89.         monitor.setCursorPos(1, 6)
  90.         monitor.write("       ")
  91.  
  92.         -- #SECTION draw level bar
  93.  
  94.         local barHeight = math.floor(value * (12 / 15))
  95.         local barColor = colors.black
  96.         for i, colData in ipairs(LEVEL_COLORS) do
  97.             local percent, col = colData[1], colData[2]
  98.             if level >= percent then
  99.                 barColor = col
  100.                 break
  101.             end
  102.         end
  103.  
  104.         local filledIdx = 1
  105.         for i = 19, 8, -1 do
  106.             if filledIdx > barHeight then
  107.                 monitor.setBackgroundColor(UNFILLED_BAR_BG)
  108.             else
  109.                 monitor.setBackgroundColor(barColor)
  110.             end
  111.             monitor.setCursorPos(2, i)
  112.             monitor.write("     ")
  113.             filledIdx = filledIdx + 1
  114.         end
  115.     end
  116. end
  117.  
  118. -- Converts a 0-15 redstone signal to a 0-100 percent value
  119. -- Magnitude is the number of decimal places, as a power of 10
  120. -- (1 = 1 decimal place, 10 = 2 decimal places, 100 = 3 decimal places, etc.)
  121. function redstoneToPercent(value, magnitude)
  122.     if magnitude == nil then magnitude = 1 end
  123.     return math.floor(value * (100 / 15) * magnitude) / magnitude
  124. end
  125.  
  126. function tableLength(table)
  127.     local count = 0
  128.     for _ in pairs(table) do count = count + 1 end
  129.     return count
  130. end
  131.  
  132. function run()
  133.     print("\n| Tank Level Display by Sv443")
  134.     print("| https://github.com/Sv443/ComputerCraft-Projects\n")
  135.     while true do
  136.         updateDisplay()
  137.         os.sleep(UPDATE_INTERVAL)
  138.     end
  139. end
  140.  
  141. run()
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement