/// <summary>
        /// Calculates the path from start to finish on the specified map.
        /// </summary>
        /// <param name="map">
        /// Map to find the path on.
        /// </param>
        /// <param name="path">
        /// Path from start to finish on the specified map.
        /// </param>
        /// <param name="visitedTiles">
        /// Map tiles that have been visited looking for the shortest path.
        /// </param>
        /// <returns>
        /// <c>True</c>, if a path could be found, and <c>false</c> otherwise.
        /// </returns>
        public bool CalculatePath(Map map, out List<MapTile> path, out IList<MapTile> visitedTiles)
        {
            var mapGraph = new MapGraph(map);

            // Reset Dijkstra.
            foreach (var tile in map.Tiles)
            {
                tile.Predecessor = null;
            }

            Dijkstra.FindPaths(mapGraph.Graph, map.Start, out visitedTiles);

            // Build path from predecessor pointers.
            path = new List<MapTile>();
            var mapTile = map.Finish;

            while (mapTile != null)
            {
                path.Add(mapTile);
                mapTile = mapTile.Predecessor as MapTile;
            }

            path.Reverse();

            return path.Contains(map.Start);
        }
        /// <summary>
        /// Calculates the path from start to finish on the specified map.
        /// </summary>
        /// <param name="map">
        /// Map to find the path on.
        /// </param>
        /// <param name="path">
        /// Path from start to finish on the specified map.
        /// </param>
        /// <param name="visitedTiles">
        /// Map tiles that have been visited looking for the shortest path.
        /// </param>
        /// <returns>
        /// <c>True</c>, if a path could be found, and <c>false</c> otherwise.
        /// </returns>
        public bool CalculatePath(Map map, out List<MapTile> path, out IList<MapTile> visitedTiles)
        {
            var mapGraph = new MapGraph(map);

            // Reset A*.
            foreach (var tile in map.Tiles)
            {
                tile.Discovered = false;
                tile.Visited = false;
            }

            path = AStar.FindPath(mapGraph.Graph, map.Start, map.Finish, out visitedTiles);
            return path != null;
        }
Exemple #3
0
        /// <summary>
        /// Converts the passed map to graph with edges between walkable tiles.
        /// </summary>
        /// <param name="map">Map to build the graph from.</param>
        private void MapToGraph(Map map)
        {
            this.Graph = new Graph<MapTile, int>(map.Tiles);

            for (var x = 0; x < map.Size; x++)
            {
                for (var y = 0; y < map.Size; y++)
                {
                    var tile = map[x, y];

                    this.AddMapGraphEdge(tile, new Vector2I(0, +1));
                    this.AddMapGraphEdge(tile, new Vector2I(0, -1));
                    this.AddMapGraphEdge(tile, new Vector2I(+1, 0));
                    this.AddMapGraphEdge(tile, new Vector2I(-1, 0));

                    this.AddMapGraphEdge(tile, new Vector2I(+1, +1));
                    this.AddMapGraphEdge(tile, new Vector2I(-1, -1));
                    this.AddMapGraphEdge(tile, new Vector2I(+1, -1));
                    this.AddMapGraphEdge(tile, new Vector2I(-1, +1));
                }
            }
        }
        /// <summary>
        ///     Calculates the path from start to finish on the specified map.
        /// </summary>
        /// <param name="map">Map to find the path on.</param>
        /// <param name="path">Path from start to finish on the specified map.</param>
        /// <param name="visitedTiles">Map tiles that have been visited looking for the shortest path.</param>
        /// <returns>
        ///     <c>True</c>, if a path could be found, and <c>false</c> otherwise.
        /// </returns>
        public bool CalculatePath(Map map, out List<MapTile> path, out IList<MapTile> visitedTiles)
        {
            // Grow new RRT from start to finish.
            var t = new RapidlyExploringRandomTree<MapTile, List<MapTile>>();
            var c = new MapConfigurationSpace(map);

            t.GrowTree(c, map.Start, ConfigurationCount);

            // Get visited tiles from tree edges.
            path = new List<MapTile>();
            visitedTiles = new List<MapTile>();

            foreach (RapidlyExploringRandomTreeEdge<MapTile, List<MapTile>> e in t.Edges)
            {
                foreach (var tile in e.Input)
                {
                    path.Add(tile);
                    visitedTiles.Add(tile);
                }
            }

            return path.Contains(map.Start);
        }
Exemple #5
0
        /// <summary>
        /// Constructs a new graph from the specified map.
        /// </summary>
        /// <param name="map">Map to build the graph from.</param>
        public MapGraph(Map map)
        {
            this.Map = map;

            this.MapToGraph(map);
        }