Example #1
0
    public Stack <Vector2> aStarInstant()
    {
        int i = 0;
        List <NodeHolder>          closedList = new List <NodeHolder>();
        PriorityQueue <NodeHolder> openList   = new PriorityQueue <NodeHolder>();
        NodeHolder current = new NodeHolder(m_startPosition, null, m_endPosition);

        openList.EnQueue(current);

        while (openList.size() != 0)
        {
            // Check to see if we are stuck
            i = i + 1;
            //Debug.Log(openList.Size() + " " + closedList.Count);
            if (i > 1000)
            {
                Debug.LogError("failed to find path");
                return(new Stack <Vector2>());
            }

            NodeHolder currentNode = openList.DeQueue();
            closedList.Add(currentNode);
            List <PathFindingNode> neighbors = currentNode.GetTile().GetConnections();

            foreach (PathFindingNode neighbor in neighbors)
            {
                NodeHolder neighborNode = new NodeHolder(neighbor, currentNode, m_endPosition);
                if (neighbor.Equals(m_endPosition))
                {
                    m_finalNode = neighborNode;
                    return(MakePathFromNodes());
                }

                if (openList.Contains(neighborNode))
                {
                    NodeHolder node = openList.FindItem(neighborNode);
                    if (neighborNode.GetGValue() < node.GetGValue())
                    {
                        node.UpdateG(neighborNode);
                        openList.IncreasePriority(openList.IndexOf(node));
                    }
                }
                else if (closedList.Contains(neighborNode))
                {
                    int        index = closedList.IndexOf(neighborNode);
                    NodeHolder node  = closedList[index];
                    if (neighborNode.GetGValue() < node.GetGValue())
                    {
                        closedList.Remove(node);
                    }


                    openList.EnQueue(neighborNode);
                }
                else
                {
                    openList.EnQueue(neighborNode);
                }
            }
        }
        return(new Stack <Vector2>());
    }
Example #2
0
    public IEnumerator aStar(PathFindingCallBack callBack)
    {
        Debug.Log("start aStar");
        int i = 0;
        List <NodeHolder>          closedList = new List <NodeHolder>();
        PriorityQueue <NodeHolder> openList   = new PriorityQueue <NodeHolder>();
        NodeHolder current = new NodeHolder(m_startPosition, null, m_endPosition);

        openList.EnQueue(current);
        pathing = true;

        while (openList.size() != 0)
        {
            // Check to see if we are stuck or told to quit finding a path
            i = i + 1;
            if (i > 2000 || !pathing)
            {
                pathing = false;
                Debug.LogError("failed to find path");
                yield break;
            }

            NodeHolder currentNode = openList.DeQueue();
            closedList.Add(currentNode);
            List <PathFindingNode> neighbors = currentNode.GetTile().GetConnections();
            foreach (PathFindingNode neighbor in neighbors)
            {
                NodeHolder neighborNode = new NodeHolder(neighbor, currentNode, m_endPosition);
                if (neighbor.Equals(m_endPosition))
                {
                    Debug.Log("Found Path");
                    m_finalNode = neighborNode;
                    callBack(m_finalNode);
                    yield break;
                }

                if (openList.Contains(neighborNode))
                {
                    NodeHolder node = openList.FindItem(neighborNode);
                    if (neighborNode.GetGValue() < node.GetGValue())
                    {
                        node.UpdateG(neighborNode);
                        openList.IncreasePriority(openList.IndexOf(node));
                    }
                    if (i % 600 == 0)
                    {
                        yield return(null);
                    }
                }
                else if (closedList.Contains(neighborNode))
                {
                    int        index = closedList.IndexOf(neighborNode);
                    NodeHolder node  = closedList[index];
                    if (neighborNode.GetGValue() < node.GetGValue())
                    {
                        closedList.Remove(node);
                    }
                    openList.EnQueue(neighborNode);

                    if (i % 600 == 0)
                    {
                        yield return(null);
                    }
                }
                else
                {
                    openList.EnQueue(neighborNode);
                }
                if (i % 600 == 0)
                {
                    yield return(null);
                }
            }
        }
        yield break;
    }