Small improvements to entity movement (#1421)

This commit is contained in:
Arektor 2022-10-02 04:12:54 +02:00 committed by GitHub
parent caaad1693e
commit 95fa6607ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 60 deletions

View File

@ -75,7 +75,16 @@ public final class AttributeInstance {
* @param modifier the modifier to remove
*/
public void removeModifier(@NotNull AttributeModifier modifier) {
if (modifiers.remove(modifier.getId()) != null) {
removeModifier(modifier.getId());
}
/**
* Remove a modifier from this instance.
*
* @param uuid The UUID of the modifier to remove
*/
public void removeModifier(@NotNull UUID uuid) {
if (modifiers.remove(uuid) != null) {
refreshCachedValue();
}
}

View File

@ -4,6 +4,7 @@ import com.extollit.gaming.ai.path.HydrazinePathFinder;
import com.extollit.gaming.ai.path.PathOptions;
import com.extollit.gaming.ai.path.model.IPath;
import net.minestom.server.collision.CollisionUtils;
import net.minestom.server.collision.PhysicsResult;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
@ -43,7 +44,7 @@ public final class Navigator {
* @param direction the targeted position
* @param speed define how far the entity will move
*/
public void moveTowards(@NotNull Point direction, double speed) {
public PhysicsResult moveTowards(@NotNull Point direction, double speed) {
final Pos position = entity.getPosition();
final double dx = direction.x() - position.x();
final double dy = direction.y() - position.y();
@ -62,6 +63,7 @@ public final class Navigator {
// Prevent ghosting
final var physicsResult = CollisionUtils.handlePhysics(entity, new Vec(speedX, speedY, speedZ));
this.entity.refreshPosition(physicsResult.newPosition().withView(yaw, pitch));
return physicsResult;
}
public void jump(float height) {

View File

@ -3,6 +3,7 @@ package net.minestom.server.entity.pathfinding;
import com.extollit.gaming.ai.path.model.IBlockDescription;
import com.extollit.gaming.ai.path.model.IBlockObject;
import com.extollit.linalg.immutable.AxisAlignedBBox;
import net.minestom.server.collision.Shape;
import net.minestom.server.instance.block.Block;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@ -32,9 +33,10 @@ public final class PFBlock implements IBlockDescription, IBlockObject {
@Override
public AxisAlignedBBox bounds() {
Shape shape = this.block.registry().collisionShape();
return new AxisAlignedBBox(
0, 0, 0,
1, 1, 1
shape.relativeStart().x(), shape.relativeStart().y(), shape.relativeStart().z(),
shape.relativeEnd().x(), shape.relativeEnd().y(), shape.relativeEnd().z()
);
}
@ -47,7 +49,7 @@ public final class PFBlock implements IBlockDescription, IBlockObject {
return true;
}
// Return all walls
// It just so happens that their namespace IDs all end with "door".
// It just so happens that their namespace IDs all end with "wall".
return block.namespace().asString().endsWith("wall");
}
@ -78,60 +80,11 @@ public final class PFBlock implements IBlockDescription, IBlockObject {
@Override
public boolean isFullyBounded() {
// TODO: Use Hitbox (would probably be faster as well)
// Return false for anything that does not have a full hitbox but impedes
// e.g. Anvils, Lilypads, Ladders, Walls, Fences, EnchantmentTables
// Fences & Walls
if (isFenceLike()) {
return false;
}
// Ladders and Vines
if (isClimbable()) {
return false;
}
// All doors/trapdoors.
if (isDoor()) {
return false;
}
if (block.name().startsWith("potted")) {
return false;
}
// Skulls & Heads
if (block.name().contains("skull") || block.name().contains("head")) {
// NOTE: blocks.getName().contains("head") also matches Piston_Head
// I could not find out by documentation if piston_head is fully bounded, I would presume it is NOT.
return false;
}
// Carpets
if (block.name().endsWith("carpet")) {
return false;
}
// Slabs
if (block.name().contains("slab")) {
return false;
}
// Beds
if (block.name().endsWith("bed")) {
return false;
}
// Glass Panes
if (block.name().endsWith("pane")) {
return false;
}
return !Block.CHORUS_FLOWER.compare(block) && !Block.CHORUS_PLANT.compare(block) && !Block.BAMBOO.compare(block)
&& !Block.BAMBOO_SAPLING.compare(block) && !Block.SEA_PICKLE.compare(block)
&& !Block.TURTLE_EGG.compare(block) && !Block.SNOW.compare(block) && !Block.FLOWER_POT.compare(block)
&& !Block.LILY_PAD.compare(block) && !Block.ANVIL.compare(block) && !Block.CHIPPED_ANVIL.compare(block)
&& !Block.DAMAGED_ANVIL.compare(block) && !Block.CAKE.compare(block) && !Block.CACTUS.compare(block)
&& !Block.BREWING_STAND.compare(block) && !Block.LECTERN.compare(block)
&& !Block.DAYLIGHT_DETECTOR.compare(block) && !Block.CAMPFIRE.compare(block)
&& !Block.SOUL_CAMPFIRE.compare(block) && !Block.ENCHANTING_TABLE.compare(block)
&& !Block.CHEST.compare(block) && !Block.ENDER_CHEST.compare(block) && !Block.GRINDSTONE.compare(block)
&& !Block.TRAPPED_CHEST.compare(block) && !Block.SOUL_SAND.compare(block)
&& !Block.SOUL_SOIL.compare(block) && !Block.LANTERN.compare(block) && !Block.COCOA.compare(block)
&& !Block.CONDUIT.compare(block) && !Block.DIRT_PATH.compare(block) && !Block.FARMLAND.compare(block)
&& !Block.END_ROD.compare(block) && !Block.STONECUTTER.compare(block) && !Block.BELL.compare(block);
Shape shape = block.registry().collisionShape();
return shape.relativeStart().isZero()
&& shape.relativeEnd().x() == 1.0d
&& shape.relativeEnd().y() == 1.0d
&& shape.relativeEnd().z() == 1.0d;
}
@Override

View File

@ -192,7 +192,10 @@ public final class PFPathingEntity implements IPathingEntity {
public void moveTo(Vec3d position, Passibility passibility, Gravitation gravitation) {
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() + 0.00001D; // After any negative y movement, entities will always be extremely
// slightly below floor level. This +0.00001D is here to offset this
// error and stop the entity from permanently jumping.
if (entityY < targetPosition.y()) {
this.navigator.jump(1);
}