Skip to content

Commit 3d3ce9c

Browse files
committed
Fix: Search too far
1 parent 31f4677 commit 3d3ce9c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

common/src/main/kotlin/com/lambda/util/world/EntityUtils.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ object EntityUtils {
2222
range: Double = 6.0,
2323
noinline predicate: (T) -> Boolean = { true },
2424
): T? {
25-
2625
// Speculative execution trolling
2726
val entities =
2827
if (range > 64) getEntities(predicate)
28+
// I have an idea for optimization.
29+
//
30+
// Since the search operates linearly, eventually it will reach the midpoint.
31+
// Calculate the distance between the first and last entities.
32+
// Obtain the delta value.
33+
// Theoretically, the closest entity should be within a cubic space of delta^3 blocks.
34+
// If there are no entities within this delta box, examine the outer box. (Although this is unlikely given the fact that the closest entity is within the delta box.)
35+
// The performance improvement is relative to the initial state.
2936
else getFastEntities(pos, range, predicate)
3037

3138
return entities.minByOrNull { it.squaredDistanceTo(pos) }
@@ -79,7 +86,7 @@ object EntityUtils {
7986
for (z in sectionZ - chunks..sectionZ + chunks) {
8087
val section = world.entityManager.cache.findTrackingSection(ChunkSectionPos.asLong(x, y, z)) ?: continue
8188
section.collection.filterIsInstanceTo(entities) { entity ->
82-
entity != player && predicate(entity)
89+
entity != player && entity.squaredDistanceTo(pos) <= distance * distance && predicate(entity)
8390
}
8491
}
8592
}

0 commit comments

Comments
 (0)