Пример #1
0
        private bool SearchForPath(PathRequest request)
        {
            try
            {
                var fromCell = request.From;
                var toCell   = request.To;

                _searchFrontierPhase += 2;

                if (_searchFrontier == null)
                {
                    _searchFrontier = new CellPriorityQueue();
                }
                else
                {
                    _searchFrontier.Clear();
                }

                fromCell.SearchPhase    = _searchFrontierPhase;
                fromCell.SearchDistance = 0;
                _searchFrontier.Enqueue(fromCell);

                while (_searchFrontier.Count > 0)
                {
                    var current = _searchFrontier.Dequeue();
                    current.SearchPhase++;

                    if (current == toCell)
                    {
                        return(true);
                    }

                    for (var d = Direction.N; d <= Direction.NW; d++)
                    {
                        var neighbor = current.GetNeighbor(d);

                        if (neighbor == null || neighbor.SearchPhase > _searchFrontierPhase)
                        {
                            continue;
                        }

                        var neighborTravelCost = request.CalculateTravelCost.Invoke(current, neighbor);
                        if (neighborTravelCost < 0)
                        {
                            continue;
                        }

                        var distance = current.SearchDistance + neighborTravelCost;
                        if (neighbor.SearchPhase < _searchFrontierPhase)
                        {
                            neighbor.SearchPhase     = _searchFrontierPhase;
                            neighbor.SearchDistance  = distance;
                            neighbor.PathFrom        = current;
                            neighbor.SearchHeuristic = neighbor.DistanceTo(toCell);
                            _searchFrontier.Enqueue(neighbor);
                        }
                        else if (distance < neighbor.SearchDistance)
                        {
                            var oldPriority = neighbor.SearchPriority;
                            neighbor.SearchDistance = distance;
                            neighbor.PathFrom       = current;
                            _searchFrontier.Change(neighbor, oldPriority);
                        }
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                Debug.LogWarning("Pathing error: " + ex.ToString());
                throw;
            }
        }
 public InvalidPathException(PathRequest pathRequest)
 {
     PathRequest = pathRequest;
 }
Пример #3
0
 public void AddPathRequest(PathRequest pathRequest)
 {
     _pathQueue.Enqueue(pathRequest);
 }