mirror of https://github.com/Minestom/Minestom.git
Small cleanup
This commit is contained in:
parent
48cf157370
commit
5be7c3a85a
|
@ -2,14 +2,13 @@ package net.minestom.server.entity.pathfinding;
|
|||
|
||||
import net.minestom.server.attribute.Attribute;
|
||||
import net.minestom.server.collision.CollisionUtils;
|
||||
import net.minestom.server.collision.PhysicsResult;
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.coordinate.Vec;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.LivingEntity;
|
||||
import net.minestom.server.particle.Particle;
|
||||
import net.minestom.server.particle.ParticleCreator;
|
||||
import net.minestom.server.utils.PacketUtils;
|
||||
import net.minestom.server.utils.position.PositionUtils;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -39,9 +38,9 @@ public final class Navigator {
|
|||
final Pos position = entity.getPosition();
|
||||
|
||||
// Find the direction
|
||||
double dx = direction.x() - position.x();
|
||||
double dy = direction.y() - position.y();
|
||||
double dz = direction.z() - position.z();
|
||||
final double dx = direction.x() - position.x();
|
||||
final double dy = direction.y() - position.y();
|
||||
final double dz = direction.z() - position.z();
|
||||
|
||||
// the purpose of these few lines is to slow down entities when they reach their destination
|
||||
double distSquared = dx * dx + dy * dy + dz * dz;
|
||||
|
@ -49,23 +48,23 @@ public final class Navigator {
|
|||
speed = distSquared;
|
||||
}
|
||||
// Find the movement speed
|
||||
double radians = Math.atan2(dz, dx);
|
||||
double speedX = Math.cos(radians) * speed;
|
||||
double speedY = dy * speed;
|
||||
double speedZ = Math.sin(radians) * speed;
|
||||
final double radians = Math.atan2(dz, dx);
|
||||
final double speedX = Math.cos(radians) * speed;
|
||||
final double speedY = dy * speed;
|
||||
final double speedZ = Math.sin(radians) * speed;
|
||||
|
||||
// Now calculate the new yaw/pitch
|
||||
float oldYaw = position.yaw();
|
||||
float oldPitch = position.pitch();
|
||||
float newYaw = PositionUtils.getLookYaw(dx, dz);
|
||||
float newPitch = PositionUtils.getLookPitch(dx, dy, dz);
|
||||
final float oldYaw = position.yaw();
|
||||
final float oldPitch = position.pitch();
|
||||
final float newYaw = PositionUtils.getLookYaw(dx, dz);
|
||||
final float newPitch = PositionUtils.getLookPitch(dx, dy, dz);
|
||||
|
||||
// Average the pitch and yaw to avoid jittering
|
||||
float yaw = PositionUtils.averageYaw(PositionUtils.averageYaw(oldYaw, newYaw), oldYaw);
|
||||
float pitch = PositionUtils.averagePitch(PositionUtils.averagePitch(oldPitch, newPitch), oldPitch);
|
||||
final float yaw = PositionUtils.averageYaw(PositionUtils.averageYaw(oldYaw, newYaw), oldYaw);
|
||||
final float pitch = PositionUtils.averagePitch(PositionUtils.averagePitch(oldPitch, newPitch), oldPitch);
|
||||
|
||||
// Prevent ghosting, and refresh position
|
||||
final var physicsResult = CollisionUtils.handlePhysics(entity, new Vec(speedX, speedY, speedZ));
|
||||
final PhysicsResult physicsResult = CollisionUtils.handlePhysics(entity, new Vec(speedX, speedY, speedZ));
|
||||
this.entity.refreshPosition(physicsResult.newPosition().withView(yaw, pitch));
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ import java.util.List;
|
|||
public interface Pathfinder {
|
||||
/**
|
||||
* This query will return the next point to reach the target, from the current position.
|
||||
*
|
||||
* @param currentPoint the current position
|
||||
* @return the next point to reach the target, or null if a path does not currently exist
|
||||
*/
|
||||
|
@ -24,12 +25,14 @@ public interface Pathfinder {
|
|||
|
||||
/**
|
||||
* This method will update the path to the given target, starting a new path if none exists.
|
||||
*
|
||||
* @param target the new target, or null to cancel the path
|
||||
*/
|
||||
void updatePath(@Nullable Point target);
|
||||
|
||||
/**
|
||||
* This method will force the path to the given target.
|
||||
*
|
||||
* @param target the new target
|
||||
* @return the list of points to reach the target
|
||||
*/
|
||||
|
|
|
@ -6,7 +6,6 @@ import net.minestom.server.entity.Entity;
|
|||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.particle.Particle;
|
||||
import net.minestom.server.utils.block.BlockIterator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
@ -16,7 +15,7 @@ import java.util.*;
|
|||
* A simplified, synchronized A* pathfinder.
|
||||
* This pathfinder is focussed on stability, not performance.
|
||||
* <p>
|
||||
* If you would like to limit the entity to a
|
||||
* If you would like to limit the entity to a
|
||||
* </p>
|
||||
*/
|
||||
final class PathfinderImpl implements Pathfinder {
|
||||
|
@ -32,9 +31,10 @@ final class PathfinderImpl implements Pathfinder {
|
|||
|
||||
/**
|
||||
* Creates an instance of this pathfinder.
|
||||
* @param entity the entity to find a path for
|
||||
*
|
||||
* @param entity the entity to find a path for
|
||||
* @param blocked the predicate to check if a block is blocked, {@link BlockedPredicate#BLOCK_SOLID_BLOCKS} if null
|
||||
* @param cost the cost function to calculate the cost of a block, {@link CostFunction#BLOCK_SPEED_FACTOR} if null
|
||||
* @param cost the cost function to calculate the cost of a block, {@link CostFunction#BLOCK_SPEED_FACTOR} if null
|
||||
*/
|
||||
PathfinderImpl(@NotNull Entity entity, @Nullable BlockedPredicate blocked, @Nullable CostFunction cost) {
|
||||
this.entity = entity;
|
||||
|
@ -81,8 +81,8 @@ final class PathfinderImpl implements Pathfinder {
|
|||
@Nullable List<Point> findPath(Point start, Point goal) {
|
||||
// The step is half of the lowest dimension of the entity's bounding box
|
||||
// This is used so that the entity will never be able to skip over any point in the path
|
||||
BoundingBox box = entity.getBoundingBox();
|
||||
double step = Math.min(box.width(), Math.min(box.height(), box.depth())) / 2;
|
||||
final BoundingBox box = entity.getBoundingBox();
|
||||
final double step = Math.min(box.width(), Math.min(box.height(), box.depth())) / 2;
|
||||
|
||||
// The distance cost is the distance between the current point and the goal, plus the cost of the current point
|
||||
Comparator<Point> distanceCost = Comparator.comparingDouble(p ->
|
||||
|
@ -150,7 +150,7 @@ final class PathfinderImpl implements Pathfinder {
|
|||
}
|
||||
|
||||
private static Point[] neighbors(Point point, double step) {
|
||||
return new Point[] {
|
||||
return new Point[]{
|
||||
// Direct neighbors
|
||||
point.add(step, 0, 0),
|
||||
point.add(-step, 0, 0),
|
||||
|
@ -181,7 +181,7 @@ final class PathfinderImpl implements Pathfinder {
|
|||
};
|
||||
}
|
||||
|
||||
public interface BlockedPredicate {
|
||||
interface BlockedPredicate {
|
||||
/**
|
||||
* A predicate used as default that blocks movement if any of the blocks are solid.
|
||||
*/
|
||||
|
@ -193,15 +193,17 @@ final class PathfinderImpl implements Pathfinder {
|
|||
|
||||
/**
|
||||
* Returns true if the given entity cannot move between the two points, false otherwise.
|
||||
*
|
||||
* @param entity The entity to check.
|
||||
* @param from The starting point.
|
||||
* @param to The ending point.
|
||||
* @param from The starting point.
|
||||
* @param to The ending point.
|
||||
* @return True if the entity cannot move between the two points, false otherwise.
|
||||
*/
|
||||
boolean test(@NotNull Entity entity, @NotNull Point from, @NotNull Point to);
|
||||
|
||||
/**
|
||||
* Combines this predicate with another one.
|
||||
*
|
||||
* @param other The other predicate.
|
||||
* @return A new predicate that returns true if either this or the other predicate returns true.
|
||||
*/
|
||||
|
@ -210,7 +212,7 @@ final class PathfinderImpl implements Pathfinder {
|
|||
}
|
||||
}
|
||||
|
||||
public interface CostFunction {
|
||||
interface CostFunction {
|
||||
/**
|
||||
* A cost function used as default that returns the block speed factor
|
||||
*/
|
||||
|
@ -229,14 +231,16 @@ final class PathfinderImpl implements Pathfinder {
|
|||
|
||||
/**
|
||||
* Returns the cost of moving from one point to another.
|
||||
*
|
||||
* @param from The starting point.
|
||||
* @param to The ending point.
|
||||
* @param to The ending point.
|
||||
* @return The cost of moving from one point to another.
|
||||
*/
|
||||
double getCost(@NotNull Entity entity, @NotNull Point from, @NotNull Point to);
|
||||
|
||||
/**
|
||||
* Combines this cost function with another cost function.
|
||||
*
|
||||
* @param other The other cost function.
|
||||
* @return A new cost function that combines this cost function with the other cost function.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue