public override void IterateOpenSet() { OpenSetIterations++; //Path doesnt exist if the openset runs out if (OpenSet.Count <= 0) { Log.Print("no path found"); GridManager.Instance.HideAllParentIndicators(); FindingPathway = false; return; } //Take the front node from the open set Node Current = OpenSet[0]; OpenSet.Remove(Current); Current.Closed = true; //Pathway is completed once the end node has been reached if (Current == PathEnd) { Log.Print("Pathway found after " + OpenSetIterations + " iterations."); FindingPathway = false; GridManager.Instance.HideAllParentIndicators(); foreach (Node Step in GridManager.Instance.GetCompletedPathway(PathStart, PathEnd)) { Step.SetType(NodeType.Pathway); } return; } //Process all the nodes neighbours foreach (Node Neighbour in GridManager.Instance.GetTraversableNeighbours(Current)) { //Skip neighbours already searched if (Neighbour.Closed || Neighbour.Opened) { continue; } //Move this neighbour onto the open list, open it and update its parent OpenSet.Add(Neighbour); Neighbour.Opened = true; Neighbour.Parent = Current; Neighbour.ToggleParentIndicator(true); Neighbour.PointIndicator(Neighbour.GetDirection(Current)); } }
public override void IterateOpenSet() { //Track how many iterations have taken place to find this pathway so far OpenSetIterations++; //If the open set is empty, then no pathway was able to be found if (OpenSet.Count <= 0) { //Print a failure message and reset the grid Log.Print("Unable to find a valid pathway using A* algorithm."); FindingPathway = false; GridManager.Instance.HideAllParentIndicators(); return; } //Get the cheapest node currently being stored in the open set Node Current = GridManager.Instance.FindCheapestNode(OpenSet); OpenSet.Remove(Current); //When Current matches the end node, the pathway is ready to be reconstructed if (Current == PathEnd) { //Announce the pathway has been found and how long it took to find Log.Print("A* pathfinding completed after " + OpenSetIterations + " iterations."); //Hide all parent indicators GridManager.Instance.HideAllParentIndicators(); //Grab and display the final pathway List <Node> Pathway = GridManager.Instance.GetCompletedPathway(PathStart, PathEnd); foreach (Node PathStep in Pathway) { PathStep.SetType(NodeType.Pathway); } //Finalize the process FindingPathway = false; return; } //Remove the current node from the open set, then iterate over its neighbours OpenSet.Remove(Current); foreach (Node Neighbour in GridManager.Instance.GetTraversableNeighbours(Current)) { if (Current.GScore < Neighbour.GScore) { //Update this as the preferred way to travel Neighbour.Parent = Current; Neighbour.ToggleParentIndicator(true); Neighbour.PointIndicator(Neighbour.GetDirection(Current)); //Update neighbours score values Neighbour.GScore = Current.GScore; Neighbour.FScore = Neighbour.GScore + GridManager.FindHeuristic(Neighbour, PathEnd); //Add neighbour to open set if its not there already if (!OpenSet.Contains(Neighbour)) { OpenSet.Add(Neighbour); } } } }
public override void IterateOpenSet() { //Track how many iterations have taken place to find this pathway OpenSetIterations++; //Once the openset has been exhausted its time to complete the pathway if (OpenSet.Count <= 0) { //Get the completed pathway List <Node> Pathway = GridManager.Instance.GetCompletedPathway(PathStart, PathEnd); //If the path is empty, then no pathway was able to be found between the target nodes if (Pathway == null) { //Print a failure message and reset the grid Log.Print("Unable to find a valid pathway using Dijkstras algorithm."); FindingPathway = false; GridManager.Instance.HideAllParentIndicators(); return; } //Announce the pathway has been found Log.Print("Dijkstras pathfinding completed after " + OpenSetIterations + " iterations."); //Hide all the neighbour indicators GridManager.Instance.HideAllParentIndicators(); //Change the type of all nodes in the pathway to display it in the game foreach (Node PathStep in Pathway) { PathStep.SetType(NodeType.Pathway); } //Complete the process FindingPathway = false; return; } //Find the new current node, then iterate through all its neighbours Node Current = GridManager.Instance.FindCheapestNode(OpenSet); OpenSet.Remove(Current); foreach (Node Neighbour in GridManager.Instance.GetTraversableNeighbours(Current)) { //Ignore nodes not listed in the open set if (!OpenSet.Contains(Neighbour)) { continue; } //check if its cheaper to travel over this neighbour float NeighbourCost = Current.FScore + GridManager.FindHeuristic(Current, Neighbour); if (NeighbourCost < Neighbour.FScore) { //update this neighbour as the best way to travel Neighbour.FScore = NeighbourCost; Neighbour.Parent = Current; Neighbour.ToggleParentIndicator(true); Neighbour.PointIndicator(Neighbour.GetDirection(Current)); } } }