MtnMCG

sos serv main

Jun 6th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. -- Stationary Computer: receiver.lua
  2. -- By Gemini
  3.  
  4. -- CONFIGURATION --
  5. local logFile = "gps_log.txt" -- The name of the file to save coordinates to.
  6. local maxEntries = 10 -- The maximum number of coordinate entries to keep.
  7.  
  8. -- -- -- -- -- -- --
  9.  
  10. -- This table will hold the list of the last known coordinates.
  11. local coordHistory = {}
  12.  
  13. -- Open the ender modem to receive messages.
  14. -- Replace "top" with the side your modem is on.
  15. rednet.open("top")
  16.  
  17. print("GPS Receiver is running.")
  18. print("Listening for incoming coordinates...")
  19. print("Log file: " .. logFile)
  20. print("Press CTRL+T to terminate.")
  21.  
  22. -- Main loop to listen for messages.
  23. while true do
  24. -- Wait for a message with the protocol "gps_coords".
  25. local senderId, message, protocol = rednet.receive("gps_coords")
  26.  
  27. if message then
  28. -- Deserialize the message from a string back into a Lua table.
  29. local coordinates = textutils.unserialize(message)
  30.  
  31. if coordinates and coordinates.x then
  32. local coordString = string.format("X: %d, Y: %d, Z: %d", coordinates.x, coordinates.y, coordinates.z)
  33. print("Received from " .. senderId .. ": " .. coordString)
  34.  
  35. -- Add the new coordinate string to the beginning of our history table.
  36. table.insert(coordHistory, 1, coordString)
  37.  
  38. -- If the history has more than the max number of entries, remove the oldest one.
  39. if #coordHistory > maxEntries then
  40. table.remove(coordHistory)
  41. end
  42.  
  43. -- Now, save the updated history to the file.
  44. local file = fs.open(logFile, "w")
  45. if file then
  46. for i, entry in ipairs(coordHistory) do
  47. file.writeLine(entry)
  48. end
  49. file.close()
  50. else
  51. print("Error: Could not open " .. logFile .. " for writing.")
  52. end
  53. end
  54. end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment