Archived
0

added CompactedCoords util

This commit is contained in:
2018-08-12 23:37:48 +03:00
parent b8a71c070c
commit d4785cda14
2 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package mc.core.utils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CompactedCoords {
public static int compressXZ(int x, int z) {
if (x < Short.MIN_VALUE || x > Short.MAX_VALUE ||
z < Short.MIN_VALUE || z > Short.MAX_VALUE) {
log.warn("Coord over range: [{},{}]", x, z);
}
return ((x & 0xFFFF) << 16) | (z & 0xFFFF);
}
public static int[] uncompressXZ(int compactValue) {
//TODO не нравится мне такие костыли
return new int[]{
(int)(short) (compactValue >> 16),
(int)(short) (compactValue | 0xFFFF0000)
};
}
}

View File

@@ -0,0 +1,45 @@
package mc.core.utils;
import org.junit.Assert;
import org.junit.Test;
import java.util.Random;
public class TestCompactedCoords {
@Test
public void testSimple() {
for (int z = -100; z <= 100; z++) {
for (int x = -100; x <= 100; x++) {
int compressXZ = CompactedCoords.compressXZ(x, z);
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
Assert.assertEquals(x, xz[0]);
Assert.assertEquals(z, xz[1]);
}
}
}
@Test
public void testRandom() {
Random random = new Random();
int x,z;
for (int i = 0; i < 100; i++) {
do {
x = random.nextInt();
} while (x < Short.MIN_VALUE || x > Short.MAX_VALUE);
do {
z = random.nextInt();
} while (z < Short.MIN_VALUE || z > Short.MAX_VALUE);
int compressXZ = CompactedCoords.compressXZ(x, z);
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
Assert.assertEquals(x, xz[0]);
Assert.assertEquals(z, xz[1]);
}
}
}