Exemplo n.º 1
0
        public IList <Position> GetFirstLongestNaturalLine(IList <Position> jumpPoints, Func <Position, bool> isWalkable)
        {
            if (jumpPoints.Count == 1)
            {
                return(jumpPoints);
            }
            IList <Position> naturalJumpPoints = GetNaturalJumpPoints(jumpPoints);

            if (naturalJumpPoints.Count == 2)
            {
                return(_rasterLineCreator.GetRasterLinePermissive(jumpPoints[0].x, jumpPoints[0].y, jumpPoints[1].x, jumpPoints[1].y,
                                                                  isWalkable, -1, false));
            }
            IList <Position> firstThreeNaturalJumpPoints = naturalJumpPoints.Take(3).ToList();
            Position         firstJumpPoint      = firstThreeNaturalJumpPoints[0];
            Position         rangeCheckBeginning = firstThreeNaturalJumpPoints[1];
            Position         rangeCheckEnd       = firstThreeNaturalJumpPoints[2];
            IList <Position> rangeToCheck        =     // note that it's going from range end to range beginning
                                                   _rasterLineCreator.GetRasterLinePermissive(rangeCheckEnd.x, rangeCheckEnd.y, rangeCheckBeginning.x, rangeCheckBeginning.y,
                                                                                              position => true, -1);
            IList <Position> naturalWay = null;

            foreach (Position checkedPosition in rangeToCheck)
            {
                var xxx
                    = _rasterLineCreator.GetRasterLinePermissive(3, 0, 5, 2, isWalkable, -1, false);

                IList <Position> bresenhamLineToChecked =
                    _rasterLineCreator.GetRasterLinePermissive(firstJumpPoint.x, firstJumpPoint.y, checkedPosition.x, checkedPosition.y, isWalkable, -1, false);
                bool clearWayToThirdExists = bresenhamLineToChecked.Any() && bresenhamLineToChecked.Last() == checkedPosition;
                if (clearWayToThirdExists)
                {
                    naturalWay = bresenhamLineToChecked;
                    break;
                }
            }
            return(naturalWay);
        }
Exemplo n.º 2
0
        public virtual NavigationData GetNavigationData(Position startPosition, Position targetPosition)
        {
            if (!_grid.IsWalkable(targetPosition))
            {
                return(null);
            }

            var navigationData = new NavigationData
            {
                Destination = targetPosition,
                RemainingStepsInCurrentSegment = new Stack <Position>(),
                LastStep = startPosition
            };

            if (startPosition == targetPosition)
            {
                return(navigationData);
            }

            IList <Position> line = _rasterLineCreator.GetRasterLinePermissive(startPosition.x, startPosition.y, targetPosition.x,
                                                                               targetPosition.y, _grid.IsWalkable);

            bool rasterLineReachesTarget = line.Last() == targetPosition;

            if (rasterLineReachesTarget)
            {
                _debugPathPresenter.PresentStraight(line);
                // performance: if GetRasterLinePermissive returned a stack, we wouldn't need to allocate memory for the list
                navigationData.RemainingNodes = new[] { targetPosition }.ToList();
                return(navigationData);
            }

            PathfindingResponse result = _pathfinder.FindJumpPointsWithJps(startPosition, targetPosition);

            if (result.Result != PathfindingResult.Success)
            {
                return(null);
            }
            IEnumerable <Position> naturalJumpPoints = _naturalLineCreator.GetNaturalJumpPoints(result.Positions).Skip(1);

            navigationData.RemainingNodes = naturalJumpPoints.ToList();
            _debugPathPresenter.PresentNonStraight(navigationData.RemainingNodes);

            return(navigationData);
        }