Advertisement
MtnMCG

GPS CONSTELATION

Jul 22nd, 2024 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.20 KB | None | 0 0
  1. -- GPS Host Program with Coordinate Storage
  2.  
  3. -- File to store coordinates
  4. local COORD_FILE = "gps_coords.txt"
  5.  
  6. -- Function to read coordinates from file
  7. local function readCoordsFromFile()
  8.     if fs.exists(COORD_FILE) then
  9.         local file = fs.open(COORD_FILE, "r")
  10.         local x = tonumber(file.readLine())
  11.         local y = tonumber(file.readLine())
  12.         local z = tonumber(file.readLine())
  13.         file.close()
  14.         if x and y and z then
  15.             return x, y, z
  16.         end
  17.     end
  18.     return nil
  19. end
  20.  
  21. -- Function to write coordinates to file
  22. local function writeCoordsToFile(x, y, z)
  23.     local file = fs.open(COORD_FILE, "w")
  24.     file.writeLine(x)
  25.     file.writeLine(y)
  26.     file.writeLine(z)
  27.     file.close()
  28. end
  29.  
  30. -- Function to get coordinates from user input
  31. local function getCoords()
  32.     print("Enter this computer's exact Minecraft block coordinates:")
  33.     io.write("X: ")
  34.     local x = tonumber(io.read())
  35.     io.write("Y: ")
  36.     local y = tonumber(io.read())
  37.     io.write("Z: ")
  38.     local z = tonumber(io.read())
  39.     return x, y, z
  40. end
  41.  
  42. -- Main program
  43. local x, y, z = readCoordsFromFile()
  44.  
  45. if not x or not y or not z then
  46.     x, y, z = getCoords()
  47.     if not x or not y or not z then
  48.         print("Invalid coordinates. Exiting.")
  49.         return
  50.     end
  51.     writeCoordsToFile(x, y, z)
  52.     print("Coordinates saved to file.")
  53. else
  54.     print("Using stored coordinates:", x, y, z)
  55. end
  56.  
  57. print("Starting GPS Host at Minecraft coordinates:", x, y, z)
  58.  
  59. -- Ensure the modem is on
  60. local sides = {"top", "bottom", "left", "right", "front", "back"}
  61. local modemOpened = false
  62.  
  63. for _, side in ipairs(sides) do
  64.     if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  65.         rednet.open(side)
  66.         modemOpened = true
  67.         print("Modem opened on side: " .. side)
  68.         break
  69.     end
  70. end
  71.  
  72. if not modemOpened then
  73.     print("No modem found. Please attach a modem to the computer.")
  74.     return
  75. end
  76.  
  77. -- Start the GPS host
  78. print("Starting GPS host...")
  79. shell.run("gps", "host", x, y, z)
  80.  
  81. -- This point is reached only if shell.run returns, which it shouldn't under normal operation
  82. print("GPS Host has stopped. Please check for errors.")
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement