Search in sources :

Example 1 with PathNavigator

use of net.minecraft.pathfinding.PathNavigator in project Mekanism by mekanism.

the class RobitAIPickup method canUse.

@Override
public boolean canUse() {
    if (!theRobit.getDropPickup()) {
        return false;
    }
    PathNavigator navigator = getNavigator();
    if (validateClosest() && navigator.createPath(closest, 0) != null) {
        return true;
    }
    // Ensure we don't have the closest one set
    closest = null;
    // Cached for slight performance
    double closestDistance = -1;
    // TODO: Look at and potentially mimic the way piglins search for items to pickup once their AI has mappings
    List<ItemEntity> items = theRobit.level.getEntitiesOfClass(ItemEntity.class, new AxisAlignedBB(theRobit.getX() - SEARCH_RADIUS, theRobit.getY() - SEARCH_RADIUS, theRobit.getZ() - SEARCH_RADIUS, theRobit.getX() + SEARCH_RADIUS, theRobit.getY() + SEARCH_RADIUS, theRobit.getZ() + SEARCH_RADIUS), itemPredicate);
    for (ItemEntity entity : items) {
        double distance = theRobit.distanceToSqr(entity);
        if (distance <= SEARCH_RADIUS_SQ) {
            if (closestDistance == -1 || distance < closestDistance) {
                if (navigator.createPath(entity, 0) != null) {
                    closest = entity;
                    closestDistance = distance;
                }
            }
        }
    }
    // No valid items
    return closest != null;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ItemEntity(net.minecraft.entity.item.ItemEntity) PathNavigator(net.minecraft.pathfinding.PathNavigator)

Example 2 with PathNavigator

use of net.minecraft.pathfinding.PathNavigator in project VariousOddities by Lyinginbedmon.

the class EntityGroupKobold method reassignAttackers.

/**
 * For each unoccupied member,<br>
 * 	Find the nearest pathable target based on their last known location<br>
 * 		If target is observed, attack them<br>
 * 		If target is unobserved, move towards their last known location<br>
 */
public void reassignAttackers() {
    List<MobEntity> unoccupied = new ArrayList<>();
    for (MobEntity entity : members.keySet()) if (entity.isAlive() && (entity.getAttackTarget() == null || !entity.getAttackTarget().isAlive()))
        unoccupied.add(entity);
    for (MobEntity entity : unoccupied) {
        BlockPos entPos = entity.getPosition();
        PathNavigator navigator = entity.getNavigator();
        LivingEntity nearest = null;
        double minDist = Double.MAX_VALUE;
        for (LivingEntity target : targets.keySet()) {
            BlockPos targetPos = targets.get(target).location();
            if (navigator.getPathToPos(targetPos, (int) entity.getAttribute(Attributes.FOLLOW_RANGE).getValue()) != null) {
                double dist = targetPos.distanceSq(entPos);
                if (nearest == null || dist < minDist) {
                    nearest = target;
                    minDist = dist;
                }
            }
        }
        if (nearest != null) {
            if (isObserved(nearest))
                entity.setAttackTarget(nearest);
            else if (navigator.noPath()) {
                BlockPos location = targets.get(nearest).location();
                navigator.tryMoveToXYZ(location.getX(), location.getY(), location.getZ(), 1.0D);
            }
        }
    }
}
Also used : LivingEntity(net.minecraft.entity.LivingEntity) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) PathNavigator(net.minecraft.pathfinding.PathNavigator) MobEntity(net.minecraft.entity.MobEntity)

Example 3 with PathNavigator

use of net.minecraft.pathfinding.PathNavigator in project minecolonies by ldtteam.

the class MovementHandler method tick.

