diff --git a/api/src/main/java/com/seibel/lod/core/util/BitShiftUtil.java b/api/src/main/java/com/seibel/lod/core/util/BitShiftUtil.java index 4dde773e0..38011672a 100644 --- a/api/src/main/java/com/seibel/lod/core/util/BitShiftUtil.java +++ b/api/src/main/java/com/seibel/lod/core/util/BitShiftUtil.java @@ -32,4 +32,24 @@ public class BitShiftUtil */ public static int half(int value) { return value >> 1; } + /** + * Equivalent to:
+ * value << 1,
+ * value^2,
+ * Math.pow(value, 2)

+ * + * Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same. + */ + public static int square(int value) { return value << 1; } + + /** + * Equivalent to:
+ * value << power,
+ * value^power,
+ * Math.pow(value, power)

+ * + * Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same. + */ + public static int pow(int value, int power) { return value << power; } + } diff --git a/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java b/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java index 966e93988..bcbf7ab63 100644 --- a/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java +++ b/core/src/main/java/com/seibel/lod/core/pos/DhLodPos.java @@ -86,7 +86,7 @@ public class DhLodPos implements Comparable this.z * 2 + BitShiftUtil.half(child0to3 & 2)); } /** Returns this position's child index in its parent */ - public int getChildIndexOfParent() { return (this.x & 1) + ((this.z & 1) << 1); } + public int getChildIndexOfParent() { return (this.x & 1) + BitShiftUtil.square(this.z & 1); } public boolean overlaps(DhLodPos other) { diff --git a/core/src/main/java/com/seibel/lod/core/pos/DhLodUnit.java b/core/src/main/java/com/seibel/lod/core/pos/DhLodUnit.java index b7dd65cd8..c177b268f 100644 --- a/core/src/main/java/com/seibel/lod/core/pos/DhLodUnit.java +++ b/core/src/main/java/com/seibel/lod/core/pos/DhLodUnit.java @@ -25,7 +25,7 @@ public class DhLodUnit /** @return the size of this LOD unit in Minecraft blocks */ - public int toBlockWidth() { return this.numberOfLodSectionsWide << this.detailLevel; } + public int toBlockWidth() { return BitShiftUtil.pow(this.numberOfLodSectionsWide, this.detailLevel); } /** @return the LOD Unit relative to the given block width and detail level */ public static DhLodUnit fromBlockWidth(int blockWidth, byte targetDetailLevel) { return new DhLodUnit(targetDetailLevel, Math.floorDiv(blockWidth, BitShiftUtil.powerOfTwo(targetDetailLevel))); } diff --git a/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java b/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java index 6bf0b412f..5e283adc9 100644 --- a/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java +++ b/core/src/main/java/com/seibel/lod/core/pos/DhSectionPos.java @@ -92,7 +92,7 @@ public class DhSectionPos this.sectionZ * 2 + BitShiftUtil.half(child0to3 & 2)); } /** Returns this position's child index in its parent */ - public int getChildIndexOfParent() { return (this.sectionX & 1) + ((this.sectionZ & 1) << 1); } + public int getChildIndexOfParent() { return (this.sectionX & 1) + BitShiftUtil.square(this.sectionZ & 1); } /** Applies the given consumer to all 4 of this position's children. */ public void forEachChild(Consumer callback)