From f508b94e9d95ea570bfb5997f6282f480893fff3 Mon Sep 17 00:00:00 2001 From: Ruling-0 <47913168+Ruling-0@users.noreply.github.com> Date: Sun, 11 Jan 2026 22:43:03 -0600 Subject: [PATCH] Add ability to check 4D map using outside range (#255) --- .../space/ArrayProximityMap4D.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/space/ArrayProximityMap4D.java b/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/space/ArrayProximityMap4D.java index 5fbc508f..a83df2f4 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/space/ArrayProximityMap4D.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/space/ArrayProximityMap4D.java @@ -106,6 +106,20 @@ public class ArrayProximityMap4D { } } + public void forEachInRange(int dim, double x, double y, double z, double range, Consumer action) { + final DimensionData dimData = getDataForDim(dim); + if (dimData == null) { + return; + } + if (shape == VolumeShape.SPHERE) { + dimData.forEachInSphere(x, y, z, range, action); + } else if (shape == VolumeShape.CUBE) { + dimData.forEachInCube(x, y, z, range, action); + } else { + throw new IllegalArgumentException("Invalid shape"); + } + } + @SuppressWarnings("ForLoopReplaceableByForEach") public int size() { int size = 0; @@ -311,6 +325,22 @@ public class ArrayProximityMap4D { } } + @SuppressWarnings("unchecked") + public void forEachInSphere(double x, double y, double z, double radius, Consumer action) { + final int maxIndex = size * 4; + final int[] a = data; + final double rSq = radius * radius; + for (int i = 0; i < maxIndex; i += 4) { + final double dx = x + 0.5D - a[i]; + final double dy = y + 0.5D - a[i + 1]; + final double dz = z + 0.5D - a[i + 2]; + final double distSq = dx * dx + dy * dy + dz * dz; + if (distSq < rSq) { + action.accept((T) objects[i / 4]); + } + } + } + @SuppressWarnings("unchecked") public void forEachInCube(double x, double y, double z, Consumer action) { final int maxIndex = size * 4; @@ -330,6 +360,24 @@ public class ArrayProximityMap4D { } } + @SuppressWarnings("unchecked") + public void forEachInCube(double x, double y, double z, double radius, Consumer action) { + final int maxIndex = size * 4; + final int[] a = data; + final double centerX = x + 0.5D; + final double centerY = y + 0.5D; + final double centerZ = z + 0.5D; + for (int i = 0; i < maxIndex; i += 4) { + if (centerX - radius < a[i] && a[i] < centerX + radius + && centerY - radius < a[i + 1] + && a[i + 1] < centerY + radius + && centerZ - radius < a[i + 2] + && a[i + 2] < centerZ + radius) { + action.accept((T) objects[i / 4]); + } + } + } + public boolean isEmpty() { return size == 0; }