Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Stationary Computer: receiver.lua
- -- By Gemini
- -- CONFIGURATION --
- local logFile = "gps_log.txt" -- The name of the file to save coordinates to.
- local maxEntries = 10 -- The maximum number of coordinate entries to keep.
- -- -- -- -- -- -- --
- -- This table will hold the list of the last known coordinates.
- local coordHistory = {}
- -- Open the ender modem to receive messages.
- -- Replace "top" with the side your modem is on.
- rednet.open("top")
- print("GPS Receiver is running.")
- print("Listening for incoming coordinates...")
- print("Log file: " .. logFile)
- print("Press CTRL+T to terminate.")
- -- Main loop to listen for messages.
- while true do
- -- Wait for a message with the protocol "gps_coords".
- local senderId, message, protocol = rednet.receive("gps_coords")
- if message then
- -- Deserialize the message from a string back into a Lua table.
- local coordinates = textutils.unserialize(message)
- if coordinates and coordinates.x then
- local coordString = string.format("X: %d, Y: %d, Z: %d", coordinates.x, coordinates.y, coordinates.z)
- print("Received from " .. senderId .. ": " .. coordString)
- -- Add the new coordinate string to the beginning of our history table.
- table.insert(coordHistory, 1, coordString)
- -- If the history has more than the max number of entries, remove the oldest one.
- if #coordHistory > maxEntries then
- table.remove(coordHistory)
- end
- -- Now, save the updated history to the file.
- local file = fs.open(logFile, "w")
- if file then
- for i, entry in ipairs(coordHistory) do
- file.writeLine(entry)
- end
- file.close()
- else
- print("Error: Could not open " .. logFile .. " for writing.")
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment