| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.tags.DamageTypeTags;
|
| 9 | import net.minecraft.tags.FluidTags;
|
| 10 | import net.minecraft.tags.TagKey;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.damagesource.DamageType;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.entity.PathfinderMob;
|
| 15 | import net.minecraft.world.entity.ai.Brain;
|
| 16 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 17 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 18 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 19 | import net.minecraft.world.entity.ai.util.AirAndWaterRandomPos;
|
| 20 | import net.minecraft.world.entity.ai.util.LandRandomPos;
|
| 21 | import net.minecraft.world.level.LevelReader;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.blockscan.OrderedBlockMatcher;
|
| 24 | import net.minecraft.world.phys.Vec3;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class AnimalPanic<E extends PathfinderMob> extends Behavior<E> {
|
| 28 | private static final int PANIC_MIN_DURATION = 100;
|
| 29 | private static final int PANIC_MAX_DURATION = 120;
|
| 30 | private static final int PANIC_DISTANCE_HORIZONTAL = 5;
|
| 31 | private static final int PANIC_DISTANCE_VERTICAL = 4;
|
| 32 | private final float speedMultiplier;
|
| 33 | private final Function<PathfinderMob, TagKey<DamageType>> panicCausingDamageTypes;
|
| 34 | private final Function<E, Vec3> positionGetter;
|
| 35 |
|
| 36 | public AnimalPanic(final float speedMultiplier) {
|
| 37 | this(speedMultiplier, var0 -> DamageTypeTags.PANIC_CAUSES, mob -> LandRandomPos.getPos(mob, 5, 4));
|
| 38 | }
|
| 39 |
|
| 40 | public AnimalPanic(final float speedMultiplier, final int flyHeight) {
|
| 41 | this(
|
| 42 | speedMultiplier,
|
| 43 | var0 -> DamageTypeTags.PANIC_CAUSES,
|
| 44 | mob -> AirAndWaterRandomPos.getPos(mob, 5, 4, flyHeight, mob.getViewVector(0.0F).x, mob.getViewVector(0.0F).z, (float) (Math.PI / 2))
|
| 45 | );
|
| 46 | }
|
| 47 |
|
| 48 | public AnimalPanic(final float speedMultiplier, final Function<PathfinderMob, TagKey<DamageType>> panicCausingDamageTypes) {
|
| 49 | this(speedMultiplier, panicCausingDamageTypes, mob -> LandRandomPos.getPos(mob, 5, 4));
|
| 50 | }
|
| 51 |
|
| 52 | public AnimalPanic(
|
| 53 | final float speedMultiplier, final Function<PathfinderMob, TagKey<DamageType>> panicCausingDamageTypes, final Function<E, Vec3> positionGetter
|
| 54 | ) {
|
| 55 | super(Map.of(MemoryModuleType.IS_PANICKING, MemoryStatus.REGISTERED, MemoryModuleType.HURT_BY, MemoryStatus.REGISTERED), 100, 120);
|
| 56 | this.speedMultiplier = speedMultiplier;
|
| 57 | this.panicCausingDamageTypes = panicCausingDamageTypes;
|
| 58 | this.positionGetter = positionGetter;
|
| 59 | }
|
| 60 |
|
| 61 | protected boolean checkExtraStartConditions(final ServerLevel level, final E body) {
|
| 62 | return body.getBrain().getMemory(MemoryModuleType.HURT_BY).map(d -> d.is(this.panicCausingDamageTypes.apply(body))).orElse(false)
|
| 63 | || body.getBrain().hasMemoryValue(MemoryModuleType.IS_PANICKING);
|
| 64 | }
|
| 65 |
|
| 66 | protected boolean canStillUse(final ServerLevel level, final E body, final long timestamp) {
|
| 67 | return true;
|
| 68 | }
|
| 69 |
|
| 70 | protected void start(final ServerLevel level, final E body, final long timestamp) {
|
| 71 | body.getBrain().setMemory(MemoryModuleType.IS_PANICKING, true);
|
| 72 | body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 73 | body.getNavigation().stop();
|
| 74 | }
|
| 75 |
|
| 76 | protected void stop(final ServerLevel level, final E body, final long timestamp) {
|
| 77 | Brain<?> brain = body.getBrain();
|
| 78 | brain.eraseMemory(MemoryModuleType.IS_PANICKING);
|
| 79 | }
|
| 80 |
|
| 81 | protected void tick(final ServerLevel level, final E body, final long timestamp) {
|
| 82 | if (body.getNavigation().isDone()) {
|
| 83 | Vec3 panicToPos = this.getPanicPos(body, level);
|
| 84 | if (panicToPos != null) {
|
| 85 | body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(panicToPos, this.speedMultiplier, 0));
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private @Nullable Vec3 getPanicPos(final E body, final ServerLevel level) {
|
| 91 | if (body.isOnFire()) {
|
| 92 | Optional<Vec3> nearestWater = this.lookForWater(level, body).map(Vec3::atBottomCenterOf);
|
| 93 | if (nearestWater.isPresent()) {
|
| 94 | return nearestWater.get();
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | return this.positionGetter.apply(body);
|
| 99 | }
|
| 100 |
|
| 101 | private Optional<BlockPos> lookForWater(final LevelReader level, final Entity mob) {
|
| 102 | BlockPos mobPosition = mob.blockPosition();
|
| 103 | if (!level.getBlockState(mobPosition).getCollisionShape(level, mobPosition).isEmpty()) {
|
| 104 | return Optional.empty();
|
| 105 | }
|
| 106 |
|
| 107 | OrderedBlockMatcher blockMatcher = level.findBlocksInBoxByManhattanDistance(mobPosition, 5, 1).filterState(AnimalPanic::isWater);
|
| 108 | return Mth.ceil(mob.getBbWidth()) == 2
|
| 109 | ? blockMatcher.findFirst(
|
| 110 | (pos, var2) -> isWater(level.getBlockState(pos.south())) && isWater(level.getBlockState(pos.east())) && isWater(level.getBlockState(pos.south().east()))
|
| 111 | )
|
| 112 | : blockMatcher.findFirst();
|
| 113 | }
|
| 114 |
|
| 115 | private static boolean isWater(final BlockState state) {
|
| 116 | return state.getFluidState().is(FluidTags.WATER);
|
| 117 | }
|
| 118 | }
|
| 119 |
|