Example #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);
    }
Example #2
0
 /*
  * AIBiDirectionalAStartAgentOp's constructor will set up the initial values for instance variables
  * Parameters:	(Vector3)goalPositionToAdd is the position that the goal this search is looking for is at
  *              (AIPolygon[])polygonsToAdd is the array of polygons that it will search through
  *              (bool)isBackwardsToAdd is the bool to tell if this search is going from the goal or from the agent
  */
 public AIBiDirectionalAStarAgentOp(Vector3 goalPositionToAdd, AIPolygon[] polygonsToAdd, bool isBackwardsToAdd)
 {
     polygonArray      = polygonsToAdd;
     goalPosition      = goalPositionToAdd;
     isBackwards       = isBackwardsToAdd;
     polygonFinalCount = 0;
     maxQueueSize      = 0;
     nodesVisited      = 0;
     openList          = new AIAgentAStarSearchListOp(goalPosition, polygonArray.Length);
     closedList        = new AIAgentAStarSearchNode[polygonArray.Length];
     for (int count = 0; count < polygonArray.Length; count++)
     {
         closedList [count] = null;
     }
     if (isBackwards == true)                                      //going from the goal
     {
         for (int count = 0; count < polygonArray.Length; count++) // looks for the polygon with the agent GameObject inside it
         {
             if (polygonArray [count].getHasGoal() == true)
             {
                 openList.addNode(polygonArray [count], null, 0);              //adds the first polygon to the openList
                 count = polygonArray.Length;
             }
         }
     }
     else                                                          //going from the agent
     {
         for (int count = 0; count < polygonArray.Length; count++) // looks for the polygon with the agent GameObject inside it
         {
             if (polygonArray [count].getHasAgent() == true)
             {
                 openList.addNode(polygonArray [count], null, 0);              //adds the first polygon to the openList
                 count = polygonArray.Length;
             }
         }
     }
     gCost = 0f;
 }
 /*
  * searchStart method will begin the search by first setting the first node in the open list, which holds the
  *      polygon that has the agent GameObject in it, then it will call the functions to do the search, build the
  *      finalSolution array, and set the wayPoints
  *	Parameters:	none
  *	Return:	none
  */
 public void startSearch()
 {
     openList = new AIAgentAStarSearchListOp(goalPosition, polygonArray.Length);
     for (int count = 0; count < polygonArray.Length; count++)         // looks for the polygon with the agent GameObject inside it
     {
         if (polygonArray [count].getHasAgent() == true && polygonArray[count].getAgentID() == 1)
         {
             openList.addNode(polygonArray [count], null, 0);          //adds the first polygon to the openList
             count = polygonArray.Length;
         }
     }
     AStarSearch();              //does the AStar search
     printFinalSolution();       //prints the finalSolution for debugging
     addFinalSolutionPolygons(); //adds the finalsolution to the finalsolution array
 }
    /*
     * 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();
            }
        }
    }