Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static List<string>? Dijkstra(GameLocation start, GameLocation end, Dictionary<string, GameLocation?> locations) {
- try {
- var queue = new SimplePriorityQueue<GameLocation, int>((x, y) => x - y);
- var previous = new Dictionary<GameLocation, GameLocation?>(Game1.locations.Count);
- queue.Enqueue(start, 0);
- foreach (var location in Game1.locations) {
- if (previous.TryAdd(location, null)) {
- queue.EnqueueWithoutDuplicates(location, int.MaxValue);
- }
- }
- while (queue.Count != 0) {
- var distance = queue.GetPriority(queue.First);
- var location = queue.Dequeue();
- if (distance == int.MaxValue) {
- return null; // No path
- }
- if (location == end) {
- var result = new List<string>(distance + 1);
- GameLocation? current = location;
- while (current is not null) {
- result.Add(current.Name);
- current = previous.GetValueOrDefault(current, null);
- }
- result.Reverse();
- return result;
- }
- void ProcessNeighbor(GameLocation node) {
- if (!queue!.Contains(node)) {
- return;
- }
- var nodeDistance = distance + 1;
- if (nodeDistance < queue.GetPriority(node)) {
- previous![node] = location;
- queue.UpdatePriority(node, nodeDistance);
- }
- }
- foreach (var warp in location.warps) {
- var neighbor = warp.GetTarget(locations);
- if (neighbor is null) {
- continue;
- }
- ProcessNeighbor(neighbor);
- }
- foreach (var door in location.doors.Pairs) {
- var neighbor = door.GetTarget(locations);
- if (neighbor is null) {
- continue;
- }
- ProcessNeighbor(neighbor);
- }
- }
- }
- catch (Exception ex) {
- Debug.Error("Exception generating warp points route list", ex);
- }
- return null; // Also no path
- }
Advertisement
Add Comment
Please, Sign In to add comment