Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Wrap modem on the correct side
- local modem = peripheral.wrap("left") -- change "left" to your modem side
- modem.open(1) -- open channel 1 for receiving messages
- -- Try to find a connected monitor
- local display = peripheral.find("monitor") or term
- if display ~= term then
- display.setTextScale(0.5) -- adjust as you like
- end
- -- Initialize tanks table as empty
- local tanks = {}
- local lastClearTime = os.clock()
- local clearInterval = 300 -- seconds
- -- Function to render a tank
- local function renderTank(x, y, widthPercent, heightPercent, tank)
- display.setBackgroundColor(colors.black)
- display.setTextColor(colors.white)
- local w, h = display.getSize()
- local width = math.max(1, math.floor(w * widthPercent))
- local height = math.max(1, math.floor(h * heightPercent))
- local fillRatio = tank.amount / tank.capacity
- local filledHeight = math.floor(height * fillRatio)
- local fluidColorMap = {
- ["white"] = colors.white,
- ["orange"] = colors.orange,
- ["magenta"] = colors.magenta,
- ["lightBlue"] = colors.lightBlue,
- ["yellow"] = colors.yellow,
- ["lime"] = colors.lime,
- ["pink"] = colors.pink,
- ["gray"] = colors.gray,
- ["lightGray"] = colors.lightGray,
- ["cyan"] = colors.cyan,
- ["purple"] = colors.purple,
- ["blue"] = colors.blue,
- ["brown"] = colors.brown,
- ["green"] = colors.green,
- ["red"] = colors.red,
- ["black"] = colors.black,
- default = colors.green
- }
- local fillColor = fluidColorMap[tank.color] or fluidColorMap.default
- -- Extract only the last part of the fluid name after the last colon and replace underscores with spaces
- local label = tank.fluid:match("([^:]+)$"):gsub("_", " ")
- local labelWidth = #label
- -- Calculate centered x for the indicator
- local indicatorX = x + math.floor((width - width) / 2)
- for i = 1, height do
- display.setCursorPos(indicatorX, y + height - i)
- if i <= filledHeight then
- display.setBackgroundColor(fillColor)
- else
- display.setBackgroundColor(colors.gray)
- end
- display.write(string.rep(" ", width))
- end
- -- Draw fill percent inside bar at top of filled section, centered
- if filledHeight > 0 then
- local percentText = string.format("%d%%", fillRatio * 100)
- local textX = indicatorX + math.floor((width - #percentText) / 2)
- display.setCursorPos(textX, y + height - filledHeight)
- display.setBackgroundColor(fillColor)
- display.setTextColor(colors.white)
- display.write(percentText)
- display.setTextColor(colors.white)
- end
- display.setBackgroundColor(colors.black)
- -- Print fluid name below, centered relative to indicator
- local labelX = indicatorX + math.floor((width - labelWidth) / 2)
- display.setCursorPos(labelX, y + height + 1)
- display.write(label)
- -- Print tank name below fluid name, also centered
- if tank.name then
- local nameLabel = tank.name:gsub("_", " ")
- local nameWidth = #nameLabel
- local nameX = indicatorX + math.floor((width - nameWidth) / 2)
- display.setCursorPos(nameX, y + height + 2)
- display.write(nameLabel)
- end
- end
- -- Main loop to listen for modem messages and update tanks
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- -- Clear tanks every clearInterval seconds
- if os.clock() - lastClearTime >= clearInterval then
- tanks = {}
- lastClearTime = os.clock()
- end
- -- Expecting message to be a table with tank data
- if type(message) == "table" and message.name then
- tanks[message.name] = message -- update or insert tank by name
- end
- -- Clear and redraw all tanks
- display.setBackgroundColor(colors.black)
- display.clear()
- local w, h = display.getSize()
- local numTanks = 0
- for _ in pairs(tanks) do numTanks = numTanks + 1 end
- if numTanks > 0 then
- local padding = 2
- local spacing = 2
- local totalSpacing = spacing * (numTanks - 1)
- local availableWidth = w - (padding * 2) - totalSpacing
- local tankWidth = math.floor(availableWidth / numTanks)
- local tankWidthPercent = tankWidth / w
- local totalUsedWidth = (tankWidth * numTanks) + totalSpacing
- local startX = math.floor((w - totalUsedWidth) / 2) + 1
- local x = startX
- for _, tank in pairs(tanks) do
- renderTank(x, 2, tankWidthPercent, 0.8, tank)
- x = x + tankWidth + spacing
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement