AcquirePoi .java

net.minecraft.world.entity.ai.behavior.AcquirePoi (class): members: onPoiAcquisitionEvent, JitteredLinearRetry, memoryToValidate, memoryToAcquire, poiType.

net.minecraft.world.entity.ai.behaviorclass182 linescommon6.4 KB

API Surface (7)

BehaviorControl<PathfinderMob> create(final Predicate<Holder<PoiType>> poiType, final MemoryModuleType<GlobalPos> memoryToAcquire, final boolean onlyIfAdult, final Optional<Byte> onPoiAcquisitionEvent, final BiPredicate<ServerLevel, BlockPos> validPoi)
BehaviorControl<PathfinderMob> create(final Predicate<Holder<PoiType>> poiType, final MemoryModuleType<GlobalPos> memoryToAcquire, final boolean onlyIfAdult, final Optional<Byte> onPoiAcquisitionEvent)
BehaviorControl<PathfinderMob> create(final Predicate<Holder<PoiType>> poiType, final MemoryModuleType<GlobalPos> memoryToValidate, final MemoryModuleType<GlobalPos> memoryToAcquire, final boolean onlyIfAdult, final Optional<Byte> onPoiAcquisitionEvent, final BiPredicate<ServerLevel, BlockPos> validPoi)
void markAttempt(final long timestamp)
boolean isStillValid(final long timestamp)
boolean shouldRetry(final long timestamp)
String toString()

Fields & Constants (6)

SCAN_RANGE
poiType
memoryToAcquire
onlyIfAdult
onPoiAcquisitionEvent
memoryToValidate

String literals (3)

"RetryMarker{, previousAttemptAt="
", nextScheduledAttemptAt="
", currentDelay="

Full Source

