mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-10 05:39:11 +01:00
Simplify path finding code, do not expose internal parts
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
619a9b3209
commit
be9b11f238
@ -1,7 +1,6 @@
|
|||||||
package net.minestom.server.entity;
|
package net.minestom.server.entity;
|
||||||
|
|
||||||
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
||||||
import net.minestom.server.attribute.Attribute;
|
|
||||||
import net.minestom.server.coordinate.Pos;
|
import net.minestom.server.coordinate.Pos;
|
||||||
import net.minestom.server.entity.ai.EntityAI;
|
import net.minestom.server.entity.ai.EntityAI;
|
||||||
import net.minestom.server.entity.ai.EntityAIGroup;
|
import net.minestom.server.entity.ai.EntityAIGroup;
|
||||||
@ -49,7 +48,7 @@ public class EntityCreature extends LivingEntity implements NavigableEntity, Ent
|
|||||||
aiTick(time);
|
aiTick(time);
|
||||||
|
|
||||||
// Path finding
|
// Path finding
|
||||||
this.navigator.tick(getAttributeValue(Attribute.MOVEMENT_SPEED));
|
this.navigator.tick();
|
||||||
|
|
||||||
// Fire, item pickup, ...
|
// Fire, item pickup, ...
|
||||||
super.update(time);
|
super.update(time);
|
||||||
|
@ -2,7 +2,6 @@ package net.minestom.server.entity.fakeplayer;
|
|||||||
|
|
||||||
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.attribute.Attribute;
|
|
||||||
import net.minestom.server.coordinate.Pos;
|
import net.minestom.server.coordinate.Pos;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.entity.pathfinding.NavigableEntity;
|
import net.minestom.server.entity.pathfinding.NavigableEntity;
|
||||||
@ -118,9 +117,8 @@ public class FakePlayer extends Player implements NavigableEntity {
|
|||||||
@Override
|
@Override
|
||||||
public void update(long time) {
|
public void update(long time) {
|
||||||
super.update(time);
|
super.update(time);
|
||||||
|
|
||||||
// Path finding
|
// Path finding
|
||||||
this.navigator.tick(getAttributeValue(Attribute.MOVEMENT_SPEED));
|
this.navigator.tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -14,6 +14,7 @@ import net.minestom.server.instance.Instance;
|
|||||||
import net.minestom.server.instance.WorldBorder;
|
import net.minestom.server.instance.WorldBorder;
|
||||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||||
import net.minestom.server.utils.position.PositionUtils;
|
import net.minestom.server.utils.position.PositionUtils;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -22,11 +23,9 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
/**
|
/**
|
||||||
* Necessary object for all {@link NavigableEntity}.
|
* Necessary object for all {@link NavigableEntity}.
|
||||||
*/
|
*/
|
||||||
public class Navigator {
|
public final class Navigator {
|
||||||
|
|
||||||
private final PFPathingEntity pathingEntity;
|
private final PFPathingEntity pathingEntity;
|
||||||
private HydrazinePathFinder pathFinder;
|
private HydrazinePathFinder pathFinder;
|
||||||
private IPath path;
|
|
||||||
private Point pathPosition;
|
private Point pathPosition;
|
||||||
|
|
||||||
private final Entity entity;
|
private final Entity entity;
|
||||||
@ -88,30 +87,24 @@ public class Navigator {
|
|||||||
// Tried to set path to the same target position
|
// Tried to set path to the same target position
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Instance instance = entity.getInstance();
|
final Instance instance = entity.getInstance();
|
||||||
|
|
||||||
if (pathFinder == null) {
|
if (pathFinder == null) {
|
||||||
// Unexpected error
|
// Unexpected error
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
this.pathFinder.reset();
|
||||||
pathFinder.reset();
|
|
||||||
if (point == null) {
|
if (point == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can't path with a null instance.
|
// Can't path with a null instance.
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can't path outside the world border
|
// Can't path outside the world border
|
||||||
final WorldBorder worldBorder = instance.getWorldBorder();
|
final WorldBorder worldBorder = instance.getWorldBorder();
|
||||||
if (!worldBorder.isInside(point)) {
|
if (!worldBorder.isInside(point)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can't path in an unloaded chunk
|
// Can't path in an unloaded chunk
|
||||||
final Chunk chunk = instance.getChunkAt(point);
|
final Chunk chunk = instance.getChunkAt(point);
|
||||||
if (!ChunkUtils.isLoaded(chunk)) {
|
if (!ChunkUtils.isLoaded(chunk)) {
|
||||||
@ -126,11 +119,9 @@ public class Navigator {
|
|||||||
point.y(),
|
point.y(),
|
||||||
point.z(),
|
point.z(),
|
||||||
pathOptions);
|
pathOptions);
|
||||||
this.path = path;
|
|
||||||
|
|
||||||
final boolean success = path != null;
|
final boolean success = path != null;
|
||||||
this.pathPosition = success ? point : null;
|
this.pathPosition = success ? point : null;
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,57 +132,16 @@ public class Navigator {
|
|||||||
return setPathTo(position, true);
|
return setPathTo(position, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void tick(float speed) {
|
@ApiStatus.Internal
|
||||||
// No pathfinding tick for dead entities
|
public synchronized void tick() {
|
||||||
|
if (pathPosition == null) return; // No path
|
||||||
if (entity instanceof LivingEntity && ((LivingEntity) entity).isDead())
|
if (entity instanceof LivingEntity && ((LivingEntity) entity).isDead())
|
||||||
return;
|
return; // No pathfinding tick for dead entities
|
||||||
|
if (pathFinder.updatePathFor(pathingEntity) == null) {
|
||||||
if (pathPosition != null) {
|
reset();
|
||||||
IPath path = pathFinder.updatePathFor(pathingEntity);
|
|
||||||
this.path = path;
|
|
||||||
|
|
||||||
if (path != null) {
|
|
||||||
final Point targetPosition = pathingEntity.getTargetPosition();
|
|
||||||
if (targetPosition != null) {
|
|
||||||
moveTowards(targetPosition, speed);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (pathPosition != null) {
|
|
||||||
this.pathPosition = null;
|
|
||||||
pathFinder.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the pathing entity.
|
|
||||||
* <p>
|
|
||||||
* Used by the pathfinder.
|
|
||||||
*
|
|
||||||
* @return the pathing entity
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public PFPathingEntity getPathingEntity() {
|
|
||||||
return pathingEntity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the assigned pathfinder.
|
|
||||||
* <p>
|
|
||||||
* Can be null if the navigable element hasn't been assigned to an {@link Instance} yet.
|
|
||||||
*
|
|
||||||
* @return the current pathfinder, null if none
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public HydrazinePathFinder getPathFinder() {
|
|
||||||
return pathFinder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPathFinder(@Nullable HydrazinePathFinder pathFinder) {
|
|
||||||
this.pathFinder = pathFinder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the target pathfinder position.
|
* Gets the target pathfinder position.
|
||||||
*
|
*
|
||||||
@ -201,28 +151,22 @@ public class Navigator {
|
|||||||
return pathPosition;
|
return pathPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public @NotNull Entity getEntity() {
|
||||||
* Changes the position this element is trying to reach.
|
|
||||||
*
|
|
||||||
* @param pathPosition the new current path position
|
|
||||||
* @deprecated Please use {@link #setPathTo(Point)}
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public void setPathPosition(@Nullable Point pathPosition) {
|
|
||||||
this.pathPosition = pathPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public IPath getPath() {
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPath(@Nullable IPath path) {
|
|
||||||
this.path = path;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public Entity getEntity() {
|
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
|
public @NotNull PFPathingEntity getPathingEntity() {
|
||||||
|
return pathingEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
|
public void setPathFinder(@Nullable HydrazinePathFinder pathFinder) {
|
||||||
|
this.pathFinder = pathFinder;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reset() {
|
||||||
|
this.pathPosition = null;
|
||||||
|
this.pathFinder.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,15 +9,15 @@ import net.minestom.server.coordinate.Point;
|
|||||||
import net.minestom.server.coordinate.Vec;
|
import net.minestom.server.coordinate.Vec;
|
||||||
import net.minestom.server.entity.Entity;
|
import net.minestom.server.entity.Entity;
|
||||||
import net.minestom.server.entity.LivingEntity;
|
import net.minestom.server.entity.LivingEntity;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class PFPathingEntity implements IPathingEntity {
|
@ApiStatus.Internal
|
||||||
|
public final class PFPathingEntity implements IPathingEntity {
|
||||||
private final Navigator navigator;
|
private final Navigator navigator;
|
||||||
private final Entity entity;
|
private final Entity entity;
|
||||||
|
|
||||||
private float searchRange;
|
private float searchRange;
|
||||||
private Point targetPosition;
|
|
||||||
|
|
||||||
// Capacities
|
// Capacities
|
||||||
private boolean fireResistant;
|
private boolean fireResistant;
|
||||||
@ -37,10 +37,6 @@ public class PFPathingEntity implements IPathingEntity {
|
|||||||
this.searchRange = getAttributeValue(Attribute.FOLLOW_RANGE);
|
this.searchRange = getAttributeValue(Attribute.FOLLOW_RANGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getTargetPosition() {
|
|
||||||
return targetPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int age() {
|
public int age() {
|
||||||
return (int) entity.getAliveTicks();
|
return (int) entity.getAliveTicks();
|
||||||
@ -194,7 +190,8 @@ public class PFPathingEntity implements IPathingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void moveTo(Vec3d position, Passibility passibility, Gravitation gravitation) {
|
public void moveTo(Vec3d position, Passibility passibility, Gravitation gravitation) {
|
||||||
this.targetPosition = new Vec(position.x, position.y, position.z);
|
final Point targetPosition = new Vec(position.x, position.y, position.z);
|
||||||
|
this.navigator.moveTowards(targetPosition, getAttributeValue(Attribute.MOVEMENT_SPEED));
|
||||||
final double entityY = entity.getPosition().y();
|
final double entityY = entity.getPosition().y();
|
||||||
if (entityY < targetPosition.y()) {
|
if (entityY < targetPosition.y()) {
|
||||||
this.navigator.jump(1);
|
this.navigator.jump(1);
|
||||||
|
Loading…
Reference in New Issue
Block a user