/* * 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 * (AIAgentAStarSearchList) 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, AIAgentAStarSearchList 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 queueSize--; nodesVisited++; if (currentNode == null) { return(0); } closedList.enqueue(currentNode); //add currentNode to the closedList 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.isNodeOnList(currentNode.getPolygon()) == true) { 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.isNodeOnList(polygonArray[currentNode.getPolygon().getNeighborAt(count)]) == false) { 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); queueSize++; } 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); queueSize++; } } } if (openList.getSize() > maxQueueSize) { maxQueueSize = openList.getSize(); } nodesExpandedCount++; } return(0); }
/* The AIBiDirectionalAStarAgent * * Parameters: Vector3 goalPositionToAdd - location of goal in the search * AIPolygon[] polygonsToAdd - array of walkable polygons to be search for a path * bool isBackwardsToAdd - determines if polygon was added as a result of backtracking */ public AIBiDirectionalAStarAgent(Vector3 goalPositionToAdd, AIPolygon[] polygonsToAdd, bool isBackwardsToAdd) { polygonArray = polygonsToAdd; goalPosition = goalPositionToAdd; isBackwards = isBackwardsToAdd; polygonFinalCount = 0; maxQueueSize = 0; nodesVisited = 0; openList = new AIAgentAStarSearchList(goalPosition); closedList = new AIAgentAStarSearchList(goalPosition); if (isBackwards == true) { 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 { 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; } } } queueSize = 1; 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 AIAgentAStarSearchList(goalPosition); closedList = new AIAgentAStarSearchList(goalPosition);; 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 }