From e038b83732c746a313900ea1aa97677c43f5ecc2 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sun, 10 Jun 2007 20:46:08 +0000 Subject: [PATCH] --- src/ei/game/algo/AStarPathFinder.java | 294 ------------------ ...rPathfinder.java => AStarPathfinder2.java} | 0 2 files changed, 294 deletions(-) delete mode 100644 src/ei/game/algo/AStarPathFinder.java rename src/ei/game/algo/{AStarPathfinder.java => AStarPathfinder2.java} (100%) diff --git a/src/ei/game/algo/AStarPathFinder.java b/src/ei/game/algo/AStarPathFinder.java deleted file mode 100644 index bc7bbc9..0000000 --- a/src/ei/game/algo/AStarPathFinder.java +++ /dev/null @@ -1,294 +0,0 @@ -package ei.game.algo; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; - -/** - * A* path finder across a tile map - * - * @author Kevin Glass - */ -public strictfp class AStarPathFinder implements PathFinder { - /** The map being searched */ - private TileMap map; - /** The set describing the properties of the tiles on the map */ - private TileSet set; - /** The distance from the end point each point on the map is */ - private int[] distance; - /** The maximum search depth before giving up */ - private int maxsearch; - /** The starting x coordinate */ - private int sx; - /** The starting y coordiante */ - private int sy; - /** The callback notified as nodes are processed */ - private PathFinderCallback callback; - /** The currently open nodes */ - private ArrayList open = new ArrayList(); - - /** - * Create a new path finder based on the A* algorithm - * - * @param map The map being searched - * @param set The set describing the tiles on the map - */ - public AStarPathFinder(TileMap map,TileSet set) { - this(map,set,null); - } - - /** - * Create a new path finder based on the A* algorithm - * - * @param map The map being searched - * @param set The set describing the tiles on the map - * @param callback The callback notified as nodes are traversed - */ - public AStarPathFinder(TileMap map,TileSet set,PathFinderCallback callback) { - this.map = map; - this.set = set; - this.callback = callback; - distance = new int[map.getMapWidth()*map.getMapHeight()]; - } - - /** - * @see org.newdawn.util.map.PathFinder#reset() - */ - public void reset() { - open.clear(); - Arrays.fill(distance,0); - } - - /** - * @see org.newdawn.util.map.PathFinder#getSearchData() - */ - public int[] getSearchData() { - return distance; - } - - /** - * @see org.newdawn.util.map.PathFinder#findPath(int, int, int, int, int) - */ - public synchronized Path findPath(int sx,int sy,int dx,int dy,int maxsearch) { - this.sx = sx; - this.sy = sy; - - this.maxsearch = maxsearch; - - Step step = new Step(null,dx,dy); - open.add(step); - - return processNodes(); - } - - /** - * Process the nodes of the search graph - * - * @return The path found or null if no path could be found - */ - private Path processNodes() { - Step step = findBest(); - - while (!step.is(sx,sy)) { - for (int x=-1;x<2;x++) { - for (int y=-1;y<2;y++) { - if ((x != 0) || (y != 0)) { - int xp = step.x + x; - int yp = step.y + y; - - if (!checkDiaganolBlock(step.x,step.y,x,y)) { - if (validNode(xp,yp)) { - open.add(new Step(step,xp,yp)); - } - } - } - } - } - - step = findBest(); - if (step == null) { - return null; - } - - if (callback != null) { - distance[step.x+(step.y*map.getMapWidth())] = 1; - callback.fireNode(step.x,step.y); - } - } - - Path path = new Path(); - - while (step.parent != null) { - path.addPoint(step.x,step.y); - step = step.parent; - } - path.addPoint(step.x,step.y); - - System.out.println(); - for(int y=0; y 9) || (best.depth > maxsearch)) { - open.remove(best); - return findBest(); - } - - return best; - } - - /** - * Evaluate the heuristic for a particular node - * - * @param x The x position of the node to evaluate - * @param y The y position of the node to evaluate - * @return The heuristic for the specified node - */ - @SuppressWarnings("unused") - private int evalH(int x,int y) { - return Math.abs(sx-x) + Math.abs(sy-y); - } - - /** - * A step on the search path - * - * @author Kevin Glass - */ - private class Step implements Comparable { - /** The x position of this step */ - public int x; - /** The y position of this step */ - public int y; - /** The heuristic for this step's node */ - public int h; - /** The number of times this step has been used as a source */ - public int count; - /** The depth of this search */ - public int depth; - /** The parent step this step was spawned from */ - public Step parent; - - /** - * Create a new step - * - * @param parent The step we came from - * @param x The x position we've moved to - * @param y The y position we've moved to - */ - public Step(Step parent,int x,int y) { - this.x = x; - this.y = y; - this.parent = parent; - if (parent != null) { - depth = parent.depth+1; - } else { - depth = 0; - } - - h = Math.abs(sx-x) + Math.abs(sy-y); - - if (depth <= maxsearch) { - distance[x+(y*map.getMapWidth())] = 2; - } - } - - /** - * Like an equals method, only for the data inside - * - * @param x The x position to check against - * @param y The y position to check against - * @return True if this steps position is that specified - */ - public boolean is(int x,int y) { - return (x == this.x) && (y == this.y); - } - - /** - * @see java.lang.Comparable#compareTo(java.lang.Object) - */ - public int compareTo(Object o) { - return h - ((Step) o).h; - } - } - - - /** - * Check if a particular location on the map blocks movement - * - * @param x The x location to check - * @param y The y location to check - * @return True if the locaiton is blocked - */ - public boolean isBlocked(int x,int y) { - if(x == sx && y == sy){ - return false; - } - if (map.isBlocked(x,y)) { - return true; - } - - return set.blocksMovement(map.getTileAt(x,y,0)); - } -} diff --git a/src/ei/game/algo/AStarPathfinder.java b/src/ei/game/algo/AStarPathfinder2.java similarity index 100% rename from src/ei/game/algo/AStarPathfinder.java rename to src/ei/game/algo/AStarPathfinder2.java