mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
hollow-cube/distance-squared-optimization
* Use distance squared where possible * Update EntityFinder.java * Update FollowTargetGoal.java * Consistent methods for squared
This commit is contained in:
parent
c8362a7d2c
commit
4e0bc47429
@ -1001,6 +1001,10 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
||||
return getDistance(entity.getPosition());
|
||||
}
|
||||
|
||||
public double getDistanceSquared(@NotNull Point point) {
|
||||
return getPosition().distanceSquared(point);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance squared between two entities.
|
||||
*
|
||||
@ -1717,7 +1721,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
||||
|
||||
Optional<Entity> nearby = instance.getNearbyEntities(position, range).stream()
|
||||
.filter(finalPredicate)
|
||||
.min(Comparator.comparingDouble(e -> e.getDistance(this.position)));
|
||||
.min(Comparator.comparingDouble(e -> e.getDistanceSquared(this)));
|
||||
|
||||
return nearby.orElse(null);
|
||||
}
|
||||
|
@ -99,10 +99,10 @@ public class ExperienceOrb extends Entity {
|
||||
Player closest = entity.getInstance()
|
||||
.getPlayers()
|
||||
.stream()
|
||||
.min(Comparator.comparingDouble(a -> a.getDistance(entity)))
|
||||
.min(Comparator.comparingDouble(a -> a.getDistanceSquared(entity)))
|
||||
.orElse(null);
|
||||
if (closest == null) return null;
|
||||
if (closest.getDistance(entity) > maxDistance) return null;
|
||||
if (closest.getDistanceSquared(entity) > maxDistance * maxDistance) return null;
|
||||
return closest;
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ public class ItemEntity extends Entity {
|
||||
EntityTracker.Target.ITEMS, itemEntity -> {
|
||||
if (itemEntity == this) return;
|
||||
if (!itemEntity.isPickable() || !itemEntity.isMergeable()) return;
|
||||
if (getDistance(itemEntity) > mergeRange) return;
|
||||
if (getDistanceSquared(itemEntity) > mergeRange * mergeRange) return;
|
||||
|
||||
final ItemStack itemStackEntity = itemEntity.getItemStack();
|
||||
final StackingRule stackingRule = StackingRule.get();
|
||||
|
@ -34,7 +34,7 @@ public class FollowTargetGoal extends GoalSelector {
|
||||
Entity target = entityCreature.getTarget();
|
||||
if (target == null) target = findTarget();
|
||||
if (target == null) return false;
|
||||
final boolean result = target.getPosition().distance(entityCreature.getPosition()) >= 2;
|
||||
final boolean result = target.getPosition().distanceSquared(entityCreature.getPosition()) >= 2 * 2;
|
||||
if (result) {
|
||||
this.target = target;
|
||||
}
|
||||
@ -54,7 +54,7 @@ public class FollowTargetGoal extends GoalSelector {
|
||||
this.entityCreature.setTarget(target);
|
||||
Navigator navigator = entityCreature.getNavigator();
|
||||
this.lastTargetPos = target.getPosition();
|
||||
if (lastTargetPos.distance(entityCreature.getPosition()) < 2) {
|
||||
if (lastTargetPos.distanceSquared(entityCreature.getPosition()) < 2 * 2) {
|
||||
// Target is too far
|
||||
this.forceEnd = true;
|
||||
navigator.setPathTo(null);
|
||||
@ -88,7 +88,7 @@ public class FollowTargetGoal extends GoalSelector {
|
||||
return forceEnd ||
|
||||
target == null ||
|
||||
target.isRemoved() ||
|
||||
target.getPosition().distance(entityCreature.getPosition()) < 2;
|
||||
target.getPosition().distanceSquared(entityCreature.getPosition()) < 2 * 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -80,7 +80,7 @@ public class MeleeAttackGoal extends GoalSelector {
|
||||
if (!stop) {
|
||||
|
||||
// Attack the target entity
|
||||
if (entityCreature.getDistance(target) <= range) {
|
||||
if (entityCreature.getDistanceSquared(target) <= range * range) {
|
||||
entityCreature.lookAt(target);
|
||||
if (!Cooldown.hasCooldown(time, lastHit, delay)) {
|
||||
entityCreature.attack(target, true);
|
||||
|
@ -32,6 +32,6 @@ public class LastEntityDamagerTarget extends TargetSelector {
|
||||
return null;
|
||||
}
|
||||
// Check range
|
||||
return entityCreature.getDistance(entity) < range ? entity : null;
|
||||
return entityCreature.getDistanceSquared(entity) < range * range ? entity : null;
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ public class EntityFinder {
|
||||
final int minDistance = distance.getMinimum();
|
||||
final int maxDistance = distance.getMaximum();
|
||||
result = result.stream()
|
||||
.filter(entity -> MathUtils.isBetween(entity.getDistance(pos), minDistance, maxDistance))
|
||||
.filter(entity -> MathUtils.isBetween(entity.getPosition().distanceSquared(pos), minDistance * minDistance, maxDistance * maxDistance))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@ -223,11 +223,11 @@ public class EntityFinder {
|
||||
case ARBITRARY, RANDOM ->
|
||||
// RANDOM is handled below
|
||||
1;
|
||||
case FURTHEST -> pos.distance(ent1.getPosition()) >
|
||||
pos.distance(ent2.getPosition()) ?
|
||||
case FURTHEST -> pos.distanceSquared(ent1.getPosition()) >
|
||||
pos.distanceSquared(ent2.getPosition()) ?
|
||||
1 : 0;
|
||||
case NEAREST -> pos.distance(ent1.getPosition()) <
|
||||
pos.distance(ent2.getPosition()) ?
|
||||
case NEAREST -> pos.distanceSquared(ent1.getPosition()) <
|
||||
pos.distanceSquared(ent2.getPosition()) ?
|
||||
1 : 0;
|
||||
})
|
||||
.limit(limit != null ? limit : Integer.MAX_VALUE)
|
||||
@ -311,7 +311,7 @@ public class EntityFinder {
|
||||
instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers();
|
||||
if (targetSelector == TargetSelector.NEAREST_PLAYER) {
|
||||
return players.stream()
|
||||
.min(Comparator.comparingDouble(p -> p.getPosition().distance(startPosition)))
|
||||
.min(Comparator.comparingDouble(p -> p.getPosition().distanceSquared(startPosition)))
|
||||
.<List<Entity>>map(Collections::singletonList).orElse(List.of());
|
||||
} else if (targetSelector == TargetSelector.RANDOM_PLAYER) {
|
||||
final int index = ThreadLocalRandom.current().nextInt(players.size());
|
||||
|
Loading…
Reference in New Issue
Block a user