Advertisement
MrFinn

ComputerCraft - Glados monitors client

Dec 1st, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. -- Configuration
  2. local TEXT_SCALE = 2 -- Text size (0.5, 1, 2, ...)
  3. local TEXT_COLOR = colors.orange -- Text color (use colors.<color>)
  4.  
  5. -- Setup monitors
  6. local monitors = {}
  7. for _, name in ipairs(peripheral.getNames()) do
  8. if peripheral.getType(name) == "monitor" and peripheral.isPresent(name) then
  9. local monitor = peripheral.wrap(name)
  10. monitor.setTextScale(TEXT_SCALE)
  11. table.insert(monitors, monitor)
  12. end
  13. end
  14.  
  15. -- Wireless modem setup
  16. local modem = peripheral.wrap("left")
  17. assert(modem, "Wireless modem not connected.")
  18. modem.open(1)
  19.  
  20. local function splitIntoLines(text, maxWidth)
  21. local lines = {}
  22. while #text > maxWidth do
  23. local splitPoint = maxWidth
  24. for i = maxWidth, 1, -1 do
  25. if text:sub(i, i):match("%s") then
  26. splitPoint = i
  27. break
  28. end
  29. end
  30. local line = text:sub(1, splitPoint):gsub("^%s+", ""):gsub("%s+$", "")
  31. table.insert(lines, line)
  32. text = text:sub(splitPoint + 1):gsub("^%s+", "")
  33. end
  34. if #text > 0 then
  35. table.insert(lines, text)
  36. end
  37. return lines
  38. end
  39.  
  40. local function sanitizeText(text)
  41. -- Replace UTF-8 artifacts with proper characters
  42. text = text:gsub("é", "é"):gsub("è", "è"):gsub("ê", "ê"):gsub("ë", "ë")
  43. text = text:gsub("Ã ", "à"):gsub("Ã ", "â"):gsub("Ã ", "ä")
  44. local replacements = {
  45. ["é"] = "e", ["è"] = "e", ["ê"] = "e", ["ë"] = "e",
  46. ["à"] = "a", ["â"] = "a", ["ä"] = "a",
  47. ["ç"] = "c",
  48. ["ù"] = "u", ["û"] = "u", ["ü"] = "u",
  49. ["î"] = "i", ["ï"] = "i",
  50. ["ô"] = "o", ["ö"] = "o",
  51. ["â"] = "a", ["ã"] = "a",
  52. ["œ"] = "oe", ["æ"] = "ae",
  53. ["ÿ"] = "y",
  54. ["É"] = "E", ["È"] = "E", ["Ê"] = "E", ["Ë"] = "E",
  55. ["À"] = "A", ["Â"] = "A", ["Ä"] = "A",
  56. ["Ç"] = "C",
  57. ["Ù"] = "U", ["Û"] = "U", ["Ü"] = "U",
  58. ["Î"] = "I", ["Ï"] = "I",
  59. ["Ô"] = "O", ["Ö"] = "O",
  60. ["Â"] = "A", ["Ã"] = "A",
  61. ["Œ"] = "OE", ["Æ"] = "AE",
  62. ["Ÿ"] = "Y"
  63. }
  64. -- Replace each character in the text based on the replacements table
  65. for original, replacement in pairs(replacements) do
  66. text = text:gsub(original, replacement)
  67. end
  68. return text
  69. end
  70.  
  71. local function displayMessageCentered(message)
  72. message = sanitizeText(message)
  73. for _, monitor in ipairs(monitors) do
  74. monitor.clear()
  75. monitor.setTextColor(TEXT_COLOR)
  76. local width, height = monitor.getSize()
  77. local lines = splitIntoLines(message, width)
  78. local yStart = math.floor((height - #lines) / 2) + 1
  79.  
  80. for i, line in ipairs(lines) do
  81. local xStart = math.floor((width - #line) / 2) + 1
  82. monitor.setCursorPos(xStart, yStart + i - 1)
  83. monitor.write(line)
  84. end
  85. end
  86. end
  87.  
  88. -- Listen for incoming messages
  89. while true do
  90. local event, side, senderChannel, replyChannel, message, distance = os.pullEvent("modem_message")
  91. if senderChannel == 1 then
  92. displayMessageCentered(message)
  93. end
  94. end
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement