Beispiel #1
0
        public void FindAllPathsToAllGoals_SmallMapWithTwoGoals_Finds2PathsWith6Points()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            List <List <Point> > paths = goalMap.FindAllPathsToAllGoals(3, 4);

            string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #    0    1    2    3    #    0    #
                                                  #    1    #    3    4    #    1    #
                                                  #    2    #    4    5    #    2    #
                                                  #    3    4    5    5    4    3    #
                                                  #    #    #    #    #    #    #    #";

            Assert.AreEqual(expectedGoalMapRepresentation.Replace(" ", string.Empty), goalMap.ToString().Replace(" ", string.Empty));
            Assert.AreEqual(2, paths.Count);
            Assert.AreEqual(6, paths[0].Count);
            Assert.AreEqual(6, paths[1].Count);
        }
Beispiel #2
0
        public void FindPathAvoiding_BoxedInCornerWithObstacle_ExpectedPath()
        {
            string mapRepresentation = @"###########
                                      #.....#...#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....s.#.#
                                      ###########";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(2, 2, 0);
            Point  obstacle     = new Point(2, 2);
            string expectedPath = "........s...........";

            List <Point> path = goalMap.FindPathAvoidingGoals(1, 2, new List <Point> {
                obstacle
            });
            var actualPath = new StringBuilder();

            foreach (Point p in path)
            {
                actualPath.Append(map.GetCell(p.X, p.Y).ToString());
            }

            Assert.AreEqual(expectedPath, actualPath.ToString());
        }
Beispiel #3
0
        public void FindPath_SmallMapAfterAddingAndClearingGoals_PathHasOnePointAndAllCellsAreMax()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            goalMap.ClearGoals();
            List <Point> obstacles = new List <Point> {
                new Point(1, 2), new Point(3, 2)
            };
            List <Point> path = goalMap.FindPath(3, 4, obstacles);

            Assert.AreEqual(1, path.Count);
            Assert.AreEqual(new Point(3, 4), path[0]);
            string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #   48   48   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48   48   48   48   48   48    #
                                                  #    #    #    #    #    #    #    #";

            Assert.AreEqual(expectedGoalMapRepresentation.Replace(" ", string.Empty), goalMap.ToString().Replace(" ", string.Empty));
        }
Beispiel #4
0
        public void GlobalSetup()
        {
            var map = new Generator(MapSize, MapSize)
                      .ConfigAndGenerateSafe(gen => gen.AddSteps(DefaultAlgorithms.RectangleMapSteps()))
                      .Context.GetFirst <IGridView <bool> >("WallFloor");

            var goalsBase =
                new LambdaTranslationGridView <bool, GoalState>(map, val => val ? GoalState.Clear : GoalState.Obstacle);

            var singleGoalView = new ArrayView <GoalState>(map.Width, map.Height);

            singleGoalView.ApplyOverlay(goalsBase);
            singleGoalView[map.Bounds().Center] = GoalState.Goal;

            _singleGoalMap = new GoalMap(singleGoalView, DistanceCalc);
            _singleFleeMap = new FleeMap(_singleGoalMap);

            var dualGoalView = new ArrayView <GoalState>(map.Width, map.Height);

            dualGoalView.ApplyOverlay(goalsBase);
            foreach (var rect in dualGoalView.Bounds().BisectVertically())
            {
                dualGoalView[rect.Center] = GoalState.Goal;
            }

            _dualGoalMap = new GoalMap(dualGoalView, DistanceCalc);
            _dualFleeMap = new FleeMap(_dualGoalMap);
        }
        public void GlobalSetup()
        {
            var map = new Generator(MapSize, MapSize)
                      .ConfigAndGenerateSafe(gen => gen.AddSteps(DefaultAlgorithms.RectangleMapSteps()))
                      .Context.GetFirst <IGridView <bool> >("WallFloor");

            var goalView = new ArrayView <GoalState>(map.Width, map.Height);

            goalView.ApplyOverlay(pos => map[pos] ? GoalState.Clear : GoalState.Obstacle);
            goalView[map.Bounds().Center] = GoalState.Goal;

            // Create the goal maps used as the desires.  They all use the same goals and weights, which in general is not
            // realistic, but will provide a mathematically solid test base nonetheless.
            var maps = new List <GoalMap>();

            for (int i = 0; i < NumGoalMaps; i++)
            {
                var desireMap = new GoalMap(goalView, DistanceCalc);
                maps.Add(desireMap);
            }

            // Create overall weighted goal map
            _desireMap = new WeightedGoalMap(maps);

            // Pre-calculate the point we'll access
            _threeQuarters = new Point(map.Width, map.Height) * 0.75;
        }
