Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem")
- local monitor = peripheral.find("monitor")
- local speaker = peripheral.find("speaker")
- monitor.setTextScale(1)
- monitor.setTextColour(colours.green)
- local configFileName = "lobby_display_config.txt"
- local computerID = os.getComputerID()
- local config = {channel = "", currentFloor = "", computerID = ""}
- local elevatorFloor = 0
- local prevFloor = 0
- -- Save Config to file
- local function saveConfig(config)
- local configData = textutils.serializeJSON(config)
- local file = fs.open(configFileName, "w")
- file.write(configData)
- file.close()
- end
- -- Load Config from file
- local function loadConfig()
- if fs.exists(configFileName) then
- local file = fs.open(configFileName, "r")
- local configData = file.readAll()
- file.close()
- config = textutils.unserializeJSON(configData)
- end
- end
- local function refreshMonitor()
- monitor.clear()
- monitor.setCursorPos(1, 1)
- end
- local function animateFloorChange(newFloor)
- local width, height = monitor.getSize()
- local x = math.ceil(width / 2)
- local startY, endY
- if newFloor > prevFloor then
- startY = height
- endY = math.ceil(height / 2)
- else
- startY = 1
- endY = math.ceil(height / 2)
- end
- for y = startY, endY, newFloor > prevFloor and -1 or 1 do
- refreshMonitor()
- monitor.setCursorPos(x, y)
- monitor.write(tostring(newFloor))
- sleep(0.1)
- end
- prevFloor = newFloor
- end
- local function mainLoop(config)
- modem.open(tonumber(config.channel))
- while true do
- local event, modem, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
- if event == "modem_message" and senderChannel == tonumber(config.channel) then -- Use config.channel here
- if string.sub(message, 1, 5) == "idle:" then
- elevatorFloor = tonumber(string.sub(message, 6))
- animateFloorChange(elevatorFloor)
- print("Elevator floor: " .. elevatorFloor)
- if elevatorFloor == tonumber(config.currentFloor) then -- Use config.currentFloor here
- if prevFloor ~= elevatorFloor then
- speaker.playSound("minecraft:block.note_block.pling")
- else
- speaker.playSound("minecraft:block.note_block.bell", 0.5, 0.3)
- end
- end
- end
- end
- end
- end
- -- Initialization
- local function initializeClient()
- loadConfig()
- if not config.channel or config.channel == "" then
- print("Please enter the channel to use:")
- config.channel = tonumber(read())
- end
- if not config.currentFloor or config.currentFloor == "" then
- print("Please enter the current floor:")
- config.currentFloor = tonumber(read())
- end
- if not config.computerID then
- config.computerID = tostring(computerID) -- Convert to string if needed
- end
- saveConfig(config)
- mainLoop(config)
- end
- -- Entry Point
- initializeClient()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement