Search in sources :

Example 6 with EntityNavigation

use of net.minecraft.entity.ai.pathing.EntityNavigation in project Sprout by Toadstool-Studios.

the class FindPlantGoal method start.

@Override
public void start() {
    super.start();
    if (targetPosition != null && !elephant.getIfEating()) {
        EntityNavigation nav = elephant.getNavigation();
        nav.startMovingTo(targetPosition.getX() + 0.5, targetPosition.getY() + 0.75, targetPosition.getZ() + 0.5, 0.3);
    }
}
Also used : EntityNavigation(net.minecraft.entity.ai.pathing.EntityNavigation)

Example 7 with EntityNavigation

use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.

the class LongJumpTaskMixin method run.

/**
 * @author 2No2Name
 * @reason only evaluate 20+ instead of ~100 possible jumps without affecting behavior
 * [VanillaCopy] the whole method, commented changes
 */
@Overwrite
public void run(ServerWorld serverWorld, MobEntity mobEntity, long l) {
    this.potentialTargets.clear();
    this.potentialWeights.clear();
    int potentialTotalWeight = 0;
    this.lastTarget = Optional.empty();
    this.cooldown = MAX_COOLDOWN;
    this.targets.clear();
    this.lastPos = Optional.of(mobEntity.getPos());
    BlockPos goatPos = mobEntity.getBlockPos();
    int goatX = goatPos.getX();
    int goatY = goatPos.getY();
    int goatZ = goatPos.getZ();
    Iterable<BlockPos> iterable = BlockPos.iterate(goatX - this.horizontalRange, goatY - this.verticalRange, goatZ - this.horizontalRange, goatX + this.horizontalRange, goatY + this.verticalRange, goatZ + this.horizontalRange);
    EntityNavigation entityNavigation = mobEntity.getNavigation();
    BlockPos.Mutable targetPosCopy = new BlockPos.Mutable();
    for (BlockPos targetPos : iterable) {
        if (goatX == targetPos.getX() && goatZ == targetPos.getZ()) {
            continue;
        }
        double squaredDistance = targetPos.getSquaredDistance(goatPos);
        // Optimization: Evaluate the flight path check later (after random selection, but before world can be modified)
        if (entityNavigation.isValidPosition(targetPos) && mobEntity.getPathfindingPenalty(LandPathNodeMaker.getLandNodeType(mobEntity.world, targetPosCopy.set(targetPos))) == 0.0F) {
            this.potentialTargets.add(targetPos.asLong());
            int weight = MathHelper.ceil(squaredDistance);
            this.potentialWeights.add((short) weight);
            potentialTotalWeight += weight;
        }
    }
    // up to MAX_COOLDOWN random targets can be selected in keepRunning, so only this number of targets needs to be generated
    while (this.targets.size() < MAX_COOLDOWN) {
        // the number of random calls will be different from vanilla, but this is not reasonably detectable (not affecting world generation)
        if (potentialTotalWeight == 0) {
            // collection is empty/fully consumed, no more possible targets available
            return;
        }
        int chosenIndex = findIndex(this.potentialWeights, serverWorld.random.nextInt(potentialTotalWeight));
        long chosenPos = this.potentialTargets.getLong(chosenIndex);
        short chosenWeight = this.potentialWeights.set(chosenIndex, (short) 0);
        potentialTotalWeight -= chosenWeight;
        // Very expensive method call, it shifts bounding boxes around and checks for collisions with them
        Optional<Vec3d> optional = this.getRammingVelocity(mobEntity, Vec3d.ofCenter(targetPosCopy.set(chosenPos)));
        // noinspection OptionalIsPresent
        if (optional.isPresent()) {
            // the weight in Target should be unused, as the random selection already took place
            this.targets.add(new LongJumpTask.Target(new BlockPos(targetPosCopy), optional.get(), chosenWeight));
        }
    }
}
Also used : Vec3d(net.minecraft.util.math.Vec3d) LongJumpTask(net.minecraft.entity.ai.brain.task.LongJumpTask) EntityNavigation(net.minecraft.entity.ai.pathing.EntityNavigation) BlockPos(net.minecraft.util.math.BlockPos) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 8 with EntityNavigation

use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.

the class MobEntityMixin method updateNavigationRegistration.

@Override
public void updateNavigationRegistration() {
    if (this.isRegisteredToWorld()) {
        EntityNavigation navigation = this.getNavigation();
        if (this.registeredNavigation != navigation) {
            ((ServerWorldExtended) this.world).setNavigationInactive((MobEntity) (Object) this);
            this.registeredNavigation = navigation;
            if (navigation.getCurrentPath() != null) {
                ((ServerWorldExtended) this.world).setNavigationActive((MobEntity) (Object) this);
            }
        }
    }
}
Also used : EntityNavigation(net.minecraft.entity.ai.pathing.EntityNavigation) ServerWorldExtended(me.jellysquid.mods.lithium.common.world.ServerWorldExtended)

Example 9 with EntityNavigation

use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.

the class ServerEntityHandlerMixin method startListeningOnEntityLoad.

@Redirect(method = "startTracking(Lnet/minecraft/entity/Entity;)V", at = @At(value = "INVOKE", target = "Ljava/util/Set;add(Ljava/lang/Object;)Z"))
private boolean startListeningOnEntityLoad(Set<MobEntity> set, Object mobEntityObj) {
    MobEntity mobEntity = (MobEntity) mobEntityObj;
    EntityNavigation navigation = mobEntity.getNavigation();
    ((NavigatingEntity) mobEntity).setRegisteredToWorld(navigation);
    if (navigation.getCurrentPath() != null) {
        ((ServerWorldExtended) this.outer).setNavigationActive(mobEntity);
    }
    return set.add(mobEntity);
}
Also used : EntityNavigation(net.minecraft.entity.ai.pathing.EntityNavigation) ServerWorldExtended(me.jellysquid.mods.lithium.common.world.ServerWorldExtended) NavigatingEntity(me.jellysquid.mods.lithium.common.entity.NavigatingEntity) MobEntity(net.minecraft.entity.mob.MobEntity) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 10 with EntityNavigation

use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.

the class ServerEntityHandlerMixin method stopListeningOnEntityUnload.

@Redirect(method = "stopTracking(Lnet/minecraft/entity/Entity;)V", at = @At(value = "INVOKE", target = "Ljava/util/Set;remove(Ljava/lang/Object;)Z"))
private boolean stopListeningOnEntityUnload(Set<MobEntity> set, Object mobEntityObj) {
    MobEntity mobEntity = (MobEntity) mobEntityObj;
    NavigatingEntity navigatingEntity = (NavigatingEntity) mobEntity;
    if (navigatingEntity.isRegisteredToWorld()) {
        EntityNavigation registeredNavigation = navigatingEntity.getRegisteredNavigation();
        if (registeredNavigation.getCurrentPath() != null) {
            ((ServerWorldExtended) this.outer).setNavigationInactive(mobEntity);
        }
        navigatingEntity.setRegisteredToWorld(null);
    }
    return set.remove(mobEntity);
}
Also used : EntityNavigation(net.minecraft.entity.ai.pathing.EntityNavigation) ServerWorldExtended(me.jellysquid.mods.lithium.common.world.ServerWorldExtended) NavigatingEntity(me.jellysquid.mods.lithium.common.entity.NavigatingEntity) MobEntity(net.minecraft.entity.mob.MobEntity) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

EntityNavigation (net.minecraft.entity.ai.pathing.EntityNavigation)10 Redirect (org.spongepowered.asm.mixin.injection.Redirect)4 ServerWorldExtended (me.jellysquid.mods.lithium.common.world.ServerWorldExtended)3 EntityNavigationExtended (me.jellysquid.mods.lithium.common.entity.EntityNavigationExtended)2 NavigatingEntity (me.jellysquid.mods.lithium.common.entity.NavigatingEntity)2 MobEntity (net.minecraft.entity.mob.MobEntity)2 LivingEntity (net.minecraft.entity.LivingEntity)1 LongJumpTask (net.minecraft.entity.ai.brain.task.LongJumpTask)1 PathNodeMaker (net.minecraft.entity.ai.pathing.PathNodeMaker)1 BlockStateParticleEffect (net.minecraft.particle.BlockStateParticleEffect)1 BlockPos (net.minecraft.util.math.BlockPos)1 Vec3d (net.minecraft.util.math.Vec3d)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1