Beispiel #6
0
        public void FindPathAvoiding_BoxedInCornerWithObstacle_ExpectedPath()
        {
            string mapRepresentation = @"###########
                                      #.....#...#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....s.#.#
                                      ###########";

             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );
             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 2, 2, 0 );
             Point obstacle = new Point( 2, 2 );
             string expectedPath = "........s...........";

             List<Point> path = goalMap.FindPathAvoidingGoals( 1, 2, new List<Point> { obstacle } );
             var actualPath = new StringBuilder();
             foreach( Point p in path )
             {
            actualPath.Append( map.GetCell( p.X, p.Y ).ToString() );
             }

             Assert.AreEqual( expectedPath, actualPath.ToString() );
        }
Beispiel #7
0
        public void TryFindPaths_SmallMapWithTwoGoalsOfEqualWeightAndDistance_Finds2PathsWith6Points()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            ReadOnlyCollection <Path> paths = goalMap.TryFindPaths(3, 4);

            string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #    0    1    2    3    #    0    #
                                                  #    1    #    3    4    #    1    #
                                                  #    2    #    4    5    #    2    #
                                                  #    3    4    5    5    4    3    #
                                                  #    #    #    #    #    #    #    #";

            Assert.AreEqual(RemoveWhiteSpace(expectedGoalMapRepresentation), RemoveWhiteSpace(goalMap.ToString()));
            Assert.AreEqual(2, paths.Count);
            Assert.AreEqual(6, paths[0].Length);
            Assert.AreEqual(6, paths[1].Length);
        }
Beispiel #8
0
        public void FindAllPathsToAllGoals_SmallMapWithTwoGoals_Finds2PathsWith6Points()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );

             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 1, 1, 0 );
             goalMap.AddGoal( 6, 1, 0 );
             List<List<Point>> paths = goalMap.FindAllPathsToAllGoals( 3, 4 );

             string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #    0    1    2    3    #    0    #
                                                  #    1    #    3    4    #    1    #
                                                  #    2    #    4    5    #    2    #
                                                  #    3    4    5    5    4    3    #
                                                  #    #    #    #    #    #    #    #";
             Assert.AreEqual( expectedGoalMapRepresentation.Replace( " ", string.Empty ), goalMap.ToString().Replace( " ", string.Empty ) );
             Assert.AreEqual( 2, paths.Count );
             Assert.AreEqual( 6, paths[0].Count );
             Assert.AreEqual( 6, paths[1].Count );
        }
Beispiel #9
0
 public GoalMapPathFinder(GoalMap goalMap)
 {
     _goalMap     = goalMap;
     _paths       = new List <Path>();
     _currentPath = new Stack <ICell>();
     _visited     = new HashSet <ICell>();
 }
Beispiel #10
0
        public void FindPath_SmallMapWithTwoGoalsAndTwoObstacles_FindsBestPath()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            List <Point> obstacles = new List <Point> {
                new Point(1, 2), new Point(3, 2)
            };

            goalMap.AddObstacles(obstacles);
            Path path = goalMap.FindPath(3, 4);

            Assert.AreEqual(7, path.Length);
            ICell stepForward = path.StepForward();

            Assert.AreEqual(new Cell(4, 4, true, true, false), stepForward);
        }
Beispiel #11
0
        private GoalMap CreateGoalMap(IGameObject target)
        {
            GoalMap goalMap = new GoalMap(new GoalMapTranslator(_baseMap, target), Distance.EUCLIDEAN);

            _updateMaps      += goalMap.Update;
            _goalMaps[target] = goalMap;
            return(goalMap);
        }
