Exemplo n.º 1
0
        public static WayPoints FromPath(Path path)
        {
            WayPoints points = new WayPoints();

            while (path != null && !path.Finished)
            {
                IGridCell current = path.GetNextWaypoint();

                if (current != null)
                {
                    points.Push(new HexPoint(current.X, current.Y));
                    points._goal = new HexPoint(current.X, current.Y);
                }
            }

            return points;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Calculates a path from start to end. When no path can be found in
        /// reasonable time the search is aborted and an incomplete path is returned.
        /// When refresh is not set to true a cached path is returned where possible.
        /// </summary>
        /// <param name="start">start position in 2d map space</param>
        /// <param name="end">end position in 2d map space</param>
        /// <param name="refresh">force to recalculate the path</param>
        /// <returns></returns>
        public Path CalculatePath(Unit unit, HexPoint start, HexPoint end, bool refresh = false)
        {
            // swap points to calculate the path backwards (from end to start)
            HexPoint temp = end;
            end = start;
            start = temp;

            // Check whether the requested path is already known
            PathRequest request = new PathRequest(unit,start, end);

            if (!refresh && knownPaths.ContainsKey(request))
            {
                return knownPaths[request].Copy();
            }

            // priority queue of nodes that yet have to be explored sorted in
            // ascending order by node costs (F)
            PriorityQueue<PathNode> open = new PriorityQueue<PathNode>();

            // list of nodes that have already been explored
            LinkedList<IGridCell> closed = new LinkedList<IGridCell>();

            // start is to be explored first
            PathNode startNode = new PathNode(unit,null, map[start], end);
            open.Enqueue(startNode);

            int steps = 0;
            PathNode current;

            do
            {
                // abort if calculation is too expensive
                if (++steps > stepLimit) return null;

                // examine the cheapest node among the yet to be explored
                current = open.Dequeue();

                // Finish?
                if (current.Cell.Matches(end))
                {
                    // paths which lead to the requested goal are cached for reuse
                    Path path = new Path(current);

                    if (knownPaths.ContainsKey(request))
                    {
                        knownPaths[request] = path.Copy();
                    }
                    else
                    {
                        knownPaths.Add(request, path.Copy());
                    }

                    return path;
                }

                // Explore all neighbours of the current cell
                ICollection<IGridCell> neighbours = map.GetNeighbourCells(current.Cell);

                foreach (IGridCell cell in neighbours)
                {
                    // discard nodes that are not of interest
                    if (closed.Contains(cell) || (cell.Matches(end) == false && !cell.IsWalkable(unit)))
                    {
                        continue;
                    }

                    // successor is one of current's neighbours
                    PathNode successor = new PathNode(unit,current, cell, end);
                    PathNode contained = open.Find(successor);

                    if (contained != null && successor.F >= contained.F)
                    {
                        // This cell is already in the open list represented by
                        // another node that is cheaper
                        continue;
                    }
                    else if (contained != null && successor.F < contained.F)
                    {
                        // This cell is already in the open list but on a more expensive
                        // path -> "integrate" the node into the current path
                        contained.Predecessor = current;
                        contained.Update();
                        open.Update(contained);
                    }
                    else
                    {
                        // The cell is not in the open list and therefore still has to
                        // be explored
                        open.Enqueue(successor);
                    }
                }

                // add current to the list of the already explored nodes
                closed.AddLast(current.Cell);

            } while (open.Peek() != null);

            return null;
        }
Exemplo n.º 3
0
        ///// <summary>
        ///// Just for debugging: Renders the complete path from beginning.
        ///// </summary>
        //[Conditional("DEBUG")]
        //public void DrawPath()
        //{
        //    DrawPath(start, start.Predecessor);
        //}

        ///// <summary>
        ///// Recursive function for rendering a path segment as a yellow line.
        ///// </summary>
        ///// <param name="from">Start node</param>
        ///// <param name="to">End node</param>
        //[Conditional("DEBUG")]
        //private void DrawPath(PathNode from, PathNode to)
        //{
        //    if (to == null) return;
        //    DebugDisplay.Instance.DrawLine(from.Cell.Position3D, to.Cell.Position3D, from == start ? Color.Red : Color.Yellow);
        //    DrawPath(to, to.Predecessor);
        //}

        /// <summary>
        /// Copies this instance of the path.
        /// </summary>
        /// <returns></returns>
        public Path Copy()
        {
            Path copy = new Path(start);
            return copy;
        }