Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FLOOR = 1
- function rednet_or_redstone(protocol_filter, timeout)
- -- The parameters used to be ( nTimeout ), detect this case for backwards compatibility
- if type(protocol_filter) == "number" and timeout == nil then
- protocol_filter, timeout = nil, protocol_filter
- end
- expect(1, protocol_filter, "string", "nil")
- expect(2, timeout, "number", "nil")
- -- Start the timer
- local timer = nil
- if timeout then
- timer = os.startTimer(timeout)
- end
- -- Wait for events
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "rednet_message" then
- -- Return the first matching rednet_message
- local sender_id, message, protocol = p1, p2, p3
- if protocol_filter == nil or protocol == protocol_filter then
- return "rednet_message", sender_id, message, protocol
- end
- elseif event == "timer" then
- -- Return nil if we timeout
- if p1 == timer then
- return nil
- end
- elseif event == "redstone" then
- return "redstone"
- end
- end
- end
- local monitor = peripheral.find("monitor")
- monitor.setBackgroundColor(colors.white)
- monitor.setTextColor(colors.black)
- monitor.setTextScale(5)
- monitor.clear()
- function display_floor(floor)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.write(floor)
- end
- peripheral.find("modem", rednet.open)
- while true do
- local type, id, message = rednet_or_redstone()
- if type == "redstone" then
- if redstone.getInput("back") then
- local data = {
- event = "arrived",
- floor = FLOOR
- }
- rednet.broadcast(textutils.serialise(data))
- display_floor(FLOOR)
- end
- elseif type == "rednet_message" then
- local data = textutils.unserialise(message)
- local event = data["event"]
- if event == "arrived" then
- local floor = data["floor"]
- display_floor(floor)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment