| 1 | package net.minecraft.world.level.pathfinder;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 6 | import java.util.Map;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.tags.FluidTags;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.world.entity.Mob;
|
| 12 | import net.minecraft.world.level.PathNavigationRegion;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.material.FluidState;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public class SwimNodeEvaluator extends NodeEvaluator {
|
| 18 | private final boolean allowBreaching;
|
| 19 | private final Long2ObjectMap<PathType> pathTypesByPosCache = new Long2ObjectOpenHashMap<>();
|
| 20 |
|
| 21 | public SwimNodeEvaluator(final boolean allowBreaching) {
|
| 22 | this.allowBreaching = allowBreaching;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public void prepare(final PathNavigationRegion level, final Mob entity) {
|
| 27 | super.prepare(level, entity);
|
| 28 | this.pathTypesByPosCache.clear();
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public void done() {
|
| 33 | super.done();
|
| 34 | this.pathTypesByPosCache.clear();
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public Node getStart() {
|
| 39 | return this.getNode(Mth.floor(this.mob.getBoundingBox().minX), Mth.floor(this.mob.getBoundingBox().minY + 0.5), Mth.floor(this.mob.getBoundingBox().minZ));
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public Target getTarget(final double x, final double y, final double z) {
|
| 44 | return this.getTargetNodeAt(x, y, z);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public int getNeighbors(final Node[] neighbors, final Node pos) {
|
| 49 | int count = 0;
|
| 50 | Map<Direction, Node> nodes = Maps.newEnumMap(Direction.class);
|
| 51 |
|
| 52 | for (Direction direction : Direction.values()) {
|
| 53 | Node node = this.findAcceptedNode(pos.x + direction.getStepX(), pos.y + direction.getStepY(), pos.z + direction.getStepZ());
|
| 54 | nodes.put(direction, node);
|
| 55 | if (this.isNodeValid(node)) {
|
| 56 | neighbors[count++] = node;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 61 | Direction secondDirection = direction.getClockWise();
|
| 62 | if (hasMalus(nodes.get(direction)) && hasMalus(nodes.get(secondDirection))) {
|
| 63 | Node diagonalNode = this.findAcceptedNode(
|
| 64 | pos.x + direction.getStepX() + secondDirection.getStepX(), pos.y, pos.z + direction.getStepZ() + secondDirection.getStepZ()
|
| 65 | );
|
| 66 | if (this.isNodeValid(diagonalNode)) {
|
| 67 | neighbors[count++] = diagonalNode;
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return count;
|
| 73 | }
|
| 74 |
|
| 75 | protected boolean isNodeValid(final @Nullable Node node) {
|
| 76 | return node != null && !node.closed;
|
| 77 | }
|
| 78 |
|
| 79 | private static boolean hasMalus(final @Nullable Node node) {
|
| 80 | return node != null && node.costMalus >= 0.0F;
|
| 81 | }
|
| 82 |
|
| 83 | protected @Nullable Node findAcceptedNode(final int x, final int y, final int z) {
|
| 84 | Node best = null;
|
| 85 | PathType pathType = this.getCachedBlockType(x, y, z);
|
| 86 | if (this.allowBreaching && pathType == PathType.BREACH || pathType == PathType.WATER) {
|
| 87 | float pathCost = this.mob.getPathfindingMalus(pathType);
|
| 88 | if (pathCost >= 0.0F) {
|
| 89 | best = this.getNode(x, y, z);
|
| 90 | best.type = pathType;
|
| 91 | best.costMalus = Math.max(best.costMalus, pathCost);
|
| 92 | if (this.currentContext.level().getFluidState(new BlockPos(x, y, z)).isEmpty()) {
|
| 93 | best.costMalus += 8.0F;
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | return best;
|
| 99 | }
|
| 100 |
|
| 101 | protected PathType getCachedBlockType(final int x, final int y, final int z) {
|
| 102 | return this.pathTypesByPosCache.computeIfAbsent(BlockPos.asLong(x, y, z), k -> this.getPathType(this.currentContext, x, y, z));
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public PathType getPathType(final PathfindingContext context, final int x, final int y, final int z) {
|
| 107 | return this.getPathTypeOfMob(context, x, y, z, this.mob);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public PathType getPathTypeOfMob(final PathfindingContext context, final int x, final int y, final int z, final Mob mob) {
|
| 112 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 113 |
|
| 114 | for (int xx = x; xx < x + this.entityWidth; xx++) {
|
| 115 | for (int yy = y; yy < y + this.entityHeight; yy++) {
|
| 116 | for (int zz = z; zz < z + this.entityDepth; zz++) {
|
| 117 | BlockState blockState = context.getBlockState(pos.set(xx, yy, zz));
|
| 118 | FluidState fluidState = blockState.getFluidState();
|
| 119 | BlockState belowState = context.getBlockState(pos.below());
|
| 120 | if (fluidState.isEmpty() && belowState.isPathfindable(PathComputationType.WATER) && blockState.isAir()) {
|
| 121 | return PathType.BREACH;
|
| 122 | }
|
| 123 |
|
| 124 | if (!fluidState.is(FluidTags.WATER)) {
|
| 125 | return PathType.BLOCKED;
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | BlockState blockState = context.getBlockState(pos);
|
| 132 | return blockState.isPathfindable(PathComputationType.WATER) ? PathType.WATER : PathType.BLOCKED;
|
| 133 | }
|
| 134 | }
|
| 135 |
|