@@ -42,15 +42,18 @@ object WorldUtils {
4242 /* *
4343 * Gets the closest entity of type [T] within a specified range.
4444 *
45+ * Because we don't want to troll the CPU speculative execution, we only use the [getFastEntities] function.
46+ * This should not be an issue as the performance of this function is optimized for small distances.
47+ *
4548 * @param pos The position to search from.
4649 * @param range The maximum distance to search for entities.
47- * @param predicate Optional predicate to filter entities.
50+ * @param predicate Predicate to filter entities.
4851 * @return The first entity of type [T] that is closest to the position within the specified range.
4952 */
5053 inline fun <reified T : Entity > SafeContext.getClosestEntity (
51- pos : Vec3d = player.pos ,
52- range : Double = 6.0 ,
53- noinline predicate : (T ) -> Boolean = { true },
54+ pos : Vec3d ,
55+ range : Double ,
56+ predicate : (T ) -> Boolean = { true },
5457 ): T ? {
5558 var closest: T ? = null
5659 var closestDistance = Double .MAX_VALUE
@@ -63,9 +66,7 @@ object WorldUtils {
6366 }
6467 }
6568
66- // Speculative execution trolling
67- if (range > 64 ) getEntities(null , comparator, predicate)
68- else getFastEntities(pos, range, null , comparator, predicate)
69+ getFastEntities(pos, range, null , comparator, predicate)
6970
7071 return closest
7172 }
@@ -95,17 +96,15 @@ object WorldUtils {
9596 * @param pos The position to search from.
9697 * @param distance The maximum distance to search for entities.
9798 * @param pointer The mutable list to store the entities in.
98- * @param predicate Optional predicate to filter entities. It allows custom filtering based on entity properties.
99- * @param iterator Optional iterator to perform operations on each entity.
100- * @return A list of entities of type [T] within the specified distance from the position, excluding the player.
101- *
99+ * @param iterator Iterator to perform operations on each entity.
100+ * @param predicate Predicate to filter entities.
102101 */
103102 inline fun <reified T : Entity > SafeContext.getFastEntities (
104103 pos : Vec3d ,
105104 distance : Double ,
106105 pointer : MutableList <T >? = null,
107- noinline iterator : (T ) -> Unit = { },
108- noinline predicate : (T ) -> Boolean = { true },
106+ iterator : (T ) -> Unit = {},
107+ predicate : (T ) -> Boolean = { true },
109108 ) {
110109 val chunks = ceil(distance / 16 ).toInt()
111110 val sectionX = pos.x.toInt() shr 4
@@ -118,9 +117,13 @@ object WorldUtils {
118117 for (x in sectionX - chunks.. sectionX + chunks) {
119118 for (y in sectionY - chunks.. sectionY + chunks) {
120119 for (z in sectionZ - chunks.. sectionZ + chunks) {
121- val section = world.entityManager.cache.findTrackingSection(ChunkSectionPos .asLong(x, y, z)) ? : continue
120+ val section =
121+ world.entityManager.cache.findTrackingSection(ChunkSectionPos .asLong(x, y, z)) ? : continue
122+
122123 section.collection.filterPointer(pointer, iterator) { entity ->
123- entity != player && entity.squaredDistanceTo(pos) <= distance * distance && predicate(entity)
124+ entity != player &&
125+ entity.squaredDistanceTo(pos) <= distance * distance &&
126+ predicate(entity)
124127 }
125128 }
126129 }
@@ -134,16 +137,20 @@ object WorldUtils {
134137 * [getFastEntities], it traverses all entities in the world to find matches, while also excluding the player entity.
135138 *
136139 * @param pointer The mutable list to store the entities in.
137- * @param predicate Optional predicate to filter entities. It allows custom filtering based on entity properties .
138- * @param iterator Optional iterator to perform operations on each entity .
140+ * @param iterator Iterator to perform operations on each entity .
141+ * @param predicate Predicate to filter entities .
139142 */
140143 inline fun <reified T : Entity > SafeContext.getEntities (
144+ pos : Vec3d ,
145+ distance : Double ,
141146 pointer : MutableList <T >? = null,
142- noinline iterator : (T ) -> Unit = { },
143- noinline predicate : (T ) -> Boolean = { true },
147+ iterator : (T ) -> Unit = {},
148+ predicate : (T ) -> Boolean = { true },
144149 ) {
145150 world.entities.filterPointer(pointer, iterator) { entity ->
146- entity != player && predicate(entity)
151+ entity != player &&
152+ entity.squaredDistanceTo(pos) <= distance * distance &&
153+ predicate(entity)
147154 }
148155 }
149156
@@ -155,18 +162,16 @@ object WorldUtils {
155162 * @param rangeY The maximum distance to search for entities in the y-axis.
156163 * @param rangeZ The maximum distance to search for entities in the z-axis.
157164 * @param pointer The mutable list to store the positions in.
158- * @param predicate Optional predicate to filter the blocks.
159- * @param iterator Optional iterator to perform operations on each block.
160- *
161- * @return A list of positions that match the predicate.
165+ * @param iterator Iterator to perform operations on each block.
166+ * @param predicate Predicate to filter the blocks.
162167 */
163- fun SafeContext.searchBlock (
168+ inline fun SafeContext.searchBlock (
164169 pos : Vec3i ,
165170 rangeX : Int ,
166171 rangeY : Int ,
167172 rangeZ : Int ,
168173 pointer : MutableList <Block >? = null,
169- iterator : (Block ) -> Unit = { },
174+ iterator : (Block ) -> Unit = {},
170175 predicate : (Block ) -> Boolean = { true },
171176 ) = searchBlock(pos, Vec3i (rangeX, rangeY, rangeZ), pointer, iterator, predicate)
172177
@@ -176,21 +181,20 @@ object WorldUtils {
176181 * @param pos The position to search from.
177182 * @param range The maximum distance to search for entities in each axis.
178183 * @param pointer The mutable list to store the positions in.
179- * @param predicate Optional predicate to filter the blocks.
180- * @param iterator Optional iterator to perform operations on each block.
181- *
182- * @return A list of positions that match the predicate.
184+ * @param iterator Iterator to perform operations on each block.
185+ * @param predicate Predicate to filter the blocks.
183186 */
184- fun SafeContext.searchBlock (
187+ inline fun SafeContext.searchBlock (
185188 pos : Vec3i ,
186189 range : Vec3i ,
187190 pointer : MutableList <Block >? = null,
188- iterator : (Block ) -> Unit = { },
191+ iterator : (Block ) -> Unit = {},
189192 predicate : (Block ) -> Boolean = { true },
190193 ) {
191194 // TODO: Implement O(1) pointer mapping
192195 BlockPos .iterateOutwards(BlockPos (pos), range.x, range.y, range.z)
193- .map { world.getBlockState(it).block }.filterPointer(pointer, iterator) { block ->
196+ .map { world.getBlockState(it).block }
197+ .filterPointer(pointer, iterator) { block ->
194198 predicate(block)
195199 }
196200 }
@@ -201,19 +205,20 @@ object WorldUtils {
201205 * @param pos The position to search from.
202206 * @param range The maximum distance to search for fluids in each axis.
203207 * @param pointer The mutable list to store the positions in.
204- * @param predicate Optional predicate to filter the fluids .
205- * @param iterator Optional iterator to perform operations on each fluid .
208+ * @param iterator Iterator to perform operations on each fluid .
209+ * @param predicate Predicate to filter the fluids .
206210 */
207211 inline fun <reified T : Fluid > SafeContext.searchFluid (
208212 pos : Vec3i ,
209213 range : Vec3i ,
210214 pointer : MutableList <T >? = null,
211- noinline predicate : (T ) -> Boolean = { true },
212- noinline iterator : (T ) -> Unit = { },
215+ iterator : (T ) -> Unit = {},
216+ predicate : (T ) -> Boolean = { true },
213217 ) {
214218 // TODO: Implement O(1) pointer mapping
215219 BlockPos .iterateOutwards(BlockPos (pos), range.x, range.y, range.z)
216- .map { world.getFluidState(it).fluid }.filterPointer(pointer, iterator) { fluid ->
220+ .map { world.getFluidState(it).fluid }
221+ .filterPointer(pointer, iterator) { fluid ->
217222 predicate(fluid)
218223 }
219224 }
0 commit comments