@Override
public void tick() {
    if (this.operation == net.minecraft.entity.ai.controller.MovementController.Action.STRAFE) {
        final float speedAtt = (float) speedAtr.getValue();
        float speed = (float) this.speedModifier * speedAtt;
        float forward = this.strafeForwards;
        float strafe = this.strafeRight;
        float totalMovement = MathHelper.sqrt(forward * forward + strafe * strafe);
        if (totalMovement < 1.0F) {
            totalMovement = 1.0F;
        }
        totalMovement = speed / totalMovement;
        forward = forward * totalMovement;
        strafe = strafe * totalMovement;
        final float sinRotation = MathHelper.sin(this.mob.yRot * ((float) Math.PI / 180F));
        final float cosRotation = MathHelper.cos(this.mob.yRot * ((float) Math.PI / 180F));
        final float rot1 = forward * cosRotation - strafe * sinRotation;
        final float rot2 = strafe * cosRotation + forward * sinRotation;
        final PathNavigator pathnavigator = this.mob.getNavigation();
        final NodeProcessor nodeprocessor = pathnavigator.getNodeEvaluator();
        if (nodeprocessor.getBlockPathType(this.mob.level, MathHelper.floor(this.mob.getX() + (double) rot1), MathHelper.floor(this.mob.getY()), MathHelper.floor(this.mob.getZ() + (double) rot2)) != PathNodeType.WALKABLE) {
            this.strafeForwards = 1.0F;
            this.strafeRight = 0.0F;
            speed = speedAtt;
        }
        this.mob.setSpeed(speed);
        this.mob.setZza(this.strafeForwards);
        this.mob.setXxa(this.strafeRight);
        this.operation = net.minecraft.entity.ai.controller.MovementController.Action.WAIT;
    } else if (this.operation == net.minecraft.entity.ai.controller.MovementController.Action.MOVE_TO) {
        this.operation = net.minecraft.entity.ai.controller.MovementController.Action.WAIT;
        final double xDif = this.wantedX - this.mob.getX();
        final double zDif = this.wantedZ - this.mob.getZ();
        final double yDif = this.wantedY - this.mob.getY();
        final double dist = xDif * xDif + yDif * yDif + zDif * zDif;
        if (dist < (double) 2.5000003E-7F) {
            this.mob.setZza(0.0F);
            return;
        }
        final float range = (float) (MathHelper.atan2(zDif, xDif) * (double) (180F / (float) Math.PI)) - 90.0F;
        this.mob.yRot = this.rotlerp(this.mob.yRot, range, 90.0F);
        this.mob.setSpeed((float) (this.speedModifier * speedAtr.getValue()));
        final BlockPos blockpos = new BlockPos(this.mob.position());
        final BlockState blockstate = this.mob.level.getBlockState(blockpos);
        final Block block = blockstate.getBlock();
        final VoxelShape voxelshape = blockstate.getCollisionShape(this.mob.level, blockpos);
        if ((yDif > (double) this.mob.maxUpStep && xDif * xDif + zDif * zDif < (double) Math.max(1.0F, this.mob.getBbWidth())) || (!voxelshape.isEmpty() && this.mob.getY() < voxelshape.max(Direction.Axis.Y) + (double) blockpos.getY() && !block.is(BlockTags.DOORS) && !block.is(BlockTags.FENCES) && !block.is(BlockTags.FENCE_GATES)) && !block.isLadder(blockstate, this.mob.level, blockpos, this.mob)) {
            this.mob.getJumpControl().jump();
            this.operation = net.minecraft.entity.ai.controller.MovementController.Action.JUMPING;
        }
    } else if (this.operation == net.minecraft.entity.ai.controller.MovementController.Action.JUMPING) {
        this.mob.setSpeed((float) (this.speedModifier * speedAtr.getValue()));
        // Avoid beeing stuck in jumping while in liquids
        final BlockPos blockpos = new BlockPos(this.mob.position());
        final BlockState blockstate = this.mob.level.getBlockState(blockpos);
        if (this.mob.isOnGround() || blockstate.getMaterial().isLiquid()) {
            this.operation = net.minecraft.entity.ai.controller.MovementController.Action.WAIT;
        }
    } else {
        this.mob.setZza(0.0F);
    }
}
Also used : NodeProcessor(net.minecraft.pathfinding.NodeProcessor) BlockState(net.minecraft.block.BlockState) VoxelShape(net.minecraft.util.math.shapes.VoxelShape) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) PathNavigator(net.minecraft.pathfinding.PathNavigator)

Example 4 with PathNavigator

use of net.minecraft.pathfinding.PathNavigator in project minecolonies by Minecolonies.

the class MovementHandler method tick.

