Advertisement
MtnMCG

gps locate2

Jul 22nd, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. -- Custom GPS Locate Program for CC:Tweaked
  2.  
  3. -- Function to open the modem on any available side
  4. local function openModem()
  5. local sides = {"top", "bottom", "left", "right", "front", "back"}
  6. for _, side in ipairs(sides) do
  7. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  8. rednet.open(side)
  9. return true
  10. end
  11. end
  12. return false
  13. end
  14.  
  15. -- Function to locate position
  16. local function locate()
  17. rednet.broadcast("PING", "GPS_REQUEST")
  18. local responses = {}
  19. local timeout = os.startTimer(2)
  20.  
  21. while #responses < 3 do
  22. local event, id, message, protocol = os.pullEvent()
  23. if event == "rednet_message" and protocol == "GPS_RESPONSE" then
  24. table.insert(responses, {id = id, pos = message})
  25. elseif event == "timer" and id == timeout then
  26. break
  27. end
  28. end
  29.  
  30. if #responses < 3 then
  31. print("Not enough GPS hosts responded")
  32. return nil
  33. end
  34.  
  35. -- Simple trilateration (this is a very basic implementation and may not be accurate)
  36. local x = (responses[1].pos.x + responses[2].pos.x + responses[3].pos.x) / 3
  37. local y = (responses[1].pos.y + responses[2].pos.y + responses[3].pos.y) / 3
  38. local z = (responses[1].pos.z + responses[2].pos.z + responses[3].pos.z) / 3
  39.  
  40. return x, y, z
  41. end
  42.  
  43. -- Main program
  44. if not openModem() then
  45. print("No modem found. Please attach a modem to the computer.")
  46. return
  47. end
  48.  
  49. local x, y, z = locate()
  50. if x then
  51. print("Your position is approximately:")
  52. print("X:", math.floor(x + 0.5))
  53. print("Y:", math.floor(y + 0.5))
  54. print("Z:", math.floor(z + 0.5))
  55. else
  56. print("Unable to determine position")
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement