From b7a720ee2d9fd3dda152a3d156dd157fe439a4ac Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Fri, 7 Aug 2020 06:36:03 +0200 Subject: [PATCH] Update Hydrazine --- build.gradle | 2 +- .../server/entity/EntityCreature.java | 90 ++++--------------- .../minestom/server/entity/LivingEntity.java | 5 +- .../entity/ai/goal/RandomStrollGoal.java | 6 +- 4 files changed, 23 insertions(+), 80 deletions(-) diff --git a/build.gradle b/build.gradle index fc57cfc04..4ad572a6d 100644 --- a/build.gradle +++ b/build.gradle @@ -70,7 +70,7 @@ dependencies { annotationProcessor 'org.projectlombok:lombok:1.18.12' // Pathfinding - api 'com.github.MadMartian:hydrazine-path-finding:1.1.0' + api 'com.github.MadMartian:hydrazine-path-finding:1.2.3' api "org.jetbrains.kotlin:kotlin-stdlib-jdk8" api 'com.github.jglrxavpok:Hephaistos:v1.0.4' diff --git a/src/main/java/net/minestom/server/entity/EntityCreature.java b/src/main/java/net/minestom/server/entity/EntityCreature.java index c0858178a..26e6645db 100644 --- a/src/main/java/net/minestom/server/entity/EntityCreature.java +++ b/src/main/java/net/minestom/server/entity/EntityCreature.java @@ -4,7 +4,6 @@ import com.extollit.gaming.ai.path.HydrazinePathFinder; import com.extollit.gaming.ai.path.model.PathObject; import net.minestom.server.MinecraftServer; import net.minestom.server.attribute.Attribute; -import net.minestom.server.collision.CollisionUtils; import net.minestom.server.entity.ai.GoalSelector; import net.minestom.server.entity.ai.TargetSelector; import net.minestom.server.entity.pathfinding.PFPathingEntity; @@ -14,7 +13,9 @@ import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.WorldBorder; import net.minestom.server.item.ItemStack; -import net.minestom.server.network.packet.server.play.*; +import net.minestom.server.network.packet.server.play.EntityEquipmentPacket; +import net.minestom.server.network.packet.server.play.EntityPacket; +import net.minestom.server.network.packet.server.play.SpawnLivingEntityPacket; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; @@ -114,20 +115,16 @@ public abstract class EntityCreature extends LivingEntity { // Path finding - path = pathFinder.update(); + path = pathFinder.updatePathFor(pathingEntity); if (path != null) { - path.update(pathingEntity); - if (path.done()) { - pathFinder.reset(); - } else { - final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED); - final Position targetPosition = pathingEntity.getTargetPosition(); - moveTowards(targetPosition, speed); - } + final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED); + final Position targetPosition = pathingEntity.getTargetPosition(); + moveTowards(targetPosition, speed); } else { - // TODO not call this every tick (code above with #done() is never called) - pathFinder.reset(); - pathPosition = null; + if (pathPosition != null) { + this.pathPosition = null; + this.pathFinder.reset(); + } } super.update(time); @@ -139,64 +136,6 @@ public abstract class EntityCreature extends LivingEntity { this.pathFinder = new HydrazinePathFinder(pathingEntity, instance.getInstanceSpace()); } - /** - * @param x X movement offset - * @param y Y movement offset - * @param z Z movement offset - * @param updateView should the entity move its head toward the position? - */ - public void move(float x, float y, float z, boolean updateView) { - // TODO: remove ? Entity#tick already performs this behaviour, and syncs it properly - final Position position = getPosition(); - Position newPosition = new Position(); - // Calculate collisions boxes - onGround = CollisionUtils.handlePhysics(this, new Vector(x, y, z), newPosition, new Vector()); - // Refresh target position - final float newX = newPosition.getX(); - final float newY = newPosition.getY(); - final float newZ = newPosition.getZ(); - - // Creatures cannot move in unloaded chunk - if (ChunkUtils.isChunkUnloaded(getInstance(), newX, newZ)) - return; - - final float lastYaw = position.getYaw(); - final float radians = (float) Math.atan2(newZ - position.getZ(), newX - position.getX()); - - final float yaw = (float) (radians * (180.0 / Math.PI)) - 90; - final float pitch = position.getPitch(); // TODO - - final short deltaX = (short) ((newX * 32 - position.getX() * 32) * 128); - final short deltaY = (short) ((newY * 32 - position.getY() * 32) * 128); - final short deltaZ = (short) ((newZ * 32 - position.getZ() * 32) * 128); - - if (updateView) { - EntityPositionAndRotationPacket entityPositionAndRotationPacket = new EntityPositionAndRotationPacket(); - entityPositionAndRotationPacket.entityId = getEntityId(); - entityPositionAndRotationPacket.deltaX = deltaX; - entityPositionAndRotationPacket.deltaY = deltaY; - entityPositionAndRotationPacket.deltaZ = deltaZ; - entityPositionAndRotationPacket.yaw = yaw; - entityPositionAndRotationPacket.pitch = pitch; - entityPositionAndRotationPacket.onGround = isOnGround(); - sendPacketToViewers(entityPositionAndRotationPacket); - } else { - EntityPositionPacket entityPositionPacket = new EntityPositionPacket(); - entityPositionPacket.entityId = getEntityId(); - entityPositionPacket.deltaX = deltaX; - entityPositionPacket.deltaY = deltaY; - entityPositionPacket.deltaZ = deltaZ; - entityPositionPacket.onGround = isOnGround(); - sendPacketToViewers(entityPositionPacket); - } - - if (lastYaw != yaw) { - setView(yaw, pitch); - } - - refreshPosition(newX, newY, newZ); - } - @Override public void spawn() { @@ -403,7 +342,12 @@ public abstract class EntityCreature extends LivingEntity { } position = position.clone(); - this.path = pathFinder.initiatePathTo(position.getX(), position.getY(), position.getZ()); + + try { + this.path = pathFinder.initiatePathTo(position.getX(), position.getY(), position.getZ()); + } catch (NullPointerException | IndexOutOfBoundsException e) { + this.path = null; + } final boolean success = path != null; diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index bafc7ba4c..eb51b3390 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -386,7 +386,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { /** * Get if the entity is dead or not * - * @return true if the entity is dead, false otherwise + * @return true if the entity is dead */ public boolean isDead() { return isDead; @@ -472,6 +472,9 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { return propertiesPacket; } + /** + * Set all the attributes to {@link Attribute#getDefaultValue()} + */ private void setupAttributes() { for (Attribute attribute : Attribute.values()) { setAttribute(attribute, attribute.getDefaultValue()); diff --git a/src/main/java/net/minestom/server/entity/ai/goal/RandomStrollGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/RandomStrollGoal.java index 4347d153d..00259c680 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/RandomStrollGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/RandomStrollGoal.java @@ -7,13 +7,9 @@ import net.minestom.server.utils.Position; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Random; public class RandomStrollGoal extends GoalSelector { - // Random used to get a position from the close blocks list - private static final Random RANDOM = new Random(); - private static final long DELAY = 2500; private int radius; @@ -38,7 +34,7 @@ public class RandomStrollGoal extends GoalSelector { Collections.shuffle(closePositions); for (Position position : closePositions) { - Position target = position.clone().add(entityCreature.getPosition()); + final Position target = position.clone().add(entityCreature.getPosition()); final boolean result = entityCreature.setPathTo(target); if (result) { break;