/** The start node need to be special cased and checked here if it is a valid target */ protected override void CompletePathIfStartIsValidTarget() { var pNode = pathHandler.GetPathNode(startNode); if (endingCondition.TargetFound(pNode)) { ChangeEndNode(startNode); Trace(pNode); CompleteState = PathCompleteState.Complete; } }
protected override void CalculateStep(long targetTick) { int counter = 0; //Continue to search as long as we haven't encountered an error and we haven't found the target while (CompleteState == PathCompleteState.NotCalculated) { searchedNodes++; //--- Here's the important stuff //Close the current node, if the current node satisfies the ending condition, the path is finished if (endingCondition.TargetFound(currentR)) { CompleteState = PathCompleteState.Complete; break; } if (!currentR.flag1) { //Add Node to allNodes allNodes.Add(currentR.node); currentR.flag1 = true; } #if ASTARDEBUG Debug.DrawRay((Vector3)currentR.node.position, Vector3.up * 5, Color.cyan); #endif //--- Here the important stuff ends //Debug.DrawRay ((Vector3)currentR.node.Position, Vector3.up*2,Color.red); //Loop through all walkable neighbours of the node and add them to the open list. currentR.node.Open(this, currentR, pathHandler); //any nodes left to search? if (pathHandler.heap.isEmpty) { CompleteState = PathCompleteState.Complete; break; } //Select the node with the lowest F score and remove it from the open list currentR = pathHandler.heap.Remove(); //Check for time every 500 nodes, roughly every 0.5 ms usually if (counter > 500) { //Have we exceded the maxFrameTime, if so we should wait one frame before continuing the search since we don't want the game to lag if (DateTime.UtcNow.Ticks >= targetTick) { //Return instead of yield'ing, a separate function handles the yield (CalculatePaths) return; } counter = 0; if (searchedNodes > 1000000) { throw new Exception("Probable infinite loop. Over 1,000,000 nodes searched"); } } counter++; } }