예제 #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);
    }
예제 #2
0
    /* enqueue adds a node to the front of this list
     * Parameter: (AIAgentAStarSearchNode) nodeToAdd is the node to be added to the front of the list
     * Return: none
     */
    public void enqueue(AIAgentAStarSearchNode nodeToAdd)
    {
        AIAgentAStarSearchNode newNode = new AIAgentAStarSearchNode(nodeToAdd.getPolygon(), nodeToAdd.getParentNode(), nodeToAdd.getGFromStartingNode(), goalPosition);

        newNode.setNextNode(frontOfList);
        frontOfList = newNode;
    }