@Override
public void tick() {
    if (this.operation == net.minecraft.entity.ai.controller.MovementController.Action.STRAFE) {
        final float speedAtt = (float) speedAtr.getValue();
        float speed = (float) this.speedModifier * speedAtt;
        float forward = this.strafeForwards;
        float strafe = this.strafeRight;
        float totalMovement = MathHelper.sqrt(forward * forward + strafe * strafe);
        if (totalMovement < 1.0F) {
            totalMovement = 1.0F;
        }
        totalMovement = speed / totalMovement;
        forward = forward * totalMovement;
        strafe = strafe * totalMovement;
        final float sinRotation = MathHelper.sin(this.mob.yRot * ((float) Math.PI / 180F));
        final float cosRotation = MathHelper.cos(this.mob.yRot * ((float) Math.PI / 180F));
        final float rot1 = forward * cosRotation - strafe * sinRotation;
        final float rot2 = strafe * cosRotation + forward * sinRotation;
        final PathNavigator pathnavigator = this.mob.getNavigation();
        final NodeProcessor nodeprocessor = pathnavigator.getNodeEvaluator();
        if (nodeprocessor.getBlockPathType(this.mob.level, MathHelper.floor(this.mob.getX() + (double) rot1), MathHelper.floor(this.mob.getY()), MathHelper.floor(this.mob.getZ() + (double) rot2)) != PathNodeType.WALKABLE) {
            this.strafeForwards = 1.0F;
            this.strafeRight = 0.0F;
            speed = speedAtt;
        }
        this.mob.setSpeed(speed);
        this.mob.setZza(this.strafeForwards);
        this.mob.setXxa(this.strafeRight);
        this.operation = net.minecraft.entity.ai.controller.MovementController.Action.WAIT;
    } else if (this.operation == net.minecraft.entity.ai.controller.MovementController.Action.MOVE_TO) {
        this.operation = net.minecraft.entity.ai.controller.MovementController.Action.WAIT;
        final double xDif = this.wantedX - this.mob.getX();
        final double zDif = this.wantedZ - this.mob.getZ();
        final double yDif = this.wantedY - this.mob.getY();
        final double dist = xDif * xDif + yDif * yDif + zDif * zDif;
        if (dist < (double) 2.5000003E-7F) {
            this.mob.setZza(0.0F);
            return;
        }
        final float range = (float) (MathHelper.atan2(zDif, xDif) * (double) (180F / (float) Math.PI)) - 90.0F;
        this.mob.yRot = this.rotlerp(this.mob.yRot, range, 90.0F);
        this.mob.setSpeed((float) (this.speedModifier * speedAtr.getValue()));
        final BlockPos blockpos = new BlockPos(this.mob.position());
        final BlockState blockstate = this.mob.level.getBlockState(blockpos);
        final Block block = blockstate.getBlock();
        final VoxelShape voxelshape = blockstate.getCollisionShape(this.mob.level, blockpos);
        if ((yDif > (double) this.mob.maxUpStep && xDif * xDif + zDif * zDif < (double) Math.max(1.0F, this.mob.getBbWidth())) || (!voxelshape.isEmpty() && this.mob.getY() < voxelshape.max(Direction.Axis.Y) + (double) blockpos.getY() && !block.is(BlockTags.DOORS) && !block.is(BlockTags.FENCES) && !block.is(BlockTags.FENCE_GATES)) && !block.isLadder(blockstate, this.mob.level, blockpos, this.mob)) {
            this.mob.getJumpControl().jump();
            this.operation = net.minecraft.entity.ai.controller.MovementController.Action.JUMPING;
        }
    } else if (this.operation == net.minecraft.entity.ai.controller.MovementController.Action.JUMPING) {
        this.mob.setSpeed((float) (this.speedModifier * speedAtr.getValue()));
        // Avoid beeing stuck in jumping while in liquids
        final BlockPos blockpos = new BlockPos(this.mob.position());
        final BlockState blockstate = this.mob.level.getBlockState(blockpos);
        if (this.mob.isOnGround() || blockstate.getMaterial().isLiquid()) {
            this.operation = net.minecraft.entity.ai.controller.MovementController.Action.WAIT;
        }
    } else {
        this.mob.setZza(0.0F);
    }
}
Also used : NodeProcessor(net.minecraft.pathfinding.NodeProcessor) BlockState(net.minecraft.block.BlockState) VoxelShape(net.minecraft.util.math.shapes.VoxelShape) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) PathNavigator(net.minecraft.pathfinding.PathNavigator)

Example 5 with PathNavigator

use of net.minecraft.pathfinding.PathNavigator in project bioplethora by AquexTheSeal.

the class BPAvoidEntityGoal method generateRandomPos.

