Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local LONG_PING = 5
- local MEDIUM_PING = 1
- local SHORT_PING = 0.2
- local inst1 = "pling"
- local inst2 = "iron_xylophone"
- local inst3 = "bit"
- local melody = {
- {duration=0.5, notes={{inst1, 5, 20}, {inst2, 5, 20}, {inst3, 5, 4}}},
- {duration=0.5, notes={{inst1, 5, 16}, {inst2, 5, 16}, {inst3, 5, 8}}},
- {duration=0.5, notes={{inst1, 5, 18}, {inst2, 5, 18}, {inst3, 5, 6}}},
- {duration=0.5, notes={{inst1, 5, 11}, {inst2, 5, 11}, {inst3, 5, 11}}}
- }
- local stations = {}
- local speakers = {}
- for _, name in pairs(peripheral.getNames()) do
- pType = peripheral.getType(name)
- if pType == "Create_Station" then
- local station = peripheral.wrap(name)
- local sName = station.getStationName()
- station.name=string.gfind(string.gsub(sName, "%s%d", ""), "[%a%s%p]+")()
- station.platform=tonumber(string.gfind(sName, "%d+$")())
- station.present=station.isTrainPresent()
- table.insert(stations, station)
- elseif pType == "speaker" then
- table.insert(speakers, peripheral.wrap(name))
- end
- end
- shell.run("clear")
- local pingTimer = os.startTimer(SHORT_PING)
- local noteTimer = nil
- local noteIndex = 1
- while true do
- local event, timerId = os.pullEvent("timer")
- local noteStart = false
- if timerId == pingTimer then
- local newTime = LONG_PING
- for i, station in pairs(stations) do
- if station.isTrainPresent() then
- if not station.present then
- stations[i].present = true
- newTime = math.min(newTime, MEDIUM_PING)
- noteStart = true
- local name = station.getTrainName()
- print(name.." arrived on platform "..station.platform)
- end
- else
- stations[i].present = false
- if station.isTrainEnroute() then
- newTime = math.min(newTime, MEDIUM_PING)
- elseif station.isTrainImminent() then
- newTime = math.min(newTime, SHORT_PING)
- end
- end
- end
- pingTimer = os.startTimer(newTime)
- end
- if timerId == noteTimer or noteStart then
- local notes = melody[noteIndex]
- if notes == nil then
- noteIndex = 1
- else
- for _, note in pairs(notes.notes) do
- for _, speaker in pairs(speakers) do
- speaker.playNote(unpack(note))
- end
- end
- noteIndex = noteIndex + 1
- noteTimer = os.startTimer(notes.duration)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement