Beispiel #1
0
    //return 0 if nothing found
    //1 if agent/goal found
    //2 if node on close list is found

    /*
     * doSearch method will run the A* search till a goal is found, or a node is in another agent closed list, or the
     *      nodes expanded is equal to the nodesToExpand variable
     * Parameter:	(int)nodesToExpand is the number of nodes that the search can expand
     *              (AIAgentAStarSearchListOp) secondSearchClosedList is the closed list from the other agent
     * Return:		(int)
     *                  0 if nothing was found
     *                  1 if the goal for this search agent was found
     *                  2 if a node on the second agent's closed list was found
     */
    public int doSearch(int nodesToExpand, AIAgentAStarSearchNode[] secondSearchClosedList)
    {
        int nodesExpandedCount = 0;

        while (openList.isEmpty() == false && nodesExpandedCount < nodesToExpand) //goes until nothing is left on the open list meaning a path could not be found
        {
            currentNode = openList.popNode();                                     //take the first(Best) polygon off the openList
            nodesVisited++;
            if (currentNode == null)
            {
                return(0);
            }
            closedList[currentNode.getPolygon().getID()] = currentNode;
            if (isBackwards == true)
            {
                if (currentNode.getPolygon().getHasAgent() == true)
                {
                    finalSolutionStart = currentNode;
                    return(1);
                }
            }
            else
            {
                if (currentNode.getPolygon().getHasGoal() == true)                //checks to see if the currentNode has the goal inside its polygon
                {
                    finalSolutionStart = currentNode;
                    return(1);
                }
            }
            if (secondSearchClosedList[currentNode.getPolygon().getID()] != null)
            {
                finalSolutionStart = currentNode;
                return(2);
            }
            for (int count = 0; count < currentNode.getPolygon().getNeighborsHeld(); count++)            //adds all the neighbors that are not on the closed list to the open list
            {
                if (closedList[currentNode.getPolygon().getNeighborAt(count)] == null)
                {
                    gCost = (currentNode.getPolygon().getCenterVector() - polygonArray[currentNode.getPolygon().getNeighborAt(count)].getCenterVector()).magnitude + currentNode.getGFromStartingNode();
                    if (openList.isNodeOnList(polygonArray[currentNode.getPolygon().getNeighborAt(count)]) == false)
                    {
                        openList.addNode(polygonArray[currentNode.getPolygon().getNeighborAt(count)], currentNode, gCost);
                    }
                    else if (openList.getNodeOnList(polygonArray[currentNode.getPolygon().getNeighborAt(count)]).compareToG(gCost) > 0f)                    //updates the a Nodes information if the new GCost (cost from start to node) is less then what was previously in it
                    {
                        openList.updateNode(openList.getNodeOnList(polygonArray[currentNode.getPolygon().getNeighborAt(count)]), currentNode, gCost);
                    }
                }
            }
            if (openList.getSize() > maxQueueSize)
            {
                maxQueueSize = openList.getSize();
            }
            nodesExpandedCount++;
        }
        return(0);
    }
    /*
     * AStarSearch method will preform an A* algorithm for searching a space to find a goal. It does this by taking the best
     *      polygon for the search off the openList, adding its neighbors to the openlist, placing the node on the closed list
     *      then repeating until it found a polygon that has the goal gameObject inside it
     * Parameters:	none
     * Return:	none
     */
    void AStarSearch()
    {
        maxQueueSize = 1;
        AIAgentAStarSearchNode currentNode;
        float gCost = 0f;

        bool[] closedList2 = new bool[polygonArray.Length];         //use to replace the close list to help reduce linear searches
        for (int count = 0; count < polygonArray.Length; count++)
        {
            closedList2 [count] = false;
        }
        while (openList.isEmpty() == false)       //goes until nothing is left on the open list meaning a path could not be found
        {
            currentNode = openList.popNode();     //take the first(Best) polygon off the openList
            nodesVisited++;
            if (currentNode == null)
            {
                return;
            }
            closedList2[currentNode.getPolygon().getID()] = true;         //adds node to the close list
            if (currentNode.getPolygon().getHasGoal() == true)            //checks to see if the currentNode has the goal inside its polygon
            {
                finalSolutionStart = currentNode;
                return;
            }
            for (int count = 0; count < currentNode.getPolygon().getNeighborsHeld(); count++)          //adds all the neighbors that are not on the closed list to the open list
            {
                if (closedList2[currentNode.getPolygon().getNeighborAt(count)] == false)               //checks to see if a node is logically should be in the closed list
                {
                    gCost = (currentNode.getPolygon().getCenterVector() - polygonArray[currentNode.getPolygon().getNeighborAt(count)].getCenterVector()).magnitude + currentNode.getGFromStartingNode();
                    if (openList.isNodeOnList(polygonArray[currentNode.getPolygon().getNeighborAt(count)]) == false)
                    {
                        openList.addNode(polygonArray[currentNode.getPolygon().getNeighborAt(count)], currentNode, gCost);
                    }
                    else if (openList.getNodeOnList(polygonArray[currentNode.getPolygon().getNeighborAt(count)]).compareToG(gCost) > 0f)                    //updates the a Nodes information if the new GCost (cost from start to node) is less then what was previously in it
                    {
                        openList.updateNode(openList.getNodeOnList(polygonArray[currentNode.getPolygon().getNeighborAt(count)]), currentNode, gCost);
                    }
                }
            }
            if (openList.getSize() > maxQueueSize)
            {
                maxQueueSize = openList.getSize();
            }
        }
    }