added CompactedCoords util
This commit is contained in:
23
core/src/main/java/mc/core/utils/CompactedCoords.java
Normal file
23
core/src/main/java/mc/core/utils/CompactedCoords.java
Normal 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)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
45
core/src/test/java/mc/core/utils/TestCompactedCoords.java
Normal file
45
core/src/test/java/mc/core/utils/TestCompactedCoords.java
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user