I have this game: http://www.java-gaming.org/topics/iconified/26136/view.html
…and it seems to clog up the FPS, once I make the map bigger, but worst is it when I begin to use pathfinding with just six entities.
Problem is, I dont know exactly where it happens. It could be my heuristic, it could be too many pathfinding checks - I cant tell.
Is there a way to check what code executes slower/where in the code there might be a bottleneck?
Also, I know my getNearestNeighbor function sucks, and takes like 4 times more heuristic calls than it needs to.
/**
* Returns the path the nearest neighbor.
* @param sx Source x coord
* @param sy Source y coord
* @param tx Target x coord
* @param ty Target y coord
* @param map Map we're moving around in
* @return The coords of the nearest neighbor
*/
public Path nearestNeighbor(Mover mover, int sx, int sy, int tx, int ty) {
Path closest = null;
int stepAmount = Integer.MAX_VALUE;
for (int x=-1;x<2;x++) {
for (int y=-1;y<2;y++) {
if ((x == 0) && (y == 0)) { // it's not a neighbour
continue;
}
if ((x != 0) && (y != 0)) { // no diagonal movement
continue;
}
// determine the location of the neighbour and evaluate it
int xp = x + tx;
int yp = y + ty;
if (isValidLocation(mover, sx, sy, xp, yp)) {
Path path = findPath(mover, sx, sy, xp, yp);
if (path != null) { // If there was not a path, continue on, adventurer!
int steps = path.getLength();
if (steps < stepAmount) { // If it has less steps there than anything before
closest = findPath(mover, sx, sy, xp, yp);
stepAmount = steps;
}
}
}
}
} // We've run out of search folks!
if (closest != null) { // Something was found!
return closest;
} else { // Nothing was found!
return null;
}
}
I’m rendering a lot of images too, from a lot of different sheets. Should I pack them all on one sheet, so I can call drawEmbedded in Slick?
Does that matter a lot at all? When I look at my map, I only get 45 fps/60 fps.
Should I use multithreading for this heavy logic? Help!
I can upload source if you want to check yourself
This was my old heuristic:
float dx = tx - x;
float dy = ty - y;
float result = (float) (Math.sqrt((dx*dx)+(dy*dy)));
…and I cant notice the difference with this one:
float dx = Math.abs(tx - x);
float dy = Math.abs(ty - y);
float result = dx+dy;