Advertisement
AlexPshkov

Untitled

May 2nd, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1.  
  2. import org.bukkit.Location;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7.  
  8. public class PathFinder {
  9.  
  10.     private final Location startLoc;
  11.     private final Location endLoc;
  12.     private final boolean canDiagonal;
  13.  
  14.     public PathFinder(Location startLoc, Location endLoc, boolean canDiagonal) {
  15.         this.startLoc = startLoc;
  16.         this.endLoc = endLoc;
  17.         this.canDiagonal = canDiagonal;
  18.     }
  19.  
  20.     public LinkedList<Location> makePath() {
  21.         LinkedList<Location> path = new LinkedList<>();
  22.  
  23.         Location curLocation = startLoc;
  24.         path.add(curLocation);
  25.         while (!curLocation.equals(endLoc)) {
  26.             List<Location> neighbors = getNeighbors(curLocation);
  27.             Location nearest = getNearest(neighbors, endLoc);
  28.             curLocation = nearest;
  29.             path.add(curLocation);
  30.         }
  31.         return path;
  32.     }
  33.  
  34.     public Location getNearest(List<Location> locs, Location newLoc) {
  35.         Location nearest = null;
  36.         double nearestDist = Double.MAX_VALUE;
  37.         for (Location loc : locs) {
  38.             double dist = getDist(loc, newLoc);
  39.             if(dist < nearestDist) {
  40.                 nearest = loc;
  41.                 nearestDist = dist;
  42.             }
  43.         }
  44.         return nearest;
  45.     }
  46.  
  47.     public double getDist(Location curLoc, Location newLoc) {
  48.         return Math.sqrt(Math.pow((newLoc.getBlockX() - curLoc.getBlockX()),2) + Math.pow((newLoc.getBlockY() - curLoc.getBlockY()),2) + Math.pow((newLoc.getBlockZ() - curLoc.getBlockZ()),2));
  49.     }
  50.  
  51.     public List<Location> getNeighbors(Location loc) {
  52.         List<Location> neighbors = new ArrayList<>();
  53.  
  54.         //X
  55.         neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ()));
  56.         neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY(), loc.getBlockZ()));
  57.  
  58.         //Y
  59.         neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY() + 1, loc.getBlockZ()));
  60.         neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()));
  61.  
  62.         //Z
  63.         neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() + 1));
  64.         neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() - 1));
  65.  
  66.         if (canDiagonal) {
  67.             //XY
  68.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY() + 1, loc.getBlockZ()));
  69.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY() - 1, loc.getBlockZ()));
  70.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1 , loc.getBlockY() - 1, loc.getBlockZ()));
  71.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY() + 1, loc.getBlockZ()));
  72.  
  73.             //ZY
  74.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY() + 1, loc.getBlockZ() + 1));
  75.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ() - 1));
  76.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY() + 1, loc.getBlockZ() - 1));
  77.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ() + 1));
  78.  
  79.             //XZ
  80.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ() + 1));
  81.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY(), loc.getBlockZ() - 1));
  82.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ() - 1));
  83.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY(), loc.getBlockZ() + 1));
  84.  
  85.             //XZY+
  86.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY() + 1, loc.getBlockZ() + 1));
  87.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY() + 1, loc.getBlockZ() - 1));
  88.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY() + 1, loc.getBlockZ() - 1));
  89.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY() + 1, loc.getBlockZ() + 1));
  90.  
  91.             //XZY-
  92.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY() - 1, loc.getBlockZ() + 1));
  93.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY() - 1, loc.getBlockZ() - 1));
  94.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() + 1, loc.getBlockY() - 1, loc.getBlockZ() - 1));
  95.             neighbors.add(new Location(loc.getWorld(), loc.getBlockX() - 1, loc.getBlockY() - 1, loc.getBlockZ() + 1));
  96.         }
  97.         return neighbors;
  98.     }
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement