Search in sources :

Example 1 with BlockPathTypes

use of net.minecraft.world.level.pathfinder.BlockPathTypes in project ResourcefulBees by Resourceful-Bees.

the class RandomPositionGenerator method findTarget.

@Nullable
private static Vec3 findTarget(PathfinderMob bee, int horizontalOffset, int verticalOffset, int zero, @Nullable Vec3 vector3d, boolean pathOnWater, ToDoubleFunction<BlockPos> blockWeightOfBeePOS) {
    PathNavigation pathnavigator = bee.getNavigation();
    Random random = bee.getRandom();
    // is bee within distance of home position?
    boolean inDistanceOfHome = bee.hasRestriction() && bee.getRestrictCenter().closerThan(bee.position(), (bee.getRestrictRadius() + horizontalOffset) + 1.0D);
    boolean flag1 = false;
    double d0 = Double.NEGATIVE_INFINITY;
    BlockPos beePos = bee.blockPosition();
    // loop 10 times getting the best valid path as far from the entities current position as possible
    for (int i = 0; i < 10; ++i) {
        // get random offset
        BlockPos randomBlockpos = getRandomOffset(random, horizontalOffset, verticalOffset, zero, vector3d);
        if (randomBlockpos != null) {
            // if position is not null which should always be true
            int rndPosX = randomBlockpos.getX();
            int rndPosY = randomBlockpos.getY();
            int rndPosZ = randomBlockpos.getZ();
            if (bee.hasRestriction() && horizontalOffset > 1) {
                // if bee has a home && horizontal offset is greater than 1
                // get home position
                BlockPos beeHomePosition = bee.getRestrictCenter();
                // checks if bee is east of home and sets position closer based on direction
                int nextInt = random.nextInt(horizontalOffset / 2);
                rndPosX = bee.getX() > beeHomePosition.getX() ? rndPosX - nextInt : rndPosX + nextInt;
                // checks if bee is north of home and sets position closer based on direction
                int nextInt1 = random.nextInt(horizontalOffset / 2);
                rndPosZ = bee.getZ() > beeHomePosition.getZ() ? rndPosZ - nextInt1 : rndPosZ + nextInt1;
            }
            // create new target block position using relative position and offset
            BlockPos targetPos = new BlockPos(rndPosX + bee.getX(), rndPosY + bee.getY(), rndPosZ + bee.getZ());
            // if target Y is between 0 and world height AND (is not in Distance of home OR target pos is in distance of home) AND entity can stand on target pos
            if (MathUtils.inRangeInclusive(targetPos.getY(), 0, bee.level.getMaxBuildHeight()) && (!inDistanceOfHome || bee.isWithinRestriction(targetPos)) && pathnavigator.isStableDestination(targetPos)) {
                // flip a coin heads = check block above is air if so find valid position above else go below
                if (random.nextBoolean() && bee.level.isEmptyBlock(bee.blockPosition().above())) {
                    targetPos = findValidPositionAbove(targetPos, random.nextInt(3) + 1, bee.level.getMaxBuildHeight(), pos -> bee.level.getBlockState(pos).getMaterial().isSolid());
                } else {
                    targetPos = findValidPositionBelow(targetPos, random.nextInt(3) + 1, pos -> bee.level.getBlockState(pos).getMaterial().isSolid());
                }
                // if can travel through water or target pos is not tagged as water
                if (pathOnWater || !bee.level.getFluidState(targetPos).is(FluidTags.WATER)) {
                    // set path node type based on target position
                    BlockPathTypes pathnodetype = WalkNodeEvaluator.getBlockPathTypeStatic(bee.level, targetPos.mutable());
                    if (bee.getPathfindingMalus(pathnodetype) == 0.0F) {
                        // calculate if weight of new target position is better than previous target position
                        double d1 = blockWeightOfBeePOS.applyAsDouble(targetPos);
                        if (d1 > d0) {
                            d0 = d1;
                            beePos = targetPos;
                            flag1 = true;
                        }
                    }
                }
            }
        }
    }
    // if target position is not equal to negative infinity then return center of target pos otherwise null
    return flag1 ? Vec3.atBottomCenterOf(beePos) : null;
}
Also used : FluidTags(net.minecraft.tags.FluidTags) BlockPathTypes(net.minecraft.world.level.pathfinder.BlockPathTypes) Predicate(java.util.function.Predicate) Random(java.util.Random) HALF_PI(com.teamresourceful.resourcefulbees.utils.MathUtils.HALF_PI) Nullable(org.jetbrains.annotations.Nullable) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) ToDoubleFunction(java.util.function.ToDoubleFunction) WalkNodeEvaluator(net.minecraft.world.level.pathfinder.WalkNodeEvaluator) PathfinderMob(net.minecraft.world.entity.PathfinderMob) Mth(net.minecraft.util.Mth) ModConstants(com.teamresourceful.resourcefulbees.lib.constants.ModConstants) BlockPathTypes(net.minecraft.world.level.pathfinder.BlockPathTypes) Random(java.util.Random) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) BlockPos(net.minecraft.core.BlockPos) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with BlockPathTypes

