public static void RequestPath(Vector3 _start, Vector3 _end, DistanceHeuristic _distanceType, bool _simplifiedPath, Action <Vector3[], bool> callback) { PathRequest newRequest = new PathRequest(_start, _end, _distanceType, _simplifiedPath, callback); instance.PathQueue.Enqueue(newRequest); instance.ProcessNext(); }
public PathRequest(Vector3 _start, Vector3 _end, DistanceHeuristic _distanceType, bool _simplified, Action <Vector3[], bool> _callback) { start = _start; end = _end; distanceType = _distanceType; simplified = _simplified; callback = _callback; }
public PathRequest(Vector3 _start, Vector3 _end, DistanceHeuristic _distanceType, bool _simplified, Action<Vector3[], bool> _callback) { start = _start; end = _end; distanceType = _distanceType; simplified = _simplified; callback = _callback; }
public void AStarWithDistance() { DistanceHeuristic <Vector> heuristic = new DistanceHeuristic <Vector>(); AStarSearch <Vector> aStarSearch = new AStarSearch <Vector>(_problem, heuristic); SearchResult <Vector> searchResult = aStarSearch.Search(); List <Vector> states = searchResult.States.ToList(); Console.WriteLine("AStar with distance"); Console.WriteLine($"OpenList:{aStarSearch.OpenList.Count}"); Console.WriteLine($"ClosedList:{aStarSearch.ClosedList.Count}"); foreach (var state in states) { Console.WriteLine(state); } }
/// <summary> /// Gets the distance between two tiles /// Uses Diagonal Distance for calculating /// the distance /// </summary> /// <param name="_currentTile"></param> /// <param name="_endTile"></param> /// <returns></returns> public int GetDistance(Node _currentTile, Node _endTile, DistanceHeuristic _distanceType) { int dx = Mathf.Abs(_currentTile.gridX - _endTile.gridX); int dy = Mathf.Abs(_currentTile.gridY - _endTile.gridY); ///////DEFUALT//////// //Square Grid that allows 8 directions of movement if (_distanceType == DistanceHeuristic.DIAGONAL) { //3/2 are used for calculating the diagonal distance if (dx > dy) { return(14 * dy + 10 * (dx - dy)); } return(14 * dx + 10 * (dy - dx)); } else //Square Grid that allows 4 directions of movement //UP, DOWN, LEFT, RIGHT if (_distanceType == DistanceHeuristic.MANHATTAN) { return(14 * (dx + dy)); } else //Square grid that allows any direction of movement //NOT restricted to a grid, **SLOWER if (_distanceType == DistanceHeuristic.EUCLIDEAN) { return((int)(14 * Mathf.Sqrt(dx * dx + dy * dy))); } return(0); }
public override IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, DistanceHeuristic distanceType, bool simplified) { this.myProperties.sw = new Stopwatch(); this.myProperties.sw.Start(); #region Init Node startTile = this.myProperties.myGraph.GetNode(startPos); Node targetTile = this.myProperties.myGraph.GetNode(targetPos); Vector3[] wayPoints = new Vector3[0]; bool success = false; //List<Tile> open = new List<Tile>(); PriorityQueue<Node> open = new PriorityQueue<Node>(this.myProperties.myGraph.Size); HashSet<Node> close = new HashSet<Node>(); open.Enqueue(startTile); //Add the starting tile to be processed #endregion Init //if (startTile.walkable) { while (open.Count > 0) { Node currentTile = open.Dequeue(); //Set the currentTile to the next elem in open //If we got to the target, the create the path to it //and exit the loop if (currentTile == targetTile) { this.myProperties.sw.Stop(); print("A*: " + this.myProperties.sw.ElapsedMilliseconds + " ms"); success = true; break; } close.Add(currentTile); // foreach (Node adjacent in this.myProperties.myGraph.GetAdjacents(currentTile)) { //Ignore the adjacent neightbor which is already evaluated if (!adjacent.walkable || close.Contains(adjacent)) continue; //Length of this path this.myProperties.tentativeGScore = currentTile.gScore + this.GetDistance(currentTile, adjacent, distanceType); //Find new tiles if (this.myProperties.tentativeGScore < adjacent.gScore || !open.Contains(adjacent)) { adjacent.gScore = this.myProperties.tentativeGScore; adjacent.hScore = this.GetDistance(adjacent, targetTile, distanceType); adjacent.myParent = currentTile; if (!open.Contains(adjacent)) open.Enqueue(adjacent); else open.UpdateElement(adjacent); } } } yield return new WaitForSeconds(0.0000001f); if (success) { wayPoints = CreatePath(startTile, targetTile, simplified); } this.myProperties.myManager.DoneProcessing(wayPoints, success); //} }
public override void StartFindPath(Vector3 startPos, Vector3 targetPos, DistanceHeuristic distanceType, bool simplified) { StartCoroutine(FindPath(startPos, targetPos, distanceType, simplified)); }
/// <summary> /// Gets the distance between two tiles /// Uses Diagonal Distance for calculating /// the distance /// </summary> /// <param name="_currentTile"></param> /// <param name="_endTile"></param> /// <returns></returns> public int GetDistance(Node _currentTile, Node _endTile, DistanceHeuristic _distanceType) { int dx = Mathf.Abs(_currentTile.gridX - _endTile.gridX); int dy = Mathf.Abs(_currentTile.gridY - _endTile.gridY); ///////DEFUALT//////// //Square Grid that allows 8 directions of movement if (_distanceType == DistanceHeuristic.DIAGONAL) { //3/2 are used for calculating the diagonal distance if (dx > dy) return 14 * dy + 10 * (dx - dy); return 14 * dx + 10 * (dy - dx); } else //Square Grid that allows 4 directions of movement //UP, DOWN, LEFT, RIGHT if (_distanceType == DistanceHeuristic.MANHATTAN) { return 14 * (dx + dy); } else //Square grid that allows any direction of movement //NOT restricted to a grid, **SLOWER if (_distanceType == DistanceHeuristic.EUCLIDEAN) { return (int)(14 * Mathf.Sqrt(dx * dx + dy * dy)); } return 0; }
public abstract void StartFindPath(Vector3 startPos, Vector3 targetPos, DistanceHeuristic distanceType, bool simplified);
/// <summary> /// Gets the path for an AI to follow /// Concrete implementations use different algorithms /// such as A*, Concurrent Dijkstra, etc.. /// </summary> /// <param name="startPos"></param> /// <param name="targetPos"></param> public abstract IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, DistanceHeuristic distanceType, bool simplified);
public static void RequestPath(Vector3 _start, Vector3 _end, DistanceHeuristic _distanceType, bool _simplifiedPath, Action<Vector3[], bool> callback) { PathRequest newRequest = new PathRequest(_start, _end, _distanceType, _simplifiedPath, callback); instance.PathQueue.Enqueue(newRequest); instance.ProcessNext(); }
public override IEnumerator FindPath(Vector3 startPos, Vector3 targetPos, DistanceHeuristic distanceType, bool simplified) { this.myProperties.sw = new Stopwatch(); this.myProperties.sw.Start(); #region Init Node startTile = this.myProperties.myGraph.GetNode(startPos); Node targetTile = this.myProperties.myGraph.GetNode(targetPos); Vector3[] wayPoints = new Vector3[0]; bool success = false; //List<Tile> open = new List<Tile>(); PriorityQueue <Node> open = new PriorityQueue <Node>(this.myProperties.myGraph.Size); HashSet <Node> close = new HashSet <Node>(); open.Enqueue(startTile); //Add the starting tile to be processed #endregion Init //if (startTile.walkable) { while (open.Count > 0) { Node currentTile = open.Dequeue(); //Set the currentTile to the next elem in open //If we got to the target, the create the path to it //and exit the loop if (currentTile == targetTile) { this.myProperties.sw.Stop(); print("A*: " + this.myProperties.sw.ElapsedMilliseconds + " ms"); success = true; break; } close.Add(currentTile); // foreach (Node adjacent in this.myProperties.myGraph.GetAdjacents(currentTile)) { //Ignore the adjacent neightbor which is already evaluated if (!adjacent.walkable || close.Contains(adjacent)) { continue; } //Length of this path this.myProperties.tentativeGScore = currentTile.gScore + this.GetDistance(currentTile, adjacent, distanceType); //Find new tiles if (this.myProperties.tentativeGScore < adjacent.gScore || !open.Contains(adjacent)) { adjacent.gScore = this.myProperties.tentativeGScore; adjacent.hScore = this.GetDistance(adjacent, targetTile, distanceType); adjacent.myParent = currentTile; if (!open.Contains(adjacent)) { open.Enqueue(adjacent); } else { open.UpdateElement(adjacent); } } } } yield return(new WaitForSeconds(0.0000001f)); if (success) { wayPoints = CreatePath(startTile, targetTile, simplified); } this.myProperties.myManager.DoneProcessing(wayPoints, success); //} }