Pathfinding update

This commit is contained in:
Felix Cravic 2020-07-24 19:31:15 +02:00
parent 4fb7d61a7c
commit 1adaa0553e
5 changed files with 59 additions and 12 deletions

View File

@ -52,7 +52,7 @@ public class PlayerInit {
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageFolder);
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
instanceContainer.enableAutoChunkLoad(true);
instanceContainer.setChunkGenerator(noiseTestGenerator);
instanceContainer.setChunkGenerator(chunkGeneratorDemo);
netherTest = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.NETHER);
netherTest.enableAutoChunkLoad(true);
@ -240,7 +240,7 @@ public class PlayerInit {
//player.setHeldItemSlot((byte) 5);
player.setGlowing(true);
//player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 127));
/*for (int i = 0; i < 9; i++) {
player.getInventory().setItemStack(i, new ItemStack(Material.STONE, (byte) 127));
}*/

View File

@ -53,7 +53,7 @@ public class SimpleCommand implements CommandProcessor {
Instance instance = player.getInstance();
ChickenCreature chickenCreature = new ChickenCreature(new Position(-10, 42, -10));
ChickenCreature chickenCreature = new ChickenCreature(new Position(-10, 40, -10));
chickenCreature.setInstance(instance);
chickenCreature.setPathTo(player.getPosition());

View File

@ -5,7 +5,7 @@ public enum Attribute {
MAX_HEALTH("generic.max_health", 20, 1024),
FOLLOW_RANGE("generic.follow_range", 32, 2048),
KNOCKBACK_RESISTANCE("generic.knockback_resistance", 0, 1),
MOVEMENT_SPEED("generic.movement_speed", 0.2f, 1024),
MOVEMENT_SPEED("generic.movement_speed", 0.25f, 1024),
ATTACK_DAMAGE("generic.attack_damage", 2, 2048),
ATTACK_SPEED("generic.attack_speed", 4, 1024),
FLYING_SPEED("generic.flying_speed", 0.4f, 1024),

View File

@ -2,6 +2,7 @@ package net.minestom.server.entity;
import com.extollit.gaming.ai.path.HydrazinePathFinder;
import com.extollit.gaming.ai.path.model.PathObject;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.collision.CollisionUtils;
import net.minestom.server.entity.pathfinding.PFPathingEntity;
import net.minestom.server.event.entity.EntityAttackEvent;
@ -15,6 +16,7 @@ import net.minestom.server.utils.Vector;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.item.ItemStackUtils;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.validate.Check;
public abstract class EntityCreature extends LivingEntity {
@ -56,6 +58,11 @@ public abstract class EntityCreature extends LivingEntity {
path.update(pathingEntity);
if (path.done()) {
pathFinder.reset();
} else {
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
Position targetPosition = pathingEntity.getTargetPosition();
//targetPosition = new Position(-5, 40, -5);
moveTowards(targetPosition, speed);
}
}
}
@ -292,10 +299,45 @@ public abstract class EntityCreature extends LivingEntity {
* @param speed define how far the entity will move
*/
public void moveTowards(Position direction, float speed) {
final float radians = (float) Math.atan2(direction.getZ() - position.getZ(), direction.getX() - position.getX());
Check.notNull(direction, "The direction cannot be null");
final float currentX = position.getX();
final float currentZ = position.getZ();
final float targetX = direction.getX();
final float targetZ = direction.getZ();
final float radians = (float) Math.atan2(targetZ - currentZ, targetX - currentX);
final float speedX = (float) (Math.cos(radians) * speed);
final float speedZ = (float) (Math.sin(radians) * speed);
move(speedX, 0, speedZ, true);
float finalX = speedX;
float finalZ = speedZ;
//System.out.println("TEST X: " + targetX + " : " + currentX + " : " + speedX);
//System.out.println("TEST Z: "+targetZ+" : "+currentZ+" : "+speedZ);
// X checks
/*if (targetX > currentX && currentX + speedX > targetX) {
// Try to go further +X
finalX = targetX;
System.out.println("CHECK 1");
} else if (targetX < currentX && currentX + speedX < targetX) {
// Try to go further -X
finalX = targetX;
System.out.println("CHECK 2");
}
// Z checks
if (targetZ > currentZ && currentZ + speedZ >= targetZ) {
// Try to go further +X
finalZ = targetZ;
System.out.println("CHECK 3");
} else if (targetZ < currentZ && currentZ + speedZ < targetZ) {
// Try to go further -X
finalZ = targetX;
System.out.println("CHECK 4");
}*/
move(finalX, 0, finalZ, true);
}
private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) {

View File

@ -12,10 +12,16 @@ public class PFPathingEntity implements IPathingEntity {
private EntityCreature entity;
private Position targetPosition;
public PFPathingEntity(EntityCreature entity) {
this.entity = entity;
}
public Position getTargetPosition() {
return targetPosition;
}
@Override
public int age() {
return (int) entity.getAliveTicks();
@ -73,21 +79,20 @@ public class PFPathingEntity implements IPathingEntity {
@Override
public void moveTo(Vec3d position) {
final Position entityPosition = entity.getPosition();
final float entityY = entityPosition.getY();
final float speed = entity.getAttributeValue(Attribute.MOVEMENT_SPEED);
final float x = (float) position.x;
final float y = (float) position.y;
final float z = (float) position.z;
entity.moveTowards(new Position(x, y, z), speed);
this.targetPosition = new Position(x, y, z);
final float entityY = entity.getPosition().getY();
if (entityY < y) {
entity.jump(1);
//entity.jump(1);
}
}
@Override
public Vec3d coordinates() {
Position position = entity.getPosition();
final Position position = entity.getPosition();
return new Vec3d(position.getX(), position.getY(), position.getZ());
}