Beispiel #12
0
        public void FindPath_BigMapFromGame_ExpectedPath()
        {
            string mapRepresentation = @"###########################################################
                                      #.....#...............#...#.....#.....#.s.....s...........#
                                      #.....#.#.###########.#...#.#.#.#.....#.#.....#.#.#######.#
                                      #.....#.#.#.....s...#.#...s.#.#.#.....#.#.....#.#.#.....#.#
                                      #.....#.#.#.....#...#####s#s#######s#####.....#.#.#.....#.#
                                      #.....s.#.s.....#...s.....#.......#.#...#.....#.#.#.....s.#
                                      #######s#.#######...#.....#.......#.#...#.....#.#.#######.#
                                      #.......#.#.....#...#.....#.......#.s...#.....#.#.......#.#
                                      #.......#.s.....#####s#.###.......#.#...#####s#.#.####s####
                                      #.......#.#.....s.#...#...#.......s.#...#.s...#.#.#.......#
                                      #.......###########...#.###############s###...#.#.#.......#
                                      #.......s...#.#...#...#.#.......#.#.......#...#.#.#.......#
                                      #######s#...#.#...##s##.s.......#.#.......#...#######s#####
                                      #.#.....#...#.#...#...#.#.......#.#.......s...#.....#...#.#
                                      #.#.....#...#.#...#####.#.......s.##s######...#.....#...#.#
                                      #.#.....#...#.#...#...#.#.......#.#.....#.#...s.....#...#.#
                                      #.#.....#...s.###s#...#.####s######.....#s#####s#####...s.#
                                      #.#.....#...#.....s...#...#...#...#.....s.....#.....#...#.#
                                      #.#.....#####.#.#.#...#.#.#...s...#s#####.....###s###...#.#
                                      #.#.....s...#.#.#.#...#.#.#...#...#.....#.....#.....#...#.#
                                      #.##s##############...###.######s##############.....#######
                                      #.........#.......#...#.......#...#.......#...#.....s.#...#
                                      #.#.#.#.#.#.......#####.......#####.......#.###########...#
                                      #.#.#.#.#.s.......#...#.......#...#.......s.#.....#...#...#
                                      ###s###############...#.......#...####s####.s.....s...#...#
                                      #...#.......#.....s...#.......s...#.......#.#.....#...#...#
                                      #...#.......#######s###.......##########s##.###s###...s...#
                                      #...#.......s...s...#.#.......#.s...#.....#.#...#.#...#...#
                                      #...#.......#...#...s.###########...#.....#.#...#.#########
                                      #...#.......#...#...#...........#...#.....#.#...#.........#
                                      #...#.......######s##.###s#####.#...#.....#.#...#.#######.#
                                      #...s.......#.......#.#.......#.s...#.....#.#...s.#.....#.#
                                      ########s####.#.#.#.#.#.......#.#...###s###.#######.....s.#
                                      #...........#.#.#.#.#.#.......#.#...s.....#.......#.....#.#
                                      ###########################################################";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(51, 31, 0);
            goalMap.AddGoal(51, 33, 0);
            string expectedPath = ".....s.....s.......s...........s.....s..........s....s.......s...........s...s....s..............s.....s...s......s...s....s..s....s.....s..............s......";

            List <Point> path       = goalMap.FindPath(23, 7, new List <Point>());
            var          actualPath = new StringBuilder();

            foreach (Point p in path)
            {
                actualPath.Append(map.GetCell(p.X, p.Y).ToString());
            }

            Assert.AreEqual(expectedPath, actualPath.ToString());
        }
