| 1 | package net.minecraft.world.level.pathfinder;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.world.entity.Mob;
|
| 7 | import net.minecraft.world.level.PathNavigationRegion;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public class AmphibiousNodeEvaluator extends WalkNodeEvaluator {
|
| 11 | private final boolean prefersShallowSwimming;
|
| 12 | private float oldWalkableCost;
|
| 13 | private float oldWaterBorderCost;
|
| 14 |
|
| 15 | public AmphibiousNodeEvaluator(final boolean prefersShallowSwimming) {
|
| 16 | this.prefersShallowSwimming = prefersShallowSwimming;
|
| 17 | }
|
| 18 |
|
| 19 | @Override
|
| 20 | public void prepare(final PathNavigationRegion level, final Mob entity) {
|
| 21 | super.prepare(level, entity);
|
| 22 | entity.setPathfindingMalus(PathType.WATER, 0.0F);
|
| 23 | this.oldWalkableCost = entity.getPathfindingMalus(PathType.WALKABLE);
|
| 24 | entity.setPathfindingMalus(PathType.WALKABLE, 6.0F);
|
| 25 | this.oldWaterBorderCost = entity.getPathfindingMalus(PathType.WATER_BORDER);
|
| 26 | entity.setPathfindingMalus(PathType.WATER_BORDER, 4.0F);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public void done() {
|
| 31 | this.mob.setPathfindingMalus(PathType.WALKABLE, this.oldWalkableCost);
|
| 32 | this.mob.setPathfindingMalus(PathType.WATER_BORDER, this.oldWaterBorderCost);
|
| 33 | super.done();
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public Node getStart() {
|
| 38 | return !this.mob.isInWater()
|
| 39 | ? super.getStart()
|
| 40 | : this.getStartNode(
|
| 41 | new BlockPos(Mth.floor(this.mob.getBoundingBox().minX), Mth.floor(this.mob.getBoundingBox().minY + 0.5), Mth.floor(this.mob.getBoundingBox().minZ))
|
| 42 | );
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public Target getTarget(final double x, final double y, final double z) {
|
| 47 | return this.getTargetNodeAt(x, y + 0.5, z);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public int getNeighbors(final Node[] neighbors, final Node pos) {
|
| 52 | int numValidNeighbors = super.getNeighbors(neighbors, pos);
|
| 53 | PathType blockPathTypeAbove = this.getCachedPathType(pos.x, pos.y + 1, pos.z);
|
| 54 | PathType blockPathTypeCurrent = this.getCachedPathType(pos.x, pos.y, pos.z);
|
| 55 | int jumpSize;
|
| 56 | if (this.mob.getPathfindingMalus(blockPathTypeAbove) >= 0.0F && blockPathTypeCurrent != PathType.STICKY_HONEY) {
|
| 57 | jumpSize = Mth.floor(Math.max(1.0F, this.mob.maxUpStep()));
|
| 58 | } else {
|
| 59 | jumpSize = 0;
|
| 60 | }
|
| 61 |
|
| 62 | double posHeight = this.getFloorLevel(new BlockPos(pos.x, pos.y, pos.z));
|
| 63 | Node upNode = this.findAcceptedNode(pos.x, pos.y + 1, pos.z, Math.max(0, jumpSize - 1), posHeight, Direction.UP, blockPathTypeCurrent);
|
| 64 | Node downNode = this.findAcceptedNode(pos.x, pos.y - 1, pos.z, jumpSize, posHeight, Direction.DOWN, blockPathTypeCurrent);
|
| 65 | if (this.isVerticalNeighborValid(upNode, pos)) {
|
| 66 | neighbors[numValidNeighbors++] = upNode;
|
| 67 | }
|
| 68 |
|
| 69 | if (this.isVerticalNeighborValid(downNode, pos) && blockPathTypeCurrent != PathType.TRAPDOOR) {
|
| 70 | neighbors[numValidNeighbors++] = downNode;
|
| 71 | }
|
| 72 |
|
| 73 | for (int i = 0; i < numValidNeighbors; i++) {
|
| 74 | Node neighbor = neighbors[i];
|
| 75 | if (neighbor.type == PathType.WATER && this.prefersShallowSwimming && neighbor.y < this.mob.level().getSeaLevel() - 10) {
|
| 76 | neighbor.costMalus++;
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return numValidNeighbors;
|
| 81 | }
|
| 82 |
|
| 83 | private boolean isVerticalNeighborValid(final @Nullable Node verticalNode, final Node pos) {
|
| 84 | return this.isNeighborValid(verticalNode, pos) && verticalNode.type == PathType.WATER;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected boolean isAmphibious() {
|
| 89 | return true;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public PathType getPathType(final PathfindingContext context, final int x, final int y, final int z) {
|
| 94 | PathType blockPathType = context.getPathTypeFromState(x, y, z);
|
| 95 | if (blockPathType == PathType.WATER) {
|
| 96 | BlockPos.MutableBlockPos reusablePos = new BlockPos.MutableBlockPos();
|
| 97 |
|
| 98 | for (Direction direction : Direction.values()) {
|
| 99 | reusablePos.set(x, y, z).move(direction);
|
| 100 | PathType pathType = context.getPathTypeFromState(reusablePos.getX(), reusablePos.getY(), reusablePos.getZ());
|
| 101 | if (pathType == PathType.BLOCKED) {
|
| 102 | return PathType.WATER_BORDER;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | return PathType.WATER;
|
| 107 | } else {
|
| 108 | return super.getPathType(context, x, y, z);
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|