From c23a284423e0a492b0a0e031057a5f4e240082bf Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sat, 27 Aug 2016 15:25:39 -0400 Subject: [PATCH] v0.4.2: array access and general performance improvements --- .../java/com/watabou/utils/PathFinder.java | 7 +-- .../actors/hero/Hero.java | 8 +++- .../shatteredpixeldungeon/levels/Level.java | 48 ++++++++----------- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/SPD-classes/src/main/java/com/watabou/utils/PathFinder.java b/SPD-classes/src/main/java/com/watabou/utils/PathFinder.java index 741f3b98a..843bd2d15 100644 --- a/SPD-classes/src/main/java/com/watabou/utils/PathFinder.java +++ b/SPD-classes/src/main/java/com/watabou/utils/PathFinder.java @@ -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. diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java index 656575b41..4ef625335 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java @@ -159,6 +159,10 @@ public class Hero extends Char { public int exp = 0; private ArrayList 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 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; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java index 92fd5b814..c95ada2d0 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java @@ -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; } } }