Beispiel #13
0
        public void TryFindPath_BigMapFromGameAllowingDiagonalMovement_ExpectedPath()
        {
            string mapRepresentation = @"###########################################################
                                      #.....#...............#...#.....#.....#.s.....s...........#
                                      #.....#.#.###########.#...#.#.#.#.....#.#.....#.#.#######.#
                                      #.....#.#.#.....s...#.#...s.#.#.#.....#.#.....#.#.#.....#.#
                                      #.....#.#.#.....#...#####s#s#######s#####.....#.#.#.....#.#
                                      #.....s.#.s.....#...s.....#.......#.#...#.....#.#.#.....s.#
                                      #######s#.#######...#.....#.......#.#...#.....#.#.#######.#
                                      #.......#.#.....#...#.....#.......#.s...#.....#.#.......#.#
                                      #.......#.s.....#####s#.###.......#.#...#####s#.#.####s####
                                      #.......#.#.....s.#...#...#.......s.#...#.s...#.#.#.......#
                                      #.......###########...#.###############s###...#.#.#.......#
                                      #.......s...#.#...#...#.#.......#.#.......#...#.#.#.......#
                                      #######s#...#.#...##s##.s.......#.#.......#...#######s#####
                                      #.#.....#...#.#...#...#.#.......#.#.......s...#.....#...#.#
                                      #.#.....#...#.#...#####.#.......s.##s######...#.....#...#.#
                                      #.#.....#...#.#...#...#.#.......#.#.....#.#...s.....#...#.#
                                      #.#.....#...s.###s#...#.####s######.....#s#####s#####...s.#
                                      #.#.....#...#.....s...#...#...#...#.....s.....#.....#...#.#
                                      #.#.....#####.#.#.#...#.#.#...s...#s#####.....###s###...#.#
                                      #.#.....s...#.#.#.#...#.#.#...#...#.....#.....#.....#...#.#
                                      #.##s##############...###.######s##############.....#######
                                      #.........#.......#...#.......#...#.......#...#.....s.#...#
                                      #.#.#.#.#.#.......#####.......#####.......#.###########...#
                                      #.#.#.#.#.s.......#...#.......#...#.......s.#.....#...#...#
                                      ###s###############...#.......#...####s####.s.....s...#...#
                                      #...#.......#.....s...#.......s...#.......#.#.....#...#...#
                                      #...#.......#######s###.......##########s##.###s###...s...#
                                      #...#.......s...s...#.#.......#.s...#.....#.#...#.#...#...#
                                      #...#.......#...#...s.###########...#.....#.#...#.#########
                                      #...#.......#...#...#...........#...#.....#.#...#.........#
                                      #...#.......######s##.###s#####.#...#.....#.#...#.#######.#
                                      #...s.......#.......#.#.......#.s...#.....#.#...s.#.....#.#
                                      ########s####.#.#.#.#.#.......#.#...###s###.#######.....s.#
                                      #...........#.#.#.#.#.#.......#.#...s.....#.......#.....#.#
                                      ###########################################################";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map, true);

            goalMap.AddGoal(51, 31, 0);
            goalMap.AddGoal(51, 33, 0);
            string expectedPath = "...s...s.....s........s.....s.......s...s......s.......s...s...s...........s...s..s.....s.s...s.s..s....s..........s.....";

            Path path       = goalMap.TryFindPath(23, 7);
            var  actualPath = new StringBuilder();

            foreach (ICell cell in path.Steps)
            {
                actualPath.Append(cell.ToString());
            }

            Assert.AreEqual(expectedPath, actualPath.ToString());
        }
Beispiel #14
0
        public bool Act()
        {
            var   ac        = parent.GetComponent <Actor>(ComponentType.Actor);
            var   pos       = parent.GetComponent <SpriteAnimation>(ComponentType.SpriteAnimation).Position;
            IMap  map       = ac.Map.MapData.Map;
            Point playerPos = ac.Map.CameraFollow.GetComponent <SpriteAnimation>(ComponentType.SpriteAnimation).Position;

            ac.Map.MapData.SetIsWalkable(pos.X, pos.Y, true);
            ac.Map.MapData.SetIsWalkable(playerPos.X, playerPos.Y, true);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(playerPos.X, playerPos.Y, 0);

            Path path = null;

            try
            {
                path = goalMap.FindPathAvoidingGoals(pos.X, pos.Y);
            }
            catch (PathNotFoundException)
            {
                System.Console.WriteLine("RunAway - PathNotFoundException");
                if (parent.logger != null)
                {
                    parent.logger.Write(string.Format("{0}, Finds itself cornered.", parent.NAME));
                }
            }
            ac.Map.MapData.SetIsWalkable(pos.X, pos.Y, false);
            ac.Map.MapData.SetIsWalkable(playerPos.X, playerPos.Y, false);

            if (path != null)
            {
                try
                {
                    ac.Sprite.Move(path.StepForward());
                }
                catch (NoMoreStepsException)
                {
                    System.Console.WriteLine("RunAway - NoMoreStepsException");
                    if (parent.logger != null)
                    {
                        parent.logger.Write(string.Format("{0}, Has nowhere to run.", parent.NAME));
                    }
                }
            }
            return(true);
        }
