Пример #1
0
    /// <summary>
    /// The core of the A* algorithm logic. Processes a single node per call, which is the most propable
    ///	one (closest to target) from a list of unchecked nodes. Checks its adjacent nodes for other propable
    ///	nodes and adds them into the list to be checked. Returns true if end has been reached, or false if
    /// path finding should be continued.
    /// </summary>
    /// <returns>True if current node is the target node or if there are no more nodes to check.</returns>
    private bool SearchForPath()
    {
        // 1. Get the lowest F cost node from the open list and set is as current.
        //    The open list is kept sorted by F cost, so just return the first value from the list.
        var current = _openList.First.Value;

        // 2. Switch the node to the closed list
        SwitchToClosedList(current);

        // 3. Add visible/reachable nodes to the open list, if they are not already in the closed list.
        //    Also, if a node is already in the open list, check if the current path is better.
        var visibilityMap = _visibilityGraph.Get(current);

        foreach (var node in visibilityMap)
        {
            // Ignore nodes which are already in the closed list
            if (_closedList.Contains(node))
            {
                continue;
            }

            // If node is already on the open list check if current path
            // to that node is a better one, using G cost as measure
            if (_openList.Contains(node))
            {
                CalculateAlternativeCost(node, current);
            }
            else
            {
                AddToOpenList(node, current);
            }
        }

        // 4. Stop when the current node is the target node,
        //    or if we fail to find the target meaning the open list is empty.
        return(HasFoundTarget(current) || HasFailedToFindTarget());
    }