| 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 it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
|
| 7 | import java.util.EnumSet;
|
| 8 | import java.util.Set;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.tags.BlockTags;
|
| 12 | import net.minecraft.tags.FluidTags;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.world.entity.Mob;
|
| 15 | import net.minecraft.world.level.BlockGetter;
|
| 16 | import net.minecraft.world.level.PathNavigationRegion;
|
| 17 | import net.minecraft.world.level.block.BaseRailBlock;
|
| 18 | import net.minecraft.world.level.block.Block;
|
| 19 | import net.minecraft.world.level.block.Blocks;
|
| 20 | import net.minecraft.world.level.block.DoorBlock;
|
| 21 | import net.minecraft.world.level.block.FenceGateBlock;
|
| 22 | import net.minecraft.world.level.block.LeavesBlock;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.material.FluidState;
|
| 25 | import net.minecraft.world.phys.AABB;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public class WalkNodeEvaluator extends NodeEvaluator {
|
| 31 | public static final double SPACE_BETWEEN_WALL_POSTS = 0.5;
|
| 32 | private static final double DEFAULT_MOB_JUMP_HEIGHT = 1.125;
|
| 33 | private final Long2ObjectMap<PathType> pathTypesByPosCacheByMob = new Long2ObjectOpenHashMap<>();
|
| 34 | private final Object2BooleanMap<AABB> collisionCache = new Object2BooleanOpenHashMap<>();
|
| 35 | private final Node[] reusableNeighbors = new Node[Direction.Plane.HORIZONTAL.length()];
|
| 36 |
|
| 37 | @Override
|
| 38 | public void prepare(final PathNavigationRegion level, final Mob entity) {
|
| 39 | super.prepare(level, entity);
|
| 40 | entity.onPathfindingStart();
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void done() {
|
| 45 | this.mob.onPathfindingDone();
|
| 46 | this.pathTypesByPosCacheByMob.clear();
|
| 47 | this.collisionCache.clear();
|
| 48 | super.done();
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public Node getStart() {
|
| 53 | BlockPos.MutableBlockPos reusablePos = new BlockPos.MutableBlockPos();
|
| 54 | int startY = this.mob.getBlockY();
|
| 55 | BlockState blockState = this.currentContext.getBlockState(reusablePos.set(this.mob.getX(), startY, this.mob.getZ()));
|
| 56 | if (!this.mob.canStandOnFluid(blockState.getFluidState())) {
|
| 57 | if (this.canFloat() && this.mob.isInFloatableFluid()) {
|
| 58 | while (blockState.getFluidState().is(FluidTags.ENTITY_FLOATABLE)) {
|
| 59 | blockState = this.currentContext.getBlockState(reusablePos.set(this.mob.getX(), ++startY, this.mob.getZ()));
|
| 60 | }
|
| 61 |
|
| 62 | startY--;
|
| 63 | } else if (this.mob.onGround()) {
|
| 64 | startY = Mth.floor(this.mob.getY() + 0.5);
|
| 65 | } else {
|
| 66 | reusablePos.set(this.mob.getX(), this.mob.getY() + 1.0, this.mob.getZ());
|
| 67 |
|
| 68 | while (reusablePos.getY() > this.currentContext.level().getMinY()) {
|
| 69 | startY = reusablePos.getY();
|
| 70 | reusablePos.setY(reusablePos.getY() - 1);
|
| 71 | BlockState belowBlockState = this.currentContext.getBlockState(reusablePos);
|
| 72 | if (!belowBlockState.isAir() && !belowBlockState.isPathfindable(PathComputationType.LAND)) {
|
| 73 | break;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | } else {
|
| 78 | while (this.mob.canStandOnFluid(blockState.getFluidState())) {
|
| 79 | blockState = this.currentContext.getBlockState(reusablePos.set(this.mob.getX(), ++startY, this.mob.getZ()));
|
| 80 | }
|
| 81 |
|
| 82 | startY--;
|
| 83 | }
|
| 84 |
|
| 85 | BlockPos startPos = this.mob.blockPosition();
|
| 86 | if (!this.canStartAt(reusablePos.set(startPos.getX(), startY, startPos.getZ()))) {
|
| 87 | AABB mobBB = this.mob.getBoundingBox();
|
| 88 | if (this.canStartAt(reusablePos.set(mobBB.minX, startY, mobBB.minZ))
|
| 89 | || this.canStartAt(reusablePos.set(mobBB.minX, startY, mobBB.maxZ))
|
| 90 | || this.canStartAt(reusablePos.set(mobBB.maxX, startY, mobBB.minZ))
|
| 91 | || this.canStartAt(reusablePos.set(mobBB.maxX, startY, mobBB.maxZ))) {
|
| 92 | return this.getStartNode(reusablePos);
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return this.getStartNode(new BlockPos(startPos.getX(), startY, startPos.getZ()));
|
| 97 | }
|
| 98 |
|
| 99 | protected Node getStartNode(final BlockPos pos) {
|
| 100 | Node node = this.getNode(pos);
|
| 101 | node.type = this.getCachedPathType(node.x, node.y, node.z);
|
| 102 | node.costMalus = this.mob.getPathfindingMalus(node.type);
|
| 103 | return node;
|
| 104 | }
|
| 105 |
|
| 106 | protected boolean canStartAt(final BlockPos pos) {
|
| 107 | PathType blockPathType = this.getCachedPathType(pos.getX(), pos.getY(), pos.getZ());
|
| 108 | return blockPathType != PathType.OPEN && this.mob.getPathfindingMalus(blockPathType) >= 0.0F;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public Target getTarget(final double x, final double y, final double z) {
|
| 113 | return this.getTargetNodeAt(x, y, z);
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public int getNeighbors(final Node[] neighbors, final Node pos) {
|
| 118 | int p = 0;
|
| 119 | int jumpSize = 0;
|
| 120 | PathType blockPathTypeAbove = this.getCachedPathType(pos.x, pos.y + 1, pos.z);
|
| 121 | PathType blockPathTypeCurrent = this.getCachedPathType(pos.x, pos.y, pos.z);
|
| 122 | if (this.mob.getPathfindingMalus(blockPathTypeAbove) >= 0.0F && blockPathTypeCurrent != PathType.STICKY_HONEY) {
|
| 123 | jumpSize = Mth.floor(Math.max(1.0F, this.mob.maxUpStep()));
|
| 124 | }
|
| 125 |
|
| 126 | double posHeight = this.getFloorLevel(new BlockPos(pos.x, pos.y, pos.z));
|
| 127 |
|
| 128 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 129 | Node node = this.findAcceptedNode(pos.x + direction.getStepX(), pos.y, pos.z + direction.getStepZ(), jumpSize, posHeight, direction, blockPathTypeCurrent);
|
| 130 | this.reusableNeighbors[direction.get2DDataValue()] = node;
|
| 131 | if (this.isNeighborValid(node, pos)) {
|
| 132 | neighbors[p++] = node;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 137 | Direction secondDirection = direction.getClockWise();
|
| 138 | if (this.isDiagonalValid(pos, this.reusableNeighbors[direction.get2DDataValue()], this.reusableNeighbors[secondDirection.get2DDataValue()])) {
|
| 139 | Node diagonalNode = this.findAcceptedNode(
|
| 140 | pos.x + direction.getStepX() + secondDirection.getStepX(),
|
| 141 | pos.y,
|
| 142 | pos.z + direction.getStepZ() + secondDirection.getStepZ(),
|
| 143 | jumpSize,
|
| 144 | posHeight,
|
| 145 | direction,
|
| 146 | blockPathTypeCurrent
|
| 147 | );
|
| 148 | if (this.isDiagonalValid(diagonalNode)) {
|
| 149 | neighbors[p++] = diagonalNode;
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | return p;
|
| 155 | }
|
| 156 |
|
| 157 | protected boolean isNeighborValid(final @Nullable Node neighbor, final Node current) {
|
| 158 | return neighbor != null && !neighbor.closed && (neighbor.costMalus >= 0.0F || current.costMalus < 0.0F);
|
| 159 | }
|
| 160 |
|
| 161 | protected boolean isDiagonalValid(final Node pos, final @Nullable Node ew, final @Nullable Node ns) {
|
| 162 | if (ns == null || ew == null || ns.y > pos.y || ew.y > pos.y) {
|
| 163 | return false;
|
| 164 | }
|
| 165 |
|
| 166 | if (ew.type != PathType.WALKABLE_DOOR && ns.type != PathType.WALKABLE_DOOR) {
|
| 167 | if (!(this.mob.getBbWidth() > 1.0F) || !(ew.costMalus > 0.0F) && !(ns.costMalus > 0.0F)) {
|
| 168 | boolean canPassBetweenPosts = ns.type == PathType.FENCE && ew.type == PathType.FENCE && this.mob.getBbWidth() < 0.5;
|
| 169 | return (ns.y < pos.y || ns.costMalus >= 0.0F || canPassBetweenPosts) && (ew.y < pos.y || ew.costMalus >= 0.0F || canPassBetweenPosts);
|
| 170 | } else {
|
| 171 | return false;
|
| 172 | }
|
| 173 | } else {
|
| 174 | return false;
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | protected boolean isDiagonalValid(final @Nullable Node diagonal) {
|
| 179 | if (diagonal == null || diagonal.closed) {
|
| 180 | return false;
|
| 181 | } else {
|
| 182 | return diagonal.type == PathType.WALKABLE_DOOR ? false : diagonal.costMalus >= 0.0F;
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | private static boolean doesBlockHavePartialCollision(final PathType type) {
|
| 187 | return type == PathType.FENCE || type == PathType.DOOR_WOOD_CLOSED || type == PathType.DOOR_IRON_CLOSED;
|
| 188 | }
|
| 189 |
|
| 190 | private boolean canReachWithoutCollision(final Node posTo) {
|
| 191 | AABB bb = this.mob.getBoundingBox();
|
| 192 | Vec3 delta = new Vec3(
|
| 193 | posTo.x - this.mob.getX() + bb.getXsize() / 2.0, posTo.y - this.mob.getY() + bb.getYsize() / 2.0, posTo.z - this.mob.getZ() + bb.getZsize() / 2.0
|
| 194 | );
|
| 195 | int steps = Mth.ceil(delta.length() / bb.getSize());
|
| 196 | delta = delta.scale(1.0F / steps);
|
| 197 |
|
| 198 | for (int i = 1; i <= steps; i++) {
|
| 199 | bb = bb.move(delta);
|
| 200 | if (this.hasCollisions(bb)) {
|
| 201 | return false;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | return true;
|
| 206 | }
|
| 207 |
|
| 208 | protected double getFloorLevel(final BlockPos pos) {
|
| 209 | BlockGetter level = this.currentContext.level();
|
| 210 | return (this.canFloat() || this.isAmphibious()) && level.getFluidState(pos).is(FluidTags.ENTITY_FLOATABLE) ? pos.getY() + 0.5 : getFloorLevel(level, pos);
|
| 211 | }
|
| 212 |
|
| 213 | public static double getFloorLevel(final BlockGetter level, final BlockPos pos) {
|
| 214 | BlockPos target = pos.below();
|
| 215 | VoxelShape shape = level.getBlockState(target).getCollisionShape(level, target);
|
| 216 | return target.getY() + (shape.isEmpty() ? 0.0 : shape.max(Direction.Axis.Y));
|
| 217 | }
|
| 218 |
|
| 219 | protected boolean isAmphibious() {
|
| 220 | return false;
|
| 221 | }
|
| 222 |
|
| 223 | protected @Nullable Node findAcceptedNode(
|
| 224 | final int x, final int y, final int z, final int jumpSize, final double nodeHeight, final Direction travelDirection, final PathType blockPathTypeCurrent
|
| 225 | ) {
|
| 226 | Node best = null;
|
| 227 | BlockPos.MutableBlockPos reusablePos = new BlockPos.MutableBlockPos();
|
| 228 | double maxYTarget = this.getFloorLevel(reusablePos.set(x, y, z));
|
| 229 | if (maxYTarget - nodeHeight > this.getMobJumpHeight()) {
|
| 230 | return null;
|
| 231 | }
|
| 232 |
|
| 233 | PathType pathType = this.getCachedPathType(x, y, z);
|
| 234 | float pathCost = this.mob.getPathfindingMalus(pathType);
|
| 235 | if (pathCost >= 0.0F) {
|
| 236 | best = this.getNodeAndUpdateCostToMax(x, y, z, pathType, pathCost);
|
| 237 | }
|
| 238 |
|
| 239 | if (doesBlockHavePartialCollision(blockPathTypeCurrent) && best != null && best.costMalus >= 0.0F && !this.canReachWithoutCollision(best)) {
|
| 240 | best = null;
|
| 241 | }
|
| 242 |
|
| 243 | if (pathType != PathType.WALKABLE && (!this.isAmphibious() || pathType != PathType.WATER)) {
|
| 244 | if ((best == null || best.costMalus < 0.0F)
|
| 245 | && jumpSize > 0
|
| 246 | && (pathType != PathType.FENCE || this.canWalkOverFences())
|
| 247 | && pathType != PathType.UNPASSABLE_RAIL
|
| 248 | && pathType != PathType.TRAPDOOR
|
| 249 | && pathType != PathType.POWDER_SNOW) {
|
| 250 | best = this.tryJumpOn(x, y, z, jumpSize, nodeHeight, travelDirection, blockPathTypeCurrent, reusablePos);
|
| 251 | } else if (!this.isAmphibious() && pathType == PathType.WATER && !this.canFloat()) {
|
| 252 | best = this.tryFindFirstNonWaterBelow(x, y, z, best);
|
| 253 | } else if (pathType == PathType.OPEN) {
|
| 254 | best = this.tryFindFirstGroundNodeBelow(x, y, z);
|
| 255 | } else if (doesBlockHavePartialCollision(pathType) && best == null) {
|
| 256 | best = this.getClosedNode(x, y, z, pathType);
|
| 257 | }
|
| 258 |
|
| 259 | return best;
|
| 260 | } else {
|
| 261 | return best;
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | private double getMobJumpHeight() {
|
| 266 | return Math.max(1.125, this.mob.maxUpStep());
|
| 267 | }
|
| 268 |
|
| 269 | private Node getNodeAndUpdateCostToMax(final int x, final int y, final int z, final PathType pathType, final float cost) {
|
| 270 | Node node = this.getNode(x, y, z);
|
| 271 | node.type = pathType;
|
| 272 | node.costMalus = Math.max(node.costMalus, cost);
|
| 273 | return node;
|
| 274 | }
|
| 275 |
|
| 276 | private Node getBlockedNode(final int x, final int y, final int z) {
|
| 277 | Node node = this.getNode(x, y, z);
|
| 278 | node.type = PathType.BLOCKED;
|
| 279 | node.costMalus = -1.0F;
|
| 280 | return node;
|
| 281 | }
|
| 282 |
|
| 283 | private Node getClosedNode(final int x, final int y, final int z, final PathType pathType) {
|
| 284 | Node node = this.getNode(x, y, z);
|
| 285 | node.closed = true;
|
| 286 | node.type = pathType;
|
| 287 | node.costMalus = pathType.getMalus();
|
| 288 | return node;
|
| 289 | }
|
| 290 |
|
| 291 | private @Nullable Node tryJumpOn(
|
| 292 | final int x,
|
| 293 | final int y,
|
| 294 | final int z,
|
| 295 | final int jumpSize,
|
| 296 | final double nodeHeight,
|
| 297 | final Direction travelDirection,
|
| 298 | final PathType blockPathTypeCurrent,
|
| 299 | final BlockPos.MutableBlockPos reusablePos
|
| 300 | ) {
|
| 301 | Node nodeAbove = this.findAcceptedNode(x, y + 1, z, jumpSize - 1, nodeHeight, travelDirection, blockPathTypeCurrent);
|
| 302 | if (nodeAbove == null) {
|
| 303 | return null;
|
| 304 | }
|
| 305 |
|
| 306 | if (this.mob.getBbWidth() >= 1.0F) {
|
| 307 | return nodeAbove;
|
| 308 | }
|
| 309 |
|
| 310 | if (nodeAbove.type != PathType.OPEN && nodeAbove.type != PathType.WALKABLE) {
|
| 311 | return nodeAbove;
|
| 312 | }
|
| 313 |
|
| 314 | double centerX = x - travelDirection.getStepX() + 0.5;
|
| 315 | double centerZ = z - travelDirection.getStepZ() + 0.5;
|
| 316 | double halfWidth = this.mob.getBbWidth() / 2.0;
|
| 317 | AABB grow = new AABB(
|
| 318 | centerX - halfWidth,
|
| 319 | this.getFloorLevel(reusablePos.set(centerX, y + 1, centerZ)) + 0.001,
|
| 320 | centerZ - halfWidth,
|
| 321 | centerX + halfWidth,
|
| 322 | this.mob.getBbHeight() + this.getFloorLevel(reusablePos.set((double)nodeAbove.x, (double)nodeAbove.y, (double)nodeAbove.z)) - 0.002,
|
| 323 | centerZ + halfWidth
|
| 324 | );
|
| 325 | return this.hasCollisions(grow) ? null : nodeAbove;
|
| 326 | }
|
| 327 |
|
| 328 | private @Nullable Node tryFindFirstNonWaterBelow(final int x, int y, final int z, @Nullable Node best) {
|
| 329 | y--;
|
| 330 |
|
| 331 | while (y > this.mob.level().getMinY()) {
|
| 332 | PathType pathTypeLocal = this.getCachedPathType(x, y, z);
|
| 333 | if (pathTypeLocal != PathType.WATER) {
|
| 334 | return best;
|
| 335 | }
|
| 336 |
|
| 337 | best = this.getNodeAndUpdateCostToMax(x, y, z, pathTypeLocal, this.mob.getPathfindingMalus(pathTypeLocal));
|
| 338 | y--;
|
| 339 | }
|
| 340 |
|
| 341 | return best;
|
| 342 | }
|
| 343 |
|
| 344 | private Node tryFindFirstGroundNodeBelow(final int x, final int y, final int z) {
|
| 345 | for (int currentY = y - 1; currentY >= this.mob.level().getMinY(); currentY--) {
|
| 346 | if (y - currentY > this.mob.getMaxFallDistance()) {
|
| 347 | return this.getBlockedNode(x, currentY, z);
|
| 348 | }
|
| 349 |
|
| 350 | PathType pathType = this.getCachedPathType(x, currentY, z);
|
| 351 | float pathCost = this.mob.getPathfindingMalus(pathType);
|
| 352 | if (pathType != PathType.OPEN) {
|
| 353 | if (pathCost >= 0.0F) {
|
| 354 | return this.getNodeAndUpdateCostToMax(x, currentY, z, pathType, pathCost);
|
| 355 | }
|
| 356 |
|
| 357 | return this.getBlockedNode(x, currentY, z);
|
| 358 | }
|
| 359 | }
|
| 360 |
|
| 361 | return this.getBlockedNode(x, y, z);
|
| 362 | }
|
| 363 |
|
| 364 | private boolean hasCollisions(final AABB aabb) {
|
| 365 | return this.collisionCache.computeIfAbsent(aabb, bb -> !this.currentContext.level().noCollision(this.mob, aabb));
|
| 366 | }
|
| 367 |
|
| 368 | protected PathType getCachedPathType(final int x, final int y, final int z) {
|
| 369 | return this.pathTypesByPosCacheByMob.computeIfAbsent(BlockPos.asLong(x, y, z), k -> this.getPathTypeOfMob(this.currentContext, x, y, z, this.mob));
|
| 370 | }
|
| 371 |
|
| 372 | @Override
|
| 373 | public PathType getPathTypeOfMob(final PathfindingContext context, final int x, final int y, final int z, final Mob mob) {
|
| 374 | Set<PathType> blockTypes = this.getPathTypeWithinMobBB(context, x, y, z);
|
| 375 | if (blockTypes.size() == 1) {
|
| 376 | return blockTypes.iterator().next();
|
| 377 | }
|
| 378 |
|
| 379 | if (blockTypes.contains(PathType.FENCE)) {
|
| 380 | return PathType.FENCE;
|
| 381 | }
|
| 382 |
|
| 383 | if (blockTypes.contains(PathType.UNPASSABLE_RAIL)) {
|
| 384 | return PathType.UNPASSABLE_RAIL;
|
| 385 | }
|
| 386 |
|
| 387 | PathType highestMalusPathTypeWithinBB = PathType.BLOCKED;
|
| 388 | float highestMalusWithinBB = mob.getPathfindingMalus(highestMalusPathTypeWithinBB);
|
| 389 |
|
| 390 | for (PathType pathType : blockTypes) {
|
| 391 | float malusForPathType = mob.getPathfindingMalus(pathType);
|
| 392 | if (malusForPathType < 0.0F) {
|
| 393 | return pathType;
|
| 394 | }
|
| 395 |
|
| 396 | if (malusForPathType >= highestMalusWithinBB) {
|
| 397 | highestMalusWithinBB = malusForPathType;
|
| 398 | highestMalusPathTypeWithinBB = pathType;
|
| 399 | }
|
| 400 | }
|
| 401 |
|
| 402 | PathType currentNodePathType = this.getPathType(context, x, y, z);
|
| 403 | boolean isLargeMob = this.entityWidth > 1;
|
| 404 | if (isLargeMob) {
|
| 405 | boolean isCurrentNodeCheaper = mob.getPathfindingMalus(currentNodePathType) < highestMalusWithinBB;
|
| 406 | boolean capMalusDueToCheapNode = isCurrentNodeCheaper && mob.getPathfindingMalus(PathType.BIG_MOBS_CLOSE_TO_DANGER) < highestMalusWithinBB;
|
| 407 | return capMalusDueToCheapNode ? PathType.BIG_MOBS_CLOSE_TO_DANGER : highestMalusPathTypeWithinBB;
|
| 408 | } else {
|
| 409 | return currentNodePathType == PathType.OPEN && highestMalusPathTypeWithinBB != PathType.OPEN && highestMalusWithinBB == 0.0F
|
| 410 | ? PathType.OPEN
|
| 411 | : highestMalusPathTypeWithinBB;
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | public Set<PathType> getPathTypeWithinMobBB(final PathfindingContext context, final int x, final int y, final int z) {
|
| 416 | EnumSet<PathType> blockTypes = EnumSet.noneOf(PathType.class);
|
| 417 |
|
| 418 | for (int dx = 0; dx < this.entityWidth; dx++) {
|
| 419 | for (int dy = 0; dy < this.entityHeight; dy++) {
|
| 420 | for (int dz = 0; dz < this.entityDepth; dz++) {
|
| 421 | int xx = dx + x;
|
| 422 | int yy = dy + y;
|
| 423 | int zz = dz + z;
|
| 424 | PathType blockType = this.getPathType(context, xx, yy, zz);
|
| 425 | BlockPos mobPosition = this.mob.blockPosition();
|
| 426 | boolean canPassDoors = this.canPassDoors();
|
| 427 | if (blockType == PathType.DOOR_WOOD_CLOSED && this.canOpenDoors() && canPassDoors) {
|
| 428 | blockType = PathType.WALKABLE_DOOR;
|
| 429 | }
|
| 430 |
|
| 431 | if (blockType == PathType.DOOR_OPEN && !canPassDoors) {
|
| 432 | blockType = PathType.BLOCKED;
|
| 433 | }
|
| 434 |
|
| 435 | if (blockType == PathType.RAIL
|
| 436 | && this.getPathType(context, mobPosition.getX(), mobPosition.getY(), mobPosition.getZ()) != PathType.RAIL
|
| 437 | && this.getPathType(context, mobPosition.getX(), mobPosition.getY() - 1, mobPosition.getZ()) != PathType.RAIL) {
|
| 438 | blockType = PathType.UNPASSABLE_RAIL;
|
| 439 | }
|
| 440 |
|
| 441 | blockTypes.add(blockType);
|
| 442 | }
|
| 443 | }
|
| 444 | }
|
| 445 |
|
| 446 | return blockTypes;
|
| 447 | }
|
| 448 |
|
| 449 | @Override
|
| 450 | public PathType getPathType(final PathfindingContext context, final int x, final int y, final int z) {
|
| 451 | return getPathTypeStatic(context, new BlockPos.MutableBlockPos(x, y, z));
|
| 452 | }
|
| 453 |
|
| 454 | public static PathType getPathTypeStatic(final Mob mob, final BlockPos pos) {
|
| 455 | return getPathTypeStatic(new PathfindingContext(mob.level(), mob), pos.mutable());
|
| 456 | }
|
| 457 |
|
| 458 | public static PathType getPathTypeStatic(final PathfindingContext context, final BlockPos.MutableBlockPos pos) {
|
| 459 | int x = pos.getX();
|
| 460 | int y = pos.getY();
|
| 461 | int z = pos.getZ();
|
| 462 | PathType blockPathType = context.getPathTypeFromState(x, y, z);
|
| 463 | if (blockPathType == PathType.OPEN && y >= context.level().getMinY() + 1) {
|
| 464 | return switch (context.getPathTypeFromState(x, y - 1, z)) {
|
| 465 | case OPEN, WATER, LAVA, WALKABLE -> PathType.OPEN;
|
| 466 | case FIRE -> PathType.FIRE;
|
| 467 | case DAMAGING -> PathType.DAMAGING;
|
| 468 | case STICKY_HONEY -> PathType.STICKY_HONEY;
|
| 469 | case POWDER_SNOW -> PathType.ON_TOP_OF_POWDER_SNOW;
|
| 470 | case DAMAGE_CAUTIOUS -> PathType.DAMAGE_CAUTIOUS;
|
| 471 | case TRAPDOOR -> PathType.ON_TOP_OF_TRAPDOOR;
|
| 472 | default -> checkNeighbourBlocks(context, x, y, z, PathType.WALKABLE);
|
| 473 | };
|
| 474 | } else {
|
| 475 | return blockPathType;
|
| 476 | }
|
| 477 | }
|
| 478 |
|
| 479 | public static PathType checkNeighbourBlocks(final PathfindingContext context, final int x, final int y, final int z, final PathType blockPathType) {
|
| 480 | for (int dx = -1; dx <= 1; dx++) {
|
| 481 | for (int dy = -1; dy <= 1; dy++) {
|
| 482 | for (int dz = -1; dz <= 1; dz++) {
|
| 483 | if (dx != 0 || dz != 0) {
|
| 484 | PathType pathType = context.getPathTypeFromState(x + dx, y + dy, z + dz);
|
| 485 | if (pathType == PathType.DAMAGING) {
|
| 486 | return PathType.DAMAGING_IN_NEIGHBOR;
|
| 487 | }
|
| 488 |
|
| 489 | if (pathType == PathType.FIRE || pathType == PathType.LAVA) {
|
| 490 | return PathType.FIRE_IN_NEIGHBOR;
|
| 491 | }
|
| 492 |
|
| 493 | if (pathType == PathType.WATER) {
|
| 494 | return PathType.WATER_BORDER;
|
| 495 | }
|
| 496 |
|
| 497 | if (pathType == PathType.DAMAGE_CAUTIOUS) {
|
| 498 | return PathType.DAMAGE_CAUTIOUS;
|
| 499 | }
|
| 500 | }
|
| 501 | }
|
| 502 | }
|
| 503 | }
|
| 504 |
|
| 505 | return blockPathType;
|
| 506 | }
|
| 507 |
|
| 508 | protected static PathType getPathTypeFromState(final BlockGetter level, final BlockPos pos) {
|
| 509 | BlockState blockState = level.getBlockState(pos);
|
| 510 | Block block = blockState.getBlock();
|
| 511 | if (blockState.isAir()) {
|
| 512 | return PathType.OPEN;
|
| 513 | }
|
| 514 |
|
| 515 | if (blockState.is(BlockTags.TRAPDOORS) || blockState.is(Blocks.LILY_PAD) || blockState.is(Blocks.BIG_DRIPLEAF)) {
|
| 516 | return PathType.TRAPDOOR;
|
| 517 | }
|
| 518 |
|
| 519 | if (blockState.is(Blocks.POWDER_SNOW)) {
|
| 520 | return PathType.POWDER_SNOW;
|
| 521 | }
|
| 522 |
|
| 523 | if (blockState.is(Blocks.CACTUS) || blockState.is(Blocks.SWEET_BERRY_BUSH)) {
|
| 524 | return PathType.DAMAGING;
|
| 525 | }
|
| 526 |
|
| 527 | if (blockState.is(Blocks.HONEY_BLOCK)) {
|
| 528 | return PathType.STICKY_HONEY;
|
| 529 | }
|
| 530 |
|
| 531 | if (blockState.is(Blocks.COCOA)) {
|
| 532 | return PathType.COCOA;
|
| 533 | }
|
| 534 |
|
| 535 | if (!blockState.is(Blocks.WITHER_ROSE) && !blockState.is(BlockTags.SPELEOTHEMS)) {
|
| 536 | FluidState fluidState = blockState.getFluidState();
|
| 537 | if (fluidState.is(FluidTags.LAVA)) {
|
| 538 | return PathType.LAVA;
|
| 539 | }
|
| 540 |
|
| 541 | if (isBurningBlock(blockState)) {
|
| 542 | return PathType.FIRE;
|
| 543 | }
|
| 544 |
|
| 545 | if (block instanceof DoorBlock door) {
|
| 546 | if (blockState.getValue(DoorBlock.OPEN)) {
|
| 547 | return PathType.DOOR_OPEN;
|
| 548 | } else {
|
| 549 | return door.type().canOpenByHand() ? PathType.DOOR_WOOD_CLOSED : PathType.DOOR_IRON_CLOSED;
|
| 550 | }
|
| 551 | } else {
|
| 552 | if (block instanceof BaseRailBlock) {
|
| 553 | return PathType.RAIL;
|
| 554 | }
|
| 555 |
|
| 556 | if (block instanceof LeavesBlock) {
|
| 557 | return PathType.LEAVES;
|
| 558 | }
|
| 559 |
|
| 560 | if (!blockState.is(BlockTags.FENCES) && !blockState.is(BlockTags.WALLS) && (!(block instanceof FenceGateBlock) || blockState.getValue(FenceGateBlock.OPEN))
|
| 561 | )
|
| 562 | {
|
| 563 | if (!blockState.isPathfindable(PathComputationType.LAND)) {
|
| 564 | return PathType.BLOCKED;
|
| 565 | } else {
|
| 566 | return fluidState.is(FluidTags.WATER) ? PathType.WATER : PathType.OPEN;
|
| 567 | }
|
| 568 | } else {
|
| 569 | return PathType.FENCE;
|
| 570 | }
|
| 571 | }
|
| 572 | } else {
|
| 573 | return PathType.DAMAGE_CAUTIOUS;
|
| 574 | }
|
| 575 | }
|
| 576 | }
|
| 577 |
|