Beispiel #15
0
        public void FindPath_SourceCellNotWalkable_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);

            goalMap.FindPath(0, 1);
        }
Beispiel #16
0
        public void FindPath_DestinationNotWalkableAnd13StepsAway_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(7, 1, 0);

            goalMap.FindPath(1, 1);
        }
Beispiel #17
0
        private GoalMap CreateGoalMap()
        {
            var goalMap = new GoalMap(map.GetIMap());

            // Aim for our random goal
            goalMap.AddGoal(this.goal.X, this.goal.Y, 100);
            // Avoid ANYTHING that's solid
            foreach (var e in map.Objects)
            {
                if (e.IsSolid && e != this)
                {
                    goalMap.AddObstacle(e.X, e.Y);
                }
            }

            return(goalMap);
        }
Beispiel #18
0
        public void TryFindPath_DestinationNotWalkableAnd13StepsAway_ReturnsNull()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(7, 1, 0);

            Path path = goalMap.TryFindPath(1, 1);

            Assert.AreEqual(null, path);
        }
Beispiel #19
0
        public static TimeSpan TimeForGoalMap(IMapView <bool> map, IEnumerable <Coord> goals, int iterations)
        {
            Stopwatch s = new Stopwatch();

            var mapGoals   = createGoalStateMap(map, goals);
            var mapBuilder = new GoalMap(mapGoals, Distance.CHEBYSHEV);

            mapBuilder.Update();

            s.Start();
            for (int i = 0; i < iterations; i++)
            {
                mapBuilder.Update();
            }
            s.Stop();

            return(s.Elapsed);
        }
Beispiel #20
0
        public void TryFindPath_DestinationUnreachable_ReturnsNull()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #....#.#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(6, 1, 0);

            var paths = goalMap.TryFindPath(1, 1);

            Assert.AreEqual(null, paths);
        }
Beispiel #21
0
        public void FindPath_SmallMapAfterAddingAndClearingGoals_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            goalMap.ClearGoals();

            goalMap.FindPath(3, 4);
        }
Beispiel #22
0
        public bool Act(Monster monster, CommandSystem commandSystem)
        {
            DungeonMap dungeonMap = Game.DungeonMap;
            Player     player     = Game.Player;

            // Set the cells the monster and player are on to walkable so the pathfinder doesn't bail early
            dungeonMap.SetIsWalkable(monster.X, monster.Y, true);
            dungeonMap.SetIsWalkable(player.X, player.Y, true);

            GoalMap goalMap = new GoalMap(dungeonMap);

            goalMap.AddGoal(player.X, player.Y, 0);

            Path path = null;

            try
            {
                path = goalMap.FindPathAvoidingGoals(monster.X, monster.Y);
            }
            catch (PathNotFoundException)
            {
                Game.MessageLog.Add($"{monster.Name} cowers in fear");
            }


            // Reset the cell the monster and player are on  back to not walkable
            dungeonMap.SetIsWalkable(monster.X, monster.Y, false);
            dungeonMap.SetIsWalkable(player.X, player.Y, false);

            if (path != null)
            {
                try
                {
                    commandSystem.MoveMonster(monster, path.StepForward());
                }
                catch (NoMoreStepsException)
                {
                    Game.MessageLog.Add($"{monster.Name} cowers in fear");
                }
            }

            return(true);
        }
Beispiel #23
0
        public void FindPathAvoiding_NoPathExists_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"###############################################
                                      #..........#......................##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #.....................##......................#
                                      ###############################################";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(2, 2, 0);
            goalMap.AddObstacle(2, 1);
            goalMap.AddObstacle(1, 2);

            Path path = goalMap.FindPathAvoidingGoals(1, 1);
        }
Beispiel #24
0
        public void TryFindPath_SmallMapAfterAddingAndClearingGoals_ReturnsNull()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            goalMap.ClearGoals();

            Path path = goalMap.TryFindPath(3, 4);

            Assert.AreEqual(null, path);
        }
