Advertisement
TechManDylan

MightyDoor

May 24th, 2025 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.80 KB | None | 0 0
  1. -- Find and wrap all connected speaker peripherals
  2. local speakers = {}
  3. for _, name in ipairs(peripheral.getNames()) do
  4.   if peripheral.getType(name) == "speaker" then
  5.     table.insert(speakers, peripheral.wrap(name))
  6.   end
  7. end
  8.  
  9. -- Find and wrap the first monitor peripheral
  10. local monitor = nil
  11. for _, name in ipairs(peripheral.getNames()) do
  12.   if peripheral.getType(name) == "monitor" then
  13.     monitor = peripheral.wrap(name)
  14.     break
  15.   end
  16. end
  17.  
  18. -- Load quotes from file
  19. local quotes = {}
  20. local quoteIndex = 1
  21.  
  22. local function loadQuotesFromFile(filename)
  23.   local file = fs.open(filename, "r")
  24.   if not file then
  25.     print("Could not open quotes file:", filename)
  26.     return
  27.   end
  28.  
  29.   while true do
  30.     local line = file.readLine()
  31.     if not line then break end
  32.     line = line:match("^%s*(.-)%s*$") -- trim whitespace
  33.     if line ~= "" then
  34.       table.insert(quotes, line)
  35.     end
  36.   end
  37.  
  38.   file.close()
  39. end
  40.  
  41. -- Fisher-Yates shuffle
  42. local function shuffleQuotes()
  43.   for i = #quotes, 2, -1 do
  44.     local j = math.random(i)
  45.     quotes[i], quotes[j] = quotes[j], quotes[i]
  46.   end
  47. end
  48.  
  49. -- Get the next quote, reshuffle when all have been used
  50. local function getNextQuote()
  51.   if #quotes == 0 then
  52.     return "No quotes loaded."
  53.   end
  54.  
  55.   if quoteIndex > #quotes then
  56.     shuffleQuotes()
  57.     quoteIndex = 1
  58.   end
  59.  
  60.   local quote = quotes[quoteIndex]
  61.   quoteIndex = quoteIndex + 1
  62.   return quote
  63. end
  64.  
  65. -- Word-wrap a string to fit monitor width
  66. local function wrapText(text, width)
  67.   local lines = {}
  68.   for line in text:gmatch("[^\n]+") do
  69.     while #line > width do
  70.       local wrapAt = line:sub(1, width):match(".*()%s") or width
  71.       table.insert(lines, line:sub(1, wrapAt))
  72.       line = line:sub(wrapAt + 1):gsub("^%s+", "")
  73.     end
  74.     table.insert(lines, line)
  75.   end
  76.   return lines
  77. end
  78.  
  79. -- Available text colors (excluding black)
  80. local textColors = {
  81.   colors.white, colors.orange, colors.magenta, colors.lightBlue,
  82.   colors.yellow, colors.lime, colors.pink, colors.gray,
  83.   colors.lightGray, colors.cyan, colors.purple, colors.blue,
  84.   colors.brown, colors.green, colors.red
  85. }
  86.  
  87. -- Display a wrapped quote on the monitor in random color
  88. local function updateMonitorWithQuote()
  89.   if not monitor then return end
  90.  
  91.   monitor.setTextScale(3)
  92.   monitor.clear()
  93.   monitor.setCursorPos(1, 1)
  94.  
  95.   -- Check for color support
  96.   if monitor.isColor and not monitor.isColor() then
  97.     monitor.setTextColor(colors.white)
  98.   else
  99.     local color = textColors[math.random(#textColors)]
  100.     monitor.setTextColor(color)
  101.   end
  102.  
  103.   local quote = getNextQuote()
  104.   local width, height = monitor.getSize()
  105.   local lines = wrapText(quote, width)
  106.  
  107.   for i = 1, math.min(#lines, height) do
  108.     monitor.setCursorPos(1, i)
  109.     monitor.write(lines[i])
  110.   end
  111.  
  112.   monitor.setTextColor(colors.white) -- reset
  113. end
  114.  
  115. -- Play a song through all speakers
  116. local function playSong()
  117.   if #speakers == 0 then
  118.     print("No speakers found!")
  119.     return
  120.   end
  121.  
  122.   updateMonitorWithQuote()
  123.  
  124.   local notes = {13, 12, 9, 3, 2, 10, 14, 18}
  125.   for _, pitch in ipairs(notes) do
  126.     for _, speaker in ipairs(speakers) do
  127.       speaker.playNote("bit", 1, pitch)
  128.     end
  129.     os.sleep(0.15)
  130.   end
  131. end
  132.  
  133. -- Check for redstone signal on any side
  134. local function isRedstonePowered()
  135.   local sides = {"top", "bottom", "left", "right", "front", "back"}
  136.   for _, side in ipairs(sides) do
  137.     if redstone.getInput(side) then
  138.       return true
  139.     end
  140.   end
  141.   return false
  142. end
  143.  
  144. -- Load quotes at startup and shuffle
  145. loadQuotesFromFile("quotes.txt")
  146. shuffleQuotes()
  147.  
  148. -- Main loop: play song when redstone signal is received
  149. while true do
  150.   if isRedstonePowered() then
  151.     playSong()
  152.  
  153.     -- Wait for signal to turn off
  154.     repeat
  155.       os.sleep(0.1)
  156.     until not isRedstonePowered()
  157.   end
  158.  
  159.   os.sleep(0.1)
  160. end
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement