| 1 | package net.minecraft.world.entity.ai.util;
|
| 2 |
|
| 3 | import java.util.function.ToDoubleFunction;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.world.entity.PathfinderMob;
|
| 6 | import net.minecraft.world.phys.Vec3;
|
| 7 | import org.jspecify.annotations.Nullable;
|
| 8 |
|
| 9 | public class LandRandomPos {
|
| 10 | public static @Nullable Vec3 getPos(final PathfinderMob mob, final int horizontalDist, final int verticalDist) {
|
| 11 | return getPos(mob, horizontalDist, verticalDist, mob::getWalkTargetValue);
|
| 12 | }
|
| 13 |
|
| 14 | public static @Nullable Vec3 getPos(final PathfinderMob mob, final int horizontalDist, final int verticalDist, final ToDoubleFunction<BlockPos> positionWeight) {
|
| 15 | boolean restrict = GoalUtils.mobRestricted(mob, horizontalDist);
|
| 16 | return RandomPos.generateRandomPos(() -> {
|
| 17 | BlockPos direction = RandomPos.generateRandomDirection(mob.getRandom(), horizontalDist, verticalDist);
|
| 18 | BlockPos pos = generateRandomPosTowardDirection(mob, horizontalDist, restrict, direction);
|
| 19 | return pos == null ? null : movePosUpOutOfSolid(mob, pos);
|
| 20 | }, positionWeight);
|
| 21 | }
|
| 22 |
|
| 23 | public static @Nullable Vec3 getPosTowards(final PathfinderMob mob, final int horizontalDist, final int verticalDist, final Vec3 towardsPos) {
|
| 24 | Vec3 dir = towardsPos.subtract(mob.getX(), mob.getY(), mob.getZ());
|
| 25 | boolean restrict = GoalUtils.mobRestricted(mob, horizontalDist);
|
| 26 | return getPosInDirection(mob, 0.0, horizontalDist, verticalDist, dir, restrict);
|
| 27 | }
|
| 28 |
|
| 29 | public static @Nullable Vec3 getPosAway(final PathfinderMob mob, final int horizontalDist, final int verticalDist, final Vec3 avoidPos) {
|
| 30 | return getPosAway(mob, 0.0, horizontalDist, verticalDist, avoidPos);
|
| 31 | }
|
| 32 |
|
| 33 | public static @Nullable Vec3 getPosAway(
|
| 34 | final PathfinderMob mob, final double minHorizontalDist, final double maxHorizontalDist, final int verticalDist, final Vec3 avoidPos
|
| 35 | ) {
|
| 36 | Vec3 dirAway = mob.position().subtract(avoidPos);
|
| 37 | if (dirAway.length() == 0.0) {
|
| 38 | dirAway = new Vec3(mob.getRandom().nextDouble() - 0.5, 0.0, mob.getRandom().nextDouble() - 0.5);
|
| 39 | }
|
| 40 |
|
| 41 | boolean restrict = GoalUtils.mobRestricted(mob, maxHorizontalDist);
|
| 42 | return getPosInDirection(mob, minHorizontalDist, maxHorizontalDist, verticalDist, dirAway, restrict);
|
| 43 | }
|
| 44 |
|
| 45 | private static @Nullable Vec3 getPosInDirection(
|
| 46 | final PathfinderMob mob, final double minHorizontalDist, final double maxHorizontalDist, final int verticalDist, final Vec3 dir, final boolean restrict
|
| 47 | ) {
|
| 48 | return RandomPos.generateRandomPos(
|
| 49 | mob,
|
| 50 | () -> {
|
| 51 | BlockPos direction = RandomPos.generateRandomDirectionWithinRadians(
|
| 52 | mob.getRandom(), minHorizontalDist, maxHorizontalDist, verticalDist, 0, dir.x, dir.z, (float) (Math.PI / 2)
|
| 53 | );
|
| 54 | if (direction == null) {
|
| 55 | return null;
|
| 56 | }
|
| 57 |
|
| 58 | BlockPos pos = generateRandomPosTowardDirection(mob, maxHorizontalDist, restrict, direction);
|
| 59 | return pos == null ? null : movePosUpOutOfSolid(mob, pos);
|
| 60 | }
|
| 61 | );
|
| 62 | }
|
| 63 |
|
| 64 | public static @Nullable BlockPos movePosUpOutOfSolid(final PathfinderMob mob, BlockPos pos) {
|
| 65 | pos = RandomPos.moveUpOutOfSolid(pos, mob.level().getMaxY(), blockPos -> GoalUtils.isSolid(mob, blockPos));
|
| 66 | return !GoalUtils.isWater(mob, pos) && !GoalUtils.hasMalus(mob, pos) ? pos : null;
|
| 67 | }
|
| 68 |
|
| 69 | public static @Nullable BlockPos generateRandomPosTowardDirection(
|
| 70 | final PathfinderMob mob, final double horizontalDist, final boolean restrict, final BlockPos direction
|
| 71 | ) {
|
| 72 | BlockPos pos = RandomPos.generateRandomPosTowardDirection(mob, horizontalDist, mob.getRandom(), direction);
|
| 73 | return !GoalUtils.isOutsideLimits(pos, mob) && !GoalUtils.isRestricted(restrict, mob, pos) && !GoalUtils.isNotStable(mob.getNavigation(), pos) ? pos : null;
|
| 74 | }
|
| 75 | }
|
| 76 |
|