| 1 | package net.minecraft.world.entity.animal.frog;
|
| 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.function.Predicate;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.tags.BlockTags;
|
| 12 | import net.minecraft.tags.FluidTags;
|
| 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.Entity;
|
| 17 | import net.minecraft.world.entity.EntityTypes;
|
| 18 | import net.minecraft.world.entity.Mob;
|
| 19 | import net.minecraft.world.entity.ai.ActivityData;
|
| 20 | import net.minecraft.world.entity.ai.behavior.AnimalMakeLove;
|
| 21 | import net.minecraft.world.entity.ai.behavior.AnimalPanic;
|
| 22 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 23 | import net.minecraft.world.entity.ai.behavior.CountDownCooldownTicks;
|
| 24 | import net.minecraft.world.entity.ai.behavior.Croak;
|
| 25 | import net.minecraft.world.entity.ai.behavior.FollowTemptation;
|
| 26 | import net.minecraft.world.entity.ai.behavior.GateBehavior;
|
| 27 | import net.minecraft.world.entity.ai.behavior.LongJumpMidJump;
|
| 28 | import net.minecraft.world.entity.ai.behavior.LongJumpToPreferredBlock;
|
| 29 | import net.minecraft.world.entity.ai.behavior.LongJumpToRandomPos;
|
| 30 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 31 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 32 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 33 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 34 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
|
| 35 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
|
| 36 | import net.minecraft.world.entity.ai.behavior.StartAttacking;
|
| 37 | import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
|
| 38 | import net.minecraft.world.entity.ai.behavior.TryFindLand;
|
| 39 | import net.minecraft.world.entity.ai.behavior.TryFindLandNearLiquid;
|
| 40 | import net.minecraft.world.entity.ai.behavior.TryLaySpawnOnFluidNearLand;
|
| 41 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 42 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 43 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 44 | import net.minecraft.world.entity.schedule.Activity;
|
| 45 | import net.minecraft.world.item.ItemStack;
|
| 46 | import net.minecraft.world.level.Level;
|
| 47 | import net.minecraft.world.level.block.Blocks;
|
| 48 | import net.minecraft.world.level.block.state.BlockState;
|
| 49 | import net.minecraft.world.level.pathfinder.PathType;
|
| 50 | import net.minecraft.world.level.pathfinder.PathfindingContext;
|
| 51 | import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
|
| 52 |
|
| 53 | public class FrogAi {
|
| 54 | private static final float SPEED_MULTIPLIER_WHEN_PANICKING = 2.0F;
|
| 55 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 1.0F;
|
| 56 | private static final float SPEED_MULTIPLIER_ON_LAND = 1.0F;
|
| 57 | private static final float SPEED_MULTIPLIER_IN_WATER = 0.75F;
|
| 58 | private static final UniformInt TIME_BETWEEN_LONG_JUMPS = UniformInt.of(100, 140);
|
| 59 | private static final int MAX_LONG_JUMP_HEIGHT = 2;
|
| 60 | private static final int MAX_LONG_JUMP_WIDTH = 4;
|
| 61 | private static final float MAX_JUMP_VELOCITY_MULTIPLIER = 3.5714288F;
|
| 62 | private static final float SPEED_MULTIPLIER_WHEN_TEMPTED = 1.25F;
|
| 63 |
|
| 64 | protected static void initMemories(final Frog body, final RandomSource random) {
|
| 65 | body.getBrain().setMemory(MemoryModuleType.LONG_JUMP_COOLDOWN_TICKS, TIME_BETWEEN_LONG_JUMPS.sample(random));
|
| 66 | }
|
| 67 |
|
| 68 | protected static List<ActivityData<Frog>> getActivities() {
|
| 69 | return List.of(initCoreActivity(), initIdleActivity(), initSwimActivity(), initLaySpawnActivity(), initTongueActivity(), initJumpActivity());
|
| 70 | }
|
| 71 |
|
| 72 | private static ActivityData<Frog> initCoreActivity() {
|
| 73 | return ActivityData.create(
|
| 74 | Activity.CORE,
|
| 75 | 0,
|
| 76 | ImmutableList.of(
|
| 77 | new AnimalPanic<>(2.0F),
|
| 78 | new LookAtTargetSink(45, 90),
|
| 79 | new MoveToTargetSink(),
|
| 80 | new CountDownCooldownTicks(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS),
|
| 81 | new CountDownCooldownTicks(MemoryModuleType.LONG_JUMP_COOLDOWN_TICKS)
|
| 82 | )
|
| 83 | );
|
| 84 | }
|
| 85 |
|
| 86 | private static ActivityData<Frog> initIdleActivity() {
|
| 87 | return ActivityData.create(
|
| 88 | Activity.IDLE,
|
| 89 | ImmutableList.of(
|
| 90 | Pair.of(0, SetEntityLookTargetSometimes.create(EntityTypes.PLAYER, 6.0F, UniformInt.of(30, 60))),
|
| 91 | Pair.of(0, new AnimalMakeLove(EntityTypes.FROG)),
|
| 92 | Pair.of(1, new FollowTemptation(s -> 1.25F)),
|
| 93 | Pair.of(2, StartAttacking.create((level, body) -> canAttack(body), (level, body) -> body.getBrain().getMemory(MemoryModuleType.NEAREST_ATTACKABLE))),
|
| 94 | Pair.of(3, TryFindLand.create(6, 1.0F)),
|
| 95 | Pair.of(
|
| 96 | 4,
|
| 97 | new RunOne<>(
|
| 98 | ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 99 | ImmutableList.of(
|
| 100 | Pair.of(RandomStroll.stroll(1.0F), 1),
|
| 101 | Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 1),
|
| 102 | Pair.of(new Croak(), 3),
|
| 103 | Pair.of(BehaviorBuilder.triggerIf(Entity::onGround), 2)
|
| 104 | )
|
| 105 | )
|
| 106 | )
|
| 107 | ),
|
| 108 | ImmutableSet.of(Pair.of(MemoryModuleType.LONG_JUMP_MID_JUMP, MemoryStatus.VALUE_ABSENT), Pair.of(MemoryModuleType.IS_IN_WATER, MemoryStatus.VALUE_ABSENT))
|
| 109 | );
|
| 110 | }
|
| 111 |
|
| 112 | private static ActivityData<Frog> initSwimActivity() {
|
| 113 | return ActivityData.create(
|
| 114 | Activity.SWIM,
|
| 115 | ImmutableList.of(
|
| 116 | Pair.of(0, SetEntityLookTargetSometimes.create(EntityTypes.PLAYER, 6.0F, UniformInt.of(30, 60))),
|
| 117 | Pair.of(1, new FollowTemptation(s -> 1.25F)),
|
| 118 | Pair.of(2, StartAttacking.create((level, body) -> canAttack(body), (level, body) -> body.getBrain().getMemory(MemoryModuleType.NEAREST_ATTACKABLE))),
|
| 119 | Pair.of(3, TryFindLand.create(8, 1.5F)),
|
| 120 | Pair.of(
|
| 121 | 5,
|
| 122 | new GateBehavior<>(
|
| 123 | ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 124 | ImmutableSet.of(),
|
| 125 | GateBehavior.OrderPolicy.ORDERED,
|
| 126 | GateBehavior.RunningPolicy.TRY_ALL,
|
| 127 | ImmutableList.of(
|
| 128 | Pair.of(RandomStroll.swim(0.75F), 1),
|
| 129 | Pair.of(RandomStroll.stroll(1.0F, true), 1),
|
| 130 | Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 1),
|
| 131 | Pair.of(BehaviorBuilder.triggerIf(Entity::isInWater), 5)
|
| 132 | )
|
| 133 | )
|
| 134 | )
|
| 135 | ),
|
| 136 | ImmutableSet.of(Pair.of(MemoryModuleType.LONG_JUMP_MID_JUMP, MemoryStatus.VALUE_ABSENT), Pair.of(MemoryModuleType.IS_IN_WATER, MemoryStatus.VALUE_PRESENT))
|
| 137 | );
|
| 138 | }
|
| 139 |
|
| 140 | private static ActivityData<Frog> initLaySpawnActivity() {
|
| 141 | return ActivityData.create(
|
| 142 | Activity.LAY_SPAWN,
|
| 143 | ImmutableList.of(
|
| 144 | Pair.of(0, SetEntityLookTargetSometimes.create(EntityTypes.PLAYER, 6.0F, UniformInt.of(30, 60))),
|
| 145 | Pair.of(1, StartAttacking.create((level, body) -> canAttack(body), (level, body) -> body.getBrain().getMemory(MemoryModuleType.NEAREST_ATTACKABLE))),
|
| 146 | Pair.of(2, TryFindLandNearLiquid.create(8, 1.0F, FluidTags.FROG_TRIES_TO_FIND_LAND_NEAR)),
|
| 147 | Pair.of(3, TryLaySpawnOnFluidNearLand.create(Blocks.FROGSPAWN)),
|
| 148 | Pair.of(
|
| 149 | 4,
|
| 150 | new RunOne<>(
|
| 151 | ImmutableList.of(
|
| 152 | Pair.of(RandomStroll.stroll(1.0F), 2),
|
| 153 | Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 1),
|
| 154 | Pair.of(new Croak(), 2),
|
| 155 | Pair.of(BehaviorBuilder.triggerIf(Entity::onGround), 1)
|
| 156 | )
|
| 157 | )
|
| 158 | )
|
| 159 | ),
|
| 160 | ImmutableSet.of(Pair.of(MemoryModuleType.LONG_JUMP_MID_JUMP, MemoryStatus.VALUE_ABSENT), Pair.of(MemoryModuleType.IS_PREGNANT, MemoryStatus.VALUE_PRESENT))
|
| 161 | );
|
| 162 | }
|
| 163 |
|
| 164 | private static ActivityData<Frog> initJumpActivity() {
|
| 165 | return ActivityData.create(
|
| 166 | Activity.LONG_JUMP,
|
| 167 | ImmutableList.of(
|
| 168 | Pair.of(0, new LongJumpMidJump(TIME_BETWEEN_LONG_JUMPS, SoundEvents.FROG_STEP)),
|
| 169 | Pair.of(
|
| 170 | 1,
|
| 171 | new LongJumpToPreferredBlock<>(
|
| 172 | TIME_BETWEEN_LONG_JUMPS, 2, 4, 3.5714288F, var0 -> SoundEvents.FROG_LONG_JUMP, BlockTags.FROG_PREFER_JUMP_TO, 0.5F, FrogAi::isAcceptableLandingSpot
|
| 173 | )
|
| 174 | )
|
| 175 | ),
|
| 176 | ImmutableSet.of(
|
| 177 | Pair.of(MemoryModuleType.TEMPTING_PLAYER, MemoryStatus.VALUE_ABSENT),
|
| 178 | Pair.of(MemoryModuleType.BREED_TARGET, MemoryStatus.VALUE_ABSENT),
|
| 179 | Pair.of(MemoryModuleType.LONG_JUMP_COOLDOWN_TICKS, MemoryStatus.VALUE_ABSENT),
|
| 180 | Pair.of(MemoryModuleType.IS_IN_WATER, MemoryStatus.VALUE_ABSENT)
|
| 181 | )
|
| 182 | );
|
| 183 | }
|
| 184 |
|
| 185 | private static ActivityData<Frog> initTongueActivity() {
|
| 186 | return ActivityData.create(
|
| 187 | Activity.TONGUE,
|
| 188 | 0,
|
| 189 | ImmutableList.of(StopAttackingIfTargetInvalid.create(), new ShootTongue(SoundEvents.FROG_TONGUE, SoundEvents.FROG_EAT)),
|
| 190 | MemoryModuleType.ATTACK_TARGET
|
| 191 | );
|
| 192 | }
|
| 193 |
|
| 194 | private static <E extends Mob> boolean isAcceptableLandingSpot(final E body, final BlockPos targetPos) {
|
| 195 | Level level = body.level();
|
| 196 | BlockPos below = targetPos.below();
|
| 197 | if (level.getFluidState(targetPos).isEmpty() && level.getFluidState(below).isEmpty() && level.getFluidState(targetPos.above()).isEmpty()) {
|
| 198 | BlockState bs = level.getBlockState(targetPos);
|
| 199 | BlockState bsBelow = level.getBlockState(below);
|
| 200 | if (!bs.is(BlockTags.FROG_PREFER_JUMP_TO) && !bsBelow.is(BlockTags.FROG_PREFER_JUMP_TO)) {
|
| 201 | PathfindingContext context = new PathfindingContext(body.level(), body);
|
| 202 | PathType pathType = WalkNodeEvaluator.getPathTypeStatic(context, targetPos.mutable());
|
| 203 | PathType pathTypeBelow = WalkNodeEvaluator.getPathTypeStatic(context, below.mutable());
|
| 204 | return pathType != PathType.TRAPDOOR && (!bs.isAir() || pathTypeBelow != PathType.TRAPDOOR)
|
| 205 | ? LongJumpToRandomPos.defaultAcceptableLandingSpot(body, targetPos)
|
| 206 | : true;
|
| 207 | } else {
|
| 208 | return true;
|
| 209 | }
|
| 210 | } else {
|
| 211 | return false;
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | private static boolean canAttack(final Mob mob) {
|
| 216 | return !BehaviorUtils.isBreeding(mob);
|
| 217 | }
|
| 218 |
|
| 219 | public static void updateActivity(final Frog body) {
|
| 220 | body.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.TONGUE, Activity.LAY_SPAWN, Activity.LONG_JUMP, Activity.SWIM, Activity.IDLE));
|
| 221 | }
|
| 222 |
|
| 223 | public static Predicate<ItemStack> getTemptations() {
|
| 224 | return i -> i.is(ItemTags.FROG_FOOD);
|
| 225 | }
|
| 226 | }
|
| 227 |
|