| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import java.util.function.Function;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.world.entity.Entity;
|
| 8 | import net.minecraft.world.entity.PathfinderMob;
|
| 9 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 10 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 11 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 12 | import net.minecraft.world.entity.ai.util.AirAndWaterRandomPos;
|
| 13 | import net.minecraft.world.entity.ai.util.GoalUtils;
|
| 14 | import net.minecraft.world.entity.ai.util.LandRandomPos;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public class RandomStroll {
|
| 19 | private static final int MAX_XZ_DIST = 10;
|
| 20 | private static final int MAX_Y_DIST = 7;
|
| 21 | private static final int[][] SWIM_XY_DISTANCE_TIERS = new int[][]{{1, 1}, {3, 3}, {5, 5}, {6, 5}, {7, 7}, {10, 7}};
|
| 22 |
|
| 23 | public static OneShot<PathfinderMob> stroll(final float speedModifier) {
|
| 24 | return stroll(speedModifier, true);
|
| 25 | }
|
| 26 |
|
| 27 | public static OneShot<PathfinderMob> stroll(final float speedModifier, final boolean mayStrollFromWater) {
|
| 28 | return strollFlyOrSwim(speedModifier, body -> LandRandomPos.getPos(body, 10, 7), mayStrollFromWater ? b -> true : b -> !b.isInWater());
|
| 29 | }
|
| 30 |
|
| 31 | public static BehaviorControl<PathfinderMob> stroll(final float speedModifier, final int maxHorizontalDistance, final int maxVerticalDistance) {
|
| 32 | return strollFlyOrSwim(speedModifier, body -> LandRandomPos.getPos(body, maxHorizontalDistance, maxVerticalDistance), b -> true);
|
| 33 | }
|
| 34 |
|
| 35 | public static BehaviorControl<PathfinderMob> fly(final float speedModifier) {
|
| 36 | return strollFlyOrSwim(speedModifier, body -> getTargetFlyPos(body, 10, 7), b -> true);
|
| 37 | }
|
| 38 |
|
| 39 | public static BehaviorControl<PathfinderMob> swim(final float speedModifier) {
|
| 40 | return strollFlyOrSwim(speedModifier, RandomStroll::getTargetSwimPos, Entity::isInWater);
|
| 41 | }
|
| 42 |
|
| 43 | private static OneShot<PathfinderMob> strollFlyOrSwim(
|
| 44 | final float speedModifier, final Function<PathfinderMob, Vec3> fetchTargetPos, final Predicate<PathfinderMob> canRun
|
| 45 | ) {
|
| 46 | return BehaviorBuilder.create(i -> i.group(i.absent(MemoryModuleType.WALK_TARGET)).apply(i, walkTarget -> (level, body, timestamp) -> {
|
| 47 | if (!canRun.test(body)) {
|
| 48 | return false;
|
| 49 | }
|
| 50 |
|
| 51 | Optional<Vec3> pathGoalPos = Optional.ofNullable(fetchTargetPos.apply(body));
|
| 52 | walkTarget.setOrErase(pathGoalPos.map(pos -> new WalkTarget(pos, speedModifier, 0)));
|
| 53 | return true;
|
| 54 | }));
|
| 55 | }
|
| 56 |
|
| 57 | private static @Nullable Vec3 getTargetSwimPos(final PathfinderMob body) {
|
| 58 | Vec3 fallback = null;
|
| 59 | Vec3 targetPos = null;
|
| 60 |
|
| 61 | for (int[] distance : SWIM_XY_DISTANCE_TIERS) {
|
| 62 | if (fallback == null) {
|
| 63 | targetPos = BehaviorUtils.getRandomSwimmablePos(body, distance[0], distance[1]);
|
| 64 | } else {
|
| 65 | targetPos = body.position().add(body.position().vectorTo(fallback).normalize().multiply(distance[0], distance[1], distance[0]));
|
| 66 | }
|
| 67 |
|
| 68 | boolean restrict = GoalUtils.mobRestricted(body, distance[0]);
|
| 69 | if (targetPos == null || body.level().getFluidState(BlockPos.containing(targetPos)).isEmpty() || GoalUtils.isRestricted(restrict, body, targetPos)) {
|
| 70 | return fallback;
|
| 71 | }
|
| 72 |
|
| 73 | fallback = targetPos;
|
| 74 | }
|
| 75 |
|
| 76 | return targetPos;
|
| 77 | }
|
| 78 |
|
| 79 | private static @Nullable Vec3 getTargetFlyPos(final PathfinderMob body, final int maxHorizontalDistance, final int maxVerticalDistance) {
|
| 80 | Vec3 wanderDirection = body.getViewVector(0.0F);
|
| 81 | return AirAndWaterRandomPos.getPos(body, maxHorizontalDistance, maxVerticalDistance, -2, wanderDirection.x, wanderDirection.z, (float) (Math.PI / 2));
|
| 82 | }
|
| 83 | }
|
| 84 |
|