Advertisement
MtnMCG

gps

Jul 21st, 2024 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. -- Define the waypoints table (initially empty)
  2. local waypoints = {}
  3.  
  4. -- Function to display the current list of waypoints
  5. local function displayWaypoints()
  6. term.clear()
  7. term.setCursorPos(1, 1)
  8. print("Waypoints:")
  9. if next(waypoints) == nil then
  10. print("No waypoints set.")
  11. else
  12. for name, coords in pairs(waypoints) do
  13. print(name .. ": X=" .. coords.x .. ", Y=" .. coords.y .. ", Z=" .. coords.z)
  14. end
  15. end
  16. print("Type 'exit' to return to the main menu.")
  17. local input = read()
  18. while input ~= "exit" do
  19. print("Type 'exit' to return to the main menu.")
  20. input = read()
  21. end
  22. end
  23.  
  24. -- Function to add a waypoint
  25. local function addWaypoint()
  26. term.clear()
  27. term.setCursorPos(1, 1)
  28. print("Enter waypoint name:")
  29. local name = read()
  30. print("Enter X coordinate:")
  31. local x = tonumber(read())
  32. print("Enter Y coordinate:")
  33. local y = tonumber(read())
  34. print("Enter Z coordinate:")
  35. local z = tonumber(read())
  36.  
  37. waypoints[name] = {x = x, y = y, z = z}
  38. print("Waypoint '" .. name .. "' added!")
  39. os.sleep(2)
  40. end
  41.  
  42. -- Function to get the current location from GPS
  43. local function getCurrentLocation()
  44. if gps then
  45. local x, y, z = gps.locate()
  46. if x and y and z then
  47. return x, y, z
  48. else
  49. print("Failed to get current GPS coordinates.")
  50. return nil, nil, nil
  51. end
  52. else
  53. print("GPS is not available.")
  54. return nil, nil, nil
  55. end
  56. end
  57.  
  58. -- Function to calculate the direction to a waypoint
  59. local function calculateDirection(currentX, currentZ, waypointX, waypointZ)
  60. local dx = waypointX - currentX
  61. local dz = waypointZ - currentZ
  62. local angle = math.deg(math.atan2(dz, dx))
  63. if angle < 0 then angle = angle + 360 end
  64. return angle
  65. end
  66.  
  67. -- Function to display the compass
  68. local function displayCompass()
  69. term.clear()
  70. term.setCursorPos(1, 1)
  71. print("Select a waypoint (type the name):")
  72. local selectedWaypoint = read()
  73.  
  74. local waypoint = waypoints[selectedWaypoint]
  75. if not waypoint then
  76. print("Invalid waypoint selected.")
  77. os.sleep(2)
  78. return
  79. end
  80.  
  81. local currentX, _, currentZ = getCurrentLocation()
  82. if not currentX or not currentZ then
  83. return
  84. end
  85.  
  86. local direction = calculateDirection(currentX, currentZ, waypoint.x, waypoint.z)
  87. term.clear()
  88. term.setCursorPos(1, 1)
  89. print("Compass Heading:")
  90. print("Waypoint: " .. selectedWaypoint)
  91. print("Direction: " .. direction .. " degrees")
  92. print("Type 'exit' to return to the main menu.")
  93. local input = read()
  94. while input ~= "exit" do
  95. print("Type 'exit' to return to the main menu.")
  96. input = read()
  97. end
  98. end
  99.  
  100. -- Main menu function
  101. local function mainMenu()
  102. while true do
  103. term.clear()
  104. term.setCursorPos(1, 1)
  105. print("1. Add Waypoint")
  106. print("2. Display Waypoints")
  107. print("3. Show Compass")
  108. print("4. Exit")
  109. term.setCursorPos(1, 5)
  110. local choice = tonumber(read())
  111.  
  112. if choice == 1 then
  113. addWaypoint()
  114. elseif choice == 2 then
  115. displayWaypoints()
  116. elseif choice == 3 then
  117. displayCompass()
  118. elseif choice == 4 then
  119. break
  120. else
  121. print("Invalid choice. Try again.")
  122. os.sleep(2)
  123. end
  124. end
  125. end
  126.  
  127. -- Run the main menu
  128. mainMenu()
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement