/// <summary> /// Creates a new path. /// </summary> /// <param name="start">The start node.</param> /// <param name="target">The ultimate goal node.</param> public AStarElement(MapNode start, MapNode target) { chain = new List <MapNode>(); chain.Add(start); this.target = target; }
public void addNode(MapNode node) { chain.Add(node); }
/// <summary> /// Gets the path in the cache. /// </summary> /// <param name="beginPosition">The (x, y) pair describing the start position.</param> /// <param name="endPosition">The (x, y) pair describing the goal.</param> /// <returns>The path to get from start to end.</returns> public static List <MapNode> getCachedPathTo(Vector2 beginPosition, Vector2 endPosition) { MapNode start = getNearestNodeTo(beginPosition.X, beginPosition.Y, entryPoint), goal = getNearestNodeTo(endPosition.X, endPosition.Y, entryPoint); foreach (List <MapNode> path in paths) { if (path[0].Equals(start) && path[path.Count - 1].Equals(goal)) { return(path); } if (path[0].Equals(goal) && path[path.Count - 1].Equals(start)) { path.Reverse(); return(path); } } return(null); }
/// <summary> /// Determines if the given node is already a part of this path. /// </summary> /// <param name="n">The node to check.</param> /// <returns>True if the node is already part of this path, false otherwise.</returns> public bool exists(MapNode n) { return(chain.Contains(n)); }
public override bool Equals(object obj) { MapNode o = (MapNode)obj; return(position.X == o.position.X && position.Y == o.position.Y); }
/// <summary> /// Finds the shortest path from the start to the end position. /// </summary> /// <param name="beginPosition">The (x, y) pair that represents the start location.</param> /// <param name="endPosition">The (x, y) pair that represents the end location.</param> /// <returns>A list of mapNodes representing the shortest path from start to end.</returns> public static List <MapNode> getShortestPathTo(Vector2 beginPosition, Vector2 endPosition) { MapNode start = getNearestNodeTo(beginPosition.X, beginPosition.Y, entryPoint), goal = getNearestNodeTo(endPosition.X, endPosition.Y, entryPoint); List <AStarElement> pQueue = new List <AStarElement>(); //priority queue pQueue.Add(new AStarElement(start, goal)); AStarElement toAdd = null; while (!pQueue[0].isFullPath()) { //The shortest path found so far will always be at the head of the list, //so per aStar, build on that path. AStarElement best = pQueue[0]; foreach (MapNode n in best.getLastNode().getAllNodes()) { if (best.exists(n)) { continue; } toAdd = new AStarElement(best); toAdd.addNode(n); pQueue.Add(toAdd); if (n == goal) { break; } } if (!best.isFullPath()) { pQueue.Remove(best); } pQueue.Sort(); //Custom sorting that makes shortest path be sorted to head of list. } //while return(pQueue[0].getNodes()); }
/// <summary> /// Builds a map. /// </summary> /// <param name="furnishing">The list of furniture.</param> /// <param name="maxX">The max X boundary not adjusted for walls.</param> /// <param name="maxY">The max Y boundary not adjusted for walls.</param> /// <returns>The node that will be considered the start node.</returns> public static MapNode buildMap(List <Furniture> furnishing, int maxX, int maxY) { AllNodes = new List <MapNode>(); MapNode.maxX = maxX; MapNode.maxY = maxY; //StreamWriter outFile = File.CreateText("Debug.log"); MapNode startNode = null; MapNode lastX = null; MapNode lastY = null; edgeLength = 4; MapNode n = null; int currentX = 2, currentY = 2; while (currentX < maxX) { if (currentX == 2 || currentX % edgeLength == 0 || currentX == maxX - 1) { //outFile.WriteLine("Execuing x " + currentX); MapNode xn = new MapNode(new Vector2(currentX, 2)); if (lastX != null) { lastX.right = xn; xn.left = lastX; } AllNodes.Add(xn); lastX = xn; lastY = lastX; if (currentX == 2 && currentY == 2) { startNode = xn; } } else { currentX++; continue; } currentY = 2; n = null; while (currentY < maxY) { if (currentX == 2 && currentY == 2) { currentY++; continue; } if (isPotentiallyBlockedInFront(furnishing, currentY, maxX)) { n = new MapNode(new Vector2(currentX, currentY)); //outFile.WriteLine("Y potential block in front " + currentY); } else if (currentY > 2 && isPotentiallyBlockedBehind(furnishing, currentY, maxX)) { n = new MapNode(new Vector2(currentX, currentY)); //outFile.WriteLine("Y potential block behind " + currentY); } else if (!isBlocked(furnishing, currentX, currentY) && currentY % edgeLength == 0) { n = new MapNode(new Vector2(currentX, currentY)); //outFile.WriteLine("Node length reached at y " + currentY); } //Next, we need to connect with the nodes around us. if (n != null) { //outFile.WriteLine("Y " + currentY + " created."); lastY.front = n; //outFile.WriteLine(String.Format(" {0}.front={1}", lastY, n)); n.back = lastY; //outFile.WriteLine(String.Format(" {0}.back={1}", n, lastY)); if (lastY.left != null && lastY.left.front != null) { lastY.left.front.right = n; //outFile.WriteLine(String.Format("{0}.right={1}", lastY.left.front, n)); n.left = lastY.left.front; //outFile.WriteLine(String.Format("{0}.left={1}", n, lastY.left.front)); } lastY = n; AllNodes.Add(n); } if (isBlocked(furnishing, currentX, currentY)) { //outFile.WriteLine("Block at " + currentY + " resolving..."); while (currentY < maxY) { if (!isBlocked(furnishing, currentX, ++currentY)) { //outFile.WriteLine("Success, y = " + currentY); n = new MapNode(new Vector2(currentX, currentY)); MapNode addTo = getNodeToLeft(currentX, currentY, startNode); if (addTo != null) { addTo.right = n; n.left = addTo; //outFile.WriteLine(String.Format("{0}.right={1}", addTo, n)); //outFile.WriteLine(String.Format("{0}.left={1}", n, addTo)); } else { //outFile.WriteLine("Node to left is null!"); addTo = getNodeToRight(currentX, currentY, startNode); } if (addTo != null) { addTo.left = n; n.right = addTo; //outFile.WriteLine(String.Format("{0}.left={1}", addTo, n)); //outFile.WriteLine(String.Format("{0}.right={1}", n, addTo)); } else { //outFile.WriteLine("Node to right is null!"); lastY = n; } AllNodes.Add(n); break; } } //while } //if blocked currentY++; n = null; } //y currentX++; } //x //outFile.Close(); return(entryPoint = startNode); }
public int CompareTo(object obj) { MapNode m = (MapNode)obj; return((int)(position.X - m.position.X)); }
public MapNode(Vector2 position) { this.position = position; left = front = right = back = null; }