Exemple #1
0
        /*
         * public functions
         */

        /// <summary>
        /// The core function; calls all valid auxiliary functions and returns the path (advanced features can be toggled)
        /// Includes a boolean toggle for use of advanced pathfinding functions
        /// </summary>
        /// <param name="map">The Map</param>
        /// <param name="start">The starting Cell</param>
        /// <param name="end">The ending Cell</param>
        /// <param name="advanced"> A boolean toggle for advanced functions</param>
        /// <returns>The path as a list of waypoints</returns>
        public static List <Cell> between(Map map, Cell start, Cell end, bool advanced)
        {
            // begin timing the operation
            startTime   = DateTime.Now;
            intendedEnd = end;

            // convert the given Cell-based data to Node-based data
            NodeMap nodeMap   = new NodeMap(map);
            Node    nodeStart = nodeMap.getNode(start.Xcoord, start.Ycoord);
            Node    nodeEnd   = nodeMap.getNode(end.Xcoord, end.Ycoord);

            // perform advanced pre-calculation tasks
            if (advanced)
            {
                // if the end Node is invalid, replace it with the nearest valid Node
                if (!nodeEnd.isValid)
                {
                    DateTime tempStartTime = DateTime.Now;
                    nodeEnd = Advanced.nearestValidEnd(nodeMap, nodeStart, nodeEnd);
                    TimeSpan tempSpan = DateTime.Now - tempStartTime;
                    Console.WriteLine("-> Path end changed from ({0}, {1}) to ({2}, {3}) in {4}", intendedEnd.Xcoord, intendedEnd.Ycoord, nodeEnd.Xcoord, nodeEnd.Ycoord, tempSpan);
                }
            }

            // find the path
            List <Node> nodePath = Basic.findPath(nodeMap, nodeStart, nodeEnd);

            // perform advanced post-calculation tasks
            if (advanced)
            {
            }

            // convert the path from List<Node> format back to List<Cell> format
            List <Cell> path = new List <Cell>(nodePath.Count);

            for (int i = 0; i < nodePath.Count; i++)
            {
                path.Add(map.getCell(nodePath[i].Xcoord, nodePath[i].Ycoord));
            }

            // grab and print path data
            float dist = (float)(nodePath[nodePath.Count - 1].Gscore);

            span = DateTime.Now - startTime;
            printPath(path, dist);

            return(path);
        }
        /*
         * public functions
         */

        /// <summary>
        /// The core function; calls all valid auxiliary functions and returns the path (advanced features can be toggled)
        /// Includes a boolean toggle for use of advanced pathfinding functions
        /// </summary>
        /// <param name="map">The Map</param>
        /// <param name="start">The starting Cell</param>
        /// <param name="end">The ending Cell</param>
        /// <param name="advanced"> A boolean toggle for advanced functions</param>
        /// <returns>The path as a list of waypoints</returns>
        public static List <CellComponent> between(Map map, CellComponent start, CellComponent end, bool advanced)
        {
            // begin timing the operation
            DateTime startTime = DateTime.Now;

            // convert the given Cell-based data to Node-based data
            NodeMap nodeMap   = new NodeMap(map);
            Node    nodeStart = nodeMap.getNode(start.X, start.Y);
            Node    nodeEnd   = nodeMap.getNode(end.X, end.Y);

            // perform advanced pre-calculation tasks
            if (advanced)
            {
                // if the end Node is invalid, replace it with the nearest valid Node
                if (!nodeEnd.isValid)
                {
                    nodeEnd = Advanced.nearestValidEnd(nodeMap, nodeStart, nodeEnd);
                }
            }

            // find the path
            List <Node> nodePath = Basic.findPath(nodeMap, nodeStart, nodeEnd);

            // convert the path from List<Node> format back to List<Cell> format
            List <CellComponent> path = new List <CellComponent>(nodePath.Count);

            for (int i = 0; i < nodePath.Count; i++)
            {
                path.Add(map.GetCellAt(nodePath[i].X, nodePath[i].Y));
            }

            // grab and print path data
            float dist = (float)(nodePath[nodePath.Count - 1].Gscore);

            span = DateTime.Now - startTime;
            //printPath(path, dist);

            return(path);
        }