FlightPathFinder/test.lua

72 lines
2.2 KiB
Lua
Raw Permalink Normal View History

2024-12-17 21:38:41 +01:00
-- main.lua
-- Import the A* algorithm and graph
local a_star = require("a_star")
local Flights = require("Flights")
-- Input parameters
local faction = "Alliance"
local start_node = 2089
local end_node = 66 --ashenvale
-- Run the A* algorithm
local paths, cost = a_star.run(Flights, faction, start_node, end_node)
-- Output the result
if paths then
print("Total cost: " .. cost .. " seconds")
print("Paths found:")
for i, path in ipairs(paths) do
-- Construct path names
local path_names = {}
for _, node in ipairs(path) do
local name = Flights[faction][node]["name"]
table.insert(path_names, name or "Unknown")
end
-- Output each path
print(string.format("Path %d (IDs): %s", i, table.concat(path, " -> ")))
print(string.format("Path %d (Names): %s", i, table.concat(path_names, " -> ")))
end
else
print("Error: " .. cost)
end
local input_node = 2089 -- Node ID for which we want to list available neighbors
-- -- Get all neighbors with their travel time
-- local neighbors, err = a_star.get_neighbors_with_cost(Flights, faction, input_node)
-- -- Output the result
-- if neighbors then
-- print("Available end nodes from node " .. input_node .. ":")
-- for _, neighbor_info in ipairs(neighbors) do
-- local node = neighbor_info.neighbor
-- local cost = neighbor_info.cost
-- local name = Flights[faction][node]["name"] or "Unknown"
-- print(string.format("Node ID: %d, Name: %s, Time: %d seconds", node, name, cost))
-- end
-- else
-- print("Error: " .. err)
-- end
local paths, err = a_star.get_all_paths_with_cost(Flights, faction, start_node)
-- Output the result
if paths then
print("All reachable end nodes from node " .. start_node .. ":")
for _, path_info in ipairs(paths) do
local node = path_info.neighbor
local cost = path_info.cost
-- Check if the node exists in the faction and fetch its name
local node_data = Flights[faction][node]
local name = node_data and node_data["name"] or "Unknown"
print(string.format("Node ID: %d, Name: %s, Time: %d seconds", node, name, cost))
end
else
print("Error: " .. err)
end