@Nullable
public static Vector3d generateRandomPos(MobEntity p_226339_0_, int p_226339_1_, int p_226339_2_, int p_226339_3_, @Nullable Vector3d p_226339_4_, boolean p_226339_5_, double p_226339_6_, ToDoubleFunction<BlockPos> p_226339_8_, boolean p_226339_9_, int p_226339_10_, int p_226339_11_, boolean p_226339_12_) {
    PathNavigator pathnavigator = p_226339_0_.getNavigation();
    Random random = p_226339_0_.getRandom();
    boolean flag;
    if (p_226339_0_.hasRestriction()) {
        flag = p_226339_0_.getRestrictCenter().closerThan(p_226339_0_.position(), (double) (p_226339_0_.getRestrictRadius() + (float) p_226339_1_) + 1.0D);
    } else {
        flag = false;
    }
    boolean flag1 = false;
    double d0 = Double.NEGATIVE_INFINITY;
    BlockPos blockpos = p_226339_0_.blockPosition();
    for (int i = 0; i < 10; ++i) {
        BlockPos blockpos1 = getRandomDelta(random, p_226339_1_, p_226339_2_, p_226339_3_, p_226339_4_, p_226339_6_);
        if (blockpos1 != null) {
            int j = blockpos1.getX();
            int k = blockpos1.getY();
            int l = blockpos1.getZ();
            if (p_226339_0_.hasRestriction() && p_226339_1_ > 1) {
                BlockPos blockpos2 = p_226339_0_.getRestrictCenter();
                if (p_226339_0_.getX() > (double) blockpos2.getX()) {
                    j -= random.nextInt(p_226339_1_ / 2);
                } else {
                    j += random.nextInt(p_226339_1_ / 2);
                }
                if (p_226339_0_.getZ() > (double) blockpos2.getZ()) {
                    l -= random.nextInt(p_226339_1_ / 2);
                } else {
                    l += random.nextInt(p_226339_1_ / 2);
                }
            }
            BlockPos blockpos3 = new BlockPos((double) j + p_226339_0_.getX(), (double) k + p_226339_0_.getY(), (double) l + p_226339_0_.getZ());
            if (blockpos3.getY() >= 0 && blockpos3.getY() <= p_226339_0_.level.getMaxBuildHeight() && (!flag || p_226339_0_.isWithinRestriction(blockpos3)) && (!p_226339_12_ || pathnavigator.isStableDestination(blockpos3))) {
                if (p_226339_9_) {
                    blockpos3 = moveUpToAboveSolid(blockpos3, random.nextInt(p_226339_10_ + 1) + p_226339_11_, p_226339_0_.level.getMaxBuildHeight(), (p_226341_1_) -> {
                        return p_226339_0_.level.getBlockState(p_226341_1_).getMaterial().isSolid();
                    });
                }
                if (p_226339_5_ || !p_226339_0_.level.getFluidState(blockpos3).is(FluidTags.WATER)) {
                    PathNodeType pathnodetype = WalkNodeProcessor.getBlockPathTypeStatic(p_226339_0_.level, blockpos3.mutable());
                    if (p_226339_0_.getPathfindingMalus(pathnodetype) == 0.0F) {
                        double d1 = p_226339_8_.applyAsDouble(blockpos3);
                        if (d1 > d0) {
                            d0 = d1;
                            blockpos = blockpos3;
                            flag1 = true;
                        }
                    }
                }
            }
        }
    }
    return flag1 ? Vector3d.atBottomCenterOf(blockpos) : null;
}
Also used : WalkNodeProcessor(net.minecraft.pathfinding.WalkNodeProcessor) Path(net.minecraft.pathfinding.Path) LivingEntity(net.minecraft.entity.LivingEntity) FluidTags(net.minecraft.tags.FluidTags) Predicate(java.util.function.Predicate) EntityPredicates(net.minecraft.util.EntityPredicates) BlockPos(net.minecraft.util.math.BlockPos) Random(java.util.Random) PathNodeType(net.minecraft.pathfinding.PathNodeType) Goal(net.minecraft.entity.ai.goal.Goal) Vector3d(net.minecraft.util.math.vector.Vector3d) MathHelper(net.minecraft.util.math.MathHelper) ToDoubleFunction(java.util.function.ToDoubleFunction) EntityPredicate(net.minecraft.entity.EntityPredicate) MobEntity(net.minecraft.entity.MobEntity) PathNavigator(net.minecraft.pathfinding.PathNavigator) Nullable(javax.annotation.Nullable) EnumSet(java.util.EnumSet) Random(java.util.Random) BlockPos(net.minecraft.util.math.BlockPos) PathNodeType(net.minecraft.pathfinding.PathNodeType) PathNavigator(net.minecraft.pathfinding.PathNavigator) Nullable(javax.annotation.Nullable)

Aggregations

PathNavigator (net.minecraft.pathfinding.PathNavigator)7 BlockPos (net.minecraft.util.math.BlockPos)4 Block (net.minecraft.block.Block)2 BlockState (net.minecraft.block.BlockState)2 LivingEntity (net.minecraft.entity.LivingEntity)2 MobEntity (net.minecraft.entity.MobEntity)2 NodeProcessor (net.minecraft.pathfinding.NodeProcessor)2 Path (net.minecraft.pathfinding.Path)2 VoxelShape (net.minecraft.util.math.shapes.VoxelShape)2 ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 Random (java.util.Random)1 Predicate (java.util.function.Predicate)1 ToDoubleFunction (java.util.function.ToDoubleFunction)1 Nullable (javax.annotation.Nullable)1 EntityPredicate (net.minecraft.entity.EntityPredicate)1 Goal (net.minecraft.entity.ai.goal.Goal)1 ItemEntity (net.minecraft.entity.item.ItemEntity)1 GroundPathNavigator (net.minecraft.pathfinding.GroundPathNavigator)1 PathNodeType (net.minecraft.pathfinding.PathNodeType)1