hollow-cube/distance-squared-optimization

* Use distance squared where possible

* Update EntityFinder.java

* Update FollowTargetGoal.java

* Consistent methods for squared

(cherry picked from commit 4e0bc47429)
This commit is contained in:
emortaldev 2023-05-06 19:31:14 +01:00 committed by Matt Worzala
parent 8335afdf47
commit 8a6a8f0050
7 changed files with 19 additions and 15 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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());