Advertisement
fyrkantis

StationPA

Feb 20th, 2025 (edited)
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local LONG_PING = 5
  2. local MEDIUM_PING = 1
  3. local SHORT_PING = 0.2
  4.  
  5. local inst1 = "pling"
  6. local inst2 = "iron_xylophone"
  7. local inst3 = "bit"
  8. local melody = {
  9.     {duration=0.5, notes={{inst1, 5, 20}, {inst2, 5, 20}, {inst3, 5, 4}}},
  10.     {duration=0.5, notes={{inst1, 5, 16}, {inst2, 5, 16}, {inst3, 5, 8}}},
  11.     {duration=0.5, notes={{inst1, 5, 18}, {inst2, 5, 18}, {inst3, 5, 6}}},
  12.     {duration=0.5, notes={{inst1, 5, 11}, {inst2, 5, 11}, {inst3, 5, 11}}}
  13. }
  14.  
  15. local stations = {}
  16. local speakers = {}
  17. for _, name in pairs(peripheral.getNames()) do
  18.     pType = peripheral.getType(name)
  19.     if pType == "Create_Station" then
  20.         local station = peripheral.wrap(name)
  21.         local sName = station.getStationName()
  22.         station.name=string.gfind(string.gsub(sName, "%s%d", ""), "[%a%s%p]+")()
  23.         station.platform=tonumber(string.gfind(sName, "%d+$")())
  24.         station.present=station.isTrainPresent()
  25.         table.insert(stations, station)
  26.     elseif pType == "speaker" then
  27.         table.insert(speakers, peripheral.wrap(name))
  28.     end
  29. end
  30.  
  31. shell.run("clear")
  32.  
  33. local pingTimer = os.startTimer(SHORT_PING)
  34. local noteTimer = nil
  35. local noteIndex = 1
  36. while true do
  37.     local event, timerId = os.pullEvent("timer")
  38.     local noteStart = false
  39.     if timerId == pingTimer then
  40.         local newTime = LONG_PING
  41.         for i, station in pairs(stations) do
  42.             if station.isTrainPresent() then
  43.                 if not station.present then
  44.                     stations[i].present = true
  45.                     newTime = math.min(newTime, MEDIUM_PING)
  46.                     noteStart = true
  47.                     local name = station.getTrainName()
  48.                     print(name.." arrived on platform "..station.platform)
  49.                 end
  50.             else
  51.                 stations[i].present = false
  52.                 if station.isTrainEnroute() then
  53.                     newTime = math.min(newTime, MEDIUM_PING)
  54.                 elseif station.isTrainImminent() then
  55.                     newTime = math.min(newTime, SHORT_PING)
  56.                 end
  57.             end
  58.         end
  59.         pingTimer = os.startTimer(newTime)
  60.     end
  61.     if timerId == noteTimer or noteStart then
  62.         local notes = melody[noteIndex]
  63.         if notes == nil then
  64.             noteIndex = 1
  65.         else
  66.             for _, note in pairs(notes.notes) do
  67.                 for _, speaker in pairs(speakers) do
  68.                     speaker.playNote(unpack(note))
  69.                 end
  70.             end
  71.             noteIndex = noteIndex + 1
  72.             noteTimer = os.startTimer(notes.duration)
  73.         end
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement