public Route FindRoute(MapNode start_mapnode, MapNode end_mapnode, Item.Item start_item, Item.Item end_item, bool serviceRoute) { Slot start_point = new Slot(start_item.Position.X + ((IPrototype)start_item).Size.X, start_mapnode.position.Y); Slot end_point = new Slot(end_item.Position.X + ((IPrototype)end_item).Size.X, end_mapnode.position.Y); MapSearchNode nodeStart = new MapSearchNode(start_mapnode); nodeStart.parent_item = start_item; nodeStart.start_point = start_point; nodeStart.end_point = end_point; nodeStart.serviceRoute = serviceRoute; MapSearchNode nodeEnd = new MapSearchNode(end_mapnode); astarsearch.SetStartAndGoalStates(nodeStart, nodeEnd); int SearchSteps = 0; do { SearchState = astarsearch.SearchStep(); SearchSteps++; } while (SearchState == AStarPathfinder.SearchState.Searching); Route r = new Route(); buildRoute(r, start_item, end_item); clear(); return(r); }
private void FindPath() { AStarPathfinder pathfinder = new AStarPathfinder(); NodePosition startPos = m_startGrid.m_position; NodePosition endPos = m_endGrid.m_position; int width, height; var mapData = GetMapData(out width, out height); var map = new Map(mapData, width, height); pathfinder.InitiatePathfind(map); MapSearchNode nodeStart = pathfinder.AllocateMapSearchNode(startPos); MapSearchNode nodeEnd = pathfinder.AllocateMapSearchNode(endPos); pathfinder.SetStartAndGoalStates(nodeStart, nodeEnd); 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 Path newPath = new Path(); int numSolutionNodes = 0; // Don't count the starting cell in the path length // Get the start node MapSearchNode node = pathfinder.GetSolutionStart(); newPath.Add(node.position); ++numSolutionNodes; // Get all remaining solution nodes for (; ;) { node = pathfinder.GetSolutionNext(); if (node == null) { break; } ++numSolutionNodes; newPath.Add(node.position); } ; // Once you're done with the solution we can free the nodes up pathfinder.FreeSolutionNodes(); m_path = newPath; _sceneView.Repaint(); Debug.Log("Solution path length: " + numSolutionNodes); Debug.Log("Solution: " + newPath.ToString()); } else if (searchState == AStarPathfinder.SearchState.Failed) { // FAILED, no path to goal Debug.Log("Pathfind FAILED!"); } }
static bool Pathfind(NodePosition startPos, NodePosition goalPos, AStarPathfinder pathfinder) { // Reset the allocated MapSearchNode pointer 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 Path newPath = new Path(); int numSolutionNodes = 0; // Don't count the starting cell in the path length // Get the start node MapSearchNode node = pathfinder.GetSolutionStart(); newPath.Add(node.position); ++numSolutionNodes; // Get all remaining solution nodes for (; ;) { node = pathfinder.GetSolutionNext(); if (node == null) { break; } ++numSolutionNodes; newPath.Add(node.position); // DRAW PATH TO GOAL var vnName = $"vnx{node.position.x}y{node.position.y}"; VisualNode foundVisualNode = _visualGrid.FindName(vnName) as VisualNode; foundVisualNode?.SetDot(true); } ; // Once you're done with the solution we can free the nodes up pathfinder.FreeSolutionNodes(); Debug.WriteLine("Solution path length: " + numSolutionNodes); Debug.WriteLine("Solution: " + newPath.ToString()); } else if (searchState == AStarPathfinder.SearchState.Failed) { // FAILED, no path to goal Debug.WriteLine("Pathfind FAILED!"); } return(pathfindSucceeded); }
/// <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); }
IEnumerator KK() { pathfinder = new AStarPathfinder(); // Pathfind(new NodePosition(0, 0), new NodePosition(2, 4), pathfinder); // Reset the allocated MapSearchNode pointer pathfinder.InitiatePathfind(); // Create a start state MapSearchNode nodeStart = pathfinder.AllocateMapSearchNode(new NodePosition(0, 0)); // Define the goal state MapSearchNode nodeEnd = pathfinder.AllocateMapSearchNode(new NodePosition(GOAL_X, GOAL_Y)); // 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; yield return(null); do { // if (Input.GetKey(KeyCode.J)) { Debug.Log("xx-- getkeydown"); searchState = pathfinder.SearchStep(); searchSteps++; RenderTiles(pathfinder); } if (searchState == AStarPathfinder.SearchState.Searching) { yield return(null); } }while (searchState == AStarPathfinder.SearchState.Searching); // Search complete bool pathfindSucceeded = (searchState == AStarPathfinder.SearchState.Succeeded); if (pathfindSucceeded) { // Success Path newPath = new Path(); int numSolutionNodes = 0; // Don't count the starting cell in the path length // Get the start node MapSearchNode node = pathfinder.GetSolutionStart(); newPath.Add(node.position); ++numSolutionNodes; // Get all remaining solution nodes for ( ;;) { node = pathfinder.GetSolutionNext(); if (node == null) { break; } ++numSolutionNodes; newPath.Add(node.position); // GameObject obj = objs[node.position.x, node.position.y]; if (obj != null) { obj.GetComponent <MeshRenderer>().material.color = Color.green; } } ; // Once you're done with the solution we can free the nodes up pathfinder.FreeSolutionNodes(); Debug.Log("xx-- Solution path length: " + numSolutionNodes); Debug.Log("xx-- Solution: " + newPath.ToString()); } else if (searchState == AStarPathfinder.SearchState.Failed) { // FAILED, no path to goal Debug.Log("xx-- Pathfind FAILED!"); } }