Advertisement
lucasmontec

tankDisplayRemote

Jul 6th, 2025 (edited)
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1. -- Wrap modem on the correct side
  2. local modem = peripheral.wrap("left") -- change "left" to your modem side
  3. modem.open(1) -- open channel 1 for receiving messages
  4.  
  5. -- Try to find a connected monitor
  6. local display = peripheral.find("monitor") or term
  7. if display ~= term then
  8.     display.setTextScale(0.5) -- adjust as you like
  9. end
  10.  
  11. -- Initialize tanks table as empty
  12. local tanks = {}
  13. local lastClearTime = os.clock()
  14. local clearInterval = 300 -- seconds
  15.  
  16. -- Function to render a tank
  17. local function renderTank(x, y, widthPercent, heightPercent, tank)
  18.     display.setBackgroundColor(colors.black)
  19.     display.setTextColor(colors.white)
  20.  
  21.     local w, h = display.getSize()
  22.     local width = math.max(1, math.floor(w * widthPercent))
  23.     local height = math.max(1, math.floor(h * heightPercent))
  24.  
  25.     local fillRatio = tank.amount / tank.capacity
  26.     local filledHeight = math.floor(height * fillRatio)
  27.  
  28.     local fluidColorMap = {
  29.         ["white"] = colors.white,
  30.         ["orange"] = colors.orange,
  31.         ["magenta"] = colors.magenta,
  32.         ["lightBlue"] = colors.lightBlue,
  33.         ["yellow"] = colors.yellow,
  34.         ["lime"] = colors.lime,
  35.         ["pink"] = colors.pink,
  36.         ["gray"] = colors.gray,
  37.         ["lightGray"] = colors.lightGray,
  38.         ["cyan"] = colors.cyan,
  39.         ["purple"] = colors.purple,
  40.         ["blue"] = colors.blue,
  41.         ["brown"] = colors.brown,
  42.         ["green"] = colors.green,
  43.         ["red"] = colors.red,
  44.         ["black"] = colors.black,
  45.         default = colors.green
  46.     }
  47.     local fillColor = fluidColorMap[tank.color] or fluidColorMap.default
  48.  
  49.     -- Extract only the last part of the fluid name after the last colon and replace underscores with spaces
  50.     local label = tank.fluid:match("([^:]+)$"):gsub("_", " ")
  51.     local labelWidth = #label
  52.  
  53.     -- Calculate centered x for the indicator
  54.     local indicatorX = x + math.floor((width - width) / 2)
  55.  
  56.     for i = 1, height do
  57.         display.setCursorPos(indicatorX, y + height - i)
  58.         if i <= filledHeight then
  59.             display.setBackgroundColor(fillColor)
  60.         else
  61.             display.setBackgroundColor(colors.gray)
  62.         end
  63.         display.write(string.rep(" ", width))
  64.     end
  65.  
  66.     -- Draw fill percent inside bar at top of filled section, centered
  67.     if filledHeight > 0 then
  68.         local percentText = string.format("%d%%", fillRatio * 100)
  69.         local textX = indicatorX + math.floor((width - #percentText) / 2)
  70.         display.setCursorPos(textX, y + height - filledHeight)
  71.         display.setBackgroundColor(fillColor)
  72.         display.setTextColor(colors.white)
  73.         display.write(percentText)
  74.         display.setTextColor(colors.white)
  75.     end
  76.  
  77.     display.setBackgroundColor(colors.black)
  78.  
  79.     -- Print fluid name below, centered relative to indicator
  80.     local labelX = indicatorX + math.floor((width - labelWidth) / 2)
  81.     display.setCursorPos(labelX, y + height + 1)
  82.     display.write(label)
  83.  
  84.     -- Print tank name below fluid name, also centered
  85.     if tank.name then
  86.         local nameLabel = tank.name:gsub("_", " ")
  87.         local nameWidth = #nameLabel
  88.         local nameX = indicatorX + math.floor((width - nameWidth) / 2)
  89.         display.setCursorPos(nameX, y + height + 2)
  90.         display.write(nameLabel)
  91.     end
  92. end
  93.  
  94. -- Main loop to listen for modem messages and update tanks
  95. while true do
  96.     local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  97.  
  98.     -- Clear tanks every clearInterval seconds
  99.     if os.clock() - lastClearTime >= clearInterval then
  100.         tanks = {}
  101.         lastClearTime = os.clock()
  102.     end
  103.  
  104.     -- Expecting message to be a table with tank data
  105.     if type(message) == "table" and message.name then
  106.         tanks[message.name] = message -- update or insert tank by name
  107.     end
  108.  
  109.     -- Clear and redraw all tanks
  110.     display.setBackgroundColor(colors.black)
  111.     display.clear()
  112.     local w, h = display.getSize()
  113.  
  114.     local numTanks = 0
  115.     for _ in pairs(tanks) do numTanks = numTanks + 1 end
  116.  
  117.     if numTanks > 0 then
  118.         local padding = 2
  119.         local spacing = 2
  120.         local totalSpacing = spacing * (numTanks - 1)
  121.         local availableWidth = w - (padding * 2) - totalSpacing
  122.         local tankWidth = math.floor(availableWidth / numTanks)
  123.         local tankWidthPercent = tankWidth / w
  124.  
  125.         local totalUsedWidth = (tankWidth * numTanks) + totalSpacing
  126.         local startX = math.floor((w - totalUsedWidth) / 2) + 1
  127.  
  128.         local x = startX
  129.         for _, tank in pairs(tanks) do
  130.             renderTank(x, 2, tankWidthPercent, 0.8, tank)
  131.             x = x + tankWidth + spacing
  132.         end
  133.     end
  134. end
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement