/// <summary> /// Generates the list of shortest paths. /// </summary> /// <param name="startPosition">Position from which all paths are created.</param> public static void GeneratePaths(Unit movingUnit, Point startPosition, int movement, int x, int y) { List <PathToTile> newPaths; PathToTile startPath = new PathToTile(startPosition, new Point[0], 0); newPaths = new List <PathToTile>(); shortestPaths = new List <PathToTile>(); newPaths.Add(startPath); while (newPaths.Count > 0) { List <PathToTile> newNewPaths = new List <PathToTile>(); newNewPaths.AddRange(newPaths); foreach (PathToTile p in newNewPaths) { AddSurroundingTiles(movingUnit, p, newPaths, movement, x, y); newPaths.Remove(p); } } }
private static PathToTile ReconstructPath(Unit movingUnit, Dictionary <Point, Point> cameFrom, Point probablyClosest, Grid world) { Point targetPoint = probablyClosest; List <Point> path = new List <Point>(); int cost = 0; // While there's still points in the path to pass over while (cameFrom.ContainsKey(probablyClosest) && probablyClosest != movingUnit.PositionInGrid) { // Add the closest tiles, from target to start path.Add(probablyClosest); cost += (world[probablyClosest] as Tile).Cost(movingUnit); probablyClosest = cameFrom[probablyClosest]; } // And reverse the path, to make sure it's from the Unit to its target path.Reverse(); PathToTile pathToTile = new PathToTile(targetPoint, path.ToArray(), cost); return(pathToTile); }
/// <summary> /// Adds the paths to the tiles surrounding the specified tile to the list. /// </summary> /// <param name="startPath">PathToTile to the tile from which you want the neighboring tiles.</param> private static void AddSurroundingTiles(Unit movingUnit, PathToTile startPath, List <PathToTile> newPaths, int movement, int x, int y) { List <PathToTile> returnList = new List <PathToTile>(); // Creates list of points and fills it with the points surrounding the starttile List <Point> surroundingPoints = new List <Point>(); surroundingPoints.Add(startPath.target + new Point(1, 0)); surroundingPoints.Add(startPath.target + new Point(0, 1)); surroundingPoints.Add(startPath.target + new Point(-1, 0)); surroundingPoints.Add(startPath.target + new Point(0, -1)); Grid grid = movingUnit.GameWorld as Grid; foreach (Point p in surroundingPoints) { if (!grid.IsInGrid(p) || p.X >= x || p.Y >= y) { continue; } Tile tile = grid[p] as Tile; if (tile.Passable(movingUnit)) { // Forms new path to each point int startPathLength = startPath.path == null ? 0 : startPath.path.Length; Point[] pathAsPoints = new Point[startPathLength + 1]; startPath.path?.CopyTo(pathAsPoints, 0); pathAsPoints[startPathLength] = p; int cost = tile.Cost(movingUnit) + startPath.cost; PathToTile newPathToTile = new PathToTile(p, pathAsPoints, cost); PathToTile shortestPathToTile = shortestPaths.Find(path => path.target.Equals(p)); if (movement >= cost) { // If the current found path is shorter than an existent path, overrides it. if (shortestPathToTile != null) { if (cost < shortestPathToTile.cost) { shortestPaths.Remove(shortestPathToTile); newPaths.Add(newPathToTile); shortestPaths.Add(newPathToTile); } //else if(cost == shortestPathToTile.cost && newPathToTile.CumulativeDistance < shortestPathToTile.CumulativeDistance) //{ // shortestPaths.Remove(shortestPathToTile); // newPaths.Add(newPathToTile); // shortestPaths.Add(newPathToTile); //} } // If there is no known path to the specified point, adds this path to the list. else { newPaths.Add(newPathToTile); shortestPaths.Add(newPathToTile); } } } } newPaths.Remove(startPath); }