From 837f7d6b5f960d7c81c6d251c3100a49dcf9eba4 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Mon, 20 Mar 2023 19:52:13 -0500 Subject: [PATCH] QuadTree refactoring and fixes --- .../core/util/objects/quadTree/QuadTree.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) 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 7f0891d22..c92326bae 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 @@ -101,7 +101,11 @@ public class QuadTree else { // TODO give the min and max allowed positions - throw new IndexOutOfBoundsException("QuadTree GetOrSet failed. Position out of bounds, given Position: "+pos); + int width = this.widthInBlocks()/2; + + DhBlockPos2D minPos = this.getCenterBlockPos().add(new DhBlockPos2D(-width, -width)); + DhBlockPos2D maxPos =this.getCenterBlockPos().add(new DhBlockPos2D(width, width)); + throw new IndexOutOfBoundsException("QuadTree GetOrSet failed. Position out of bounds, min pos: "+minPos+", max pos: "+maxPos+", given Position: "+pos); } } @@ -118,8 +122,8 @@ public class QuadTree int minZ = this.centerBlockPos.z - halfWidthInBlocks; int maxZ = this.centerBlockPos.z + halfWidthInBlocks; - return minX <= blockPos.sectionX && blockPos.sectionX < maxX && - minZ <= blockPos.sectionZ && blockPos.sectionZ < maxZ; + return minX <= blockPos.sectionX && blockPos.sectionX <= maxX && + minZ <= blockPos.sectionZ && blockPos.sectionZ <= maxZ; } @@ -151,7 +155,7 @@ public class QuadTree //================// public void setCenterBlockPos(DhBlockPos2D newCenterPos) { this.setCenterBlockPos(newCenterPos, null); } - public void setCenterBlockPos(DhBlockPos2D newCenterPos, Consumer> removedItemConsumer) + public void setCenterBlockPos(DhBlockPos2D newCenterPos, Consumer removedItemConsumer) { this.centerBlockPos = newCenterPos; @@ -161,7 +165,13 @@ public class QuadTree if (!this.topRingList.getCenter().equals(expectedCenterPos)) { - this.topRingList.moveTo(expectedCenterPos.x, expectedCenterPos.y, removedItemConsumer); + this.topRingList.moveTo(expectedCenterPos.x, expectedCenterPos.y, (quadNode) -> + { + if (quadNode != null && removedItemConsumer != null) + { + removedItemConsumer.accept(quadNode.value); + } + }); } } @@ -191,7 +201,8 @@ public class QuadTree return count.get(); } - public int width() { return this.topRingList.getWidth(); } + public int ringListWidth() { return this.topRingList.getWidth(); } + public int widthInBlocks() { return this.ringListWidth() * BitShiftUtil.powerOfTwo(this.treeMaxDetailLevel); } // public String getDebugString() // {