Esempio n. 1
0
File: Grid.cs Progetto: bmjoy/Osnowa
 public void InitializePathfindingData(bool initializeJps)
 {
     for (int currentX = 0; currentX < XSize; currentX++)
     {
         for (int currentY = 0; currentY < YSize; currentY++)
         {
             // todo mixing zero-based and non-zero-based concepts here!
             Position positionToCheckForWall = _pathfindingDataHolder.PositionToNonZeroBasedPosition(new Position(currentX, currentY));
             bool     isWalkable             = IsWalkable(positionToCheckForWall);
             _pathfindingDataHolder.UpdateWalkability(positionToCheckForWall, isWalkable);
         }
     }
 }
Esempio n. 2
0
        public void FindJumpPointsWithJps_IntegrationTest_AllGridIsWalkable_ReturnsCorrectPath(int xOffset, int yOffset)
        {
            /* Illustration:
             * ........
             * ...jt...
             * ..sj....
             * ........*/

            Position minPositionInUnityGrid = new Position(-10, -10);
            int      unityGridXSize         = 20;
            int      unityGridYSize         = 20;
            Position offset                             = new Position(xOffset, yOffset);
            var      startPosition                      = new Position(0, 0) + offset;
            var      targetPosition                     = new Position(2, 1) + offset;
            var      expectedMiddleJumpPoint            = new Position(1, 1) + offset;
            var      expectedAlternativeMiddleJumpPoint = new Position(1, 0) + offset;
            PathfindingDataHolder pathfindingData       = new PathfindingDataHolder(unityGridXSize, unityGridYSize);

            for (int x = 0; x < unityGridXSize; x++)
            {
                for (int y = 0; y < unityGridYSize; y++)
                {
                    var position = new Position(x, y);
                    pathfindingData.UpdateWalkability(position, true);
                }
            }
            var contextManager = Mock.Of <IOsnowaContextManager>(m => m.Current.PathfindingData == pathfindingData);

            IRasterLineCreator bresenham = new BresenhamLineCreator();
            var pathfinder = new Pathfinder(contextManager, new NaturalLineCalculator(bresenham), bresenham);

            IList <Position> jumpPoints = pathfinder.FindJumpPointsWithJps(startPosition, targetPosition).Positions;
            IList <Position> jumpPointsFromSpatialAstar = pathfinder.FindJumpPointsWithSpatialAstar(startPosition, targetPosition).Positions;

            jumpPoints.Count.Should().Be(3);
            jumpPoints[0].Should().Be(startPosition);
            (jumpPoints[1] == expectedMiddleJumpPoint || jumpPoints[1] == expectedAlternativeMiddleJumpPoint).Should().BeTrue();
            jumpPoints[2].Should().Be(targetPosition);

            jumpPointsFromSpatialAstar.Count.Should().Be(3);
            jumpPointsFromSpatialAstar[0].Should().Be(startPosition);
            (jumpPointsFromSpatialAstar[1] == expectedMiddleJumpPoint ||
             jumpPointsFromSpatialAstar[1] == expectedAlternativeMiddleJumpPoint).Should().BeTrue();
            jumpPointsFromSpatialAstar[2].Should().Be(targetPosition);
        }