Exemplo n.º 1
0
        /// <summary>
        /// Reconstructs the path created from the A* algorithm. This aswell is copy pasted from wikipedia.
        /// This is definitely not optimally implemented. This is O(3 * |cameFrom|) = (|cameFrom|) and
        /// could probably be implemented with a factor of 1.
        /// </summary>
        /// <param name="cameFrom">A dictionary holding the record of the best preceding node for the given node</param>
        /// <param name="current">The node from which to reconstruct the path</param>
        /// <returns></returns>
        private static IPath ReconstructPath(IReadOnlyDictionary <INode, INode> cameFrom, INode current)
        {
            var path = new List <INode>();

            var currentNode = current;

            path.Add(current);

            while (cameFrom.ContainsKey(currentNode))
            {
                currentNode = cameFrom[currentNode];
                path.Add(currentNode);
            }

            path.Reverse();

            var sortedPath = new SortedPath();

            foreach (var node in path)
            {
                sortedPath.AddNode(node);
            }

            return(sortedPath);
        }
Exemplo n.º 2
0
        private IPath GetPathForGeneralUnits(GeneralUnit unit, INode destination, int graphIndex)
        {
            IPath path;

            try
            {
                path = PathfindingFactory.GetPathfinding().AStar(mGraphs[graphIndex], unit.CurrentNode, destination);
            }
            catch (KeyNotFoundException)
            {
                path = new SortedPath();
            }

            return(path);
        }