| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import java.util.BitSet;
|
| 4 |
|
| 5 | public class CarvingMask implements CarverOutput {
|
| 6 | private final int minY;
|
| 7 | private final int maxY;
|
| 8 | private final int height;
|
| 9 | private final BitSet mask;
|
| 10 |
|
| 11 | public CarvingMask(final int minY, final int maxY) {
|
| 12 | this.minY = minY;
|
| 13 | this.maxY = maxY;
|
| 14 | this.height = maxY - minY + 1;
|
| 15 | this.mask = new BitSet(256 * this.height);
|
| 16 | }
|
| 17 |
|
| 18 | private int getIndex(final int x, final int y, final int z) {
|
| 19 | return y - this.minY + (z + (x << 4)) * this.height;
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public int minY() {
|
| 24 | return this.minY;
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public int maxY() {
|
| 29 | return this.maxY;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void carve(final int x, final int y, final int z) {
|
| 34 | this.mask.set(this.getIndex(x, y, z));
|
| 35 | }
|
| 36 |
|
| 37 | public void visit(final CarvingMask.Visitor visitor) {
|
| 38 | int startIndex = this.mask.nextSetBit(0);
|
| 39 |
|
| 40 | while (startIndex != -1) {
|
| 41 | int endIndex = this.mask.nextClearBit(startIndex) - 1;
|
| 42 | this.visitSegment(visitor, startIndex, endIndex);
|
| 43 | startIndex = this.mask.nextSetBit(endIndex + 1);
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | private void visitSegment(final CarvingMask.Visitor visitor, final int startIndex, final int endIndex) {
|
| 48 | int startColumn = startIndex / this.height;
|
| 49 | int endColumn = endIndex / this.height;
|
| 50 |
|
| 51 | for (int column = startColumn; column <= endColumn; column++) {
|
| 52 | int columnX = column >> 4 & 15;
|
| 53 | int columnZ = column & 15;
|
| 54 | int columnBaseIndex = column * this.height;
|
| 55 | int bottomY = Math.max(startIndex - columnBaseIndex, 0) + this.minY;
|
| 56 | int topY = Math.min(endIndex - columnBaseIndex, this.height - 1) + this.minY;
|
| 57 | visitor.visitColumn(columnX, columnZ, bottomY, topY);
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | public boolean isEmpty() {
|
| 62 | return this.mask.isEmpty();
|
| 63 | }
|
| 64 |
|
| 65 | @FunctionalInterface
|
| 66 | public interface Filter {
|
| 67 | boolean test(int x, int y, int z);
|
| 68 | }
|
| 69 |
|
| 70 | @FunctionalInterface
|
| 71 | public interface Visitor {
|
| 72 | void visitColumn(int x, int z, int bottomY, int topY);
|
| 73 | }
|
| 74 | }
|
| 75 |
|