Skip to content

Commit bcd4d5a

Browse files
committed
Merge branch 'master' into feature/movement
2 parents 26f2169 + 9ad8e56 commit bcd4d5a

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

common/src/main/kotlin/com/lambda/util/combat/Explosion.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ object Explosion {
4747
* @param explosion The explosion to calculate the velocity for.
4848
* @return The velocity of the entities.
4949
*/
50-
fun SafeContext.velocity(explosion: Explosion) =
51-
getFastEntities<LivingEntity>(explosion.position, explosion.power * 2.0)
52-
.associateWith { entity -> velocity(entity, explosion) }
50+
fun SafeContext.velocity(explosion: Explosion): Map<LivingEntity, Vec3d> {
51+
val ref = ArrayList<LivingEntity>()
52+
getFastEntities<LivingEntity>(explosion.position, explosion.power * 2.0, ref)
53+
return ref.associateWith { entity -> velocity(entity, explosion) }
54+
}
5355

5456
/**
5557
* Calculates the velocity of a living entity affected by an explosion.

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ object EntityUtils {
2323
noinline predicate: (T) -> Boolean = { true },
2424
): T? {
2525
// Speculative execution trolling
26-
val entities =
27-
if (range > 64) getEntities(predicate)
26+
val entities = ArrayList<T>()
27+
if (range > 64) getEntities(entities, predicate)
2828
// I have an idea for optimization.
2929
//
3030
// Since the search operates linearly, eventually it will reach the midpoint.
@@ -33,7 +33,7 @@ object EntityUtils {
3333
// Theoretically, the closest entity should be within a cubic space of delta^3 blocks.
3434
// 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.)
3535
// The performance improvement is relative to the initial state.
36-
else getFastEntities(pos, range, predicate)
36+
else getFastEntities(pos, range, entities, predicate)
3737

3838
return entities.minByOrNull { it.squaredDistanceTo(pos) }
3939
}
@@ -69,30 +69,27 @@ object EntityUtils {
6969
inline fun <reified T : Entity> SafeContext.getFastEntities(
7070
pos: Vec3d,
7171
distance: Double,
72+
pointer: MutableList<T>,
7273
noinline predicate: (T) -> Boolean = { true },
73-
): List<T> {
74+
) {
7475
val chunks = ceil(distance / 16).toInt()
7576
val sectionX = pos.x.toInt() shr 4
7677
val sectionY = pos.y.toInt() shr 4
7778
val sectionZ = pos.z.toInt() shr 4
7879

79-
val entities = ArrayList<T>()
80-
8180
// Here we iterate over all sections within the specified distance and add all entities of type [T] to the list.
8281
// We do not have to worry about performance here, as the number of sections is very limited.
8382
// For example, if the player is on the edge of a section and the distance is 16, we only have to iterate over 9 sections.
8483
for (x in sectionX - chunks..sectionX + chunks) {
8584
for (y in sectionY - chunks..sectionY + chunks) {
8685
for (z in sectionZ - chunks..sectionZ + chunks) {
8786
val section = world.entityManager.cache.findTrackingSection(ChunkSectionPos.asLong(x, y, z)) ?: continue
88-
section.collection.filterIsInstanceTo(entities) { entity ->
87+
section.collection.filterIsInstanceTo(pointer) { entity ->
8988
entity != player && entity.squaredDistanceTo(pos) <= distance * distance && predicate(entity)
9089
}
9190
}
9291
}
9392
}
94-
95-
return entities
9693
}
9794

9895
/**
@@ -104,13 +101,12 @@ object EntityUtils {
104101
* @param predicate Optional predicate to filter entities.
105102
* @return A list of entities of type [T] within the specified distance from the position without the player.
106103
*/
107-
inline fun <reified T : Entity> SafeContext.getEntities(noinline predicate: (T) -> Boolean = { true }): List<T> {
108-
val entities = ArrayList<T>()
109-
110-
world.entities.filterIsInstanceTo(entities) { entity ->
104+
inline fun <reified T : Entity> SafeContext.getEntities(
105+
pointer: MutableList<T>,
106+
noinline predicate: (T) -> Boolean = { true }
107+
) {
108+
world.entities.filterIsInstanceTo(pointer) { entity ->
111109
entity != player && predicate(entity)
112110
}
113-
114-
return entities
115111
}
116112
}

0 commit comments

Comments
 (0)