/// <summary> /// Outputs three examples of path finding to the Console. /// </summary> /// <remarks>The examples have copied from the unit tests!</remarks> public void Run() { // Start with a clear map (don't add any obstacles) InitializeMap(); PathFinder pathFinder = new PathFinder(searchParameters); List<Point> path = pathFinder.FindPath(); ShowRoute("The algorithm should find a direct path without obstacles:", path); Console.WriteLine(); // Now add an obstacle InitializeMap(); AddWallWithGap(); pathFinder = new PathFinder(searchParameters); path = pathFinder.FindPath(); ShowRoute("The algorithm should find a route around the obstacle:", path); Console.WriteLine(); // Finally, create a barrier between the start and end points InitializeMap(); AddWallWithoutGap(); pathFinder = new PathFinder(searchParameters); path = pathFinder.FindPath(); ShowRoute("The algorithm should not be able to find a route around the barrier:", path); Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
List<Point> IgnorePool; // ignore for pathfind public PathFinder GetPathFinder() { if( pathfinder == null ) { InitializePathFindMap(); pathfinder = new PathFinder( map ); } return pathfinder; }
public void calculateMovement(Object o, Map map) { int left, top; int PCleft, PCtop; //get monster coords left = _posX; top = _posY; Point monsterLocation = new Point((left - 1), (top - 5)); //get Object's coord PCleft = o.getX(); PCtop = o.getY(); Point targetLocation = new Point(PCleft - 1, PCtop - 5); bool[,] b = map.getMapForPathFinding(); //remove himself and target from map b[monsterLocation.X, monsterLocation.Y] = true; b[targetLocation.X, targetLocation.Y] = true; //find path SearchParameters sPams = new SearchParameters(monsterLocation, targetLocation, b); Finder = new PathFinder(sPams); _route = Finder.FindPath(); //if no path was find quit if (_route.Count == 0) { _pathAvaible = false; return; } _pathAvaible = true; //clear movement list if there is something left and recreate it _movement.Clear(); for (int i = 0; i < _route.Count; i++) { if (i == 0) { _movement.Add(Game.calculateDelta(monsterLocation, _route[i])); } else { _movement.Add(Game.calculateDelta(_route[i - 1], _route[i])); } } //save to file to check for errors StreamWriter file = new StreamWriter("log.txt"); file.WriteLine("test for calculating delta for pathfinding"); file.WriteLine("Monster position on map -- X :"+monsterLocation.X+" Y :"+monsterLocation.Y); file.WriteLine("Monster position on screen -- X :"+left+" Y :"+top); file.WriteLine("Delta X :" +(left-monsterLocation.X)+" Delta Y :"+(top-monsterLocation.Y)); file.WriteLine("Object position on map -- X :" + targetLocation.X + " Y :" + targetLocation.Y); file.WriteLine("Object position on screen -- X :" + PCleft + " Y :" + PCtop); file.WriteLine("Delta X :" + (PCleft - targetLocation.X) + " Delta Y :" + (PCtop - targetLocation.Y)); file.WriteLine("_route and _movement:"); for (int i = 0; i < _route.Count; i++) { file.WriteLine("_route\t Pos X : "+_route[i].X+"\tPos Y :"+_route[i].Y+"\t\t\t_movement\t Pos X :"+_movement[i].X+"\tPos Y :"+_movement[i].Y); } file.Close(); }