Beispiel #25
0
        public bool Act(Monster monster, CommandSystem commandSystem)
        {
            DungeonMap dungeonMap = Game.DungeonMap;
            Player     player     = Game.Player;

            dungeonMap.SetIsWalkable(monster.X, monster.Y, true);
            dungeonMap.SetIsWalkable(player.X, player.Y, true);

            GoalMap goalMap = new GoalMap(dungeonMap);

            goalMap.AddGoal(player.X, player.Y, 0);

            Path path = null;

            try
            {
                path = goalMap.FindPathAvoidingGoals(monster.X, monster.Y);
            }
            catch (PathNotFoundException)
            {
                Game.MessageLog.Add($"{monster.Name} cowers in fear");
            }

            dungeonMap.SetIsWalkable(monster.X, monster.Y, false);
            dungeonMap.SetIsWalkable(player.X, player.Y, false);

            if (path != null)
            {
                try
                {
                    commandSystem.MoveMonster(monster, path.StepForward());
                }
                catch (NoMoreStepsException)
                {
                    Game.MessageLog.Add($"{monster.Name} cowers in fear");
                }
            }

            return(true);
        }
Beispiel #26
0
        public void TryFindPathAvoiding_NoPathExists_ReturnsNull()
        {
            string mapRepresentation = @"###############################################
                                      #..........#......................##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #.....................##......................#
                                      ###############################################";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(2, 2, 0);
            goalMap.AddObstacle(2, 1);
            goalMap.AddObstacle(1, 1);

            Path path = goalMap.TryFindPathAvoidingGoals(1, 1);

            Assert.AreEqual(null, path);
        }
Beispiel #27
0
        public void FindPath_SmallMapWithTwoGoalsAndTwoObstacles_FindsBestPath()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            List <Point> obstacles = new List <Point> {
                new Point(1, 2), new Point(3, 2)
            };
            List <Point> path = goalMap.FindPath(3, 4, obstacles);

            Assert.AreEqual(6, path.Count);
            Assert.AreEqual(new Point(2, 4), path[1]);
        }
Beispiel #28
0
        public void TryFindPathAvoiding_BoxedInCornerWithObstacle_ExpectedPath()
        {
            string mapRepresentation = @"###############################################
                                      #..........#......................##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #.....................##......................#
                                      ###############################################";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(2, 2, 0);
            goalMap.AddObstacle(2, 1);

            Path path = goalMap.TryFindPathAvoidingGoals(1, 1);

            Assert.AreEqual(61, path.Length);
            Assert.AreEqual(map.GetCell(1, 2), path.StepForward());
            Assert.AreEqual(map.GetCell(1, 1), path.Start);
            Assert.AreEqual(map.GetCell(45, 1), path.End);
        }
Beispiel #29
0
        public void ManualGoalMapTest()
        {
            var map = new ArrayMap <bool>(MAP_WIDTH, MAP_HEIGHT);

            RectangleMapGenerator.Generate(map);

            var stateMap = new ArrayMap <GoalState>(map.Width, map.Height);

            foreach (var pos in stateMap.Positions())
            {
                stateMap[pos] = map[pos] ? GoalState.Clear : GoalState.Obstacle;
            }

            stateMap[MAP_WIDTH / 2, MAP_WIDTH / 2]          = GoalState.Goal;
            stateMap[MAP_WIDTH / 2 + 5, MAP_HEIGHT / 2 + 5] = GoalState.Goal;

            var goalMap = new GoalMap(stateMap, Distance.EUCLIDEAN);

            goalMap.Update();

            Assert.AreEqual(null, goalMap[0, 0]);

            Console.Write(goalMap.ToString(5, "0.00"));
        }
Beispiel #30
0
        public void Constructor_NullMap_ThrowArgumentNullException()
        {
            IMap map = null;

            GoalMap goalMap = new GoalMap(map);
        }
Beispiel #31
0
        public void FindPath_SmallMapAfterAddingAndClearingGoals_PathHasOnePointAndAllCellsAreMax()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );

             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 1, 1, 0 );
             goalMap.AddGoal( 6, 1, 0 );
             goalMap.ClearGoals();
             List<Point> obstacles = new List<Point> { new Point( 1, 2 ), new Point( 3, 2 ) };
             List<Point> path = goalMap.FindPath( 3, 4, obstacles );

             Assert.AreEqual( 1, path.Count );
             Assert.AreEqual( new Point( 3, 4 ), path[0] );
             string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #   48   48   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48   48   48   48   48   48    #
                                                  #    #    #    #    #    #    #    #";
             Assert.AreEqual( expectedGoalMapRepresentation.Replace( " ", string.Empty ), goalMap.ToString().Replace( " ", string.Empty ) );
        }
