diff --git a/api/src/main/java/com/seibel/lod/core/util/MathUtil.java b/api/src/main/java/com/seibel/lod/core/util/MathUtil.java index 1f4df9af3..9ccdb1b47 100644 --- a/api/src/main/java/com/seibel/lod/core/util/MathUtil.java +++ b/api/src/main/java/com/seibel/lod/core/util/MathUtil.java @@ -45,4 +45,11 @@ public class MathUtil public static long pow2(long x) { return x * x; } + /** Equivalent to Log_2(numb) */ + public static int log2(int numb) + { + // properties of logs allow us to use the base Log_e() method + return (int)(Math.log(numb) / Math.log(2)); + } + } diff --git a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java index 2b407d70b..e22e0da12 100644 --- a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java +++ b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java @@ -8,6 +8,7 @@ import com.seibel.lod.core.pos.Pos2D; import com.seibel.lod.core.util.BitShiftUtil; import com.seibel.lod.core.util.DetailDistanceUtil; import com.seibel.lod.core.util.LodUtil; +import com.seibel.lod.core.util.MathUtil; import com.seibel.lod.core.util.gridList.MovableGridRingList; import org.apache.logging.log4j.Logger; @@ -48,8 +49,9 @@ public class QuadTree this.centerBlockPos = centerBlockPos; this.widthInBlocks = widthInBlocks; - this.treeMaxDetailLevel = 10; // TODO in the future we may need to make this dynamic // detail 10 = (2^10) 1024 blocks wide this.treeMinDetailLevel = treeMinDetailLevel; + // the max detail level must be greater than 0 (to prevent divide by 0 errors) and greater than the minimum detail level + this.treeMaxDetailLevel = (byte) Math.max(Math.max(1, this.treeMinDetailLevel), MathUtil.log2(widthInBlocks)); int halfSizeInRootNodes = Math.floorDiv(this.widthInBlocks, 2) / BitShiftUtil.powerOfTwo(this.treeMaxDetailLevel); halfSizeInRootNodes = halfSizeInRootNodes + 1; // always add 1 so nodes will always have a parent, even if the tree's center is offset from the root node grid