use of net.minecraft.world.level.pathfinder.BlockPathTypes in project AlexsMobs by Alex-the-666.

the class SnowLeopardAIMelee method calculateVantagePoint.

@Nullable
private Vec3 calculateVantagePoint(LivingEntity creature, int xz, int y, int p_226339_3_, @Nullable Vec3 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_) {
    PathNavigation lvt_13_1_ = leopard.getNavigation();
    Random lvt_14_1_ = creature.getRandom();
    boolean lvt_15_2_;
    if (leopard.hasRestriction()) {
        lvt_15_2_ = leopard.getRestrictCenter().closerToCenterThan(creature.position(), (double) (leopard.getRestrictRadius() + (float) xz) + 1.0D);
    } else {
        lvt_15_2_ = false;
    }
    boolean lvt_16_1_ = false;
    double lvt_17_1_ = -1.0D / 0.0;
    BlockPos lvt_19_1_ = creature.blockPosition();
    for (int lvt_20_1_ = 0; lvt_20_1_ < 10; ++lvt_20_1_) {
        BlockPos lvt_21_1_ = getRandomDelta(lvt_14_1_, xz, y, p_226339_3_, p_226339_4_, p_226339_6_);
        if (lvt_21_1_ != null) {
            int lvt_22_1_ = lvt_21_1_.getX();
            int lvt_23_1_ = lvt_21_1_.getY();
            int lvt_24_1_ = lvt_21_1_.getZ();
            BlockPos lvt_25_2_;
            if (leopard.hasRestriction() && xz > 1) {
                lvt_25_2_ = leopard.getRestrictCenter();
                if (creature.getX() > (double) lvt_25_2_.getX()) {
                    lvt_22_1_ -= lvt_14_1_.nextInt(xz / 2);
                } else {
                    lvt_22_1_ += lvt_14_1_.nextInt(xz / 2);
                }
                if (creature.getZ() > (double) lvt_25_2_.getZ()) {
                    lvt_24_1_ -= lvt_14_1_.nextInt(xz / 2);
                } else {
                    lvt_24_1_ += lvt_14_1_.nextInt(xz / 2);
                }
            }
            lvt_25_2_ = new BlockPos((double) lvt_22_1_ + creature.getX(), (double) lvt_23_1_ + creature.getY(), (double) lvt_24_1_ + creature.getZ());
            if (lvt_25_2_.getY() >= 0 && lvt_25_2_.getY() <= creature.level.getMaxBuildHeight() && (!lvt_15_2_ || leopard.isWithinRestriction(lvt_25_2_)) && (!p_226339_12_ || lvt_13_1_.isStableDestination(lvt_25_2_))) {
                if (p_226339_9_) {
                    lvt_25_2_ = moveUpToAboveSolid(lvt_25_2_, lvt_14_1_.nextInt(p_226339_10_ + 1) + p_226339_11_, creature.level.getMaxBuildHeight(), (p_226341_1_) -> {
                        return creature.level.getBlockState(p_226341_1_).getMaterial().isSolid();
                    });
                }
                if (p_226339_5_ || !creature.level.getFluidState(lvt_25_2_).is(FluidTags.WATER)) {
                    BlockPathTypes lvt_26_1_ = WalkNodeEvaluator.getBlockPathTypeStatic(creature.level, lvt_25_2_.mutable());
                    if (leopard.getPathfindingMalus(lvt_26_1_) == 0.0F) {
                        double lvt_27_1_ = p_226339_8_.applyAsDouble(lvt_25_2_);
                        if (lvt_27_1_ > lvt_17_1_) {
                            lvt_17_1_ = lvt_27_1_;
                            lvt_19_1_ = lvt_25_2_;
                            lvt_16_1_ = true;
                        }
                    }
                }
            }
        }
    }
    if (lvt_16_1_) {
        return Vec3.atBottomCenterOf(lvt_19_1_);
    } else {
        return null;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) IAnimatedEntity(com.github.alexthe666.citadel.animation.IAnimatedEntity) BlockPathTypes(net.minecraft.world.level.pathfinder.BlockPathTypes) FluidTags(net.minecraft.tags.FluidTags) Predicate(java.util.function.Predicate) Random(java.util.Random) Player(net.minecraft.world.entity.player.Player) LandRandomPos(net.minecraft.world.entity.ai.util.LandRandomPos) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) DamageSource(net.minecraft.world.damagesource.DamageSource) ToDoubleFunction(java.util.function.ToDoubleFunction) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) WalkNodeEvaluator(net.minecraft.world.level.pathfinder.WalkNodeEvaluator) Mth(net.minecraft.util.Mth) Nullable(javax.annotation.Nullable) EnumSet(java.util.EnumSet) EntitySnowLeopard(com.github.alexthe666.alexsmobs.entity.EntitySnowLeopard) Goal(net.minecraft.world.entity.ai.goal.Goal) BlockPathTypes(net.minecraft.world.level.pathfinder.BlockPathTypes) Random(java.util.Random) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) BlockPos(net.minecraft.core.BlockPos) Nullable(javax.annotation.Nullable)

