Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Find and wrap all connected speaker peripherals
- local speakers = {}
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "speaker" then
- table.insert(speakers, peripheral.wrap(name))
- end
- end
- -- Find and wrap the first monitor peripheral
- local monitor = nil
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "monitor" then
- monitor = peripheral.wrap(name)
- break
- end
- end
- -- Load quotes from file
- local quotes = {}
- local quoteIndex = 1
- local function loadQuotesFromFile(filename)
- local file = fs.open(filename, "r")
- if not file then
- print("Could not open quotes file:", filename)
- return
- end
- while true do
- local line = file.readLine()
- if not line then break end
- line = line:match("^%s*(.-)%s*$") -- trim whitespace
- if line ~= "" then
- table.insert(quotes, line)
- end
- end
- file.close()
- end
- -- Fisher-Yates shuffle
- local function shuffleQuotes()
- for i = #quotes, 2, -1 do
- local j = math.random(i)
- quotes[i], quotes[j] = quotes[j], quotes[i]
- end
- end
- -- Get the next quote, reshuffle when all have been used
- local function getNextQuote()
- if #quotes == 0 then
- return "No quotes loaded."
- end
- if quoteIndex > #quotes then
- shuffleQuotes()
- quoteIndex = 1
- end
- local quote = quotes[quoteIndex]
- quoteIndex = quoteIndex + 1
- return quote
- end
- -- Word-wrap a string to fit monitor width
- local function wrapText(text, width)
- local lines = {}
- for line in text:gmatch("[^\n]+") do
- while #line > width do
- local wrapAt = line:sub(1, width):match(".*()%s") or width
- table.insert(lines, line:sub(1, wrapAt))
- line = line:sub(wrapAt + 1):gsub("^%s+", "")
- end
- table.insert(lines, line)
- end
- return lines
- end
- -- Available text colors (excluding black)
- local textColors = {
- colors.white, colors.orange, colors.magenta, colors.lightBlue,
- colors.yellow, colors.lime, colors.pink, colors.gray,
- colors.lightGray, colors.cyan, colors.purple, colors.blue,
- colors.brown, colors.green, colors.red
- }
- -- Display a wrapped quote on the monitor in random color
- local function updateMonitorWithQuote()
- if not monitor then return end
- monitor.setTextScale(3)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- -- Check for color support
- if monitor.isColor and not monitor.isColor() then
- monitor.setTextColor(colors.white)
- else
- local color = textColors[math.random(#textColors)]
- monitor.setTextColor(color)
- end
- local quote = getNextQuote()
- local width, height = monitor.getSize()
- local lines = wrapText(quote, width)
- for i = 1, math.min(#lines, height) do
- monitor.setCursorPos(1, i)
- monitor.write(lines[i])
- end
- monitor.setTextColor(colors.white) -- reset
- end
- -- Play a song through all speakers
- local function playSong()
- if #speakers == 0 then
- print("No speakers found!")
- return
- end
- updateMonitorWithQuote()
- local notes = {13, 12, 9, 3, 2, 10, 14, 18}
- for _, pitch in ipairs(notes) do
- for _, speaker in ipairs(speakers) do
- speaker.playNote("bit", 1, pitch)
- end
- os.sleep(0.15)
- end
- end
- -- Check for redstone signal on any side
- local function isRedstonePowered()
- local sides = {"top", "bottom", "left", "right", "front", "back"}
- for _, side in ipairs(sides) do
- if redstone.getInput(side) then
- return true
- end
- end
- return false
- end
- -- Load quotes at startup and shuffle
- loadQuotesFromFile("quotes.txt")
- shuffleQuotes()
- -- Main loop: play song when redstone signal is received
- while true do
- if isRedstonePowered() then
- playSong()
- -- Wait for signal to turn off
- repeat
- os.sleep(0.1)
- until not isRedstonePowered()
- end
- os.sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement