public void Run() { List<NodeConnection> connections = new List<NodeConnection>(); PathfindingNode nodeA = new PathfindingNode() { Identifier = "A" }; PathfindingNode nodeB = new PathfindingNode() { Identifier = "B" }; PathfindingNode nodeC = new PathfindingNode() { Identifier = "C" }; PathfindingNode nodeD = new PathfindingNode() { Identifier = "D" }; PathfindingNode nodeE = new PathfindingNode() { Identifier = "E" }; PathfindingNode nodeF = new PathfindingNode() { Identifier = "F" }; PathGraph pathFinder = new PathGraph(); pathFinder.AddConnection(nodeA, nodeB, 1); pathFinder.AddConnection(nodeA, nodeC, 4); pathFinder.AddConnection(nodeA, nodeD, 8); pathFinder.AddConnection(nodeB, nodeC, 6); pathFinder.AddConnection(nodeB, nodeC, 10); pathFinder.AddConnection(nodeC, nodeD, 3); pathFinder.AddConnection(nodeC, nodeE, 8); pathFinder.AddConnection(nodeD, nodeE, 1); pathFinder.RemoveAllConnectionsBetween(nodeA, nodeC); pathFinder.RemoveAllConnectionsBetween(nodeD, nodeE); List<PathfindingNode> fastestPath = pathFinder.FindRoute(nodeA, nodeE); if (fastestPath == null) { Console.WriteLine("No path found!"); } else { Console.WriteLine("Fastest route is:"); //while (fastestPath.Parent != null) //{ // Console.WriteLine(fastestPath.Node + " cost " + fastestPath.Cost); // fastestPath = fastestPath.Parent; //} for (int i = 0; i < fastestPath.Count; i++) { PathfindingNode node = fastestPath[i]; Console.WriteLine(i + ": " + node.Identifier); } } }
/// <summary> /// This method should be called when all of the traversable locations are entered. Note, do NOT place DOORs before calling /// this method. Doors require actions to be traversable, so they would block the path. /// </summary> private PathGraph GeneratePathfindingGraph() { DateTime startTime = DateTime.Now; PathGraph pathfindingGraph = new PathGraph(); //PathfindingNodes = new List<PathfindingNode>(); //PathfindingConnections = new List<NodeConnection>(); for (int y = 0; y < MAP_HEIGHT_IN_TILES; y++) { for (int x = 0; x < MAP_WIDTH_IN_TILES; x++) { String locationKey = GetKeyForLocation(x, y); MapTileList locationTiles = GetMapTilesForLocation(x, y); Boolean locationIsTraversable = true; Boolean isTraversableNorth = true; Boolean isTraversableEast = true; Boolean isTraversableSouth = true; Boolean isTraversableWest = true; foreach(MapTile tile in locationTiles) { // If we have already determined that all paths are blocked, then there's no sense looping any further. if (!locationIsTraversable || (!isTraversableNorth && !isTraversableEast && !isTraversableSouth && !isTraversableWest)) { break; } if (tile.BlocksMovement) { locationIsTraversable = false; } if (tile.BlocksNorth) { isTraversableNorth = false; } if (tile.BlocksEast) { isTraversableEast = false; } if (tile.BlocksSouth) { isTraversableSouth = false; } if (tile.BlocksWest) { isTraversableWest = false; } } if (locationIsTraversable) // Only add a node if the location itself may be moved on { System.Diagnostics.Debug.WriteLine("Location " + locationKey + " is traversable!"); PathfindingNode node = new PathfindingNode(locationKey); _pathfindingPoints[node] = new Point(x, y); pathfindingGraph.AddNode(node); // Look for connections in all directions: North, East, South, List<MapTile> connectingTiles; Point connectingLocation = null; String connectingKey = null; PathfindingNode connectingNode = null; Boolean isConnectionTraversable = false; if (y > 0 && isTraversableNorth) { connectingLocation = new Point(x, y - 1); connectingKey = GetKeyForPoint(connectingLocation); connectingNode = pathfindingGraph.FindNodeByIdentifier(connectingKey); if (connectingNode != null && !pathfindingGraph.HasDirectConnectionBetween(node, connectingNode)) { connectingTiles = GetMapTilesForPoint(connectingLocation); isConnectionTraversable = true; foreach (MapTile tile in connectingTiles) { if (tile.BlocksMovement || tile.BlocksSouth) { isConnectionTraversable = false; break; } } if (isConnectionTraversable) // If both connections are valid, then link them! { pathfindingGraph.AddConnection(node, connectingNode, 1); } } } if (y < MAP_HEIGHT_IN_TILES - 1 && isTraversableSouth) { connectingLocation = new Point(x, y + 1); connectingKey = GetKeyForPoint(connectingLocation); connectingNode = pathfindingGraph.FindNodeByIdentifier(connectingKey); if (connectingNode != null && !pathfindingGraph.HasDirectConnectionBetween(node, connectingNode)) { connectingTiles = GetMapTilesForPoint(connectingLocation); isConnectionTraversable = true; foreach (MapTile tile in connectingTiles) { if (tile.BlocksMovement || tile.BlocksNorth) { isConnectionTraversable = false; break; } } if (isConnectionTraversable) // If both connections are valid, then link them! { pathfindingGraph.AddConnection(node, connectingNode, 1); } } } if (x > 0 && isTraversableWest) { connectingLocation = new Point(x - 1, y); connectingKey = GetKeyForPoint(connectingLocation); connectingNode = pathfindingGraph.FindNodeByIdentifier(connectingKey); if (connectingNode != null && !pathfindingGraph.HasDirectConnectionBetween(node, connectingNode)) { connectingTiles = GetMapTilesForPoint(connectingLocation); isConnectionTraversable = true; foreach (MapTile tile in connectingTiles) { if (tile.BlocksMovement || tile.BlocksEast) { isConnectionTraversable = false; break; } } if (isConnectionTraversable) // If both connections are valid, then link them! { pathfindingGraph.AddConnection(node, connectingNode, 1); } } } if (x < MAP_WIDTH_IN_TILES-1 && isTraversableEast) { connectingLocation = new Point(x + 1, y); connectingKey = GetKeyForPoint(connectingLocation); connectingNode = pathfindingGraph.FindNodeByIdentifier(connectingKey); if (connectingNode != null && !pathfindingGraph.HasDirectConnectionBetween(node, connectingNode)) { connectingTiles = GetMapTilesForPoint(connectingLocation); isConnectionTraversable = true; foreach (MapTile tile in connectingTiles) { if (tile.BlocksMovement || tile.BlocksWest) { isConnectionTraversable = false; break; } } if (isConnectionTraversable) // If both connections are valid, then link them! { pathfindingGraph.AddConnection(node, connectingNode, 1); } } } } } } DateTime endTime = DateTime.Now; TimeSpan duration = endTime.Subtract(startTime); System.Diagnostics.Debug.WriteLine("CalculateNodesAndConnections duration: "+duration.TotalMilliseconds+"ms"); return pathfindingGraph; }
public void CalculatePathfindingGraph() { PathfindingGraph = GeneratePathfindingGraph(); }