Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package server.model.npcs.pathfinding;
  2. import java.util.ArrayList;
  3. import java.util.PriorityQueue;
  4.  
  5. import server.world.Tile;
  6. import server.world.map.VirtualWorld;
  7.  
  8. /**
  9.  * Simple Path Finder.
  10.  * This one uses the most basic tree/network searching algorithm.
  11.  * I also implemented a heuristic, so it is almost like a Greedy Best-First Search pathfinder.
  12.  * Will have to evaluate the performance in the coming days.
  13.  * @author Freddy
  14.  *
  15.  */
  16.  
  17. /**
  18.  * TO-DO
  19.  * Comment the rest of the code
  20.  * CLIPPING AROUND FENCES (EG. FENCE NEAR VARROCK WILD)
  21.  * Evaluate performance
  22.  * Make optimizations
  23.  *
  24.  *
  25.  */
  26. public class SimplePath {
  27.    
  28.     //How many ms the path finder is allowed to find a path
  29.     public static final int MS_ALLOWED = 1;
  30.    
  31.     public static Path findPath(Tile start, Tile finish) {
  32.         //List of potential paths
  33.         PriorityQueue<Path> paths = new PriorityQueue<Path>();
  34.         //The starting path
  35.         Path startingPath = new Path();
  36.         startingPath.setTarget(finish);
  37.         //Add the start node to the beginning of the starting path
  38.         startingPath.getPath().addFirst(start);
  39.         //Add the starting path to the paths list
  40.         paths.add(startingPath);
  41.         //Continue until there are no remaining open paths
  42.         long startTime = System.currentTimeMillis(); //Make sure the pathfinder isn't taking too long.
  43.         while(!paths.isEmpty() && System.currentTimeMillis() - startTime <= MS_ALLOWED) {
  44.             //Poll for the path in the front of the list
  45.             Path currentPathToExamine = paths.poll();
  46.             //Check if this path has reached the target.
  47.             if(currentPathToExamine.getPath().getLast().equals(finish)) {
  48.                 //If so, we have found our path.
  49.                 currentPathToExamine.getPath().removeFirst();
  50.                 return currentPathToExamine;
  51.             }
  52.             //Loop through all valid neighbors, extend path with each one, creating new valid paths.
  53.             for(Tile adjacentValidTiles : getValidNeighbors(currentPathToExamine.getPath().getLast())) {
  54.                 //Check if the current path contains the adjacent tile
  55.                 if(!currentPathToExamine.getPath().contains(adjacentValidTiles)) {
  56.                     //Extend the new path, add it to a new linked list object.
  57.                     Path newPath = new Path();
  58.                     newPath.setTarget(finish);
  59.                     newPath.getPath().addAll(currentPathToExamine.getPath());
  60.                     newPath.getPath().add(adjacentValidTiles);
  61.                     //Add the new path to the main path list.
  62.                     paths.add(newPath);
  63.                 }
  64.             }
  65.         }
  66.         return startingPath;
  67.     }
  68.    
  69.     public static ArrayList<Tile> getValidNeighbors(Tile home) {
  70.         ArrayList<Tile> neighbors = new ArrayList<Tile>();
  71.         //North
  72.         neighbors.add(new Tile(home.x, home.y + 1, home.height));
  73.         //West
  74.         neighbors.add(new Tile(home.x - 1, home.y, home.height));
  75.         //South
  76.         neighbors.add(new Tile(home.x, home.y - 1, home.height));
  77.         //East
  78.         neighbors.add(new Tile(home.x + 1, home.y, home.height));
  79.         //North West
  80.         neighbors.add(new Tile(home.x - 1, home.y + 1, home.height));
  81.         //South West
  82.         neighbors.add(new Tile(home.x - 1, home.y - 1, home.height));
  83.         //North East
  84.         neighbors.add(new Tile(home.x + 1, home.y + 1, home.height));
  85.         //South East
  86.         neighbors.add(new Tile(home.x + 1, home.y - 1, home.height));
  87.        
  88.         //Create a list for the valid, clear tiles.
  89.         ArrayList<Tile> validList = new ArrayList<Tile>();
  90.         //Loop through all adjacent neighbors, only add the valid tiles to the list.
  91.         for(Tile t : neighbors) {
  92.             if(VirtualWorld.I(home.height, home.x, home.y, t.x, t.y, 0)) {
  93.                 validList.add(t);
  94.             }
  95.         }
  96.         return validList;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement