Backtrace() public static method

public static Backtrace ( Node iNode ) : List
iNode Node
return List
Example #1
0
        public static List <GridPos> FindPath(JumpPointParam iParam)
        {
            try
            {
                List <Node> tOpenList             = iParam.openList;
                Node        tStartNode            = iParam.StartNode;
                Node        tEndNode              = iParam.EndNode;
                bool        revertEndNodeWalkable = false;

                // set the `g` and `f` value of the start node to be 0
                tStartNode.startToCurNodeLen      = 0;
                tStartNode.heuristicStartToEndLen = 0;

                // push the start node into the open list
                tOpenList.Add(tStartNode);
                tStartNode.isOpened = true;

                if (iParam.AllowEndNodeUnWalkable && !iParam.SearchGrid.IsWalkableAt(tEndNode.x, tEndNode.y))
                {
                    iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, 0);
                    revertEndNodeWalkable = true;
                }

                // while the open list is not empty
                while (tOpenList.Any())
                {
                    // pop the position of node which has the minimum `f` value.
                    Node tNode = tOpenList.Last();
                    tOpenList.RemoveAt(tOpenList.Count - 1);
                    tNode.isClosed = true;

                    if (tNode.Equals(tEndNode))
                    {
                        if (revertEndNodeWalkable)
                        {
                            iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, 1);
                        }
                        return(Node.Backtrace(tNode)); // rebuilding path
                    }

                    identifySuccessors(iParam, tNode);
                }

                if (revertEndNodeWalkable)
                {
                    iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, 1);
                }

                // fail to find the path
                return(new List <GridPos>());
            }
            catch
            {
                return(new List <GridPos>());
            }
        }
Example #2
0
        public static List <GridPos> FindPath(JumpPointParam iParam)
        {
            var tOpenList             = iParam.openList;
            var tStartNode            = iParam.StartNode;
            var tEndNode              = iParam.EndNode;
            var revertEndNodeWalkable = false;

            // set the `g` and `f` value of the start node to be 0
            tStartNode.startToCurNodeLen      = 0;
            tStartNode.heuristicStartToEndLen = 0;

            // push the start node into the open list
            tOpenList.Add(tStartNode);
            tStartNode.isOpened = true;

            if (iParam.CurEndNodeUnWalkableTreatment == EndNodeUnWalkableTreatment.Allow && !iParam.SearchGrid.IsWalkableAt(tEndNode.x, tEndNode.y))
            {
                iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, true);
                revertEndNodeWalkable = true;
            }

            // while the open list is not empty
            while (tOpenList.Count > 0)
            {
                // pop the position of node which has the minimum `f` value.
                var tNode = tOpenList.DeleteMin();
                tNode.isClosed = true;

                if (tNode.Equals(tEndNode))
                {
                    if (revertEndNodeWalkable)
                    {
                        iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, false);
                    }
                    return(Node.Backtrace(tNode)); // rebuilding path
                }

                IdentifySuccessors(iParam, tNode);
            }

            if (revertEndNodeWalkable)
            {
                iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, false);
            }

            // Failed to find path
            return(new List <GridPos>());
        }
Example #3
0
        public static List <GridPos> FindPath(AStarParam parameters)
        {
            object lo               = new object();
            var    openList         = new IntervalHeap <Node>();
            var    startNode        = parameters.StartNode;
            var    endNode          = parameters.EndNode;
            var    heuristic        = parameters.HeuristicFunc;
            var    grid             = parameters.SearchGrid;
            var    diagonalMovement = parameters.DiagonalMovement;
            var    weight           = parameters.Weight;

            startNode.startToCurNodeLen      = 0;
            startNode.heuristicStartToEndLen = 0;

            openList.Add(startNode);
            startNode.isOpened = true;

            while (openList.Count != 0)
            {
                var node = openList.DeleteMin();
                node.isClosed = true;

                if (node == endNode)
                {
                    return(Node.Backtrace(endNode));
                }

                var neighbors = grid.GetNeighbors(node, diagonalMovement);

                Parallel.ForEach(neighbors, neighbor =>
                {
                    if (neighbor.isClosed)
                    {
                        return;
                    }

                    var x    = neighbor.x;
                    var y    = neighbor.y;
                    float ng = node.startToCurNodeLen + (float)((x - node.x == 0 || y - node.y == 0) ? 1 : Math.Sqrt(2));

                    if (!neighbor.isOpened || ng < neighbor.startToCurNodeLen)
                    {
                        neighbor.startToCurNodeLen = ng;
                        if (neighbor.heuristicCurNodeToEndLen == null)
                        {
                            neighbor.heuristicCurNodeToEndLen = weight * heuristic(Math.Abs(x - endNode.x), Math.Abs(y - endNode.y));
                        }
                        neighbor.heuristicStartToEndLen = neighbor.startToCurNodeLen + neighbor.heuristicCurNodeToEndLen.Value;
                        neighbor.parent = node;
                        if (!neighbor.isOpened)
                        {
                            lock (lo)
                            {
                                openList.Add(neighbor);
                            }
                            neighbor.isOpened = true;
                        }
                    }
                });
            }

            return(new List <GridPos>());
        }