| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.util.Interval;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.Util;
|
| 10 | import net.minecraft.util.context.ContextKey;
|
| 11 | import net.minecraft.world.level.ChunkPos;
|
| 12 | import net.minecraft.world.level.StructureManager;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 15 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 16 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 17 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 18 | import net.minecraft.world.level.levelgen.structure.PoolElementStructurePiece;
|
| 19 | import net.minecraft.world.level.levelgen.structure.StructurePiece;
|
| 20 | import net.minecraft.world.level.levelgen.structure.StructureStart;
|
| 21 | import net.minecraft.world.level.levelgen.structure.TerrainAdjustment;
|
| 22 | import net.minecraft.world.level.levelgen.structure.pools.JigsawJunction;
|
| 23 | import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public class Beardifier implements DensitySampler {
|
| 27 | public static final Interval RANGE = Interval.INFINITE;
|
| 28 | public static final ContextKey<Beardifier> CONTEXT_KEY = ContextKey.vanilla("beardifier");
|
| 29 | public static final int BEARD_KERNEL_RADIUS = 12;
|
| 30 | private static final int BEARD_KERNEL_SIZE = 24;
|
| 31 | private static final float[] BEARD_KERNEL = Util.make(new float[13824], kernel -> {
|
| 32 | for (int zi = 0; zi < 24; zi++) {
|
| 33 | for (int xi = 0; xi < 24; xi++) {
|
| 34 | for (int yi = 0; yi < 24; yi++) {
|
| 35 | kernel[zi * 24 * 24 + xi * 24 + yi] = (float)computeBeardContribution(xi - 12, yi - 12, zi - 12);
|
| 36 | }
|
| 37 | }
|
| 38 | }
|
| 39 | });
|
| 40 | private static final float MAX_BURY_DISTANCE = 6.0F;
|
| 41 | public static final Beardifier EMPTY = new Beardifier(List.of(), List.of(), null);
|
| 42 | private final List<Beardifier.Rigid> pieces;
|
| 43 | private final List<JigsawJunction> junctions;
|
| 44 | private final @Nullable BoundingBox affectedBox;
|
| 45 |
|
| 46 | public static Beardifier forStructuresInChunk(final StructureManager structureManager, final ChunkPos chunkPos) {
|
| 47 | List<StructureStart> structureStarts = structureManager.startsForStructure(chunkPos.x(), chunkPos.z(), s -> s.terrainAdaptation() != TerrainAdjustment.NONE);
|
| 48 | if (structureStarts.isEmpty()) {
|
| 49 | return EMPTY;
|
| 50 | }
|
| 51 |
|
| 52 | int chunkStartBlockX = chunkPos.getMinBlockX();
|
| 53 | int chunkStartBlockZ = chunkPos.getMinBlockZ();
|
| 54 | List<Beardifier.Rigid> rigids = new ArrayList<>();
|
| 55 | List<JigsawJunction> junctions = new ArrayList<>();
|
| 56 | BoundingBox anyPieceBoundingBox = null;
|
| 57 |
|
| 58 | for (StructureStart start : structureStarts) {
|
| 59 | TerrainAdjustment terrainAdjustment = start.getStructure().terrainAdaptation();
|
| 60 |
|
| 61 | for (StructurePiece piece : start.getPieces()) {
|
| 62 | if (piece.isCloseToChunk(chunkPos, 12)) {
|
| 63 | if (piece instanceof PoolElementStructurePiece poolPiece) {
|
| 64 | StructureTemplatePool.Projection projection = poolPiece.getElement().getProjection();
|
| 65 | if (projection == StructureTemplatePool.Projection.RIGID) {
|
| 66 | rigids.add(new Beardifier.Rigid(poolPiece.getBoundingBox(), terrainAdjustment, poolPiece.getGroundLevelDelta()));
|
| 67 | anyPieceBoundingBox = includeBoundingBox(anyPieceBoundingBox, piece.getBoundingBox());
|
| 68 | }
|
| 69 |
|
| 70 | for (JigsawJunction junction : poolPiece.getJunctions()) {
|
| 71 | int junctionX = junction.getSourceX();
|
| 72 | int junctionZ = junction.getSourceZ();
|
| 73 | if (junctionX > chunkStartBlockX - 12
|
| 74 | && junctionZ > chunkStartBlockZ - 12
|
| 75 | && junctionX < chunkStartBlockX + 15 + 12
|
| 76 | && junctionZ < chunkStartBlockZ + 15 + 12) {
|
| 77 | junctions.add(junction);
|
| 78 | BoundingBox junctionBox = new BoundingBox(new BlockPos(junctionX, junction.getSourceGroundY(), junctionZ));
|
| 79 | anyPieceBoundingBox = includeBoundingBox(anyPieceBoundingBox, junctionBox);
|
| 80 | }
|
| 81 | }
|
| 82 | } else {
|
| 83 | rigids.add(new Beardifier.Rigid(piece.getBoundingBox(), terrainAdjustment, 0));
|
| 84 | anyPieceBoundingBox = includeBoundingBox(anyPieceBoundingBox, piece.getBoundingBox());
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | if (anyPieceBoundingBox == null) {
|
| 91 | return EMPTY;
|
| 92 | }
|
| 93 |
|
| 94 | BoundingBox affectedBox = anyPieceBoundingBox.inflatedBy(24);
|
| 95 | return new Beardifier(List.copyOf(rigids), List.copyOf(junctions), affectedBox);
|
| 96 | }
|
| 97 |
|
| 98 | private static BoundingBox includeBoundingBox(final @Nullable BoundingBox encompassingBox, final BoundingBox newBox) {
|
| 99 | return encompassingBox == null ? newBox : BoundingBox.encapsulating(encompassingBox, newBox);
|
| 100 | }
|
| 101 |
|
| 102 | @VisibleForTesting
|
| 103 | public Beardifier(final List<Beardifier.Rigid> pieces, final List<JigsawJunction> junctions, final @Nullable BoundingBox affectedBox) {
|
| 104 | this.pieces = pieces;
|
| 105 | this.junctions = junctions;
|
| 106 | this.affectedBox = affectedBox;
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 111 | outputBuffer.fill(0.0F);
|
| 112 | if (this.affectedBox != null && volume.intersects(this.affectedBox)) {
|
| 113 | int minX = Math.floorDiv(Math.max(0, this.affectedBox.minX() - volume.minBlockX()), volume.stepBlockX());
|
| 114 | int minY = Math.floorDiv(Math.max(0, this.affectedBox.minY() - volume.minBlockY()), volume.stepBlockY());
|
| 115 | int minZ = Math.floorDiv(Math.max(0, this.affectedBox.minZ() - volume.minBlockZ()), volume.stepBlockZ());
|
| 116 | int maxX = Math.min(volume.sizeX() - 1, Math.floorDiv(this.affectedBox.maxX() - volume.minBlockX(), volume.stepBlockX()));
|
| 117 | int maxY = Math.min(volume.sizeY() - 1, Math.floorDiv(this.affectedBox.maxY() - volume.minBlockY(), volume.stepBlockY()));
|
| 118 | int maxZ = Math.min(volume.sizeZ() - 1, Math.floorDiv(this.affectedBox.maxZ() - volume.minBlockZ(), volume.stepBlockZ()));
|
| 119 |
|
| 120 | for (int z = minZ; z <= maxZ; z++) {
|
| 121 | int blockZ = volume.blockZ(z);
|
| 122 |
|
| 123 | for (int x = minX; x <= maxX; x++) {
|
| 124 | int blockX = volume.blockX(x);
|
| 125 |
|
| 126 | for (int y = minY; y <= maxY; y++) {
|
| 127 | int index = volume.indexUnchecked(x, y, z);
|
| 128 | int blockY = volume.blockY(y);
|
| 129 | outputBuffer.set(index, this.sampleValueUnchecked(blockX, blockY, blockZ));
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 138 | return this.affectedBox != null && this.affectedBox.isInside(blockX, blockY, blockZ) ? this.sampleValueUnchecked(blockX, blockY, blockZ) : 0.0F;
|
| 139 | }
|
| 140 |
|
| 141 | private float sampleValueUnchecked(final int blockX, final int blockY, final int blockZ) {
|
| 142 | float noiseValue = 0.0F;
|
| 143 |
|
| 144 | for (Beardifier.Rigid rigid : this.pieces) {
|
| 145 | BoundingBox box = rigid.box();
|
| 146 | int groundLevelDelta = rigid.groundLevelDelta();
|
| 147 | int dx = Math.max(0, Math.max(box.minX() - blockX, blockX - box.maxX()));
|
| 148 | int dz = Math.max(0, Math.max(box.minZ() - blockZ, blockZ - box.maxZ()));
|
| 149 | int groundY = box.minY() + groundLevelDelta;
|
| 150 | int dyToGround = blockY - groundY;
|
| 151 |
|
| 152 | int dy = switch (rigid.terrainAdjustment()) {
|
| 153 | case NONE -> 0;
|
| 154 | case BURY, BEARD_THIN -> dyToGround;
|
| 155 | case BEARD_BOX -> Math.max(0, Math.max(groundY - blockY, blockY - box.maxY()));
|
| 156 | case ENCAPSULATE -> Math.max(0, Math.max(box.minY() - blockY, blockY - box.maxY()));
|
| 157 | };
|
| 158 |
|
| 159 | noiseValue += switch (rigid.terrainAdjustment()) {
|
| 160 | case NONE -> 0.0F;
|
| 161 | case BURY -> getBuryContribution(dx, dy / 2.0F, dz);
|
| 162 | case BEARD_THIN, BEARD_BOX -> getBeardContribution(dx, dy, dz, dyToGround) * 0.8F;
|
| 163 | case ENCAPSULATE -> getBuryContribution(dx / 2.0F, dy / 2.0F, dz / 2.0F) * 0.8F;
|
| 164 | };
|
| 165 | }
|
| 166 |
|
| 167 | for (JigsawJunction junction : this.junctions) {
|
| 168 | int dx = blockX - junction.getSourceX();
|
| 169 | int dy = blockY - junction.getSourceGroundY();
|
| 170 | int dz = blockZ - junction.getSourceZ();
|
| 171 | noiseValue += getBeardContribution(dx, dy, dz, dy) * 0.4F;
|
| 172 | }
|
| 173 |
|
| 174 | return noiseValue;
|
| 175 | }
|
| 176 |
|
| 177 | private static float getBuryContribution(final float dx, final float dy, final float dz) {
|
| 178 | float distanceSq = Mth.lengthSquared(dx, dy, dz);
|
| 179 | return distanceSq >= 36.0F ? 0.0F : 1.0F - Mth.sqrt(distanceSq) / 6.0F;
|
| 180 | }
|
| 181 |
|
| 182 | private static float getBeardContribution(final int dx, final int dy, final int dz, final int yToGround) {
|
| 183 | int xi = dx + 12;
|
| 184 | int yi = dy + 12;
|
| 185 | int zi = dz + 12;
|
| 186 | if (isInKernelRange(xi) && isInKernelRange(yi) && isInKernelRange(zi)) {
|
| 187 | float dyWithOffset = yToGround + 0.5F;
|
| 188 | float distanceSqr = Mth.lengthSquared(dx, dyWithOffset, dz);
|
| 189 | float value = -dyWithOffset * (float)Mth.fastInvSqrt(distanceSqr / 2.0F) / 2.0F;
|
| 190 | return value * BEARD_KERNEL[zi * 24 * 24 + xi * 24 + yi];
|
| 191 | } else {
|
| 192 | return 0.0F;
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | private static boolean isInKernelRange(final int xi) {
|
| 197 | return xi >= 0 && xi < 24;
|
| 198 | }
|
| 199 |
|
| 200 | private static double computeBeardContribution(final int dx, final int dy, final int dz) {
|
| 201 | return computeBeardContribution(dx, dy + 0.5, dz);
|
| 202 | }
|
| 203 |
|
| 204 | private static double computeBeardContribution(final int dx, final double dy, final int dz) {
|
| 205 | double distanceSqr = Mth.lengthSquared(dx, dy, dz);
|
| 206 | return Math.pow(Math.E, -distanceSqr / 16.0);
|
| 207 | }
|
| 208 |
|
| 209 | @VisibleForTesting
|
| 210 | public record Rigid(BoundingBox box, TerrainAdjustment terrainAdjustment, int groundLevelDelta) {
|
| 211 | }
|
| 212 | }
|
| 213 |
|