Skip to content

Commit 404d8af

Browse files
committed
Optimize POI retrieval when first point meets conditions
This helps portal search and sometimes also lightning rod search
1 parent 70c7731 commit 404d8af

File tree

3 files changed

+153
-69
lines changed

3 files changed

+153
-69
lines changed

common/src/main/java/net/caffeinemc/mods/lithium/common/util/Distances.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class Distances {
77

8-
public static double getMinChunkToBlockDistanceL2Sq(BlockPos origin, int chunkX, int chunkZ) {
8+
public static long getMinChunkToBlockDistanceL2Sq(BlockPos origin, int chunkX, int chunkZ) {
99
int chunkMinX = SectionPos.sectionToBlockCoord(chunkX);
1010
int chunkMinZ = SectionPos.sectionToBlockCoord(chunkZ);
1111

@@ -18,7 +18,7 @@ public static double getMinChunkToBlockDistanceL2Sq(BlockPos origin, int chunkX,
1818
zDistance = Math.max(0, zDistance - 15);
1919
}
2020

21-
return xDistance * xDistance + zDistance * zDistance;
21+
return (long) xDistance * (long) xDistance + (long) zDistance * (long) zDistance;
2222
}
2323

2424
public static boolean isWithinSquareRadius(BlockPos origin, int radius, BlockPos pos) {
@@ -35,12 +35,27 @@ public static int getClosestBlockCoordInSection(int blockCoord, int sectionCoord
3535
return Math.min(Math.max(blockCoord, minBlockInSection), minBlockInSection + 15);
3636
}
3737

38-
public static double getMinSectionDistanceSq(BlockPos origin, int chunkX, int chunkY, int chunkZ) {
39-
final int originX = origin.getX(), originY = origin.getY(), originZ = origin.getZ();
40-
final int distX = getClosestBlockCoordInSection(originX, chunkX) - originX;
41-
final int distY = getClosestBlockCoordInSection(originY, chunkY) - originY;
42-
final int distZ = getClosestBlockCoordInSection(originZ, chunkZ) - originZ;
38+
public static long getMinSectionDistanceSq(BlockPos origin, int chunkX, int chunkY, int chunkZ) {
39+
int originX = origin.getX(), originY = origin.getY(), originZ = origin.getZ();
40+
long distX = getClosestBlockCoordInSection(originX, chunkX) - originX;
41+
long distY = getClosestBlockCoordInSection(originY, chunkY) - originY;
42+
long distZ = getClosestBlockCoordInSection(originZ, chunkZ) - originZ;
4343

4444
return distX * distX + distY * distY + distZ * distZ;
4545
}
46+
47+
public static long distanceSq(BlockPos a, BlockPos b) {
48+
long dx = a.getX() - b.getX();
49+
long dy = a.getY() - b.getY();
50+
long dz = a.getZ() - b.getZ();
51+
return dx * dx + dy * dy + dz * dz;
52+
}
53+
54+
public static int distanceSqInt(BlockPos a, BlockPos b) {
55+
int dx = a.getX() - b.getX();
56+
int dy = a.getY() - b.getY();
57+
int dz = a.getZ() - b.getZ();
58+
// Check overflows to avoid silent incorrect results
59+
return Math.addExact(Math.addExact(Math.multiplyExact(dx, dx), Math.multiplyExact(dy, dy)), Math.multiplyExact(dz, dz));
60+
}
4661
}

common/src/main/java/net/caffeinemc/mods/lithium/common/util/tuples/SortedPointOfInterest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package net.caffeinemc.mods.lithium.common.util.tuples;
22

3+
import net.caffeinemc.mods.lithium.common.util.Distances;
34
import net.minecraft.core.BlockPos;
45
import net.minecraft.world.entity.ai.village.poi.PoiRecord;
56

6-
public record SortedPointOfInterest(PoiRecord poi, double distanceSq) {
7+
public record SortedPointOfInterest(PoiRecord poi, int distanceSq) {
78

89
public SortedPointOfInterest(PoiRecord poi, BlockPos origin) {
9-
this(poi, poi.getPos().distSqr(origin));
10+
this(poi, Distances.distanceSqInt(poi.getPos(), origin));
1011
}
1112

1213
public BlockPos getPos() {

0 commit comments

Comments
 (0)