v0.4.2: array access and general performance improvements

This commit is contained in:
Evan Debenham 2016-08-27 15:25:39 -04:00 committed by Evan Debenham
parent e87ce8949e
commit c23a284423
3 changed files with 30 additions and 33 deletions

View File

@ -37,6 +37,7 @@ public class PathFinder {
private static int[] dir;
//performance-light shortcuts for some common pathfinder cases
//they are in array-access order for increased memory performance
public static int[] NEIGHBOURS4;
public static int[] NEIGHBOURS8;
public static int[] NEIGHBOURS9;
@ -55,9 +56,9 @@ public class PathFinder {
dir = new int[]{-1, +1, -width, +width, -width-1, -width+1, +width-1, +width+1};
NEIGHBOURS4 = new int[]{-width, +1, +width, -1};
NEIGHBOURS8 = new int[]{-width, +1-width, +1, +1+width, +width, -1+width, -1, -1-width};
NEIGHBOURS9 = new int[]{0, -width, +1-width, +1, +1+width, +width, -1+width, -1, -1-width};
NEIGHBOURS4 = new int[]{-width, -1, +1, +width};
NEIGHBOURS8 = new int[]{-width-1, -width, -width+1, -1, +1, +width-1, +width, +width+1};
NEIGHBOURS9 = new int[]{-width-1, -width, -width+1, -1, 0, +1, +width-1, +width, +width+1};
}
//TODO currently this isn't used, and all pathfinding is recomputed each step.

View File

@ -159,6 +159,10 @@ public class Hero extends Char {
public int exp = 0;
private ArrayList<Mob> visibleEnemies;
//This list is maintained so that some logic checks can be skipped
// for enemies we know we aren't seeing normally, resultign in better performance
public ArrayList<Mob> mindVisionEnemies = new ArrayList<>();
public Hero() {
super();
@ -975,7 +979,7 @@ public class Hero extends Char {
newMob = true;
}
if (QuickSlotButton.autoAim(m) != -1){
if (!mindVisionEnemies.contains(m) && QuickSlotButton.autoAim(m) != -1){
if (target == null){
target = m;
} else if (distance(target) > distance(m)) {
@ -995,7 +999,7 @@ public class Hero extends Char {
interrupt();
resting = false;
}
visibleEnemies = visible;
}

View File

@ -607,10 +607,13 @@ public abstract class Level implements Bundlable {
}
}
//FIXME this is a temporary fix here to avoid changing the tiles texture
//This logic will be changed in 0.4.3 anyway
private static int[] N4Indicies = new int[]{0, 2, 3, 1};
private int getWaterTile( int pos ) {
int t = Terrain.WATER_TILES;
for (int j=0; j < PathFinder.NEIGHBOURS4.length; j++) {
if ((Terrain.flags[map[pos + PathFinder.NEIGHBOURS4[j]]] & Terrain.UNSTITCHABLE) != 0) {
if ((Terrain.flags[map[pos + PathFinder.NEIGHBOURS4[N4Indicies[j]]]] & Terrain.UNSTITCHABLE) != 0) {
t += 1 << j;
}
}
@ -942,47 +945,36 @@ public abstract class Level implements Bundlable {
//Currently only the hero can get mind vision or awareness
if (c.isAlive() && c == Dungeon.hero) {
Dungeon.hero.mindVisionEnemies.clear();
if (c.buff( MindVision.class ) != null) {
for (Mob mob : mobs) {
int p = mob.pos;
fieldOfView[p] = true;
fieldOfView[p + 1] = true;
fieldOfView[p - 1] = true;
fieldOfView[p + width() + 1] = true;
fieldOfView[p + width() - 1] = true;
fieldOfView[p - width() + 1] = true;
fieldOfView[p - width() - 1] = true;
fieldOfView[p + width()] = true;
fieldOfView[p - width()] = true;
if (!fieldOfView[p]){
Dungeon.hero.mindVisionEnemies.add(mob);
}
for (int i : PathFinder.NEIGHBOURS9)
fieldOfView[p+i] = true;
}
} else if (((Hero)c).heroClass == HeroClass.HUNTRESS) {
for (Mob mob : mobs) {
int p = mob.pos;
if (distance( c.pos, p) == 2) {
fieldOfView[p] = true;
fieldOfView[p + 1] = true;
fieldOfView[p - 1] = true;
fieldOfView[p + width() + 1] = true;
fieldOfView[p + width() - 1] = true;
fieldOfView[p - width() + 1] = true;
fieldOfView[p - width() - 1] = true;
fieldOfView[p + width()] = true;
fieldOfView[p - width()] = true;
if (!fieldOfView[p]){
Dungeon.hero.mindVisionEnemies.add(mob);
}
for (int i : PathFinder.NEIGHBOURS9)
fieldOfView[p+i] = true;
}
}
}
if (c.buff( Awareness.class ) != null) {
for (Heap heap : heaps.values()) {
int p = heap.pos;
fieldOfView[p] = true;
fieldOfView[p + 1] = true;
fieldOfView[p - 1] = true;
fieldOfView[p + width() + 1] = true;
fieldOfView[p + width() - 1] = true;
fieldOfView[p - width() + 1] = true;
fieldOfView[p - width() - 1] = true;
fieldOfView[p + width()] = true;
fieldOfView[p - width()] = true;
for (int i : PathFinder.NEIGHBOURS9)
fieldOfView[p+i] = true;
}
}
}