FlyNodeEvaluator .java

net.minecraft.world.level.pathfinder.FlyNodeEvaluator (class): members: SMALL_MOB_INFLATED_START_NODE_BOUNDING_BOX, MAX_START_NODE_CANDIDATES, canStartAt.

net.minecraft.world.level.pathfinderclass344 linescommonextends WalkNodeEvaluator11.1 KB

API Surface (8)

void prepare(final PathNavigationRegion level, final Mob entity)
void done()
Node getStart()
boolean canStartAt(final BlockPos pos)
Target getTarget(final double x, final double y, final double z)
int getNeighbors(final Node[] neighbors, final Node pos)
PathType getCachedPathType(final int x, final int y, final int z)
PathType getPathType(final PathfindingContext context, final int x, final int y, final int z)

Fields & Constants (4)

SMALL_MOB_SIZE
SMALL_MOB_INFLATED_START_NODE_BOUNDING_BOX
MAX_START_NODE_CANDIDATES
pathTypeByPosCache

Full Source

FlyNodeEvaluator.java344 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.pathfinder;
2
3
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
4
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
5
import java.util.List;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.util.Mth;
8
import net.minecraft.world.entity.Mob;
9
import net.minecraft.world.level.PathNavigationRegion;
10
import net.minecraft.world.level.block.Blocks;
11
import net.minecraft.world.level.block.state.BlockState;
12
import net.minecraft.world.phys.AABB;
13
import org.jspecify.annotations.Nullable;
14
15
public class FlyNodeEvaluator extends WalkNodeEvaluator {
16
	private final Long2ObjectMap<PathType> pathTypeByPosCache = new Long2ObjectOpenHashMap<>();
17
	private static final float SMALL_MOB_SIZE = 1.0F;
18
	private static final float SMALL_MOB_INFLATED_START_NODE_BOUNDING_BOX = 1.1F;
19
	private static final int MAX_START_NODE_CANDIDATES = 10;
20
21
	@Override
22
	public void prepare(final PathNavigationRegion level, final Mob entity) {
23
		super.prepare(level, entity);
24
		this.pathTypeByPosCache.clear();
25
		entity.onPathfindingStart();
26
	}
27
28
	@Override
29
	public void done() {
30
		this.mob.onPathfindingDone();
31
		this.pathTypeByPosCache.clear();
32
		super.done();
33
	}
34
35
	@Override
36
	public Node getStart() {
37
		int startY;
38
		if (this.canFloat() && this.mob.isInWater()) {
39
			startY = this.mob.getBlockY();
40
			BlockPos.MutableBlockPos reusableBlockPos = new BlockPos.MutableBlockPos(this.mob.getX(), startY, this.mob.getZ());
41
42
			for (BlockState state = this.currentContext.getBlockState(reusableBlockPos);
43
				state.is(Blocks.WATER);
44
				state = this.currentContext.getBlockState(reusableBlockPos)
45
			) {
46
				reusableBlockPos.set(this.mob.getX(), ++startY, this.mob.getZ());
47
			}
48
		} else {
49
			startY = Mth.floor(this.mob.getY() + 0.5);
50
		}
51
52
		BlockPos startPos = BlockPos.containing(this.mob.getX(), startY, this.mob.getZ());
53
		if (!this.canStartAt(startPos)) {
54
			for (BlockPos testedPosition : this.iteratePathfindingStartNodeCandidatePositions(this.mob)) {
55
				if (this.canStartAt(testedPosition)) {
56
					return super.getStartNode(testedPosition);
57
				}
58
			}
59
		}
60
61
		return super.getStartNode(startPos);
62
	}
63
64
	@Override
65
	protected boolean canStartAt(final BlockPos pos) {
66
		PathType blockPathType = this.getCachedPathType(pos.getX(), pos.getY(), pos.getZ());
67
		return this.mob.getPathfindingMalus(blockPathType) >= 0.0F;
68
	}
69
70
	@Override
71
	public Target getTarget(final double x, final double y, final double z) {
72
		return this.getTargetNodeAt(x, y, z);
73
	}
74
75
	@Override
76
	public int getNeighbors(final Node[] neighbors, final Node pos) {
77
		int count = 0;
78
		Node south = this.findAcceptedNode(pos.x, pos.y, pos.z + 1);
79
		if (this.isOpen(south)) {
80
			neighbors[count++] = south;
81
		}
82
83
		Node west = this.findAcceptedNode(pos.x - 1, pos.y, pos.z);
84
		if (this.isOpen(west)) {
85
			neighbors[count++] = west;
86
		}
87
88
		Node east = this.findAcceptedNode(pos.x + 1, pos.y, pos.z);
89
		if (this.isOpen(east)) {
90
			neighbors[count++] = east;
91
		}
92
93
		Node north = this.findAcceptedNode(pos.x, pos.y, pos.z - 1);
94
		if (this.isOpen(north)) {
95
			neighbors[count++] = north;
96
		}
97
98
		Node up = this.findAcceptedNode(pos.x, pos.y + 1, pos.z);
99
		if (this.isOpen(up)) {
100
			neighbors[count++] = up;
101
		}
102
103
		Node down = this.findAcceptedNode(pos.x, pos.y - 1, pos.z);
104
		if (this.isOpen(down)) {
105
			neighbors[count++] = down;
106
		}
107
108
		Node southUp = this.findAcceptedNode(pos.x, pos.y + 1, pos.z + 1);
109
		if (this.isOpen(southUp) && this.hasMalus(south) && this.hasMalus(up)) {
110
			neighbors[count++] = southUp;
111
		}
112
113
		Node westUp = this.findAcceptedNode(pos.x - 1, pos.y + 1, pos.z);
114
		if (this.isOpen(westUp) && this.hasMalus(west) && this.hasMalus(up)) {
115
			neighbors[count++] = westUp;
116
		}
117
118
		Node eastUp = this.findAcceptedNode(pos.x + 1, pos.y + 1, pos.z);
119
		if (this.isOpen(eastUp) && this.hasMalus(east) && this.hasMalus(up)) {
120
			neighbors[count++] = eastUp;
121
		}
122
123
		Node northUp = this.findAcceptedNode(pos.x, pos.y + 1, pos.z - 1);
124
		if (this.isOpen(northUp) && this.hasMalus(north) && this.hasMalus(up)) {
125
			neighbors[count++] = northUp;
126
		}
127
128
		Node southDown = this.findAcceptedNode(pos.x, pos.y - 1, pos.z + 1);
129
		if (this.isOpen(southDown) && this.hasMalus(south) && this.hasMalus(down)) {
130
			neighbors[count++] = southDown;
131
		}
132
133
		Node westDown = this.findAcceptedNode(pos.x - 1, pos.y - 1, pos.z);
134
		if (this.isOpen(westDown) && this.hasMalus(west) && this.hasMalus(down)) {
135
			neighbors[count++] = westDown;
136
		}
137
138
		Node eastDown = this.findAcceptedNode(pos.x + 1, pos.y - 1, pos.z);
139
		if (this.isOpen(eastDown) && this.hasMalus(east) && this.hasMalus(down)) {
140
			neighbors[count++] = eastDown;
141
		}
142
143
		Node northDown = this.findAcceptedNode(pos.x, pos.y - 1, pos.z - 1);
144
		if (this.isOpen(northDown) && this.hasMalus(north) && this.hasMalus(down)) {
145
			neighbors[count++] = northDown;
146
		}
147
148
		Node northEast = this.findAcceptedNode(pos.x + 1, pos.y, pos.z - 1);
149
		if (this.isOpen(northEast) && this.hasMalus(north) && this.hasMalus(east)) {
150
			neighbors[count++] = northEast;
151
		}
152
153
		Node southEast = this.findAcceptedNode(pos.x + 1, pos.y, pos.z + 1);
154
		if (this.isOpen(southEast) && this.hasMalus(south) && this.hasMalus(east)) {
155
			neighbors[count++] = southEast;
156
		}
157
158
		Node northWest = this.findAcceptedNode(pos.x - 1, pos.y, pos.z - 1);
159
		if (this.isOpen(northWest) && this.hasMalus(north) && this.hasMalus(west)) {
160
			neighbors[count++] = northWest;
161
		}
162
163
		Node southWest = this.findAcceptedNode(pos.x - 1, pos.y, pos.z + 1);
164
		if (this.isOpen(southWest) && this.hasMalus(south) && this.hasMalus(west)) {
165
			neighbors[count++] = southWest;
166
		}
167
168
		Node northEastUp = this.findAcceptedNode(pos.x + 1, pos.y + 1, pos.z - 1);
169
		if (this.isOpen(northEastUp)
170
			&& this.hasMalus(northEast)
171
			&& this.hasMalus(north)
172
			&& this.hasMalus(east)
173
			&& this.hasMalus(up)
174
			&& this.hasMalus(northUp)
175
			&& this.hasMalus(eastUp)) {
176
			neighbors[count++] = northEastUp;
177
		}
178
179
		Node southEastUp = this.findAcceptedNode(pos.x + 1, pos.y + 1, pos.z + 1);
180
		if (this.isOpen(southEastUp)
181
			&& this.hasMalus(southEast)
182
			&& this.hasMalus(south)
183
			&& this.hasMalus(east)
184
			&& this.hasMalus(up)
185
			&& this.hasMalus(southUp)
186
			&& this.hasMalus(eastUp)) {
187
			neighbors[count++] = southEastUp;
188
		}
189
190
		Node northWestUp = this.findAcceptedNode(pos.x - 1, pos.y + 1, pos.z - 1);
191
		if (this.isOpen(northWestUp)
192
			&& this.hasMalus(northWest)
193
			&& this.hasMalus(north)
194
			&& this.hasMalus(west)
195
			&& this.hasMalus(up)
196
			&& this.hasMalus(northUp)
197
			&& this.hasMalus(westUp)) {
198
			neighbors[count++] = northWestUp;
199
		}
200
201
		Node southWestUp = this.findAcceptedNode(pos.x - 1, pos.y + 1, pos.z + 1);
202
		if (this.isOpen(southWestUp)
203
			&& this.hasMalus(southWest)
204
			&& this.hasMalus(south)
205
			&& this.hasMalus(west)
206
			&& this.hasMalus(up)
207
			&& this.hasMalus(southUp)
208
			&& this.hasMalus(westUp)) {
209
			neighbors[count++] = southWestUp;
210
		}
211
212
		Node northEastDown = this.findAcceptedNode(pos.x + 1, pos.y - 1, pos.z - 1);
213
		if (this.isOpen(northEastDown)
214
			&& this.hasMalus(northEast)
215
			&& this.hasMalus(north)
216
			&& this.hasMalus(east)
217
			&& this.hasMalus(down)
218
			&& this.hasMalus(northDown)
219
			&& this.hasMalus(eastDown)) {
220
			neighbors[count++] = northEastDown;
221
		}
222
223
		Node southEastDown = this.findAcceptedNode(pos.x + 1, pos.y - 1, pos.z + 1);
224
		if (this.isOpen(southEastDown)
225
			&& this.hasMalus(southEast)
226
			&& this.hasMalus(south)
227
			&& this.hasMalus(east)
228
			&& this.hasMalus(down)
229
			&& this.hasMalus(southDown)
230
			&& this.hasMalus(eastDown)) {
231
			neighbors[count++] = southEastDown;
232
		}
233
234
		Node northWestDown = this.findAcceptedNode(pos.x - 1, pos.y - 1, pos.z - 1);
235
		if (this.isOpen(northWestDown)
236
			&& this.hasMalus(northWest)
237
			&& this.hasMalus(north)
238
			&& this.hasMalus(west)
239
			&& this.hasMalus(down)
240
			&& this.hasMalus(northDown)
241
			&& this.hasMalus(westDown)) {
242
			neighbors[count++] = northWestDown;
243
		}
244
245
		Node southWestDown = this.findAcceptedNode(pos.x - 1, pos.y - 1, pos.z + 1);
246
		if (this.isOpen(southWestDown)
247
			&& this.hasMalus(southWest)
248
			&& this.hasMalus(south)
249
			&& this.hasMalus(west)
250
			&& this.hasMalus(down)
251
			&& this.hasMalus(southDown)
252
			&& this.hasMalus(westDown)) {
253
			neighbors[count++] = southWestDown;
254
		}
255
256
		return count;
257
	}
258
259
	private boolean hasMalus(final @Nullable Node node) {
260
		return node != null && node.costMalus >= 0.0F;
261
	}
262
263
	private boolean isOpen(final @Nullable Node node) {
264
		return node != null && !node.closed;
265
	}
266
267
	protected @Nullable Node findAcceptedNode(final int x, final int y, final int z) {
268
		Node best = null;
269
		PathType pathType = this.getCachedPathType(x, y, z);
270
		float pathCost = this.mob.getPathfindingMalus(pathType);
271
		if (pathCost >= 0.0F) {
272
			best = this.getNode(x, y, z);
273
			best.type = pathType;
274
			best.costMalus = Math.max(best.costMalus, pathCost);
275
			if (pathType == PathType.WALKABLE) {
276
				best.costMalus++;
277
			}
278
		}
279
280
		return best;
281
	}
282
283
	@Override
284
	protected PathType getCachedPathType(final int x, final int y, final int z) {
285
		return this.pathTypeByPosCache.computeIfAbsent(BlockPos.asLong(x, y, z), key -> this.getPathTypeOfMob(this.currentContext, x, y, z, this.mob));
286
	}
287
288
	@Override
289
	public PathType getPathType(final PathfindingContext context, final int x, final int y, final int z) {
290
		PathType blockPathType = context.getPathTypeFromState(x, y, z);
291
		if (blockPathType == PathType.OPEN && y >= context.level().getMinY() + 1) {
292
			BlockPos belowPos = new BlockPos(x, y - 1, z);
293
			PathType belowType = context.getPathTypeFromState(belowPos.getX(), belowPos.getY(), belowPos.getZ());
294
			if (belowType == PathType.FIRE || belowType == PathType.LAVA) {
295
				blockPathType = PathType.FIRE;
296
			} else if (belowType == PathType.DAMAGING) {
297
				blockPathType = PathType.DAMAGING;
298
			} else if (belowType == PathType.COCOA) {
299
				blockPathType = PathType.COCOA;
300
			} else if (belowType == PathType.FENCE) {
301
				if (!belowPos.equals(context.mobPosition())) {
302
					blockPathType = PathType.FENCE;
303
				}
304
			} else {
305
				blockPathType = belowType != PathType.WALKABLE && belowType != PathType.OPEN && belowType != PathType.WATER ? PathType.WALKABLE : PathType.OPEN;
306
			}
307
		}
308
309
		if (blockPathType == PathType.WALKABLE || blockPathType == PathType.OPEN) {
310
			blockPathType = checkNeighbourBlocks(context, x, y, z, blockPathType);
311
		}
312
313
		return blockPathType;
314
	}
315
316
	private Iterable<BlockPos> iteratePathfindingStartNodeCandidatePositions(final Mob mob) {
317
		AABB boundingBox = mob.getBoundingBox();
318
		boolean isSmallMob = boundingBox.getSize() < 1.0;
319
		if (!isSmallMob) {
320
			return List.of(
321
				BlockPos.containing(boundingBox.minX, mob.getBlockY(), boundingBox.minZ),
322
				BlockPos.containing(boundingBox.minX, mob.getBlockY(), boundingBox.maxZ),
323
				BlockPos.containing(boundingBox.maxX, mob.getBlockY(), boundingBox.minZ),
324
				BlockPos.containing(boundingBox.maxX, mob.getBlockY(), boundingBox.maxZ)
325
			);
326
		}
327
328
		double zPadding = Math.max(0.0, 1.1F - boundingBox.getZsize());
329
		double xPadding = Math.max(0.0, 1.1F - boundingBox.getXsize());
330
		double yPadding = Math.max(0.0, 1.1F - boundingBox.getYsize());
331
		AABB inflatedBoundingBox = boundingBox.inflate(xPadding, yPadding, zPadding);
332
		return BlockPos.randomBetweenClosed(
333
			mob.getRandom(),
334
			10,
335
			Mth.floor(inflatedBoundingBox.minX),
336
			Mth.floor(inflatedBoundingBox.minY),
337
			Mth.floor(inflatedBoundingBox.minZ),
338
			Mth.floor(inflatedBoundingBox.maxX),
339
			Mth.floor(inflatedBoundingBox.maxY),
340
			Mth.floor(inflatedBoundingBox.maxZ)
341
		);
342
	}
343
}
344

Decompiled Minecraft 26.3 source of FlyNodeEvaluator (net/minecraft/world/level/pathfinder/FlyNodeEvaluator.java) with Mojang official mappings, stored verbatim on the site.