v0.4.2: improvements to pathfinding array managment

This commit is contained in:
Evan Debenham 2016-08-27 17:08:09 -04:00 committed by Evan Debenham
parent c23a284423
commit 5d9035e9d9
4 changed files with 35 additions and 6 deletions

View File

@ -693,13 +693,42 @@ public class Dungeon {
GameScene.afterObserve();
}
public static int findPath( Char ch, int from, int to, boolean pass[], boolean[] visible ) {
//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 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 {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {