2007-04-16 21:47:07 +00:00
|
|
|
package ei.game.algo;
|
|
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
import ei.engine.math.Vector2i;
|
2007-04-18 09:04:08 +00:00
|
|
|
import ei.engine.util.MultiPrintStream;
|
2007-04-16 21:47:07 +00:00
|
|
|
import ei.game.gamestate.InGameState;
|
|
|
|
|
|
|
|
|
|
public class AStar{
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
private AStarPathfinder pathfinder;
|
|
|
|
|
|
|
|
|
|
private int width;
|
|
|
|
|
private int hight;
|
|
|
|
|
private AStarNode[][] map;
|
|
|
|
|
|
|
|
|
|
protected boolean randomizeMap = true;
|
|
|
|
|
protected Random RAND = new Random(0);
|
|
|
|
|
|
|
|
|
|
public AStar() {
|
|
|
|
|
width = InGameState.getMap().getSize().getX();
|
|
|
|
|
hight = InGameState.getMap().getSize().getY();
|
|
|
|
|
initializePathfinder();
|
|
|
|
|
initializeMap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<AStarNode> startSearch(Vector2i start, Vector2i goal){
|
2007-04-19 12:45:54 +00:00
|
|
|
map[goal.getX()][goal.getY()].setBlocked(false);
|
2007-04-16 21:47:07 +00:00
|
|
|
return pathfinder.findPath(map[start.getX()][start.getY()], map[goal.getX()][goal.getY()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void initializePathfinder() {
|
|
|
|
|
System.out.println("Initializing pathfinder");
|
|
|
|
|
|
|
|
|
|
// Create the pathfinder.
|
|
|
|
|
pathfinder = new AStarPathfinder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void initializeMap() {
|
|
|
|
|
System.out.println("Initializing map");
|
|
|
|
|
// Create the map.
|
|
|
|
|
map = new AStarNode2D[width][hight];
|
2007-04-18 09:04:08 +00:00
|
|
|
for(int y = 0, nodeId = 0; hight > y; y++) {
|
|
|
|
|
MultiPrintStream.out.println();
|
|
|
|
|
for(int x = 0; width > x; x++, nodeId++) {
|
|
|
|
|
map[x][y] = new AStarNode2D(x, y, nodeId);
|
|
|
|
|
|
|
|
|
|
if(!InGameState.getMap().isPosEmpty(x, y)) {
|
|
|
|
|
map[x][y].setBlocked(true);
|
|
|
|
|
MultiPrintStream.out.print(1);
|
|
|
|
|
}
|
|
|
|
|
else MultiPrintStream.out.print(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-18 21:19:46 +00:00
|
|
|
MultiPrintStream.out.println();
|
2007-04-18 09:04:08 +00:00
|
|
|
/*
|
2007-04-16 21:47:07 +00:00
|
|
|
for(int x = 0, nodeId = 0; width > x; x++) {
|
|
|
|
|
for(int y = 0; hight > y; y++, nodeId++) {
|
|
|
|
|
map[x][y] = new AStarNode2D(x, y, nodeId);
|
2007-04-17 15:55:22 +00:00
|
|
|
|
2007-04-16 21:47:07 +00:00
|
|
|
if(!InGameState.getMap().isPosEmpty(x, y)) {
|
|
|
|
|
map[x][y].setBlocked(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-18 09:04:08 +00:00
|
|
|
*/
|
2007-04-16 21:47:07 +00:00
|
|
|
|
|
|
|
|
// Create the neighbours.
|
|
|
|
|
for(int x = 0; width > x; x++) {
|
|
|
|
|
for(int y = 0; hight > y; y++) {
|
|
|
|
|
|
|
|
|
|
List<AStarNeighbour> neighbours = new LinkedList<AStarNeighbour>();
|
|
|
|
|
|
|
|
|
|
if(y-1 >= 0)
|
2007-04-17 15:55:22 +00:00
|
|
|
neighbours.add(new AStarNeighbour(map[x][y-1], AStarNeighbour.Location.Diagonal)); // North.
|
2007-04-16 21:47:07 +00:00
|
|
|
if(x+1 < width && y-1 > 0)
|
2007-04-17 15:55:22 +00:00
|
|
|
neighbours.add(new AStarNeighbour(map[x+1][y-1], AStarNeighbour.Location.Adjacent)); // North-East.
|
2007-04-16 21:47:07 +00:00
|
|
|
if(x+1 < width)
|
2007-04-17 15:55:22 +00:00
|
|
|
neighbours.add(new AStarNeighbour(map[x+1][y], AStarNeighbour.Location.Diagonal)); // East.
|
2007-04-16 21:47:07 +00:00
|
|
|
if(x+1 < width && y+1 < hight)
|
2007-04-17 15:55:22 +00:00
|
|
|
neighbours.add(new AStarNeighbour(map[x+1][y+1], AStarNeighbour.Location.Adjacent)); // South-East.
|
2007-04-16 21:47:07 +00:00
|
|
|
if(y+1 < hight)
|
2007-04-17 15:55:22 +00:00
|
|
|
neighbours.add(new AStarNeighbour(map[x][y+1], AStarNeighbour.Location.Diagonal)); // South.
|
2007-04-16 21:47:07 +00:00
|
|
|
if(x-1 >= 0 && y+1 < hight)
|
2007-04-17 15:55:22 +00:00
|
|
|
neighbours.add(new AStarNeighbour(map[x-1][y+1], AStarNeighbour.Location.Adjacent)); // South-West.
|
2007-04-16 21:47:07 +00:00
|
|
|
if(x-1 >= 0)
|
2007-04-17 15:55:22 +00:00
|
|
|
neighbours.add(new AStarNeighbour(map[x-1][y], AStarNeighbour.Location.Diagonal)); // West.
|
|
|
|
|
if(x-1 >= 0 && y-1 > 0)
|
|
|
|
|
neighbours.add(new AStarNeighbour(map[x-1][y-1], AStarNeighbour.Location.Adjacent)); // North-West.
|
2007-04-16 21:47:07 +00:00
|
|
|
|
|
|
|
|
map[x][y].setNeighbours(neighbours);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|