Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set the vault and monitor peripherals
- function monitorSearch()
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == "monitor" then
- return peripheral.wrap(name)
- else
- --return null
- end
- end
- end
- function modemSearch()
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == "modem" then
- return peripheral.wrap(name)
- else
- --return null
- end
- end
- end
- -- Function to draw a colored border on the monitor
- function drawBorder()
- -- Define simpler border characters
- local horizontalBorder = "-"
- local verticalBorder = "|"
- local topLeftCorner = "+"
- local topRightCorner = "+"
- local bottomLeftCorner = "+"
- local bottomRightCorner = "+"
- -- Set the color for the border (e.g., colors.red)
- monitor.setTextColor(colors.red)
- -- Draw top border
- monitor.setCursorPos(1, 1)
- monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
- -- Draw bottom border
- monitor.setCursorPos(1, height)
- monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
- -- Draw left and right borders
- for y = 2, height - 1 do
- monitor.setCursorPos(1, y)
- monitor.write(verticalBorder)
- monitor.setCursorPos(width, y)
- monitor.write(verticalBorder)
- end
- -- Reset color to white for the rest of the text
- monitor.setTextColor(colors.white)
- end
- -- Function to add or update entries
- function addOrUpdateEntry(name, value, pc)
- oreValues[pc] = {oreName = name, qty = value}
- end
- function receiveData()
- local event, side, channel, replyChannel, message, distance
- event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if((channel == 1) and (message == "Hello")) then
- if (type(oreValues[replyChannel]) ~= "table") then
- print("New Device Found")
- modem.transmit(replyChannel, 1, "Confirmed")
- else
- print("Conflict Detected, Ignoring")
- end
- elseif(channel == 2) then
- charPos = string.find(message,":")
- oreName = string.sub(message,(charPos+1))
- qty = string.sub(message,0,(charPos-1))
- print(oreName.." "..qty)
- addOrUpdateEntry(oreName,qty,replyChannel)
- modem.transmit(replyChannel, 2, "Okay")
- end
- end
- function displayUpdate()
- monitor.setTextScale(1)
- width, height = monitor.getSize()
- monitor.clear()
- drawBorder()
- dispName = "Ore Collection Values"
- wPos = math.floor((width - string.len(dispName)) / 2) + 1
- hPos = 2
- monitor.setCursorPos(wPos,hPos)
- monitor.write(dispName)
- if next(oreValues) == nil then
- hPos = hPos + 2
- dispName = "Empty"
- wPos = math.floor((width - string.len(dispName)) / 2) + 1
- monitor.setCursorPos(wPos,hPos)
- monitor.write(dispName)
- return
- end
- local totalOreValues = {}
- for pc, entry in pairs(oreValues) do
- local oreName = entry.oreName
- local qty = tonumber(entry.qty) or 0
- if not totalOreValues[oreName] then
- totalOreValues[oreName] = 0
- end
- totalOreValues[oreName] = totalOreValues[oreName] + qty
- end
- for oreName, totalQty in pairs(totalOreValues) do
- hPos = hPos + 2
- dispName = oreName.. " = ".. totalQty
- wPos = math.floor((width - string.len(dispName)) / 2) + 1
- monitor.setCursorPos(wPos,hPos)
- monitor.write(dispName)
- end
- end
- -- Attach Peripherals
- monitor = monitorSearch()
- modem = modemSearch()
- -- Open Modem Channels
- modem.open(1)
- modem.open(2)
- -- Create the table
- oreValues = {}
- -- Set text scale
- while true do
- parallel.waitForAll(receiveData,displayUpdate)
- end
Advertisement