| 1 | package net.minecraft.world.entity.animal.nautilus;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableMap;
|
| 5 | import com.google.common.collect.ImmutableSet;
|
| 6 | import com.mojang.datafixers.util.Pair;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.tags.EntityTypeTags;
|
| 13 | import net.minecraft.tags.ItemTags;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.valueproviders.UniformInt;
|
| 16 | import net.minecraft.world.entity.EntityTypes;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.ai.ActivityData;
|
| 19 | import net.minecraft.world.entity.ai.behavior.AnimalMakeLove;
|
| 20 | import net.minecraft.world.entity.ai.behavior.AnimalPanic;
|
| 21 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 22 | import net.minecraft.world.entity.ai.behavior.ChargeAttack;
|
| 23 | import net.minecraft.world.entity.ai.behavior.CountDownCooldownTicks;
|
| 24 | import net.minecraft.world.entity.ai.behavior.FollowTemptation;
|
| 25 | import net.minecraft.world.entity.ai.behavior.GateBehavior;
|
| 26 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 27 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 28 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 29 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
|
| 30 | import net.minecraft.world.entity.ai.behavior.StartAttacking;
|
| 31 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 32 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 33 | import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
|
| 34 | import net.minecraft.world.entity.ai.sensing.Sensor;
|
| 35 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 36 | import net.minecraft.world.entity.schedule.Activity;
|
| 37 | import net.minecraft.world.item.ItemStack;
|
| 38 | import net.minecraft.world.level.gamerules.GameRules;
|
| 39 |
|
| 40 | public class NautilusAi {
|
| 41 | private static final float SPEED_MULTIPLIER_WHEN_IDLING_IN_WATER = 1.0F;
|
| 42 | private static final float SPEED_MULTIPLIER_WHEN_TEMPTED = 1.3F;
|
| 43 | private static final float SPEED_MULTIPLIER_WHEN_MAKING_LOVE = 0.4F;
|
| 44 | private static final float SPEED_MULTIPLIER_WHEN_PANICKING = 1.6F;
|
| 45 | private static final UniformInt TIME_BETWEEN_NON_PLAYER_ATTACKS = UniformInt.of(2400, 3600);
|
| 46 | private static final float SPEED_WHEN_ATTACKING = 0.6F;
|
| 47 | private static final float ATTACK_KNOCKBACK_FORCE = 2.0F;
|
| 48 | private static final int ANGER_DURATION = 400;
|
| 49 | private static final int TIME_BETWEEN_ATTACKS = 80;
|
| 50 | private static final double MAX_CHARGE_DISTANCE = 12.0;
|
| 51 | private static final double MAX_TARGET_DETECTION_DISTANCE = 11.0;
|
| 52 | protected static final TargetingConditions ATTACK_TARGET_CONDITIONS = TargetingConditions.forCombat()
|
| 53 | .selector(
|
| 54 | (target, level) -> (level.getGameRules().get(GameRules.MOB_GRIEFING) || !target.is(EntityTypes.ARMOR_STAND))
|
| 55 | && level.getWorldBorder().isWithinBounds(target.getBoundingBox())
|
| 56 | );
|
| 57 |
|
| 58 | protected static void initMemories(final AbstractNautilus body, final RandomSource random) {
|
| 59 | body.getBrain().setMemory(MemoryModuleType.ATTACK_TARGET_COOLDOWN, TIME_BETWEEN_NON_PLAYER_ATTACKS.sample(random));
|
| 60 | }
|
| 61 |
|
| 62 | public static List<ActivityData<Nautilus>> getActivities() {
|
| 63 | return List.of(initCoreActivity(), initIdleActivity(), initFightActivity());
|
| 64 | }
|
| 65 |
|
| 66 | private static ActivityData<Nautilus> initCoreActivity() {
|
| 67 | return ActivityData.create(
|
| 68 | Activity.CORE,
|
| 69 | 0,
|
| 70 | ImmutableList.of(
|
| 71 | new AnimalPanic<>(1.6F),
|
| 72 | new LookAtTargetSink(45, 90),
|
| 73 | new MoveToTargetSink(),
|
| 74 | new CountDownCooldownTicks(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS),
|
| 75 | new CountDownCooldownTicks(MemoryModuleType.CHARGE_COOLDOWN_TICKS),
|
| 76 | new CountDownCooldownTicks(MemoryModuleType.ATTACK_TARGET_COOLDOWN)
|
| 77 | )
|
| 78 | );
|
| 79 | }
|
| 80 |
|
| 81 | private static ActivityData<Nautilus> initIdleActivity() {
|
| 82 | return ActivityData.create(
|
| 83 | Activity.IDLE,
|
| 84 | ImmutableList.of(
|
| 85 | Pair.of(1, new AnimalMakeLove(EntityTypes.NAUTILUS, 0.4F, 2)),
|
| 86 | Pair.of(2, new FollowTemptation(mob -> 1.3F, mob -> mob.isBaby() ? 2.5 : 3.5)),
|
| 87 | Pair.of(3, StartAttacking.create(NautilusAi::findNearestValidAttackTarget)),
|
| 88 | Pair.of(
|
| 89 | 4,
|
| 90 | new GateBehavior<>(
|
| 91 | ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 92 | ImmutableSet.of(),
|
| 93 | GateBehavior.OrderPolicy.ORDERED,
|
| 94 | GateBehavior.RunningPolicy.TRY_ALL,
|
| 95 | ImmutableList.of(Pair.of(RandomStroll.swim(1.0F), 2), Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 3))
|
| 96 | )
|
| 97 | )
|
| 98 | )
|
| 99 | );
|
| 100 | }
|
| 101 |
|
| 102 | private static ActivityData<Nautilus> initFightActivity() {
|
| 103 | return ActivityData.create(
|
| 104 | Activity.FIGHT,
|
| 105 | ImmutableList.of(Pair.of(0, new ChargeAttack(80, ATTACK_TARGET_CONDITIONS, 0.6F, 2.0F, 12.0, 11.0, SoundEvents.NAUTILUS_DASH))),
|
| 106 | ImmutableSet.of(
|
| 107 | Pair.of(MemoryModuleType.ATTACK_TARGET, MemoryStatus.VALUE_PRESENT),
|
| 108 | Pair.of(MemoryModuleType.TEMPTING_PLAYER, MemoryStatus.VALUE_ABSENT),
|
| 109 | Pair.of(MemoryModuleType.BREED_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 110 | Pair.of(MemoryModuleType.CHARGE_COOLDOWN_TICKS, MemoryStatus.VALUE_ABSENT)
|
| 111 | )
|
| 112 | );
|
| 113 | }
|
| 114 |
|
| 115 | protected static Optional<? extends LivingEntity> findNearestValidAttackTarget(final ServerLevel level, final AbstractNautilus body) {
|
| 116 | if (!BehaviorUtils.isBreeding(body) && body.isInWater() && !body.isBaby() && !body.isTame()) {
|
| 117 | Optional<LivingEntity> angryAt = BehaviorUtils.getLivingEntityFromUUIDMemory(body, MemoryModuleType.ANGRY_AT)
|
| 118 | .filter(entity -> entity.isInWater() && Sensor.isEntityAttackableIgnoringLineOfSight(level, body, entity));
|
| 119 | if (angryAt.isPresent()) {
|
| 120 | return angryAt;
|
| 121 | }
|
| 122 |
|
| 123 | if (body.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET_COOLDOWN)) {
|
| 124 | return Optional.empty();
|
| 125 | }
|
| 126 |
|
| 127 | RandomSource random = level.getRandom();
|
| 128 | body.getBrain().setMemory(MemoryModuleType.ATTACK_TARGET_COOLDOWN, TIME_BETWEEN_NON_PLAYER_ATTACKS.sample(random));
|
| 129 | return random.nextFloat() < 0.5F
|
| 130 | ? Optional.empty()
|
| 131 | : body.getBrain()
|
| 132 | .getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
|
| 133 | .orElse(NearestVisibleLivingEntities.empty())
|
| 134 | .findClosest(NautilusAi::isHostileTarget);
|
| 135 | } else {
|
| 136 | return Optional.empty();
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | protected static void setAngerTarget(final ServerLevel level, final AbstractNautilus body, final LivingEntity target) {
|
| 141 | if (Sensor.isEntityAttackableIgnoringLineOfSight(level, body, target)) {
|
| 142 | body.getBrain().eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 143 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.ANGRY_AT, target.getUUID(), 400L);
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | private static boolean isHostileTarget(final LivingEntity mob) {
|
| 148 | return mob.isInWater() && mob.is(EntityTypeTags.NAUTILUS_HOSTILES);
|
| 149 | }
|
| 150 |
|
| 151 | public static void updateActivity(final Nautilus body) {
|
| 152 | body.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.FIGHT, Activity.IDLE));
|
| 153 | }
|
| 154 |
|
| 155 | public static Predicate<ItemStack> getTemptations() {
|
| 156 | return i -> i.is(ItemTags.NAUTILUS_FOOD);
|
| 157 | }
|
| 158 | }
|
| 159 |
|