AcquirePoi.java182 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.mojang.datafixers.util.Pair;
4
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
5
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
6
import java.util.HashSet;
7
import java.util.Optional;
8
import java.util.Set;
9
import java.util.function.BiPredicate;
10
import java.util.function.Predicate;
11
import java.util.stream.Collectors;
12
import net.minecraft.core.BlockPos;
13
import net.minecraft.core.GlobalPos;
14
import net.minecraft.core.Holder;
15
import net.minecraft.server.level.ServerLevel;
16
import net.minecraft.util.RandomSource;
17
import net.minecraft.world.entity.Mob;
18
import net.minecraft.world.entity.PathfinderMob;
19
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
20
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
21
import net.minecraft.world.entity.ai.village.poi.PoiManager;
22
import net.minecraft.world.entity.ai.village.poi.PoiType;
23
import net.minecraft.world.level.pathfinder.Path;
24
import org.apache.commons.lang3.mutable.MutableLong;
25
import org.jspecify.annotations.Nullable;
26
27
public class AcquirePoi {
28
	public static final int SCAN_RANGE = 48;
29
30
	public static BehaviorControl<PathfinderMob> create(
31
		final Predicate<Holder<PoiType>> poiType,
32
		final MemoryModuleType<GlobalPos> memoryToAcquire,
33
		final boolean onlyIfAdult,
34
		final Optional<Byte> onPoiAcquisitionEvent,
35
		final BiPredicate<ServerLevel, BlockPos> validPoi
36
	) {
37
		return create(poiType, memoryToAcquire, memoryToAcquire, onlyIfAdult, onPoiAcquisitionEvent, validPoi);
38
	}
39
40
	public static BehaviorControl<PathfinderMob> create(
41
		final Predicate<Holder<PoiType>> poiType,
42
		final MemoryModuleType<GlobalPos> memoryToAcquire,
43
		final boolean onlyIfAdult,
44
		final Optional<Byte> onPoiAcquisitionEvent
45
	) {
46
		return create(poiType, memoryToAcquire, memoryToAcquire, onlyIfAdult, onPoiAcquisitionEvent, (l, p) -> true);
47
	}
48
49
	public static BehaviorControl<PathfinderMob> create(
50
		final Predicate<Holder<PoiType>> poiType,
51
		final MemoryModuleType<GlobalPos> memoryToValidate,
52
		final MemoryModuleType<GlobalPos> memoryToAcquire,
53
		final boolean onlyIfAdult,
54
		final Optional<Byte> onPoiAcquisitionEvent,
55
		final BiPredicate<ServerLevel, BlockPos> validPoi
56
	) {
57
		int batchSize = 5;
58
		int rate = 20;
59
		MutableLong nextScheduledStart = new MutableLong(0L);
60
		Long2ObjectMap<AcquirePoi.JitteredLinearRetry> batchCache = new Long2ObjectOpenHashMap<>();
61
		OneShot<PathfinderMob> acquirePoi = BehaviorBuilder.create(
62
			i -> i.group(i.absent(memoryToAcquire))
63
				.apply(
64
					i,
65
					toAcquire -> (level, body, timestamp) -> {
66
						if (onlyIfAdult && body.isBaby()) {
67
							return false;
68
						}
69
70
						RandomSource random = level.getRandom();
71
						if (nextScheduledStart.longValue() == 0L) {
72
							nextScheduledStart.setValue(level.getGameTime() + random.nextInt(20));
73
							return false;
74
						}
75
76
						if (level.getGameTime() < nextScheduledStart.longValue()) {
77
							return false;
78
						}
79
80
						nextScheduledStart.setValue(timestamp + 20L + random.nextInt(20));
81
						PoiManager poiManager = level.getPoiManager();
82
						batchCache.long2ObjectEntrySet().removeIf(entry -> !entry.getValue().isStillValid(timestamp));
83
						Predicate<BlockPos> cacheTest = pos -> {
84
							AcquirePoi.JitteredLinearRetry retryMarker = batchCache.get(pos.asLong());
85
							if (retryMarker == null) {
86
								return true;
87
							}
88
89
							if (!retryMarker.shouldRetry(timestamp)) {
90
								return false;
91
							}
92
93
							retryMarker.markAttempt(timestamp);
94
							return true;
95
						};
96
						Set<Pair<Holder<PoiType>, BlockPos>> poiPositions = poiManager.findAllClosestFirstWithType(
97
								poiType, cacheTest, body.blockPosition(), 48, PoiManager.Occupancy.HAS_SPACE
98
							)
99
							.limit(5L)
100
							.filter(px -> validPoi.test(level, (BlockPos)px.getSecond()))
101
							.collect(Collectors.toSet());
102
						Path path = findPathToPois(body, poiPositions);
103
						if (path != null && path.canReach()) {
104
							BlockPos targetPos = path.getTarget();
105
							poiManager.getType(targetPos).ifPresent(type -> {
106
								poiManager.take(poiType, (t, poiPos) -> poiPos.equals(targetPos), targetPos, 1);
107
								toAcquire.set(GlobalPos.of(level.dimension(), targetPos));
108
								onPoiAcquisitionEvent.ifPresent(event -> level.broadcastEntityEvent(body, event));
109
								batchCache.clear();
110
								level.debugSynchronizers().updatePoi(targetPos);
111
							});
112
						} else {
113
							for (Pair<Holder<PoiType>, BlockPos> p : poiPositions) {
114
								batchCache.computeIfAbsent(p.getSecond().asLong(), key -> new AcquirePoi.JitteredLinearRetry(random, timestamp));
115
							}
116
						}
117
118
						return true;
119
					}
120
				)
121
		);
122
		return memoryToAcquire == memoryToValidate ? acquirePoi : BehaviorBuilder.create(i -> i.group(i.absent(memoryToValidate)).apply(i, toValidate -> acquirePoi));
123
	}
124
125
	public static @Nullable Path findPathToPois(final Mob body, final Set<Pair<Holder<PoiType>, BlockPos>> pois) {
126
		if (pois.isEmpty()) {
127
			return null;
128
		}
129
130
		Set<BlockPos> targets = new HashSet<>();
131
		int maxRange = 1;
132
133
		for (Pair<Holder<PoiType>, BlockPos> p : pois) {
134
			maxRange = Math.max(maxRange, p.getFirst().value().validRange());
135
			targets.add(p.getSecond());
136
		}
137
138
		return body.getNavigation().createPath(targets, maxRange);
139
	}
140
141
	private static class JitteredLinearRetry {
142
		private static final int MIN_INTERVAL_INCREASE = 40;
143
		private static final int MAX_INTERVAL_INCREASE = 80;
144
		private static final int MAX_RETRY_PATHFINDING_INTERVAL = 400;
145
		private final RandomSource random;
146
		private long previousAttemptTimestamp;
147
		private long nextScheduledAttemptTimestamp;
148
		private int currentDelay;
149
150
		public JitteredLinearRetry(final RandomSource random, final long firstAttemptTimestamp) {
151
			this.random = random;
152
			this.markAttempt(firstAttemptTimestamp);
153
		}
154
155
		public void markAttempt(final long timestamp) {
156
			this.previousAttemptTimestamp = timestamp;
157
			int suggestedDelay = this.currentDelay + this.random.nextInt(40) + 40;
158
			this.currentDelay = Math.min(suggestedDelay, 400);
159
			this.nextScheduledAttemptTimestamp = timestamp + this.currentDelay;
160
		}
161
162
		public boolean isStillValid(final long timestamp) {
163
			return timestamp - this.previousAttemptTimestamp < 400L;
164
		}
165
166
		public boolean shouldRetry(final long timestamp) {
167
			return timestamp >= this.nextScheduledAttemptTimestamp;
168
		}
169
170
		@Override
171
		public String toString() {
172
			return "RetryMarker{, previousAttemptAt="
173
				+ this.previousAttemptTimestamp
174
				+ ", nextScheduledAttemptAt="
175
				+ this.nextScheduledAttemptTimestamp
176
				+ ", currentDelay="
177
				+ this.currentDelay
178
				+ "}";
179
		}
180
	}
181
}
182

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