From 3941aa3fe4b1dcfa7cd1a1413868286a7b895d2c Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Wed, 24 Aug 2016 14:50:15 -0400 Subject: [PATCH] v0.4.2: array managment optimizations to Pathfinder --- .../java/com/watabou/utils/PathFinder.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 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 50e0e173c..741f3b98a 100644 --- a/SPD-classes/src/main/java/com/watabou/utils/PathFinder.java +++ b/SPD-classes/src/main/java/com/watabou/utils/PathFinder.java @@ -27,6 +27,7 @@ import java.util.LinkedList; public class PathFinder { public static int[] distance; + private static int[] maxVal; private static boolean[] goals; private static int[] queue; @@ -49,13 +50,20 @@ public class PathFinder { goals = new boolean[size]; queue = new int[size]; + maxVal = new int[size]; + Arrays.fill(maxVal, Integer.MAX_VALUE); + 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}; } - + + //TODO currently this isn't used, and all pathfinding is recomputed each step. + // Computing each step is performance expensive, but pre-computing a path leads to incorrect + // pathing in cases where passable changes. Need to look into a compromise, something that's + // correct but is less costly public static Path find( int from, int to, boolean[] passable ) { if (!buildDistanceMap( from, to, passable )) { @@ -146,8 +154,8 @@ public class PathFinder { if (from == to) { return false; } - - Arrays.fill( distance, Integer.MAX_VALUE ); + + System.arraycopy(maxVal, 0, distance, 0, maxVal.length); boolean pathFound = false; @@ -185,7 +193,7 @@ public class PathFinder { public static void buildDistanceMap( int to, boolean[] passable, int limit ) { - Arrays.fill( distance, Integer.MAX_VALUE ); + System.arraycopy(maxVal, 0, distance, 0, maxVal.length); int head = 0; int tail = 0; @@ -223,7 +231,7 @@ public class PathFinder { return false; } - Arrays.fill( distance, Integer.MAX_VALUE ); + System.arraycopy(maxVal, 0, distance, 0, maxVal.length); boolean pathFound = false; @@ -265,7 +273,7 @@ public class PathFinder { private static int buildEscapeDistanceMap( int cur, int from, float factor, boolean[] passable ) { - Arrays.fill( distance, Integer.MAX_VALUE ); + System.arraycopy(maxVal, 0, distance, 0, maxVal.length); int destDist = Integer.MAX_VALUE; @@ -312,7 +320,7 @@ public class PathFinder { @SuppressWarnings("unused") private static void buildDistanceMap( int to, boolean[] passable ) { - Arrays.fill( distance, Integer.MAX_VALUE ); + System.arraycopy(maxVal, 0, distance, 0, maxVal.length); int head = 0; int tail = 0;