public Node nextNode; //next node to process #endregion Fields #region Constructors public NodeConnection(Node next, float cost, List<Node> connection, float estimatedCost) { ID = next.ID; nextNode = next; costSoFar = cost; Connection = connection; EstimatedCostSoFar = estimatedCost; }
public NodeConnection() { ID = -1; nextNode = new Node(); costSoFar = 0; Connection = new List<Node>(); EstimatedCostSoFar = 0; }
/// <summary> /// Initializes the map with the given height and width. /// </summary> /// <param name="height"></param> /// <param name="width"></param> public Map(int height, int width) { _map = new Node[height, width]; for (var i = 0; i < height; i++) { for (var k = 0; k < width; k++) { _map[i, k] = new Node(i, k); } } }
public List<Node> Solve(Node startNode, Node endNode) { var explored = new List<Node>(); var reachable = new List<Node> { startNode }; var solution = new List<Node>(); startNode.Cost = 0; while (reachable.Any()) { var here = ChooseNode(reachable); if (here.Equals(endNode)) { while (here != null) { solution.Add(here); here = here.Previous; } return solution; } var candidates = here.Reachable.Except(explored).ToList(); while (candidates.Any()) { var nextNode = ChooseNode(candidates); if (!reachable.Contains(nextNode)) { reachable.Add(nextNode); } if (here.Cost + 1 < nextNode.Cost) { nextNode.Previous = here; nextNode.Cost = here.Cost + 1; } candidates.Remove(nextNode); } reachable.Remove(here); explored.Add(here); } return solution; }
// Get A* path. If anything at all is wrong, ABORT private List <PathFinding.Node> GetAStarPath(Vector3 destination) { if (astarGetter(Position, destination).Equals(Vector3.Zero)) { return(null); } astar.Destination = astarGetter(destination, Vector3.Up); if (astar.Destination == null) { return(null); } PathFinding.Node o = astarGetter(Position, Vector3.Up); if (o == null) { return(null); } astar.Origin = o; return(astar.GetShortestPath()); }
/// <summary> /// Reconstructs the path that ends at node [node]. /// </summary> /// <param name="node">The chasing node.</param> /// <returns>A list that holds the nodes for the entire path.</returns> private static List<Node> ReconstructPath(Node node) { var path = new List<Node>(); ReconstructPath(path, node); path.Reverse(); return path; }
/// <summary> /// Adds a node to the open list, ensuring that the list is in ascending order. /// </summary> /// <param name="node">The node to add.</param> private static void AddToOpenList(Node node) { for (var i = 0; i < _open.Count; i++) { if (node.FScore < _open[i].FScore) { _open.Insert(i, node); return; } } _open.Add(node); }
/// <summary> /// Adds a node to the closed list, ensuring that the list is in ascending order. /// </summary> /// <param name="node">The node to add.</param> private static void AddToClosedList(Node node) { for (var i = 0; i < _closed.Count; i++) { if (node.FScore < _closed[i].FScore) { _closed.Insert(i, node); return; } } _closed.Add(node); }
public float weight; //Weight value for this link #endregion Fields #region Constructors public Link(Node node) { this.node = node; }
/// <summary> /// Calculates the distance to the given node. /// </summary> /// <param name="target">The node to calculate the distance to.</param> /// <returns>The distance.</returns> public float DistanceTo(Node target) { return (float)Math.Sqrt(Math.Pow(target.X - X, 2) + Math.Pow(target.Y - Y, 2)); }
/// <summary> /// Checks if another node is equal to this node. /// </summary> /// <param name="other">The node to compare with.</param> /// <returns>True if the X and Y values of both nodes are the same; False if they are not.</returns> protected bool Equals(Node other) { return X == other.X && Y == other.Y; }
/// <summary> /// Checks if a given node is within the bounds of the map. /// </summary> /// <param name="node">The node.</param> /// <returns>True if the location is within the bounds; false if it is not.</returns> public bool WithinMap(Node node) { return WithinMap(node.X, node.Y); }
void Awake() { GenerateDungeon(); // Create the paths for every room Dictionary<Room, List<PathFinding.Node>> roomRoutes = new Dictionary<Room, List<PathFinding.Node>>(); for(int i = 0; i < rooms.Count; i++) { Room s = rooms[i]; for(int j = i+1; j < rooms.Count; j++) { Room g = rooms[j]; PathFinding.Node start = new PathFinding.Node(s.X_Cell, s.Y_Cell); PathFinding.Node goal = new PathFinding.Node(g.X_Cell, g.Y_Cell); pathFinder.findPath(cells, start, goal); //roomRoutes.Add (s, pathFinder.findPath(cells, start, goal)); } } }
/// <summary> /// Draws the map to the console. /// </summary> /// <param name="agentA">The location of agent A.</param> /// <param name="agentB">The location of agent B.</param> public void Draw(Agent agentA, Agent agentB) { Console.Clear(); for (int x = 0; x < Height; x++) { Console.Write("+"); for (int y = 0; y < Width; y++) { Console.Write("-+"); } Console.WriteLine(); Console.Write("|"); for (int y = 0; y < Width; y++) { var tempNode = new Node(x, y); if (_map[x, y].Closed) Console.Write("X"); else if (x == agentA.X && y == agentA.Y) Console.Write("A"); else if (x == agentB.X && y == agentB.Y) Console.Write("B"); else if (agentA.Path.Contains(tempNode)) Console.Write("a"); else if (agentB.Path.Contains(tempNode)) Console.Write("b"); else Console.Write(" "); Console.Write("|"); } Console.WriteLine(); } Console.Write("+"); for (int y = 0; y < Width; y++) { Console.Write("-+"); } Console.WriteLine(); Console.Write("Press <enter> to continue to next step."); }
/// <summary> /// Recursive method for constructing the path. /// </summary> /// <param name="nodes">The list of nodes for the path.</param> /// <param name="node">The node to add.</param> /// <returns>The list of nodes for the path.</returns> private static List<Node> ReconstructPath(List<Node> nodes, Node node) { if (node == null) return nodes; nodes.Add(node); return ReconstructPath(nodes, node.CameFrom); }
/// <summary> /// Finds the shortest path from [fleeing] to [chasing] using the AStar algorithm. /// </summary> /// <param name="start">The node to fleeing at.</param> /// <param name="end">The node to chasing at.</param> /// <param name="map">The map.</param> /// <returns>The path as a list of nodes.</returns> public static List<Node> FindPathTo(Node start, Node end, Map map) { // Create lists var path = new List<Node>(); _open = new List<Node>(); _closed = new List<Node>(); // Create fleeing node var tempStart = start; tempStart.CameFrom = null; tempStart.GScore = 0; tempStart.FScore = tempStart.GScore + tempStart.DistanceTo(end); AddToOpenList(tempStart); // While open is not empty... while (_open.Count > 0) { var current = _open[0]; // If we found the goal ... if (current.Equals(end)) { // ... Create the path. path = ReconstructPath(current); break; } _open.Remove(current); AddToClosedList(current); // For all neighbour nodes foreach (var neighbour in map.NeighbourNodes(current)) { if (neighbour.Closed) continue; // Calculate scores. var tentativeGScore = current.GScore + neighbour.Cost; var tentativeFScore = tentativeGScore + neighbour.DistanceTo(end); // If the neighbour is in the closed list and the tentativeFScore is higher // than the neighbour's, skip it. if (_closed.Contains(neighbour) && tentativeFScore >= _closed[_closed.IndexOf(neighbour)].FScore) { continue; } // If the neighbour is not in the open list, add it. if (!_open.Contains(neighbour) && map.WithinMap(neighbour)) { neighbour.CameFrom = current; neighbour.GScore = tentativeGScore; neighbour.FScore = tentativeFScore; AddToOpenList(neighbour); continue; } // If the neighbour is in the open list, modify it. if (_open.Contains(neighbour) && map.WithinMap(neighbour)) { var indexOfNeighbour = _open.IndexOf(neighbour); if (tentativeFScore < _open[indexOfNeighbour].FScore) { _open[indexOfNeighbour].CameFrom = current; _open[indexOfNeighbour].GScore = tentativeGScore; _open[indexOfNeighbour].FScore = tentativeFScore; } } } } return path; }
/// <summary> /// Gets the nodes that are neighbours to the given node. /// </summary> /// <param name="node">The node to find the neighbours for.</param> /// <returns>A list containing the neighbours.</returns> public List<Node> NeighbourNodes(Node node) { var neighbors = new List<Node>(); if (WithinMap(node.X - 1, node.Y)) neighbors.Add(_map[node.X - 1, node.Y]); if (WithinMap(node.X + 1, node.Y)) neighbors.Add(_map[node.X + 1, node.Y]); if (WithinMap(node.X, node.Y - 1)) neighbors.Add(_map[node.X, node.Y - 1]); if (WithinMap(node.X, node.Y + 1)) neighbors.Add(_map[node.X, node.Y + 1]); if (WithinMap(node.X - 1, node.Y - 1)) neighbors.Add(_map[node.X - 1, node.Y - 1]); if (WithinMap(node.X + 1, node.Y - 1)) neighbors.Add(_map[node.X + 1, node.Y - 1]); if (WithinMap(node.X - 1, node.Y + 1)) neighbors.Add(_map[node.X - 1, node.Y + 1]); if (WithinMap(node.X + 1, node.Y + 1)) neighbors.Add(_map[node.X + 1, node.Y + 1]); return neighbors; }