public AStar(PathfindingNode start, PathfindingNode end) { this.start = start; this.end = end; this.closedList = new LinkedList<PathfindingNode>(); this.openList = new LinkedList<PathfindingNode>(); }
public LinkedList<PathfindingNode> BuildPath(PathfindingNode start) { LinkedList<PathfindingNode> path = new LinkedList<PathfindingNode>(); int count = 0; PathfindingNode currentParent = this; while (currentParent != null) { path.AddFirst(currentParent); if (currentParent != start) { currentParent = currentParent.parent; count++; if (count > 1000) { Console.Out.WriteLine("Breaking @ Building path, something went wrong probably."); break; } } else { break; } } /*LinkedList<PathfindingNode> reversedPath = new LinkedList<PathfindingNode>(); foreach (PathfindingNode node in path) { reversedPath.AddFirst(node); }*/ return path; }
/// <summary> /// Adds a node to the list. /// </summary> /// <param name="node">The node</param> public void AddNode(PathfindingNode node) { lock (nodeLock) { this.nodeList.AddLast(node); } }
public PathfindingNodeConnection(PathfindingNode node1, PathfindingNode node2) { this.node1 = node1; this.node2 = node2; /*PathfindingNode closestNode = null; if (Util.GetHypoteneuseLength(node1.GetLocation(), new Point(0, 0)) < Util.GetHypoteneuseLength(node2.GetLocation(), new Point(0, 0))) { closestNode = node1; } else closestNode = node2; lengthDrawOffset = new Point(closestNode.x + (int)Math.Abs(node1.x - node2.x), closestNode.y + (int)Math.Abs(node1.y - node2.y));*/ }
/// <summary> /// Removes a node from the queue. /// </summary> /// <param name="node"></param> public void Remove(PathfindingNode node) { toProcess.Remove(node); }
/// <summary> /// Pushes a node onto the list. /// </summary> /// <param name="node">The node to push.</param> public void Push(PathfindingNode node) { toProcess.AddLast(node); }
/// <summary> /// Removes a connection. /// </summary> /// <param name="node">Node to remove.</param> public void RemoveConnection(PathfindingNode node) { // Can't use foreach here, because you're removing elements for (int i = 0; i < connections.Count; i++) { PathfindingNodeConnection conn = connections.ElementAt(i); if (conn.node1 == node || conn.node2 == node) { connections.Remove(conn); node.RemoveConnection(this); i--; } } }
/// <summary> /// Whether or not this node is connected to the parameter node. /// </summary> /// <param name="node">The node to check</param> /// <returns>The connection, or null otherwise</returns> public PathfindingNodeConnection IsConnected(PathfindingNode node) { foreach (PathfindingNodeConnection conn in connections) { if (conn.node1 == node || conn.node2 == node) { return conn; } } return null; }
/// <summary> /// Pushes a node onto the list. /// </summary> /// <param name="node">The node to push.</param> public void Push(PathfindingNode node) { if( !toProcess.Contains(node) ) toProcess.AddLast(node); }
public void OnConnectionsCreated(PathfindingNode source) { if (!this.initialised && !generatedOnLoadTime) { double currTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds ; // For each node we can make a connection to CustomArrayList<PathfindingNode> connectedNodes = this.GetConnectedNodes(); for( int i = 0; i < connectedNodes.Count(); i++){ if (connectedNodes.ElementAt(i) is Node) { Node connectedNode = (Node)connectedNodes.ElementAt(i); // Scedule it for reprocessing if (currTime - connectedNode.lastConnectionCreateTime > 5000) { // Remove its current nodes connectedNode.RemoveAllConnections(); connectedNode.lastConnectionCreateTime = currTime; SmartPathfindingNodeProcessor.GetInstance().Push(connectedNode); } } // Console.Out.WriteLine("Re-sceduled a node!"); } this.initialised = true; } lastConnectionCreateTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds; }
/// <summary> /// Removes a node /// </summary> /// <param name="node"></param> public void RemoveNode(PathfindingNode node) { lock (nodeLock) { this.nodeList.Remove(node); } }