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()); }
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() ); }
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); }
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); }
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); }
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); }
public void FindPathAvoiding_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.FindPathAvoidingGoals(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); }