// This generates the successors to the given Node. It uses a helper function called // AddSuccessor to give the successors to the AStar class. The A* specific initialisation // is done for each node internally, so here you just set the state information that // is specific to the application public bool GetSuccessors(AStarPathfinder aStarSearch, MapSearchNode parentNode) { Vector2Int parentPos = new Vector2Int(0, 0); if (parentNode != null) { parentPos = parentNode.nodeIndex; } if (pathfinder.volume > 0) { AddNeighbourNodeOfAttachVolume(1, 0, parentPos, aStarSearch); AddNeighbourNodeOfAttachVolume(-1, 0, parentPos, aStarSearch); AddNeighbourNodeOfAttachVolume(0, 1, parentPos, aStarSearch); AddNeighbourNodeOfAttachVolume(0, -1, parentPos, aStarSearch); AddNeighbourNodeOfAttachVolume(1, -1, parentPos, aStarSearch); AddNeighbourNodeOfAttachVolume(-1, 1, parentPos, aStarSearch); AddNeighbourNodeOfAttachVolume(1, 1, parentPos, aStarSearch); AddNeighbourNodeOfAttachVolume(-1, -1, parentPos, aStarSearch); } else { // push each possible move except allowing the search to go backwards AddNeighbourNode(-1, 0, parentPos, aStarSearch); AddNeighbourNode(0, -1, parentPos, aStarSearch); AddNeighbourNode(1, 0, parentPos, aStarSearch); AddNeighbourNode(0, 1, parentPos, aStarSearch); AddNeighbourNode(1, -1, parentPos, aStarSearch); AddNeighbourNode(-1, 1, parentPos, aStarSearch); AddNeighbourNode(1, 1, parentPos, aStarSearch); AddNeighbourNode(-1, -1, parentPos, aStarSearch); } return(true); }
void AddNeighbourNode(int xOffset, int yOffset, Vector2Int parentPos, AStarPathfinder aStarSearch) { if (ValidNeigbour(xOffset, yOffset) && !(parentPos.x == nodeIndex.x + xOffset && parentPos.y == nodeIndex.y + yOffset)) { Vector2Int neighbourPos = new Vector2Int(nodeIndex.x + xOffset, nodeIndex.y + yOffset); //Debug.Log("neighbourPos:" + neighbourPos); MapSearchNode newNode = pathfinder.AllocateMapSearchNode(neighbourPos); aStarSearch.AddSuccessor(newNode); } }
public Vector2Int[] GetPath(Vector2Int start, Vector2Int end, AStarPathfinder pathfinder) => PathFinder.Pathfind(this, start, end, pathfinder).ToArray();
/// <summary> /// /// </summary> /// <param name="grid"></param> /// <param name="startPos"></param> /// <param name="goalPos"></param> /// <param name="pathfinder"></param> /// <returns></returns> public static List <Vector2Int> Pathfind(Grid grid, Vector2Int startPos, Vector2Int goalPos, AStarPathfinder pathfinder = null) { // Reset the allocated MapSearchNode pointer if (pathfinder == null) { pathfinder = new AStarPathfinder(grid); } pathfinder.InitiatePathfind(); // Create a start state MapSearchNode nodeStart = pathfinder.AllocateMapSearchNode(startPos); // Define the goal state MapSearchNode nodeEnd = pathfinder.AllocateMapSearchNode(goalPos); // Set Start and goal states pathfinder.SetStartAndGoalStates(nodeStart, nodeEnd); // Set state to Searching and perform the search AStarPathfinder.SearchState searchState = AStarPathfinder.SearchState.Searching; uint searchSteps = 0; do { searchState = pathfinder.SearchStep(); searchSteps++; }while (searchState == AStarPathfinder.SearchState.Searching); // Search complete bool pathfindSucceeded = (searchState == AStarPathfinder.SearchState.Succeeded); if (pathfindSucceeded) { // Success List <Vector2Int> newPath = new List <Vector2Int>(); int numSolutionNodes = 0; // Don't count the starting cell in the path length // Get the start node MapSearchNode node = pathfinder.GetSolutionStart(); newPath.Add(node.nodeIndex); ++numSolutionNodes; // Get all remaining solution nodes for (; ;) { node = pathfinder.GetSolutionNext(); if (node == null) { break; } ++numSolutionNodes; newPath.Add(node.nodeIndex); } ; // Once you're done with the solution we can free the nodes up pathfinder.FreeSolutionNodes(); return(newPath); //System.Console.WriteLine("Solution path length: " + numSolutionNodes); //System.Console.WriteLine("Solution: " + newPath.ToString()); } else if (searchState == AStarPathfinder.SearchState.Failed) { // FAILED, no path to goal Debug.LogError("Pathfind FAILED!"); } return(null); }
public MapSearchNode(AStarPathfinder _pathfinder) { nodeIndex = Vector2Int.zero; pathfinder = _pathfinder; }
void AddNeighbourNodeOfAttachVolume(int xOffset, int yOffset, Vector2Int parentPos, AStarPathfinder aStarSearch) { int src_xOffset = xOffset; int src_yOffset = yOffset; bool flag = true; int volume = aStarSearch.volume; if (src_xOffset != 0) { xOffset += ((xOffset >= 0) ? volume : -volume); for (int y = -volume; y <= volume; y++) { flag = ValidNeigbour(xOffset, y + src_yOffset); if (flag == false) { return; } } } if (src_yOffset != 0) { yOffset += ((yOffset >= 0) ? volume : -volume); for (int x = -volume; x <= volume; x++) { flag = ValidNeigbour(x + src_xOffset, yOffset); if (flag == false) { return; } } } if (flag && !(parentPos.x == nodeIndex.x + src_xOffset && parentPos.y == nodeIndex.y + src_yOffset)) { Vector2Int neighbourPos = new Vector2Int(nodeIndex.x + src_xOffset, nodeIndex.y + src_yOffset); MapSearchNode newNode = pathfinder.AllocateMapSearchNode(neighbourPos); aStarSearch.AddSuccessor(newNode); } }
public MapSearchNode(Vector2Int pos, AStarPathfinder _pathfinder) { nodeIndex = new Vector2Int(pos.x, pos.y); pathfinder = _pathfinder; }