| 1 | package net.minecraft.world.entity.animal.axolotl;
|
| 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 net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.tags.FluidTags;
|
| 12 | import net.minecraft.util.valueproviders.UniformInt;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.entity.EntityTypes;
|
| 15 | import net.minecraft.world.entity.LivingEntity;
|
| 16 | import net.minecraft.world.entity.ai.ActivityData;
|
| 17 | import net.minecraft.world.entity.ai.Brain;
|
| 18 | import net.minecraft.world.entity.ai.behavior.AnimalMakeLove;
|
| 19 | import net.minecraft.world.entity.ai.behavior.BabyFollowAdult;
|
| 20 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 21 | import net.minecraft.world.entity.ai.behavior.CountDownCooldownTicks;
|
| 22 | import net.minecraft.world.entity.ai.behavior.EraseMemoryIf;
|
| 23 | import net.minecraft.world.entity.ai.behavior.FollowTemptation;
|
| 24 | import net.minecraft.world.entity.ai.behavior.GateBehavior;
|
| 25 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 26 | import net.minecraft.world.entity.ai.behavior.MeleeAttack;
|
| 27 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 28 | import net.minecraft.world.entity.ai.behavior.PositionTracker;
|
| 29 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 30 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 31 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
|
| 32 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromAttackTargetIfTargetOutOfReach;
|
| 33 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
|
| 34 | import net.minecraft.world.entity.ai.behavior.StartAttacking;
|
| 35 | import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
|
| 36 | import net.minecraft.world.entity.ai.behavior.TryFindLiquid;
|
| 37 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 38 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 39 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 40 | import net.minecraft.world.entity.schedule.Activity;
|
| 41 | import net.minecraft.world.level.Level;
|
| 42 |
|
| 43 | public class AxolotlAi {
|
| 44 | private static final UniformInt ADULT_FOLLOW_RANGE = UniformInt.of(5, 16);
|
| 45 | private static final float SPEED_MULTIPLIER_WHEN_MAKING_LOVE = 0.2F;
|
| 46 | private static final float SPEED_MULTIPLIER_ON_LAND = 0.15F;
|
| 47 | private static final float SPEED_MULTIPLIER_WHEN_IDLING_IN_WATER = 0.5F;
|
| 48 | private static final float SPEED_MULTIPLIER_WHEN_CHASING_IN_WATER = 0.6F;
|
| 49 | private static final float SPEED_MULTIPLIER_WHEN_FOLLOWING_ADULT_IN_WATER = 0.6F;
|
| 50 |
|
| 51 | protected static List<ActivityData<Axolotl>> getActivities() {
|
| 52 | return List.of(initCoreActivity(), initIdleActivity(), initFightActivity(), initPlayDeadActivity());
|
| 53 | }
|
| 54 |
|
| 55 | protected static ActivityData<Axolotl> initPlayDeadActivity() {
|
| 56 | return ActivityData.create(
|
| 57 | Activity.PLAY_DEAD,
|
| 58 | ImmutableList.of(Pair.of(0, new PlayDead()), Pair.of(1, EraseMemoryIf.create(BehaviorUtils::isBreeding, MemoryModuleType.PLAY_DEAD_TICKS))),
|
| 59 | ImmutableSet.of(Pair.of(MemoryModuleType.PLAY_DEAD_TICKS, MemoryStatus.VALUE_PRESENT)),
|
| 60 | ImmutableSet.of(MemoryModuleType.PLAY_DEAD_TICKS)
|
| 61 | );
|
| 62 | }
|
| 63 |
|
| 64 | protected static ActivityData<Axolotl> initFightActivity() {
|
| 65 | return ActivityData.create(
|
| 66 | Activity.FIGHT,
|
| 67 | 0,
|
| 68 | ImmutableList.of(
|
| 69 | StopAttackingIfTargetInvalid.create(Axolotl::onStopAttacking),
|
| 70 | SetWalkTargetFromAttackTargetIfTargetOutOfReach.create(AxolotlAi::getSpeedModifierChasing),
|
| 71 | MeleeAttack.create(20),
|
| 72 | EraseMemoryIf.create(BehaviorUtils::isBreeding, MemoryModuleType.ATTACK_TARGET)
|
| 73 | ),
|
| 74 | MemoryModuleType.ATTACK_TARGET
|
| 75 | );
|
| 76 | }
|
| 77 |
|
| 78 | protected static ActivityData<Axolotl> initCoreActivity() {
|
| 79 | return ActivityData.create(
|
| 80 | Activity.CORE,
|
| 81 | 0,
|
| 82 | ImmutableList.of(
|
| 83 | new LookAtTargetSink(45, 90), new MoveToTargetSink(), ValidatePlayDead.create(), new CountDownCooldownTicks(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS)
|
| 84 | )
|
| 85 | );
|
| 86 | }
|
| 87 |
|
| 88 | protected static ActivityData<Axolotl> initIdleActivity() {
|
| 89 | return ActivityData.create(
|
| 90 | Activity.IDLE,
|
| 91 | ImmutableList.of(
|
| 92 | Pair.of(0, SetEntityLookTargetSometimes.create(EntityTypes.PLAYER, 6.0F, UniformInt.of(30, 60))),
|
| 93 | Pair.of(1, new AnimalMakeLove(EntityTypes.AXOLOTL, 0.2F, 2)),
|
| 94 | Pair.of(
|
| 95 | 2,
|
| 96 | new RunOne<>(
|
| 97 | ImmutableList.of(
|
| 98 | Pair.of(new FollowTemptation(AxolotlAi::getSpeedModifier), 1),
|
| 99 | Pair.of(BabyFollowAdult.create(ADULT_FOLLOW_RANGE, AxolotlAi::getSpeedModifierFollowingAdult, MemoryModuleType.NEAREST_VISIBLE_ADULT, false), 1)
|
| 100 | )
|
| 101 | )
|
| 102 | ),
|
| 103 | Pair.of(3, StartAttacking.create(AxolotlAi::findNearestValidAttackTarget)),
|
| 104 | Pair.of(3, TryFindLiquid.create(6, 0.15F, FluidTags.AXOLOTL_TRIES_TO_FIND)),
|
| 105 | Pair.of(
|
| 106 | 4,
|
| 107 | new GateBehavior<>(
|
| 108 | ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 109 | ImmutableSet.of(),
|
| 110 | GateBehavior.OrderPolicy.ORDERED,
|
| 111 | GateBehavior.RunningPolicy.TRY_ALL,
|
| 112 | ImmutableList.of(
|
| 113 | Pair.of(RandomStroll.swim(0.5F), 2),
|
| 114 | Pair.of(RandomStroll.stroll(0.15F, false), 2),
|
| 115 | Pair.of(SetWalkTargetFromLookTarget.create(AxolotlAi::canSetWalkTargetFromLookTarget, AxolotlAi::getSpeedModifier, 3), 3),
|
| 116 | Pair.of(BehaviorBuilder.triggerIf(Entity::isInWater), 5),
|
| 117 | Pair.of(BehaviorBuilder.triggerIf(Entity::onGround), 5)
|
| 118 | )
|
| 119 | )
|
| 120 | )
|
| 121 | )
|
| 122 | );
|
| 123 | }
|
| 124 |
|
| 125 | private static boolean canSetWalkTargetFromLookTarget(final LivingEntity body) {
|
| 126 | Level level = body.level();
|
| 127 | Optional<PositionTracker> lookTarget = body.getBrain().getMemory(MemoryModuleType.LOOK_TARGET);
|
| 128 | if (lookTarget.isPresent()) {
|
| 129 | BlockPos pos = lookTarget.get().currentBlockPosition();
|
| 130 | return level.isWaterAt(pos) == body.isInWater();
|
| 131 | } else {
|
| 132 | return false;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | public static void updateActivity(final Axolotl body) {
|
| 137 | Brain<Axolotl> brain = body.getBrain();
|
| 138 | Activity oldActivity = brain.getActiveNonCoreActivity().orElse(null);
|
| 139 | if (oldActivity != Activity.PLAY_DEAD) {
|
| 140 | brain.setActiveActivityToFirstValid(ImmutableList.of(Activity.PLAY_DEAD, Activity.FIGHT, Activity.IDLE));
|
| 141 | if (oldActivity == Activity.FIGHT && brain.getActiveNonCoreActivity().orElse(null) != Activity.FIGHT) {
|
| 142 | brain.setMemoryWithExpiry(MemoryModuleType.HAS_HUNTING_COOLDOWN, true, 2400L);
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | private static float getSpeedModifierChasing(final LivingEntity mob) {
|
| 148 | return mob.isInWater() ? 0.6F : 0.15F;
|
| 149 | }
|
| 150 |
|
| 151 | private static float getSpeedModifierFollowingAdult(final LivingEntity mob) {
|
| 152 | return mob.isInWater() ? 0.6F : 0.15F;
|
| 153 | }
|
| 154 |
|
| 155 | private static float getSpeedModifier(final LivingEntity mob) {
|
| 156 | return mob.isInWater() ? 0.5F : 0.15F;
|
| 157 | }
|
| 158 |
|
| 159 | private static Optional<? extends LivingEntity> findNearestValidAttackTarget(final ServerLevel level, final Axolotl body) {
|
| 160 | return BehaviorUtils.isBreeding(body) ? Optional.empty() : body.getBrain().getMemory(MemoryModuleType.NEAREST_ATTACKABLE);
|
| 161 | }
|
| 162 | }
|
| 163 |
|