diff --git a/com/watabou/glwrap/Matrix.java b/com/watabou/glwrap/Matrix.java index 7b97f89..8d049cd 100644 --- a/com/watabou/glwrap/Matrix.java +++ b/com/watabou/glwrap/Matrix.java @@ -20,40 +20,46 @@ public class Matrix { public static final float G2RAD = 0.01745329251994329576923690768489f; - - public static float[] clone( float[] m ) { - + + public static float[] clone(float[] m) { + int n = m.length; float[] res = new float[n]; do { res[--n] = m[n]; } while (n > 0); - + return res; } - - public static void copy( float[] src, float[] dst ) { - + + public static void copy(float[] src, float[] dst) { + int n = src.length; do { dst[--n] = src[n]; } while (n > 0); } - - public static void setIdentity( float[] m ) { - for (int i=0 ; i < 16 ; i++) { - m[i] = 0f; - } - for (int i = 0; i < 16; i += 5) { + + public static void setIdentity(float[] m) { + for (int i = 0; i < 15; i += 5) { + m[i] = 1f; + + m[i + 1] = 0f; + m[i + 2] = 0f; + m[i + 3] = 0f; + m[i + 4] = 0f; + } + + m[15] = 1f; } - public static void rotate( float[] m, float a ) { + public static void rotate(float[] m, float a) { a *= G2RAD; - float sin = (float)Math.sin( a ); - float cos = (float)Math.cos( a ); + float sin = (float) Math.sin(a); + float cos = (float) Math.cos(a); float m0 = m[0]; float m1 = m[1]; float m4 = m[4]; @@ -62,21 +68,21 @@ public static void rotate( float[] m, float a ) { m[1] = m1 * cos + m5 * sin; m[4] = -m0 * sin + m4 * cos; m[5] = -m1 * sin + m5 * cos; - } - - public static void skewX( float[] m, float a ) { - double t = Math.tan( a * G2RAD ); + } + + public static void skewX(float[] m, float a) { + double t = Math.tan(a * G2RAD); m[4] += -m[0] * t; m[5] += -m[1] * t; - } - - public static void skewY( float[] m, float a ) { - double t = Math.tan( a * G2RAD ); + } + + public static void skewY(float[] m, float a) { + double t = Math.tan(a * G2RAD); m[0] += m[4] * t; m[1] += m[5] * t; - } - - public static void scale( float[] m, float x, float y ) { + } + + public static void scale(float[] m, float x, float y) { m[0] *= x; m[1] *= x; m[2] *= x; @@ -85,15 +91,15 @@ public static void scale( float[] m, float x, float y ) { m[5] *= y; m[6] *= y; m[7] *= y; - // android.opengl.Matrix.scaleM( m, 0, x, y, 1 ); + // android.opengl.Matrix.scaleM( m, 0, x, y, 1 ); } - - public static void translate( float[] m, float x, float y ) { + + public static void translate(float[] m, float x, float y) { m[12] += m[0] * x + m[4] * y; m[13] += m[1] * x + m[5] * y; } - - public static void multiply( float[] left, float right[], float[] result ) { - android.opengl.Matrix.multiplyMM( result, 0, left, 0, right, 0 ); + + public static void multiply(float[] left, float right[], float[] result) { + android.opengl.Matrix.multiplyMM(result, 0, left, 0, right, 0); } } \ No newline at end of file diff --git a/com/watabou/utils/PathFinder.java b/com/watabou/utils/PathFinder.java index d585247..e0db0f3 100644 --- a/com/watabou/utils/PathFinder.java +++ b/com/watabou/utils/PathFinder.java @@ -21,76 +21,77 @@ import java.util.LinkedList; public class PathFinder { - + public static int[] distance; - + private static boolean[] goals; private static int[] queue; - + private static int size = 0; private static int[] dir; - - public static void setMapSize( int width, int height ) { - + + public static void setMapSize(int width, int height) { + int size = width * height; - + if (PathFinder.size != size) { - + PathFinder.size = size; distance = new int[size]; goals = new boolean[size]; queue = new int[size]; - - dir = new int[]{-1, +1, -width, +width, -width-1, -width+1, +width-1, +width+1}; + + dir = new int[] { -1, +1, -width, +width, -width - 1, -width + 1, +width - 1, +width + 1 }; } } - - public static Path find( int from, int to, boolean[] passable ) { - if (!buildDistanceMap( from, to, passable )) { + public static Path find(int from, int to, boolean[] passable) { + + if (!buildDistanceMap(from, to, passable)) { return null; } - + Path result = new Path(); int s = from; - // From the starting position we are moving downwards, + // From the starting position we are moving downwards, // until we reach the ending point do { int minD = distance[s]; int mins = s; - - for (int i=0; i < dir.length; i++) { - - int n = s + dir[i]; - - int thisD = distance[n]; + int n = 0; + int thisD = 0; + for (int i = 0; i < dir.length; i++) { + + n = s + dir[i]; + + thisD = distance[n]; if (thisD < minD) { minD = thisD; mins = n; } } s = mins; - result.add( s ); + result.add(s); } while (s != to); - + return result; } - - public static int getStep( int from, int to, boolean[] passable ) { - - if (!buildDistanceMap( from, to, passable )) { + + public static int getStep(int from, int to, boolean[] passable) { + + if (!buildDistanceMap(from, to, passable)) { return -1; } - + // From the starting position we are making one step downwards int minD = distance[from]; int best = from; - + int step, stepD; - - for (int i=0; i < dir.length; i++) { + + for (int i = 0; i < dir.length; i++) { if ((stepD = distance[step = from + dir[i]]) < minD) { minD = stepD; @@ -100,28 +101,28 @@ public static int getStep( int from, int to, boolean[] passable ) { return best; } - - public static int getStepBack( int cur, int from, boolean[] passable ) { - int d = buildEscapeDistanceMap( cur, from, 2f, passable ); - for (int i=0; i < size; i++) { - goals[i] = distance[i] == d; + public static int getStepBack(int cur, int from, boolean[] passable) { + + int d = buildEscapeDistanceMap(cur, from, 2f, passable); + for (int i = 0; i < size; i++) { + goals[i] = distance[i] == d; } - if (!buildDistanceMap( cur, goals, passable )) { + if (!buildDistanceMap(cur, goals, passable)) { return -1; } int s = cur; - + // From the starting position we are making one step downwards int minD = distance[s]; int mins = s; - - for (int i=0; i < dir.length; i++) { + + for (int i = 0; i < dir.length; i++) { int n = s + dir[i]; int thisD = distance[n]; - + if (thisD < minD) { minD = thisD; mins = n; @@ -130,26 +131,26 @@ public static int getStepBack( int cur, int from, boolean[] passable ) { return mins; } - - private static boolean buildDistanceMap( int from, int to, boolean[] passable ) { - + + private static boolean buildDistanceMap(int from, int to, boolean[] passable) { + if (from == to) { return false; } - - Arrays.fill( distance, Integer.MAX_VALUE ); - + + Arrays.fill(distance, Integer.MAX_VALUE); + boolean pathFound = false; - + int head = 0; int tail = 0; - + // Add to queue queue[tail++] = to; distance[to] = 0; - + while (head < tail) { - + // Remove from queue int step = queue[head++]; if (step == from) { @@ -157,8 +158,8 @@ private static boolean buildDistanceMap( int from, int to, boolean[] passable ) break; } int nextDistance = distance[step] + 1; - - for (int i=0; i < dir.length; i++) { + + for (int i = 0; i < dir.length; i++) { int n = step + dir[i]; if (n == from || (n >= 0 && n < size && passable[n] && (distance[n] > nextDistance))) { @@ -166,35 +167,35 @@ private static boolean buildDistanceMap( int from, int to, boolean[] passable ) queue[tail++] = n; distance[n] = nextDistance; } - + } } - + return pathFound; } - - public static void buildDistanceMap( int to, boolean[] passable, int limit ) { - - Arrays.fill( distance, Integer.MAX_VALUE ); - + + public static void buildDistanceMap(int to, boolean[] passable, int limit) { + + Arrays.fill(distance, Integer.MAX_VALUE); + int head = 0; int tail = 0; - + // Add to queue queue[tail++] = to; distance[to] = 0; - + while (head < tail) { - + // Remove from queue int step = queue[head++]; - + int nextDistance = distance[step] + 1; if (nextDistance > limit) { return; } - - for (int i=0; i < dir.length; i++) { + + for (int i = 0; i < dir.length; i++) { int n = step + dir[i]; if (n >= 0 && n < size && passable[n] && (distance[n] > nextDistance)) { @@ -202,34 +203,34 @@ public static void buildDistanceMap( int to, boolean[] passable, int limit ) { queue[tail++] = n; distance[n] = nextDistance; } - + } } } - - private static boolean buildDistanceMap( int from, boolean[] to, boolean[] passable ) { - + + private static boolean buildDistanceMap(int from, boolean[] to, boolean[] passable) { + if (to[from]) { return false; } - - Arrays.fill( distance, Integer.MAX_VALUE ); - + + Arrays.fill(distance, Integer.MAX_VALUE); + boolean pathFound = false; - + int head = 0; int tail = 0; - + // Add to queue - for (int i=0; i < size; i++) { + for (int i = 0; i < size; i++) { if (to[i]) { queue[tail++] = i; distance[i] = 0; } } - + while (head < tail) { - + // Remove from queue int step = queue[head++]; if (step == from) { @@ -237,8 +238,8 @@ private static boolean buildDistanceMap( int from, boolean[] to, boolean[] passa break; } int nextDistance = distance[step] + 1; - - for (int i=0; i < dir.length; i++) { + + for (int i = 0; i < dir.length; i++) { int n = step + dir[i]; if (n == from || (n >= 0 && n < size && passable[n] && (distance[n] > nextDistance))) { @@ -246,45 +247,45 @@ private static boolean buildDistanceMap( int from, boolean[] to, boolean[] passa queue[tail++] = n; distance[n] = nextDistance; } - + } } - + return pathFound; } - - private static int buildEscapeDistanceMap( int cur, int from, float factor, boolean[] passable ) { - - Arrays.fill( distance, Integer.MAX_VALUE ); - + + private static int buildEscapeDistanceMap(int cur, int from, float factor, boolean[] passable) { + + Arrays.fill(distance, Integer.MAX_VALUE); + int destDist = Integer.MAX_VALUE; - + int head = 0; int tail = 0; - + // Add to queue queue[tail++] = from; distance[from] = 0; - + int dist = 0; - + while (head < tail) { - + // Remove from queue int step = queue[head++]; dist = distance[step]; - + if (dist > destDist) { return destDist; } - + if (step == cur) { - destDist = (int)(dist * factor) + 1; + destDist = (int) (dist * factor) + 1; } - + int nextDistance = dist + 1; - - for (int i=0; i < dir.length; i++) { + + for (int i = 0; i < dir.length; i++) { int n = step + dir[i]; if (n >= 0 && n < size && passable[n] && distance[n] > nextDistance) { @@ -292,32 +293,32 @@ private static int buildEscapeDistanceMap( int cur, int from, float factor, bool queue[tail++] = n; distance[n] = nextDistance; } - + } } - + return dist; } - + @SuppressWarnings("unused") - private static void buildDistanceMap( int to, boolean[] passable ) { - - Arrays.fill( distance, Integer.MAX_VALUE ); - + private static void buildDistanceMap(int to, boolean[] passable) { + + Arrays.fill(distance, Integer.MAX_VALUE); + int head = 0; int tail = 0; - + // Add to queue queue[tail++] = to; distance[to] = 0; - + while (head < tail) { - + // Remove from queue int step = queue[head++]; int nextDistance = distance[step] + 1; - - for (int i=0; i < dir.length; i++) { + + for (int i = 0; i < dir.length; i++) { int n = step + dir[i]; if (n >= 0 && n < size && passable[n] && (distance[n] > nextDistance)) { @@ -325,11 +326,11 @@ private static void buildDistanceMap( int to, boolean[] passable ) { queue[tail++] = n; distance[n] = nextDistance; } - + } } } - + @SuppressWarnings("serial") public static class Path extends LinkedList { }