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