SHOW:
|
|
- or go back to the newest paste.
1 | - | function scheduleFromFile(path) |
1 | + | local LONG_PING = 5 |
2 | - | local file = fs.open(path, "r") |
2 | + | local MEDIUM_PING = 1 |
3 | - | local body = file.readAll() |
3 | + | local SHORT_PING = 0.2 |
4 | - | file.close() |
4 | + | |
5 | - | return textutils.unserialize(body) |
5 | + | local inst1 = "pling" |
6 | local inst2 = "iron_xylophone" | |
7 | local inst3 = "bit" | |
8 | - | function scheduleFromStation() |
8 | + | local melody = { |
9 | - | return peripheral.find("Create_Station").getSchedule() |
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 | - | local args = {...} |
12 | + | {duration=0.5, notes={{inst1, 5, 11}, {inst2, 5, 11}, {inst3, 5, 11}}} |
13 | - | local filePath = args[1] |
13 | + | } |
14 | - | local schedule |
14 | + | |
15 | - | if filePath == nil then |
15 | + | local stations = {} |
16 | - | schedule = scheduleFromStation() |
16 | + | local speakers = {} |
17 | - | else |
17 | + | for _, name in pairs(peripheral.getNames()) do |
18 | - | schedule = scheduleFromFile(filePath) |
18 | + | pType = peripheral.getType(name) |
19 | if pType == "Create_Station" then | |
20 | local station = peripheral.wrap(name) | |
21 | - | local color = schedule.color |
21 | + | local sName = station.getStationName() |
22 | - | if color == nil then |
22 | + | station.name=string.gfind(string.gsub(sName, "%s%d", ""), "[%a%s%p]+")() |
23 | - | color = 2048 |
23 | + | station.platform=tonumber(string.gfind(sName, "%d+$")()) |
24 | station.present=station.isTrainPresent() | |
25 | - | term.setTextColor(colors.white) |
25 | + | table.insert(stations, station) |
26 | - | term.setBackgroundColor(colors.black) |
26 | + | elseif pType == "speaker" then |
27 | - | term.clear() |
27 | + | table.insert(speakers, peripheral.wrap(name)) |
28 | end | |
29 | - | local i = 1 |
29 | + | |
30 | - | for _, entry in ipairs(schedule.entries) do |
30 | + | |
31 | - | if entry.instruction.id == "create:destination" then |
31 | + | shell.run("clear") |
32 | - | local name = entry.instruction.data.text:gsub("%s%d+$", "") |
32 | + | |
33 | - | term.setCursorPos(4, i) |
33 | + | local pingTimer = os.startTimer(SHORT_PING) |
34 | - | term.write(name) |
34 | + | local noteTimer = nil |
35 | - | i = i + 1 |
35 | + | local noteIndex = 1 |
36 | - | end |
36 | + | while true do |
37 | local event, timerId = os.pullEvent("timer") | |
38 | - | term.setBackgroundColor(color) |
38 | + | local noteStart = false |
39 | - | for j=1,i-1 do |
39 | + | if timerId == pingTimer then |
40 | - | term.setCursorPos(2, j) |
40 | + | local newTime = LONG_PING |
41 | - | term.write(" ") |
41 | + | for i, station in pairs(stations) do |
42 | - | -- if j == 1 or j == i-1 then |
42 | + | if station.isTrainPresent() then |
43 | - | -- term.write("O") |
43 | + | if not station.present then |
44 | - | -- else |
44 | + | stations[i].present = true |
45 | - | -- term.write("|") |
45 | + | newTime = math.min(newTime, MEDIUM_PING) |
46 | - | -- end |
46 | + | noteStart = true |
47 | local name = station.getTrainName() | |
48 | - | term.setBackgroundColor(colors.black) |
48 | + | print(name.." arrived on platform "..station.platform) |
49 | - | term.setCursorPos(1, i) |
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 |