Example 3 with BlockPathTypes

use of net.minecraft.world.level.pathfinder.BlockPathTypes in project MagicPlugin by elBukkit.

the class MagicFollowOwnerGoal method canTeleportTo.

private boolean canTeleportTo(BlockPos blockPosition) {
    BlockPathTypes pathType = WalkNodeEvaluator.getBlockPathTypeStatic(mob.level, blockPosition.mutable());
    if (pathType != BlockPathTypes.WALKABLE) {
        return false;
    }
    blockPosition = blockPosition.e(this.mob.blockPosition());
    return mob.level.noCollision(this.mob, this.mob.getBoundingBox().move(blockPosition));
}
Also used : BlockPathTypes(net.minecraft.world.level.pathfinder.BlockPathTypes)

Example 4 with BlockPathTypes

use of net.minecraft.world.level.pathfinder.BlockPathTypes in project MagicPlugin by elBukkit.

the class MagicFollowOwnerGoal method canTeleportTo.

private boolean canTeleportTo(BlockPos blockPosition) {
    BlockPathTypes pathType = WalkNodeEvaluator.getBlockPathTypeStatic(mob.level, blockPosition.mutable());
    if (pathType != BlockPathTypes.WALKABLE) {
        return false;
    }
    blockPosition = blockPosition.subtract(this.mob.blockPosition());
    return mob.level.noCollision(this.mob, this.mob.getBoundingBox().move(blockPosition));
}
Also used : BlockPathTypes(net.minecraft.world.level.pathfinder.BlockPathTypes)

Example 5 with BlockPathTypes

use of net.minecraft.world.level.pathfinder.BlockPathTypes in project Citizens2 by CitizensDev.

the class PlayerNavigation method canWalkOn.

private boolean canWalkOn(int var0, int var1, int var2, int var3, int var4, int var5, Vec3 var6, double var7, double var9) {
    int var11 = var0 - var3 / 2;
    int var12 = var2 - var5 / 2;
    if (!canWalkAbove(var11, var1, var12, var3, var4, var5, var6, var7, var9))
        return false;
    for (int var13 = var11; var13 < var11 + var3; var13++) {
        for (int var14 = var12; var14 < var12 + var5; var14++) {
            double var15 = var13 + 0.5D - var6.x;
            double var17 = var14 + 0.5D - var6.z;
            if (var15 * var7 + var17 * var9 >= 0.0D) {
                BlockPathTypes var19 = this.nodeEvaluator.getBlockPathType(this.level, var13, var1 - 1, var14, this.mob, var3, var4, var5, true, true);
                if (!hasValidPathType(var19))
                    return false;
                var19 = this.nodeEvaluator.getBlockPathType(this.level, var13, var1, var14, this.mob, var3, var4, var5, true, true);
                float var20 = this.mob.getPathfindingMalus(var19);
                if (var20 < 0.0F || var20 >= 8.0F)
                    return false;
                if (var19 == BlockPathTypes.DAMAGE_FIRE || var19 == BlockPathTypes.DANGER_FIRE || var19 == BlockPathTypes.DAMAGE_OTHER)
                    return false;
            }
        }
    }
    return true;
}
Also used : BlockPathTypes(net.minecraft.world.level.pathfinder.BlockPathTypes)

Aggregations

BlockPathTypes (net.minecraft.world.level.pathfinder.BlockPathTypes)24 BlockPos (net.minecraft.core.BlockPos)15 Node (net.minecraft.world.level.pathfinder.Node)7 AABB (net.minecraft.world.phys.AABB)4 Random (java.util.Random)2 Predicate (java.util.function.Predicate)2 ToDoubleFunction (java.util.function.ToDoubleFunction)2 Nullable (javax.annotation.Nullable)2 FluidTags (net.minecraft.tags.FluidTags)2 Mth (net.minecraft.util.Mth)2 PathNavigation (net.minecraft.world.entity.ai.navigation.PathNavigation)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 WalkNodeEvaluator (net.minecraft.world.level.pathfinder.WalkNodeEvaluator)2 Vec3 (net.minecraft.world.phys.Vec3)2 EntitySnowLeopard (com.github.alexthe666.alexsmobs.entity.EntitySnowLeopard)1 IAnimatedEntity (com.github.alexthe666.citadel.animation.IAnimatedEntity)1 ModConstants (com.teamresourceful.resourcefulbees.lib.constants.ModConstants)1 HALF_PI (com.teamresourceful.resourcefulbees.utils.MathUtils.HALF_PI)1 EnumSet (java.util.EnumSet)1 DamageSource (net.minecraft.world.damagesource.DamageSource)1