| 1 | package net.minecraft.world.level.levelgen.densityfunction;
|
| 2 |
|
| 3 | import net.minecraft.util.Mth;
|
| 4 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 5 |
|
| 6 | public record DensityVolume(int sizeX, int sizeY, int sizeZ, int minBlockX, int minBlockY, int minBlockZ, int stepBlockX, int stepBlockY, int stepBlockZ) {
|
| 7 | public static final int NO_BLOCK = -1;
|
| 8 |
|
| 9 | public DensityVolume(int sizeX, int sizeY, int sizeZ, int minBlockX, int minBlockY, int minBlockZ, int stepBlockX, int stepBlockY, int stepBlockZ) {
|
| 10 | if (sizeX <= 0 || sizeY <= 0 || sizeZ <= 0) {
|
| 11 | throw new IllegalArgumentException("Size must be positive, was: " + sizeX + "x" + sizeY + "x" + sizeZ);
|
| 12 | }
|
| 13 |
|
| 14 | if (stepBlockX > 0 && stepBlockY > 0 && stepBlockZ > 0) {
|
| 15 | this.sizeX = sizeX;
|
| 16 | this.sizeY = sizeY;
|
| 17 | this.sizeZ = sizeZ;
|
| 18 | this.minBlockX = minBlockX;
|
| 19 | this.minBlockY = minBlockY;
|
| 20 | this.minBlockZ = minBlockZ;
|
| 21 | this.stepBlockX = stepBlockX;
|
| 22 | this.stepBlockY = stepBlockY;
|
| 23 | this.stepBlockZ = stepBlockZ;
|
| 24 | } else {
|
| 25 | throw new IllegalArgumentException("Step must be positive, was: " + stepBlockX + "; " + stepBlockY + "; " + stepBlockZ);
|
| 26 | }
|
| 27 | }
|
| 28 |
|
| 29 | public DensityVolume(final int sizeX, final int sizeY, final int sizeZ, final int minBlockX, final int minBlockY, final int minBlockZ) {
|
| 30 | this(sizeX, sizeY, sizeZ, minBlockX, minBlockY, minBlockZ, 1, 1, 1);
|
| 31 | }
|
| 32 |
|
| 33 | public int indexUnchecked(final int indexX, final int indexY, final int indexZ) {
|
| 34 | return indexY + (indexX + indexZ * this.sizeX) * this.sizeY;
|
| 35 | }
|
| 36 |
|
| 37 | public int blockX(final int x) {
|
| 38 | return this.minBlockX + x * this.stepBlockX;
|
| 39 | }
|
| 40 |
|
| 41 | public int blockY(final int y) {
|
| 42 | return this.minBlockY + y * this.stepBlockY;
|
| 43 | }
|
| 44 |
|
| 45 | public int blockZ(final int z) {
|
| 46 | return this.minBlockZ + z * this.stepBlockZ;
|
| 47 | }
|
| 48 |
|
| 49 | public int maxBlockX() {
|
| 50 | return this.minBlockX + this.sizeX * this.stepBlockX - 1;
|
| 51 | }
|
| 52 |
|
| 53 | public int maxBlockY() {
|
| 54 | return this.minBlockY + this.sizeY * this.stepBlockY - 1;
|
| 55 | }
|
| 56 |
|
| 57 | public int maxBlockZ() {
|
| 58 | return this.minBlockZ + this.sizeZ * this.stepBlockZ - 1;
|
| 59 | }
|
| 60 |
|
| 61 | public int size() {
|
| 62 | return this.sizeX * this.sizeY * this.sizeZ;
|
| 63 | }
|
| 64 |
|
| 65 | public boolean intersects(final BoundingBox box) {
|
| 66 | return box.intersects(this.minBlockX, this.minBlockY, this.minBlockZ, this.maxBlockX(), this.maxBlockY(), this.maxBlockZ());
|
| 67 | }
|
| 68 |
|
| 69 | private boolean containsBlockRelative(final int relativeX, final int relativeY, final int relativeZ) {
|
| 70 | return relativeX >= 0
|
| 71 | && relativeY >= 0
|
| 72 | && relativeZ >= 0
|
| 73 | && relativeX < this.sizeX * this.stepBlockX
|
| 74 | && relativeY < this.sizeY * this.stepBlockY
|
| 75 | && relativeZ < this.sizeZ * this.stepBlockZ
|
| 76 | && Math.floorMod(relativeX, this.stepBlockX) == 0
|
| 77 | && Math.floorMod(relativeY, this.stepBlockY) == 0
|
| 78 | && Math.floorMod(relativeZ, this.stepBlockZ) == 0;
|
| 79 | }
|
| 80 |
|
| 81 | public int indexOfBlock(final int blockX, final int blockY, final int blockZ) {
|
| 82 | int relativeX = blockX - this.minBlockX;
|
| 83 | int relativeY = blockY - this.minBlockY;
|
| 84 | int relativeZ = blockZ - this.minBlockZ;
|
| 85 | if (this.stepBlockX == 1 && this.stepBlockY == 1 && this.stepBlockZ == 1) {
|
| 86 | if (relativeX >= 0 && relativeY >= 0 && relativeZ >= 0 && relativeX < this.sizeX && relativeY < this.sizeY && relativeZ < this.sizeZ) {
|
| 87 | return this.indexUnchecked(relativeX, relativeY, relativeZ);
|
| 88 | }
|
| 89 | } else if (this.containsBlockRelative(relativeX, relativeY, relativeZ)) {
|
| 90 | return this.indexUnchecked(Mth.floorDiv(relativeX, this.stepBlockX), Mth.floorDiv(relativeY, this.stepBlockY), Mth.floorDiv(relativeZ, this.stepBlockZ));
|
| 91 | }
|
| 92 |
|
| 93 | return -1;
|
| 94 | }
|
| 95 | }
|
| 96 |
|