| 1 | package net.minecraft.world.phys.shapes;
|
| 2 |
|
| 3 | import java.util.BitSet;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 |
|
| 6 | public final class BitSetDiscreteVoxelShape extends DiscreteVoxelShape {
|
| 7 | private final BitSet storage;
|
| 8 | private int xMin;
|
| 9 | private int yMin;
|
| 10 | private int zMin;
|
| 11 | private int xMax;
|
| 12 | private int yMax;
|
| 13 | private int zMax;
|
| 14 |
|
| 15 | public BitSetDiscreteVoxelShape(final int xSize, final int ySize, final int zSize) {
|
| 16 | super(xSize, ySize, zSize);
|
| 17 | this.storage = new BitSet(xSize * ySize * zSize);
|
| 18 | this.xMin = xSize;
|
| 19 | this.yMin = ySize;
|
| 20 | this.zMin = zSize;
|
| 21 | }
|
| 22 |
|
| 23 | public static BitSetDiscreteVoxelShape withFilledBounds(
|
| 24 | final int xSize, final int ySize, final int zSize, final int xMin, final int yMin, final int zMin, final int xMax, final int yMax, final int zMax
|
| 25 | ) {
|
| 26 | BitSetDiscreteVoxelShape shape = new BitSetDiscreteVoxelShape(xSize, ySize, zSize);
|
| 27 | shape.xMin = xMin;
|
| 28 | shape.yMin = yMin;
|
| 29 | shape.zMin = zMin;
|
| 30 | shape.xMax = xMax;
|
| 31 | shape.yMax = yMax;
|
| 32 | shape.zMax = zMax;
|
| 33 |
|
| 34 | for (int x = xMin; x < xMax; x++) {
|
| 35 | for (int y = yMin; y < yMax; y++) {
|
| 36 | for (int z = zMin; z < zMax; z++) {
|
| 37 | shape.fillUpdateBounds(x, y, z, false);
|
| 38 | }
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | return shape;
|
| 43 | }
|
| 44 |
|
| 45 | public BitSetDiscreteVoxelShape(final DiscreteVoxelShape voxelShape) {
|
| 46 | super(voxelShape.xSize, voxelShape.ySize, voxelShape.zSize);
|
| 47 | if (voxelShape instanceof BitSetDiscreteVoxelShape bitSetDiscreteVoxelShape) {
|
| 48 | this.storage = (BitSet)bitSetDiscreteVoxelShape.storage.clone();
|
| 49 | } else {
|
| 50 | this.storage = new BitSet(this.xSize * this.ySize * this.zSize);
|
| 51 |
|
| 52 | for (int x = 0; x < this.xSize; x++) {
|
| 53 | for (int y = 0; y < this.ySize; y++) {
|
| 54 | for (int z = 0; z < this.zSize; z++) {
|
| 55 | if (voxelShape.isFull(x, y, z)) {
|
| 56 | this.storage.set(this.getIndex(x, y, z));
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | this.xMin = voxelShape.firstFull(Direction.Axis.X);
|
| 64 | this.yMin = voxelShape.firstFull(Direction.Axis.Y);
|
| 65 | this.zMin = voxelShape.firstFull(Direction.Axis.Z);
|
| 66 | this.xMax = voxelShape.lastFull(Direction.Axis.X);
|
| 67 | this.yMax = voxelShape.lastFull(Direction.Axis.Y);
|
| 68 | this.zMax = voxelShape.lastFull(Direction.Axis.Z);
|
| 69 | }
|
| 70 |
|
| 71 | private int getIndex(final int x, final int y, final int z) {
|
| 72 | return (x * this.ySize + y) * this.zSize + z;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public boolean isFull(final int x, final int y, final int z) {
|
| 77 | return this.storage.get(this.getIndex(x, y, z));
|
| 78 | }
|
| 79 |
|
| 80 | private void fillUpdateBounds(final int x, final int y, final int z, final boolean updateBounds) {
|
| 81 | this.storage.set(this.getIndex(x, y, z));
|
| 82 | if (updateBounds) {
|
| 83 | this.xMin = Math.min(this.xMin, x);
|
| 84 | this.yMin = Math.min(this.yMin, y);
|
| 85 | this.zMin = Math.min(this.zMin, z);
|
| 86 | this.xMax = Math.max(this.xMax, x + 1);
|
| 87 | this.yMax = Math.max(this.yMax, y + 1);
|
| 88 | this.zMax = Math.max(this.zMax, z + 1);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void fill(final int x, final int y, final int z) {
|
| 94 | this.fillUpdateBounds(x, y, z, true);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean isEmpty() {
|
| 99 | return this.storage.isEmpty();
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public int firstFull(final Direction.Axis axis) {
|
| 104 | return axis.choose(this.xMin, this.yMin, this.zMin);
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public int lastFull(final Direction.Axis axis) {
|
| 109 | return axis.choose(this.xMax, this.yMax, this.zMax);
|
| 110 | }
|
| 111 |
|
| 112 | public static BitSetDiscreteVoxelShape join(
|
| 113 | final DiscreteVoxelShape first,
|
| 114 | final DiscreteVoxelShape second,
|
| 115 | final IndexMerger xMerger,
|
| 116 | final IndexMerger yMerger,
|
| 117 | final IndexMerger zMerger,
|
| 118 | final BooleanOp op
|
| 119 | ) {
|
| 120 | BitSetDiscreteVoxelShape shape = new BitSetDiscreteVoxelShape(xMerger.size() - 1, yMerger.size() - 1, zMerger.size() - 1);
|
| 121 | int[] bounds = new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE};
|
| 122 | xMerger.forMergedIndexes((x1, x2, xr) -> {
|
| 123 | boolean[] updatedSlice = new boolean[]{false};
|
| 124 | yMerger.forMergedIndexes((y1, y2, yr) -> {
|
| 125 | boolean[] updatedColumn = new boolean[]{false};
|
| 126 | zMerger.forMergedIndexes((z1, z2, zr) -> {
|
| 127 | if (op.apply(first.isFullWide(x1, y1, z1), second.isFullWide(x2, y2, z2))) {
|
| 128 | shape.storage.set(shape.getIndex(xr, yr, zr));
|
| 129 | bounds[2] = Math.min(bounds[2], zr);
|
| 130 | bounds[5] = Math.max(bounds[5], zr);
|
| 131 | updatedColumn[0] = true;
|
| 132 | }
|
| 133 |
|
| 134 | return true;
|
| 135 | });
|
| 136 | if (updatedColumn[0]) {
|
| 137 | bounds[1] = Math.min(bounds[1], yr);
|
| 138 | bounds[4] = Math.max(bounds[4], yr);
|
| 139 | updatedSlice[0] = true;
|
| 140 | }
|
| 141 |
|
| 142 | return true;
|
| 143 | });
|
| 144 | if (updatedSlice[0]) {
|
| 145 | bounds[0] = Math.min(bounds[0], xr);
|
| 146 | bounds[3] = Math.max(bounds[3], xr);
|
| 147 | }
|
| 148 |
|
| 149 | return true;
|
| 150 | });
|
| 151 | shape.xMin = bounds[0];
|
| 152 | shape.yMin = bounds[1];
|
| 153 | shape.zMin = bounds[2];
|
| 154 | shape.xMax = bounds[3] + 1;
|
| 155 | shape.yMax = bounds[4] + 1;
|
| 156 | shape.zMax = bounds[5] + 1;
|
| 157 | return shape;
|
| 158 | }
|
| 159 |
|
| 160 | static void forAllBoxes(final DiscreteVoxelShape voxelShape, final DiscreteVoxelShape.IntLineConsumer consumer, final boolean mergeNeighbors) {
|
| 161 | BitSetDiscreteVoxelShape shape = new BitSetDiscreteVoxelShape(voxelShape);
|
| 162 |
|
| 163 | for (int y = 0; y < shape.ySize; y++) {
|
| 164 | for (int x = 0; x < shape.xSize; x++) {
|
| 165 | int lastStartZ = -1;
|
| 166 |
|
| 167 | for (int z = 0; z <= shape.zSize; z++) {
|
| 168 | if (shape.isFullWide(x, y, z)) {
|
| 169 | if (mergeNeighbors) {
|
| 170 | if (lastStartZ == -1) {
|
| 171 | lastStartZ = z;
|
| 172 | }
|
| 173 | } else {
|
| 174 | consumer.consume(x, y, z, x + 1, y + 1, z + 1);
|
| 175 | }
|
| 176 | } else if (lastStartZ != -1) {
|
| 177 | int endX = x;
|
| 178 | int endY = y;
|
| 179 | shape.clearZStrip(lastStartZ, z, x, y);
|
| 180 |
|
| 181 | while (shape.isZStripFull(lastStartZ, z, endX + 1, y)) {
|
| 182 | shape.clearZStrip(lastStartZ, z, endX + 1, y);
|
| 183 | endX++;
|
| 184 | }
|
| 185 |
|
| 186 | while (shape.isXZRectangleFull(x, endX + 1, lastStartZ, z, endY + 1)) {
|
| 187 | for (int cx = x; cx <= endX; cx++) {
|
| 188 | shape.clearZStrip(lastStartZ, z, cx, endY + 1);
|
| 189 | }
|
| 190 |
|
| 191 | endY++;
|
| 192 | }
|
| 193 |
|
| 194 | consumer.consume(x, y, lastStartZ, endX + 1, endY + 1, z);
|
| 195 | lastStartZ = -1;
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | private boolean isZStripFull(final int startZ, final int endZ, final int x, final int y) {
|
| 203 | return x < this.xSize && y < this.ySize ? this.storage.nextClearBit(this.getIndex(x, y, startZ)) >= this.getIndex(x, y, endZ) : false;
|
| 204 | }
|
| 205 |
|
| 206 | private boolean isXZRectangleFull(final int startX, final int endX, final int startZ, final int endZ, final int y) {
|
| 207 | for (int x = startX; x < endX; x++) {
|
| 208 | if (!this.isZStripFull(startZ, endZ, x, y)) {
|
| 209 | return false;
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | return true;
|
| 214 | }
|
| 215 |
|
| 216 | private void clearZStrip(final int startZ, final int endZ, final int x, final int y) {
|
| 217 | this.storage.clear(this.getIndex(x, y, startZ), this.getIndex(x, y, endZ));
|
| 218 | }
|
| 219 |
|
| 220 | public boolean isInterior(final int x, final int y, final int z) {
|
| 221 | boolean isInterior = x > 0 && x < this.xSize - 1 && y > 0 && y < this.ySize - 1 && z > 0 && z < this.zSize - 1;
|
| 222 | return isInterior
|
| 223 | && this.isFull(x, y, z)
|
| 224 | && this.isFull(x - 1, y, z)
|
| 225 | && this.isFull(x + 1, y, z)
|
| 226 | && this.isFull(x, y - 1, z)
|
| 227 | && this.isFull(x, y + 1, z)
|
| 228 | && this.isFull(x, y, z - 1)
|
| 229 | && this.isFull(x, y, z + 1);
|
| 230 | }
|
| 231 | }
|
| 232 |
|