Beispiel #32
0
        public void FindPath_SmallMapWithTwoGoalsAndTwoObstacles_FindsBestPath()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );

             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 1, 1, 0 );
             goalMap.AddGoal( 6, 1, 0 );
             List<Point> obstacles = new List<Point> { new Point( 1, 2 ), new Point( 3, 2 ) };
             List<Point> path = goalMap.FindPath( 3, 4, obstacles );

             Assert.AreEqual( 6, path.Count );
             Assert.AreEqual( new Point( 2, 4 ), path[1] );
        }
Beispiel #33
0
        public void FindPath_BigMapFromGame_ExpectedPath()
        {
            string mapRepresentation = @"###########################################################
                                      #.....#...............#...#.....#.....#.s.....s...........#
                                      #.....#.#.###########.#...#.#.#.#.....#.#.....#.#.#######.#
                                      #.....#.#.#.....s...#.#...s.#.#.#.....#.#.....#.#.#.....#.#
                                      #.....#.#.#.....#...#####s#s#######s#####.....#.#.#.....#.#
                                      #.....s.#.s.....#...s.....#.......#.#...#.....#.#.#.....s.#
                                      #######s#.#######...#.....#.......#.#...#.....#.#.#######.#
                                      #.......#.#.....#...#.....#.......#.s...#.....#.#.......#.#
                                      #.......#.s.....#####s#.###.......#.#...#####s#.#.####s####
                                      #.......#.#.....s.#...#...#.......s.#...#.s...#.#.#.......#
                                      #.......###########...#.###############s###...#.#.#.......#
                                      #.......s...#.#...#...#.#.......#.#.......#...#.#.#.......#
                                      #######s#...#.#...##s##.s.......#.#.......#...#######s#####
                                      #.#.....#...#.#...#...#.#.......#.#.......s...#.....#...#.#
                                      #.#.....#...#.#...#####.#.......s.##s######...#.....#...#.#
                                      #.#.....#...#.#...#...#.#.......#.#.....#.#...s.....#...#.#
                                      #.#.....#...s.###s#...#.####s######.....#s#####s#####...s.#
                                      #.#.....#...#.....s...#...#...#...#.....s.....#.....#...#.#
                                      #.#.....#####.#.#.#...#.#.#...s...#s#####.....###s###...#.#
                                      #.#.....s...#.#.#.#...#.#.#...#...#.....#.....#.....#...#.#
                                      #.##s##############...###.######s##############.....#######
                                      #.........#.......#...#.......#...#.......#...#.....s.#...#
                                      #.#.#.#.#.#.......#####.......#####.......#.###########...#
                                      #.#.#.#.#.s.......#...#.......#...#.......s.#.....#...#...#
                                      ###s###############...#.......#...####s####.s.....s...#...#
                                      #...#.......#.....s...#.......s...#.......#.#.....#...#...#
                                      #...#.......#######s###.......##########s##.###s###...s...#
                                      #...#.......s...s...#.#.......#.s...#.....#.#...#.#...#...#
                                      #...#.......#...#...s.###########...#.....#.#...#.#########
                                      #...#.......#...#...#...........#...#.....#.#...#.........#
                                      #...#.......######s##.###s#####.#...#.....#.#...#.#######.#
                                      #...s.......#.......#.#.......#.s...#.....#.#...s.#.....#.#
                                      ########s####.#.#.#.#.#.......#.#...###s###.#######.....s.#
                                      #...........#.#.#.#.#.#.......#.#...s.....#.......#.....#.#
                                      ###########################################################";

             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );
             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 51, 31, 0 );
             goalMap.AddGoal( 51, 33, 0 );
             string expectedPath = ".....s.....s.......s...........s.....s..........s....s.......s...........s...s....s..............s.....s...s......s...s....s..s....s.....s..............s......";

             List<Point> path = goalMap.FindPath( 23, 7, new List<Point>() );
             var actualPath = new StringBuilder();
             foreach( Point p in path )
             {
            actualPath.Append( map.GetCell( p.X, p.Y ).ToString() );
             }

             Assert.AreEqual( expectedPath, actualPath.ToString() );
        }