RandomPos .java

net.minecraft.world.entity.ai.util.RandomPos (class): members: generateRandomDirectionWithinRadians, generateRandomDirection, RANDOM_POS_ATTEMPTS, xDir.

net.minecraft.world.entity.ai.utilclass135 linescommon4.5 KB

API Surface (4)

BlockPos generateRandomDirection(final RandomSource random, final int horizontalDist, final int verticalDist)
BlockPos moveUpOutOfSolid(final BlockPos pos, final int maxY, final Predicate<BlockPos> solidityTester)
BlockPos moveUpToAboveSolid(final BlockPos pos, final int aboveSolidAmount, final int maxY, final Predicate<BlockPos> solidityTester)
BlockPos generateRandomPosTowardDirection(final PathfinderMob mob, final double xzDist, final RandomSource random, final BlockPos direction)

Fields & Constants (8)

RANDOM_POS_ATTEMPTS
random
minHorizontalDist
maxHorizontalDist
verticalDist
flyingHeight
xDir
zDir

String literals (2)

"aboveSolidAmount was "
", expected >= 0"

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

RandomPos.java135 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.util;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import java.util.function.Predicate;
5
import java.util.function.Supplier;
6
import java.util.function.ToDoubleFunction;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.Direction;
9
import net.minecraft.util.Mth;
10
import net.minecraft.util.RandomSource;
11
import net.minecraft.world.entity.PathfinderMob;
12
import net.minecraft.world.phys.Vec3;
13
import org.jspecify.annotations.Nullable;
14
15
public class RandomPos {
16
	private static final int RANDOM_POS_ATTEMPTS = 10;
17
18
	public static BlockPos generateRandomDirection(final RandomSource random, final int horizontalDist, final int verticalDist) {
19
		int xt = random.nextInt(2 * horizontalDist + 1) - horizontalDist;
20
		int yt = random.nextInt(2 * verticalDist + 1) - verticalDist;
21
		int zt = random.nextInt(2 * horizontalDist + 1) - horizontalDist;
22
		return new BlockPos(xt, yt, zt);
23
	}
24
25
	public static @Nullable BlockPos generateRandomDirectionWithinRadians(
26
		final RandomSource random,
27
		final double minHorizontalDist,
28
		final double maxHorizontalDist,
29
		final int verticalDist,
30
		final int flyingHeight,
31
		final double xDir,
32
		final double zDir,
33
		final double maxXzRadiansFromDir
34
	) {
35
		double yRadiansCenter = Mth.atan2(zDir, xDir) - (float) (Math.PI / 2);
36
		double yRadians = yRadiansCenter + (2.0F * random.nextFloat() - 1.0F) * maxXzRadiansFromDir;
37
		double dist = Mth.lerp(Math.sqrt(random.nextDouble()), minHorizontalDist, maxHorizontalDist) * Mth.SQRT_OF_TWO;
38
		double xt = -dist * Math.sin(yRadians);
39
		double zt = dist * Math.cos(yRadians);
40
		if (!(Math.abs(xt) > maxHorizontalDist) && !(Math.abs(zt) > maxHorizontalDist)) {
41
			int yt = random.nextInt(2 * verticalDist + 1) - verticalDist + flyingHeight;
42
			return BlockPos.containing(xt, yt, zt);
43
		} else {
44
			return null;
45
		}
46
	}
47
48
	@VisibleForTesting
49
	public static BlockPos moveUpOutOfSolid(final BlockPos pos, final int maxY, final Predicate<BlockPos> solidityTester) {
50
		if (!solidityTester.test(pos)) {
51
			return pos;
52
		}
53
54
		BlockPos.MutableBlockPos onGroundPos = pos.mutable().move(Direction.UP);
55
56
		while (onGroundPos.getY() <= maxY && solidityTester.test(onGroundPos)) {
57
			onGroundPos.move(Direction.UP);
58
		}
59
60
		return onGroundPos.immutable();
61
	}
62
63
	@VisibleForTesting
64
	public static BlockPos moveUpToAboveSolid(final BlockPos pos, final int aboveSolidAmount, final int maxY, final Predicate<BlockPos> solidityTester) {
65
		if (aboveSolidAmount < 0) {
66
			throw new IllegalArgumentException("aboveSolidAmount was " + aboveSolidAmount + ", expected >= 0");
67
		}
68
69
		if (!solidityTester.test(pos)) {
70
			return pos;
71
		}
72
73
		BlockPos.MutableBlockPos mutablePos = pos.mutable().move(Direction.UP);
74
75
		while (mutablePos.getY() <= maxY && solidityTester.test(mutablePos)) {
76
			mutablePos.move(Direction.UP);
77
		}
78
79
		int firstNonSolidY = mutablePos.getY();
80
81
		while (mutablePos.getY() <= maxY && mutablePos.getY() - firstNonSolidY < aboveSolidAmount) {
82
			mutablePos.move(Direction.UP);
83
			if (solidityTester.test(mutablePos)) {
84
				mutablePos.move(Direction.DOWN);
85
				break;
86
			}
87
		}
88
89
		return mutablePos.immutable();
90
	}
91
92
	public static @Nullable Vec3 generateRandomPos(final PathfinderMob mob, final Supplier<@Nullable BlockPos> posSupplier) {
93
		return generateRandomPos(posSupplier, mob::getWalkTargetValue);
94
	}
95
96
	public static @Nullable Vec3 generateRandomPos(final Supplier<@Nullable BlockPos> posSupplier, final ToDoubleFunction<BlockPos> positionWeightFunction) {
97
		double bestWeight = Double.NEGATIVE_INFINITY;
98
		BlockPos bestPos = null;
99
100
		for (int i = 0; i < 10; i++) {
101
			BlockPos pos = posSupplier.get();
102
			if (pos != null) {
103
				double value = positionWeightFunction.applyAsDouble(pos);
104
				if (value > bestWeight) {
105
					bestWeight = value;
106
					bestPos = pos;
107
				}
108
			}
109
		}
110
111
		return bestPos != null ? Vec3.atBottomCenterOf(bestPos) : null;
112
	}
113
114
	public static BlockPos generateRandomPosTowardDirection(final PathfinderMob mob, final double xzDist, final RandomSource random, final BlockPos direction) {
115
		double xt = direction.getX();
116
		double zt = direction.getZ();
117
		if (mob.hasHome() && xzDist > 1.0) {
118
			BlockPos center = mob.getHomePosition();
119
			if (mob.getX() > center.getX()) {
120
				xt -= random.nextDouble() * xzDist / 2.0;
121
			} else {
122
				xt += random.nextDouble() * xzDist / 2.0;
123
			}
124
125
			if (mob.getZ() > center.getZ()) {
126
				zt -= random.nextDouble() * xzDist / 2.0;
127
			} else {
128
				zt += random.nextDouble() * xzDist / 2.0;
129
			}
130
		}
131
132
		return BlockPos.containing(xt + mob.getX(), direction.getY() + mob.getY(), zt + mob.getZ());
133
	}
134
}
135

Decompiled Minecraft 26.3 source of RandomPos (net/minecraft/world/entity/ai/util/RandomPos.java) with Mojang official mappings, stored verbatim on the site.