From e44d5283b45905de218f1d5aba4dd8b8fa5c9050 Mon Sep 17 00:00:00 2001 From: Articdive <13535885+Articdive@users.noreply.github.com> Date: Sat, 8 Aug 2020 10:20:36 +0200 Subject: [PATCH] Create RandomLookAroundGoal --- .../themode/demo/entity/ZombieCreature.java | 13 +++ .../net/minestom/server/entity/Entity.java | 2 +- .../server/entity/ai/goal/EatBlockGoal.java | 10 +-- .../entity/ai/goal/RandomLookAroundGoal.java | 84 +++++++++++++++++++ .../server/entity/type/EntityZombie.java | 1 + 5 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 src/main/java/fr/themode/demo/entity/ZombieCreature.java create mode 100644 src/main/java/net/minestom/server/entity/ai/goal/RandomLookAroundGoal.java diff --git a/src/main/java/fr/themode/demo/entity/ZombieCreature.java b/src/main/java/fr/themode/demo/entity/ZombieCreature.java new file mode 100644 index 000000000..b3dd7fe83 --- /dev/null +++ b/src/main/java/fr/themode/demo/entity/ZombieCreature.java @@ -0,0 +1,13 @@ +package fr.themode.demo.entity; + +import net.minestom.server.entity.ai.goal.RandomLookAroundGoal; +import net.minestom.server.entity.type.EntityZombie; +import net.minestom.server.utils.Position; + +public class ZombieCreature extends EntityZombie { + + public ZombieCreature(Position spawnPosition) { + super(spawnPosition); + goalSelectors.add(new RandomLookAroundGoal(this, 20)); + } +} diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index e3837ad89..a61275d84 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -1077,7 +1077,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { /** * Change the entity eye height * - * @param eyeHeight the entity eye eight + * @param eyeHeight the entity eye height */ public void setEyeHeight(float eyeHeight) { this.eyeHeight = eyeHeight; diff --git a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java index 162b4c6eb..77d5b430f 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java @@ -13,14 +13,14 @@ public class EatBlockGoal extends GoalSelector { private static final Random RANDOM = new Random(); private final Map eatBelowMap; private final Map eatInMap; - private int chancePerTick; + private final int chancePerTick; private int eatAnimationTick; /** * @param entityCreature Creature that should eat a block. - * @param eatInMap Map containing the block IDs that the entity can eat (when inside the block) and the block ID of the replacement block. + * @param eatInMap Map containing the block IDs that the entity can eat (when inside the block) and the block ID of the replacement block. * @param eatBelowMap Map containing block IDs that the entity can eat (when above the block) and the block ID of the replacement block. - * @param chancePerTick The chance that the entity eats. Settings this to N would mean there is a 1 in N chance. + * @param chancePerTick The chance (per tick) that the entity eats. Settings this to N would mean there is a 1 in N chance. */ public EatBlockGoal( @NotNull EntityCreature entityCreature, @@ -40,8 +40,8 @@ public class EatBlockGoal extends GoalSelector { return false; } Instance instance = entityCreature.getInstance(); - final short blockIdIn = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1,0)); - final short blockIdBelow = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0,2,0)); + final short blockIdIn = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1, 0)); + final short blockIdBelow = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 2, 0)); return eatInMap.containsKey(blockIdIn) || eatBelowMap.containsKey(blockIdBelow); } diff --git a/src/main/java/net/minestom/server/entity/ai/goal/RandomLookAroundGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/RandomLookAroundGoal.java new file mode 100644 index 000000000..90e10e5bf --- /dev/null +++ b/src/main/java/net/minestom/server/entity/ai/goal/RandomLookAroundGoal.java @@ -0,0 +1,84 @@ +package net.minestom.server.entity.ai.goal; + +import net.minestom.server.entity.EntityCreature; +import net.minestom.server.entity.ai.GoalSelector; +import net.minestom.server.utils.Vector; +import org.jetbrains.annotations.NotNull; + +import java.util.Random; +import java.util.function.Function; +import java.util.function.Supplier; + +public class RandomLookAroundGoal extends GoalSelector { + private static final Random RANDOM = new Random(); + private final int chancePerTick; + private final Supplier minimalLookTimeSupplier; + private final Function randomDirectionFunction; + private Vector lookDirection; + private int lookTime = 0; + + public RandomLookAroundGoal(EntityCreature entityCreature, int chancePerTick) { + this(entityCreature, chancePerTick, + // These two functions act similarily enough to how MC randomly looks around. + + // Look in one direction for at most 40 ticks and at minimum 20 ticks. + () -> 20 + RANDOM.nextInt(20), + // Look at a random block + (creature) -> { + final double n = Math.PI * 2 * RANDOM.nextDouble(); + return new Vector( + (float) Math.cos(n), + 0, + (float) Math.sin(n) + ); + }); + } + + /** + * @param entityCreature Creature that should randomly look around. + * @param chancePerTick The chance (per tick) that the entity looks around. Setting this to N would mean there is a 1 in N chance. + * @param minimalLookTimeSupplier A supplier that returns the minimal amount of time an entity looks in a direction. + * @param randomDirectionFunction A function that returns a random vector that the entity will look in/at. + */ + public RandomLookAroundGoal( + EntityCreature entityCreature, + int chancePerTick, + @NotNull Supplier minimalLookTimeSupplier, + @NotNull Function randomDirectionFunction + ) { + super(entityCreature); + this.chancePerTick = chancePerTick; + this.minimalLookTimeSupplier = minimalLookTimeSupplier; + this.randomDirectionFunction = randomDirectionFunction; + } + + @Override + public boolean shouldStart() { + if (RANDOM.nextInt(chancePerTick) != 0) { + return false; + } + return entityCreature.getPathPosition() == null; + } + + @Override + public void start() { + lookTime = minimalLookTimeSupplier.get(); + lookDirection = randomDirectionFunction.apply(entityCreature); + } + + @Override + public void tick() { + --lookTime; + entityCreature.setView(entityCreature.getPosition().clone().setDirection(lookDirection)); + } + + @Override + public boolean shouldEnd() { + return this.lookTime < 0; + } + + @Override + public void end() { + + } +} diff --git a/src/main/java/net/minestom/server/entity/type/EntityZombie.java b/src/main/java/net/minestom/server/entity/type/EntityZombie.java index 0e2995efa..9d5dcebac 100644 --- a/src/main/java/net/minestom/server/entity/type/EntityZombie.java +++ b/src/main/java/net/minestom/server/entity/type/EntityZombie.java @@ -15,6 +15,7 @@ public class EntityZombie extends EntityCreature { public EntityZombie(Position spawnPosition) { super(EntityType.ZOMBIE, spawnPosition); setBoundingBox(0.6f, 1.95f, 0.6f); + setEyeHeight(1.74f); } @Override