Update Hydrazine

This commit is contained in:
Felix Cravic 2020-08-07 06:36:03 +02:00
parent 424256dcb6
commit b7a720ee2d
4 changed files with 23 additions and 80 deletions

View File

@ -70,7 +70,7 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok:1.18.12' annotationProcessor 'org.projectlombok:lombok:1.18.12'
// Pathfinding // 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 "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
api 'com.github.jglrxavpok:Hephaistos:v1.0.4' api 'com.github.jglrxavpok:Hephaistos:v1.0.4'

View File

@ -4,7 +4,6 @@ import com.extollit.gaming.ai.path.HydrazinePathFinder;
import com.extollit.gaming.ai.path.model.PathObject; import com.extollit.gaming.ai.path.model.PathObject;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.attribute.Attribute; 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.GoalSelector;
import net.minestom.server.entity.ai.TargetSelector; import net.minestom.server.entity.ai.TargetSelector;
import net.minestom.server.entity.pathfinding.PFPathingEntity; 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.Instance;
import net.minestom.server.instance.WorldBorder; import net.minestom.server.instance.WorldBorder;
import net.minestom.server.item.ItemStack; 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.network.player.PlayerConnection;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector; import net.minestom.server.utils.Vector;
@ -114,20 +115,16 @@ public abstract class EntityCreature extends LivingEntity {
// Path finding // Path finding
path = pathFinder.update(); path = pathFinder.updatePathFor(pathingEntity);
if (path != null) { if (path != null) {
path.update(pathingEntity); final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
if (path.done()) { final Position targetPosition = pathingEntity.getTargetPosition();
pathFinder.reset(); moveTowards(targetPosition, speed);
} else {
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
final Position targetPosition = pathingEntity.getTargetPosition();
moveTowards(targetPosition, speed);
}
} else { } else {
// TODO not call this every tick (code above with #done() is never called) if (pathPosition != null) {
pathFinder.reset(); this.pathPosition = null;
pathPosition = null; this.pathFinder.reset();
}
} }
super.update(time); super.update(time);
@ -139,64 +136,6 @@ public abstract class EntityCreature extends LivingEntity {
this.pathFinder = new HydrazinePathFinder(pathingEntity, instance.getInstanceSpace()); 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 @Override
public void spawn() { public void spawn() {
@ -403,7 +342,12 @@ public abstract class EntityCreature extends LivingEntity {
} }
position = position.clone(); 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; final boolean success = path != null;

View File

@ -386,7 +386,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
/** /**
* Get if the entity is dead or not * 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() { public boolean isDead() {
return isDead; return isDead;
@ -472,6 +472,9 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
return propertiesPacket; return propertiesPacket;
} }
/**
* Set all the attributes to {@link Attribute#getDefaultValue()}
*/
private void setupAttributes() { private void setupAttributes() {
for (Attribute attribute : Attribute.values()) { for (Attribute attribute : Attribute.values()) {
setAttribute(attribute, attribute.getDefaultValue()); setAttribute(attribute, attribute.getDefaultValue());

View File

@ -7,13 +7,9 @@ import net.minestom.server.utils.Position;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random;
public class RandomStrollGoal extends GoalSelector { 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 static final long DELAY = 2500;
private int radius; private int radius;
@ -38,7 +34,7 @@ public class RandomStrollGoal extends GoalSelector {
Collections.shuffle(closePositions); Collections.shuffle(closePositions);
for (Position position : 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); final boolean result = entityCreature.setPathTo(target);
if (result) { if (result) {
break; break;