Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define the waypoints table (initially empty)
- local waypoints = {}
- -- Function to display the current list of waypoints
- local function displayWaypoints()
- term.clear()
- term.setCursorPos(1, 1)
- print("Waypoints:")
- if next(waypoints) == nil then
- print("No waypoints set.")
- else
- for name, coords in pairs(waypoints) do
- print(name .. ": X=" .. coords.x .. ", Y=" .. coords.y .. ", Z=" .. coords.z)
- end
- end
- print("Type 'exit' to return to the main menu.")
- local input = read()
- while input ~= "exit" do
- print("Type 'exit' to return to the main menu.")
- input = read()
- end
- end
- -- Function to add a waypoint
- local function addWaypoint()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter waypoint name:")
- local name = read()
- print("Enter X coordinate:")
- local x = tonumber(read())
- print("Enter Y coordinate:")
- local y = tonumber(read())
- print("Enter Z coordinate:")
- local z = tonumber(read())
- waypoints[name] = {x = x, y = y, z = z}
- print("Waypoint '" .. name .. "' added!")
- os.sleep(2)
- end
- -- Function to get the current location from GPS
- local function getCurrentLocation()
- if gps then
- local x, y, z = gps.locate()
- if x and y and z then
- return x, y, z
- else
- print("Failed to get current GPS coordinates.")
- return nil, nil, nil
- end
- else
- print("GPS is not available.")
- return nil, nil, nil
- end
- end
- -- Function to calculate the direction to a waypoint
- local function calculateDirection(currentX, currentZ, waypointX, waypointZ)
- local dx = waypointX - currentX
- local dz = waypointZ - currentZ
- local angle = math.deg(math.atan2(dz, dx))
- if angle < 0 then angle = angle + 360 end
- return angle
- end
- -- Function to display the compass
- local function displayCompass()
- term.clear()
- term.setCursorPos(1, 1)
- print("Select a waypoint (type the name):")
- local selectedWaypoint = read()
- local waypoint = waypoints[selectedWaypoint]
- if not waypoint then
- print("Invalid waypoint selected.")
- os.sleep(2)
- return
- end
- local currentX, _, currentZ = getCurrentLocation()
- if not currentX or not currentZ then
- return
- end
- local direction = calculateDirection(currentX, currentZ, waypoint.x, waypoint.z)
- term.clear()
- term.setCursorPos(1, 1)
- print("Compass Heading:")
- print("Waypoint: " .. selectedWaypoint)
- print("Direction: " .. direction .. " degrees")
- print("Type 'exit' to return to the main menu.")
- local input = read()
- while input ~= "exit" do
- print("Type 'exit' to return to the main menu.")
- input = read()
- end
- end
- -- Main menu function
- local function mainMenu()
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("1. Add Waypoint")
- print("2. Display Waypoints")
- print("3. Show Compass")
- print("4. Exit")
- term.setCursorPos(1, 5)
- local choice = tonumber(read())
- if choice == 1 then
- addWaypoint()
- elseif choice == 2 then
- displayWaypoints()
- elseif choice == 3 then
- displayCompass()
- elseif choice == 4 then
- break
- else
- print("Invalid choice. Try again.")
- os.sleep(2)
- end
- end
- end
- -- Run the main menu
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement