| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 6 | import it.unimi.dsi.fastutil.ints.IntStack;
|
| 7 | import java.util.Locale;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.block.Block;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 |
|
| 16 | public class BlockUtil {
|
| 17 | public static final int MAX_POSITION_DIFFERENCE_PACKING_RADIUS = 127;
|
| 18 |
|
| 19 | public static BlockUtil.FoundRectangle getLargestRectangleAround(
|
| 20 | final BlockPos center, final Direction.Axis axis1, final int limit1, final Direction.Axis axis2, final int limit2, final Predicate<BlockPos> test
|
| 21 | ) {
|
| 22 | BlockPos.MutableBlockPos pos = center.mutable();
|
| 23 | Direction negativeDirection1 = Direction.get(Direction.AxisDirection.NEGATIVE, axis1);
|
| 24 | Direction positiveDirection1 = negativeDirection1.getOpposite();
|
| 25 | Direction negativeDirection2 = Direction.get(Direction.AxisDirection.NEGATIVE, axis2);
|
| 26 | Direction positiveDirection2 = negativeDirection2.getOpposite();
|
| 27 | int negativeDelta1 = getLimit(test, pos.set(center), negativeDirection1, limit1);
|
| 28 | int positiveDelta1 = getLimit(test, pos.set(center), positiveDirection1, limit1);
|
| 29 | int centerIndex1 = negativeDelta1;
|
| 30 | BlockUtil.IntBounds[] boundsByAxis1 = new BlockUtil.IntBounds[centerIndex1 + 1 + positiveDelta1];
|
| 31 | boundsByAxis1[centerIndex1] = new BlockUtil.IntBounds(
|
| 32 | getLimit(test, pos.set(center), negativeDirection2, limit2), getLimit(test, pos.set(center), positiveDirection2, limit2)
|
| 33 | );
|
| 34 | int centerIndex2 = boundsByAxis1[centerIndex1].min;
|
| 35 |
|
| 36 | for (int i = 1; i <= negativeDelta1; i++) {
|
| 37 | BlockUtil.IntBounds lastBounds = boundsByAxis1[centerIndex1 - (i - 1)];
|
| 38 | boundsByAxis1[centerIndex1 - i] = new BlockUtil.IntBounds(
|
| 39 | getLimit(test, pos.set(center).move(negativeDirection1, i), negativeDirection2, lastBounds.min),
|
| 40 | getLimit(test, pos.set(center).move(negativeDirection1, i), positiveDirection2, lastBounds.max)
|
| 41 | );
|
| 42 | }
|
| 43 |
|
| 44 | for (int i = 1; i <= positiveDelta1; i++) {
|
| 45 | BlockUtil.IntBounds lastBounds = boundsByAxis1[centerIndex1 + i - 1];
|
| 46 | boundsByAxis1[centerIndex1 + i] = new BlockUtil.IntBounds(
|
| 47 | getLimit(test, pos.set(center).move(positiveDirection1, i), negativeDirection2, lastBounds.min),
|
| 48 | getLimit(test, pos.set(center).move(positiveDirection1, i), positiveDirection2, lastBounds.max)
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | int minAxis1 = 0;
|
| 53 | int minAxis2 = 0;
|
| 54 | int sizeAxis1 = 0;
|
| 55 | int sizeAxis2 = 0;
|
| 56 | int[] columns = new int[boundsByAxis1.length];
|
| 57 |
|
| 58 | for (int i2 = centerIndex2; i2 >= 0; i2--) {
|
| 59 | for (int i1 = 0; i1 < boundsByAxis1.length; i1++) {
|
| 60 | BlockUtil.IntBounds bounds2 = boundsByAxis1[i1];
|
| 61 | int min2 = centerIndex2 - bounds2.min;
|
| 62 | int max2 = centerIndex2 + bounds2.max;
|
| 63 | columns[i1] = i2 >= min2 && i2 <= max2 ? max2 + 1 - i2 : 0;
|
| 64 | }
|
| 65 |
|
| 66 | Pair<BlockUtil.IntBounds, Integer> rectangle = getMaxRectangleLocation(columns);
|
| 67 | BlockUtil.IntBounds boundsAxis1 = rectangle.getFirst();
|
| 68 | int newSizeAxis1 = 1 + boundsAxis1.max - boundsAxis1.min;
|
| 69 | int newSizeAxis2 = rectangle.getSecond();
|
| 70 | if (newSizeAxis1 * newSizeAxis2 > sizeAxis1 * sizeAxis2) {
|
| 71 | minAxis1 = boundsAxis1.min;
|
| 72 | minAxis2 = i2;
|
| 73 | sizeAxis1 = newSizeAxis1;
|
| 74 | sizeAxis2 = newSizeAxis2;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | return new BlockUtil.FoundRectangle(center.relative(axis1, minAxis1 - centerIndex1).relative(axis2, minAxis2 - centerIndex2), sizeAxis1, sizeAxis2);
|
| 79 | }
|
| 80 |
|
| 81 | private static int getLimit(final Predicate<BlockPos> test, final BlockPos.MutableBlockPos pos, final Direction direction, final int limit) {
|
| 82 | int max = 0;
|
| 83 |
|
| 84 | while (max < limit && test.test(pos.move(direction))) {
|
| 85 | max++;
|
| 86 | }
|
| 87 |
|
| 88 | return max;
|
| 89 | }
|
| 90 |
|
| 91 | @VisibleForTesting
|
| 92 | static Pair<BlockUtil.IntBounds, Integer> getMaxRectangleLocation(final int[] columns) {
|
| 93 | int maxStart = 0;
|
| 94 | int maxEnd = 0;
|
| 95 | int maxHeight = 0;
|
| 96 | IntStack stack = new IntArrayList();
|
| 97 | stack.push(0);
|
| 98 |
|
| 99 | for (int column = 1; column <= columns.length; column++) {
|
| 100 | int height = column == columns.length ? 0 : columns[column];
|
| 101 |
|
| 102 | while (!stack.isEmpty()) {
|
| 103 | int stackHeight = columns[stack.topInt()];
|
| 104 | if (height >= stackHeight) {
|
| 105 | stack.push(column);
|
| 106 | break;
|
| 107 | }
|
| 108 |
|
| 109 | stack.popInt();
|
| 110 | int start = stack.isEmpty() ? 0 : stack.topInt() + 1;
|
| 111 | if (stackHeight * (column - start) > maxHeight * (maxEnd - maxStart)) {
|
| 112 | maxEnd = column;
|
| 113 | maxStart = start;
|
| 114 | maxHeight = stackHeight;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | if (stack.isEmpty()) {
|
| 119 | stack.push(column);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | return new Pair<>(new BlockUtil.IntBounds(maxStart, maxEnd - 1), maxHeight);
|
| 124 | }
|
| 125 |
|
| 126 | public static Optional<BlockPos> getTopConnectedBlock(
|
| 127 | final BlockGetter level, final BlockPos pos, final Block bodyBlock, final Direction growthDirection, final Block headBlock
|
| 128 | ) {
|
| 129 | BlockPos.MutableBlockPos forwardPos = pos.mutable();
|
| 130 |
|
| 131 | BlockState forwardState;
|
| 132 | do {
|
| 133 | forwardPos.move(growthDirection);
|
| 134 | forwardState = level.getBlockState(forwardPos);
|
| 135 | } while (forwardState.is(bodyBlock));
|
| 136 |
|
| 137 | return forwardState.is(headBlock) ? Optional.of(forwardPos) : Optional.empty();
|
| 138 | }
|
| 139 |
|
| 140 | public static int clampedPackDifferenceInPosition(final BlockPos pos, final BlockPos testPos, final int xRadius, final int yRadius, final int zRadius) {
|
| 141 | int clampedXRadius = Mth.clamp(xRadius, 0, 127);
|
| 142 | int clampedYRadius = Mth.clamp(yRadius, 0, 127);
|
| 143 | int clampedZRadius = Mth.clamp(zRadius, 0, 127);
|
| 144 | int xDiff = Mth.clamp(testPos.getX() - pos.getX(), -127, 127);
|
| 145 | int yDiff = Mth.clamp(testPos.getY() - pos.getY(), -127, 127);
|
| 146 | int zDiff = Mth.clamp(testPos.getZ() - pos.getZ(), -127, 127);
|
| 147 | return packDifferenceInPositionInternal(clampedXRadius, clampedYRadius, clampedZRadius, xDiff, yDiff, zDiff);
|
| 148 | }
|
| 149 |
|
| 150 | public static int packDifferenceInPosition(final BlockPos pos, final BlockPos testPos, final int xRadius, final int yRadius, final int zRadius) {
|
| 151 | if (xRadius <= 127 && yRadius <= 127 && zRadius <= 127) {
|
| 152 | int xDiff = checkDifferenceInBounds(testPos.getX(), pos.getX());
|
| 153 | int yDiff = checkDifferenceInBounds(testPos.getY(), pos.getY());
|
| 154 | int zDiff = checkDifferenceInBounds(testPos.getZ(), pos.getZ());
|
| 155 | return packDifferenceInPositionInternal(xRadius, yRadius, zRadius, xDiff, yDiff, zDiff);
|
| 156 | } else {
|
| 157 | throw new IllegalArgumentException("The radius cannot be larger than 127 when packing the difference between two positions");
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | private static int packDifferenceInPositionInternal(final int xRadius, final int yRadius, final int zRadius, final int xDiff, final int yDiff, final int zDiff) {
|
| 162 | return (xDiff + xRadius & 0xFF) << 16 | (yDiff + yRadius & 0xFF) << 8 | zDiff + zRadius & 0xFF;
|
| 163 | }
|
| 164 |
|
| 165 | private static int checkDifferenceInBounds(final int from, final int to) {
|
| 166 | int difference = from - to;
|
| 167 | if (difference <= 127 && difference >= -127) {
|
| 168 | return difference;
|
| 169 | } else {
|
| 170 | throw new IllegalArgumentException(String.format(Locale.ROOT, "The difference must be within [%s,%s]", 127, -127));
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | public static BlockPos unpackDifferenceInPosition(final BlockPos pos, final int packedDiff, final int xRadius, final int yRadius, final int zRadius) {
|
| 175 | if (xRadius <= 127 && yRadius <= 127 && zRadius <= 127) {
|
| 176 | int xDiff = (packedDiff >> 16 & 0xFF) - xRadius;
|
| 177 | int yDiff = (packedDiff >> 8 & 0xFF) - yRadius;
|
| 178 | int zDiff = (packedDiff & 0xFF) - zRadius;
|
| 179 | return pos.offset(xDiff, yDiff, zDiff);
|
| 180 | } else {
|
| 181 | throw new IllegalArgumentException("The radius cannot be larger than 127 when unpacking the difference between two positions.");
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | public static class FoundRectangle {
|
| 186 | public final BlockPos minCorner;
|
| 187 | public final int axis1Size;
|
| 188 | public final int axis2Size;
|
| 189 |
|
| 190 | public FoundRectangle(final BlockPos minCorner, final int axis1Size, final int axis2Size) {
|
| 191 | this.minCorner = minCorner;
|
| 192 | this.axis1Size = axis1Size;
|
| 193 | this.axis2Size = axis2Size;
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | public static class IntBounds {
|
| 198 | public final int min;
|
| 199 | public final int max;
|
| 200 |
|
| 201 | public IntBounds(final int min, final int max) {
|
| 202 | this.min = min;
|
| 203 | this.max = max;
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public String toString() {
|
| 208 | return "IntBounds{min=" + this.min + ", max=" + this.max + "}";
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|