From 5d9035e9d908fb4766e78e9dd86e96b25dd97dbf Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sat, 27 Aug 2016 17:08:09 -0400 Subject: [PATCH] v0.4.2: improvements to pathfinding array managment --- .../shatteredpixeldungeon/Dungeon.java | 35 +++++++++++++++++-- .../actors/hero/Hero.java | 2 +- .../actors/mobs/Mob.java | 2 +- .../actors/mobs/Piranha.java | 2 +- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java index b728e6f63..99a819c28 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java @@ -692,14 +692,43 @@ public class Dungeon { GameScene.afterObserve(); } + + //we store this to avoid having to re-allocate the array with each pathfind + private static boolean[] passable; + + private static void setupPassable(){ + if (passable == null || passable.length != Dungeon.level.length()) + passable = new boolean[Dungeon.level.length()]; + else + BArray.setFalse(passable); + } + + public static PathFinder.Path findPath(Char ch, int from, int to, boolean pass[], boolean[] visible ) { + + setupPassable(); + if (ch.flying || ch.buff( Amok.class ) != null) { + BArray.or( pass, Level.avoid, passable ); + } else { + System.arraycopy( pass, 0, passable, 0, Dungeon.level.length() ); + } + + for (Char c : Actor.chars()) { + if (visible[c.pos]) { + passable[c.pos] = false; + } + } + + return PathFinder.find( from, to, passable ); + + } - public static int findPath( Char ch, int from, int to, boolean pass[], boolean[] visible ) { + public static int findStep(Char ch, int from, int to, boolean pass[], boolean[] visible ) { if (level.adjacent( from, to )) { return Actor.findChar( to ) == null && (pass[to] || Level.avoid[to]) ? to : -1; } - boolean[] passable = new boolean[Dungeon.level.length()]; + setupPassable(); if (ch.flying || ch.buff( Amok.class ) != null) { BArray.or( pass, Level.avoid, passable ); } else { @@ -718,7 +747,7 @@ public class Dungeon { public static int flee( Char ch, int cur, int from, boolean pass[], boolean[] visible ) { - boolean[] passable = new boolean[Dungeon.level.length()]; + setupPassable(); if (ch.flying) { BArray.or( pass, Level.avoid, passable ); } else { 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 4ef625335..4914ac6c3 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 @@ -1048,7 +1048,7 @@ public class Hero extends Char { passable[i] = p[i] && (v[i] || m[i]); } - step = Dungeon.findPath( this, pos, target, passable, Level.fieldOfView ); + step = Dungeon.findStep( this, pos, target, passable, Level.fieldOfView ); } if (step != -1) { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java index 666cd3baf..fd6839420 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java @@ -294,7 +294,7 @@ public abstract class Mob extends Char { return false; } - int step = Dungeon.findPath( this, pos, target, + int step = Dungeon.findStep( this, pos, target, Level.passable, Level.fieldOfView ); if (step != -1) { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Piranha.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Piranha.java index 0666ccc66..00e18ad6f 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Piranha.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Piranha.java @@ -118,7 +118,7 @@ public class Piranha extends Mob { return false; } - int step = Dungeon.findPath( this, pos, target, + int step = Dungeon.findStep( this, pos, target, Level.water, Level.fieldOfView ); if (step != -1) {