Advertisement
Guest User

PathFinding

a guest
Jul 19th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package blockygames.game.pathfinding;
  2.  
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import blockygames.game.Game;
  8. import blockygames.game.level.tiles.Tile;
  9.  
  10. public class FindPath {
  11.     private boolean isFindingPath = false;
  12.     private List<Point> openList = new ArrayList<Point>();
  13.     private List<Point> closedList = new ArrayList<Point>();
  14.     private List<Point> path = new ArrayList<Point>();
  15.     private Point lastLowestF = new Point(-1, -1);
  16.    
  17.     public List<Point> findPath(Point start, Point end) {
  18.         if(isFindingPath){
  19.             return null;
  20.         }
  21.         isFindingPath = true;
  22.  
  23.         openList.clear();
  24.         closedList.clear();
  25.         path.clear();
  26.                
  27.         if(Game.level.isTileWalkable((int)end.getX(), (int)end.getY())){
  28.            
  29.             checkNodesAroundPoint(start);
  30.             changeToClosedList(start);
  31.             changeToClosedList(searchLowestF(start, end));
  32.            
  33.             while(isFindingPath && !openList.isEmpty() && closedList.size() <= 10000){
  34.                 System.out.println(openList.size() + ", " + closedList.size());
  35.                 Point lowestF = searchLowestF(start, end);
  36.                 checkNodesAroundPoint(lowestF);
  37.             }
  38.            
  39.             for (Point p : closedList) {
  40.                 Game.level.alterTile((int)p.getX(), (int)p.getY(), Tile.POSIONGRASS);
  41.             }
  42.            
  43.             isFindingPath = false;
  44.             return path;
  45.         }
  46.        
  47.         isFindingPath = false;
  48.         return null;
  49.     }
  50.    
  51.     private void changeToClosedList(Point change){
  52.         closedList.add(change);
  53.         openList.remove(change);
  54.     }
  55.    
  56.     public void checkNodesAroundPoint(Point search){
  57.         for(int x = (int)search.getX() - 1; x <= (int)search.getX() + 1; x++){
  58.             for(int y = (int)search.getY() - 1; y <= (int)search.getY() + 1; y++){
  59.                 if(!openList.contains(new Point(x,y))){
  60.                     if(Game.level.isTileWalkable(x, y)){
  61.                         if(x == (int)search.getX() - 1 && y == (int)search.getY() - 1){
  62.                             if(Game.level.isTileWalkable(x + 1, y)){
  63.                                 if(Game.level.isTileWalkable(x, y + 1)){
  64.                                     openList.add(new Point(x,y));
  65.                                     Game.level.alterTile(x, y, Tile.ROCK);     
  66.                                 }
  67.                             }
  68.                         } else if(x == (int)search.getX() - 1 && y == (int)search.getY() + 1){
  69.                             if(Game.level.isTileWalkable(x + 1, y)){
  70.                                 if(Game.level.isTileWalkable(x, y - 1)){
  71.                                     openList.add(new Point(x,y));
  72.                                     Game.level.alterTile(x, y, Tile.ROCK);     
  73.                                 }
  74.                             }
  75.                         } else if(x == (int)search.getX() + 1 && y == (int)search.getY() - 1){
  76.                             if(Game.level.isTileWalkable(x - 1, y)){
  77.                                 if(Game.level.isTileWalkable(x, y + 1)){
  78.                                     openList.add(new Point(x,y));
  79.                                     Game.level.alterTile(x, y, Tile.ROCK);     
  80.                                 }
  81.                             }
  82.                         } else if(x == (int)search.getX() + 1 && y == (int)search.getY() + 1){
  83.                             if(Game.level.isTileWalkable(x - 1, y)){
  84.                                 if(Game.level.isTileWalkable(x, y - 1)){
  85.                                     openList.add(new Point(x,y));
  86.                                     Game.level.alterTile(x, y, Tile.ROCK);     
  87.                                 }
  88.                             }
  89.                         } else{
  90.                             openList.add(new Point(x,y));
  91.                             Game.level.alterTile(x, y, Tile.ROCK);                             
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98.    
  99.     public Point searchLowestF(Point start, Point end){
  100.         Point lowestF = new Point(-1, -1);
  101.         int lowestFInList = -1;
  102.         int g = 0;
  103.         int h = 0;
  104.         int f = 0;
  105.        
  106.         for (Point p : openList) {
  107.             if(lastLowestF == new Point(-1, -1)){
  108.                 if(p.getX() == start.getX() || p.getY() == start.getY()){
  109.                     g = 10;
  110.                 } else {
  111.                     g = 14;
  112.                 }
  113.             } else {
  114.                 if(p.getX() == lastLowestF.getX() || p.getY() == lastLowestF.getY()){
  115.                     g = 10;
  116.                 } else {
  117.                     g = 14;
  118.                 }
  119.             }
  120.             if(p.getX() <= end.getX() && p.getY() <= end.getY()){
  121.                 h = (((int)end.getX() - (int)p.getX()) + ((int)end.getY() - (int)p.getY()))* 10;
  122.             } else if(p.getX() > end.getX() && p.getY() <= end.getY()){
  123.                 h = (((int)p.getX() - (int)end.getX()) + ((int)end.getY() - (int)p.getY()))* 10;
  124.             } else if(p.getX() <= end.getX() && p.getY() > end.getY()){
  125.                 h = (((int)end.getX() - (int)p.getX()) + ((int)p.getY() - (int)end.getY()))* 10;
  126.             } else if(p.getX() > end.getX() && p.getY() > end.getY()){
  127.                 h = (((int)p.getX() - (int)end.getX()) + ((int)p.getY() - (int)end.getY()))* 10;
  128.             }
  129.            
  130.             if(h == 0){
  131.                 isFindingPath = false;
  132.             }
  133.            
  134.             f = g + h;
  135.            
  136.             if(lowestFInList == -1){
  137.                 lowestFInList = f;
  138.                 lowestF = p;
  139.             } else if(f < lowestFInList){
  140.                 lowestFInList = f;
  141.                 lowestF = p;
  142.             }          
  143.         }
  144.         lastLowestF = lowestF;
  145.         changeToClosedList(lowestF);
  146.         return lowestF;
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement