| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.SharedConstants;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.core.HolderSet;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 13 | import net.minecraft.tags.BlockTags;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import net.minecraft.util.RandomSource;
|
| 16 | import net.minecraft.util.valueproviders.FloatProvider;
|
| 17 | import net.minecraft.util.valueproviders.FloatProviders;
|
| 18 | import net.minecraft.util.valueproviders.IntProvider;
|
| 19 | import net.minecraft.util.valueproviders.IntProviders;
|
| 20 | import net.minecraft.world.level.WorldGenLevel;
|
| 21 | import net.minecraft.world.level.block.Block;
|
| 22 | import net.minecraft.world.level.block.Blocks;
|
| 23 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 24 | import net.minecraft.world.level.levelgen.Column;
|
| 25 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public record LargeDripstoneFeature(
|
| 30 | HolderSet<Block> replaceableBlocks,
|
| 31 | int floorToCeilingSearchRange,
|
| 32 | IntProvider columnRadius,
|
| 33 | FloatProvider heightScale,
|
| 34 | float maxColumnRadiusToCaveHeightRatio,
|
| 35 | FloatProvider stalactiteBluntness,
|
| 36 | FloatProvider stalagmiteBluntness,
|
| 37 | FloatProvider windSpeed,
|
| 38 | int minRadiusForWind,
|
| 39 | float minBluntnessForWind
|
| 40 | ) implements Feature {
|
| 41 | public static final MapCodec<LargeDripstoneFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 42 | i -> i.group(
|
| 43 | RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("replaceable_blocks").forGetter(LargeDripstoneFeature::replaceableBlocks),
|
| 44 | Codec.intRange(1, 512).optionalFieldOf("floor_to_ceiling_search_range", 30).forGetter(LargeDripstoneFeature::floorToCeilingSearchRange),
|
| 45 | IntProviders.codec(1, 16).fieldOf("column_radius").forGetter(LargeDripstoneFeature::columnRadius),
|
| 46 | FloatProviders.codec(0.0F, 20.0F).fieldOf("height_scale").forGetter(LargeDripstoneFeature::heightScale),
|
| 47 | Codec.floatRange(0.1F, 1.0F).fieldOf("max_column_radius_to_cave_height_ratio").forGetter(LargeDripstoneFeature::maxColumnRadiusToCaveHeightRatio),
|
| 48 | FloatProviders.codec(0.1F, 10.0F).fieldOf("stalactite_bluntness").forGetter(LargeDripstoneFeature::stalactiteBluntness),
|
| 49 | FloatProviders.codec(0.1F, 10.0F).fieldOf("stalagmite_bluntness").forGetter(LargeDripstoneFeature::stalagmiteBluntness),
|
| 50 | FloatProviders.codec(0.0F, 2.0F).fieldOf("wind_speed").forGetter(LargeDripstoneFeature::windSpeed),
|
| 51 | Codec.intRange(0, 100).fieldOf("min_radius_for_wind").forGetter(LargeDripstoneFeature::minRadiusForWind),
|
| 52 | Codec.floatRange(0.0F, 5.0F).fieldOf("min_bluntness_for_wind").forGetter(LargeDripstoneFeature::minBluntnessForWind)
|
| 53 | )
|
| 54 | .apply(i, LargeDripstoneFeature::new)
|
| 55 | );
|
| 56 |
|
| 57 | @Override
|
| 58 | public MapCodec<LargeDripstoneFeature> codec() {
|
| 59 | return CODEC;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 64 | if (!SpeleothemUtils.isEmptyOrWater(level, origin)) {
|
| 65 | return false;
|
| 66 | }
|
| 67 |
|
| 68 | Optional<Column> column = Column.scan(
|
| 69 | level,
|
| 70 | origin,
|
| 71 | this.floorToCeilingSearchRange,
|
| 72 | SpeleothemUtils::isEmptyOrWater,
|
| 73 | state -> SpeleothemUtils.isBaseOrLava(state, Blocks.DRIPSTONE_BLOCK, this.replaceableBlocks)
|
| 74 | );
|
| 75 | if (!(!column.isEmpty() && column.get() instanceof Column.Range columnRange)) {
|
| 76 | return false;
|
| 77 | }
|
| 78 |
|
| 79 | if (columnRange.height() < 4) {
|
| 80 | return false;
|
| 81 | }
|
| 82 |
|
| 83 | int maxColumnRadiusBasedOnColumnHeight = (int)(columnRange.height() * this.maxColumnRadiusToCaveHeightRatio);
|
| 84 | int maxColumnRadius = Mth.clamp(maxColumnRadiusBasedOnColumnHeight, this.columnRadius.minInclusive(), this.columnRadius.maxInclusive());
|
| 85 | int radius = Mth.randomBetweenInclusive(random, this.columnRadius.minInclusive(), maxColumnRadius);
|
| 86 | LargeDripstoneFeature.LargeDripstone stalactite = makeDripstone(
|
| 87 | origin.atY(columnRange.ceiling() - 1), false, random, radius, this.stalactiteBluntness, this.heightScale
|
| 88 | );
|
| 89 | LargeDripstoneFeature.LargeDripstone stalagmite = makeDripstone(
|
| 90 | origin.atY(columnRange.floor() + 1), true, random, radius, this.stalagmiteBluntness, this.heightScale
|
| 91 | );
|
| 92 | LargeDripstoneFeature.WindOffsetter wind;
|
| 93 | if (stalactite.isSuitableForWind(this.minRadiusForWind, this.minBluntnessForWind)
|
| 94 | && stalagmite.isSuitableForWind(this.minRadiusForWind, this.minBluntnessForWind)) {
|
| 95 | wind = new LargeDripstoneFeature.WindOffsetter(origin.getY(), random, this.windSpeed, 16 - radius);
|
| 96 | } else {
|
| 97 | wind = LargeDripstoneFeature.WindOffsetter.noWind();
|
| 98 | }
|
| 99 |
|
| 100 | boolean stalactiteBaseEmbeddedInStone = stalactite.moveBackUntilBaseIsInsideStoneAndShrinkRadiusIfNecessary(level, wind);
|
| 101 | boolean stalagmiteBaseEmbeddedInStone = stalagmite.moveBackUntilBaseIsInsideStoneAndShrinkRadiusIfNecessary(level, wind);
|
| 102 | if (stalactiteBaseEmbeddedInStone) {
|
| 103 | stalactite.placeBlocks(level, random, wind);
|
| 104 | }
|
| 105 |
|
| 106 | if (stalagmiteBaseEmbeddedInStone) {
|
| 107 | stalagmite.placeBlocks(level, random, wind);
|
| 108 | }
|
| 109 |
|
| 110 | if (SharedConstants.DEBUG_LARGE_DRIPSTONE) {
|
| 111 | this.placeDebugMarkers(level, origin, columnRange, wind);
|
| 112 | }
|
| 113 |
|
| 114 | return true;
|
| 115 | }
|
| 116 |
|
| 117 | private static LargeDripstoneFeature.LargeDripstone makeDripstone(
|
| 118 | final BlockPos root, final boolean pointingUp, final RandomSource random, final int radius, final FloatProvider bluntness, final FloatProvider heightScale
|
| 119 | ) {
|
| 120 | return new LargeDripstoneFeature.LargeDripstone(root, pointingUp, radius, bluntness.sample(random), heightScale.sample(random));
|
| 121 | }
|
| 122 |
|
| 123 | private void placeDebugMarkers(final WorldGenLevel level, final BlockPos origin, final Column.Range range, final LargeDripstoneFeature.WindOffsetter wind) {
|
| 124 | level.setBlock(wind.offset(origin.atY(range.ceiling() - 1)), Blocks.DIAMOND_BLOCK.defaultBlockState(), 2);
|
| 125 | level.setBlock(wind.offset(origin.atY(range.floor() + 1)), Blocks.GOLD_BLOCK.defaultBlockState(), 2);
|
| 126 |
|
| 127 | for (BlockPos.MutableBlockPos pos = origin.atY(range.floor() + 2).mutable(); pos.getY() < range.ceiling() - 1; pos.move(Direction.UP)) {
|
| 128 | BlockPos windAdjustedPos = wind.offset(pos);
|
| 129 | if (SpeleothemUtils.isEmptyOrWater(level, windAdjustedPos) || level.getBlockState(windAdjustedPos).is(Blocks.DRIPSTONE_BLOCK)) {
|
| 130 | level.setBlock(windAdjustedPos, Blocks.CREEPER_HEAD.defaultBlockState(), 2);
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | private static final class LargeDripstone {
|
| 136 | private BlockPos root;
|
| 137 | private final boolean pointingUp;
|
| 138 | private int radius;
|
| 139 | private final double bluntness;
|
| 140 | private final double scale;
|
| 141 |
|
| 142 | private LargeDripstone(final BlockPos root, final boolean pointingUp, final int radius, final double bluntness, final double scale) {
|
| 143 | this.root = root;
|
| 144 | this.pointingUp = pointingUp;
|
| 145 | this.radius = radius;
|
| 146 | this.bluntness = bluntness;
|
| 147 | this.scale = scale;
|
| 148 | }
|
| 149 |
|
| 150 | private int getHeight() {
|
| 151 | return this.getHeightAtRadius(0.0F);
|
| 152 | }
|
| 153 |
|
| 154 | private boolean moveBackUntilBaseIsInsideStoneAndShrinkRadiusIfNecessary(final WorldGenLevel level, final LargeDripstoneFeature.WindOffsetter wind) {
|
| 155 | while (this.radius > 1) {
|
| 156 | BlockPos.MutableBlockPos newRoot = this.root.mutable();
|
| 157 | int maxTries = Math.min(10, this.getHeight());
|
| 158 |
|
| 159 | for (int i = 0; i < maxTries; i++) {
|
| 160 | if (level.getBlockState(newRoot).is(Blocks.LAVA)) {
|
| 161 | return false;
|
| 162 | }
|
| 163 |
|
| 164 | if (SpeleothemUtils.isCircleMostlyEmbeddedInStone(level, wind.offset(newRoot), this.radius)) {
|
| 165 | this.root = newRoot;
|
| 166 | return true;
|
| 167 | }
|
| 168 |
|
| 169 | newRoot.move(this.pointingUp ? Direction.DOWN : Direction.UP);
|
| 170 | }
|
| 171 |
|
| 172 | this.radius /= 2;
|
| 173 | }
|
| 174 |
|
| 175 | return false;
|
| 176 | }
|
| 177 |
|
| 178 | private int getHeightAtRadius(final float checkRadius) {
|
| 179 | return (int)SpeleothemUtils.getSpeleothemHeight(checkRadius, this.radius, this.scale, this.bluntness);
|
| 180 | }
|
| 181 |
|
| 182 | private void placeBlocks(final WorldGenLevel level, final RandomSource random, final LargeDripstoneFeature.WindOffsetter wind) {
|
| 183 | for (int dx = -this.radius; dx <= this.radius; dx++) {
|
| 184 | for (int dz = -this.radius; dz <= this.radius; dz++) {
|
| 185 | float currentRadius = Mth.sqrt(dx * dx + dz * dz);
|
| 186 | if (!(currentRadius > this.radius)) {
|
| 187 | int height = this.getHeightAtRadius(currentRadius);
|
| 188 | if (height > 0) {
|
| 189 | if (random.nextFloat() < 0.2) {
|
| 190 | height = (int)(height * Mth.randomBetween(random, 0.8F, 1.0F));
|
| 191 | }
|
| 192 |
|
| 193 | BlockPos.MutableBlockPos pos = this.root.offset(dx, 0, dz).mutable();
|
| 194 | boolean hasBeenOutOfStone = false;
|
| 195 | int maxY = this.pointingUp ? level.getHeight(Heightmap.Types.WORLD_SURFACE_WG, pos.getX(), pos.getZ()) : Integer.MAX_VALUE;
|
| 196 |
|
| 197 | for (int i = 0; i < height && pos.getY() < maxY; i++) {
|
| 198 | BlockPos windAdjustedPos = wind.offset(pos);
|
| 199 | if (SpeleothemUtils.isEmptyOrWaterOrLava(level, windAdjustedPos)) {
|
| 200 | hasBeenOutOfStone = true;
|
| 201 | Block block = SharedConstants.DEBUG_LARGE_DRIPSTONE ? Blocks.GLASS : Blocks.DRIPSTONE_BLOCK;
|
| 202 | level.setBlock(windAdjustedPos, block.defaultBlockState(), 2);
|
| 203 | } else if (hasBeenOutOfStone && level.getBlockState(windAdjustedPos).is(BlockTags.BASE_STONE_OVERWORLD)) {
|
| 204 | break;
|
| 205 | }
|
| 206 |
|
| 207 | pos.move(this.pointingUp ? Direction.UP : Direction.DOWN);
|
| 208 | }
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | private boolean isSuitableForWind(final int minRadiusForWind, final float minBluntnessForWind) {
|
| 216 | return this.radius >= minRadiusForWind && this.bluntness >= minBluntnessForWind;
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | private static final class WindOffsetter {
|
| 221 | private final int originY;
|
| 222 | private final @Nullable Vec3 windSpeed;
|
| 223 | private final int maxOffset;
|
| 224 |
|
| 225 | private WindOffsetter(final int originY, final RandomSource random, final FloatProvider windSpeedRange, final int maxOffset) {
|
| 226 | this.originY = originY;
|
| 227 | this.maxOffset = maxOffset;
|
| 228 | float speed = windSpeedRange.sample(random);
|
| 229 | float direction = Mth.randomBetween(random, 0.0F, (float) Math.PI);
|
| 230 | this.windSpeed = new Vec3(Mth.cos(direction) * speed, 0.0, Mth.sin(direction) * speed);
|
| 231 | }
|
| 232 |
|
| 233 | private WindOffsetter() {
|
| 234 | this.originY = 0;
|
| 235 | this.windSpeed = null;
|
| 236 | this.maxOffset = 0;
|
| 237 | }
|
| 238 |
|
| 239 | private static LargeDripstoneFeature.WindOffsetter noWind() {
|
| 240 | return new LargeDripstoneFeature.WindOffsetter();
|
| 241 | }
|
| 242 |
|
| 243 | private BlockPos offset(final BlockPos pos) {
|
| 244 | if (this.windSpeed == null) {
|
| 245 | return pos;
|
| 246 | }
|
| 247 |
|
| 248 | int dy = this.originY - pos.getY();
|
| 249 | Vec3 totalWindAdjust = this.windSpeed.scale(dy);
|
| 250 | int dx = Mth.clamp(Mth.floor(totalWindAdjust.x), -this.maxOffset, this.maxOffset);
|
| 251 | int dz = Mth.clamp(Mth.floor(totalWindAdjust.z), -this.maxOffset, this.maxOffset);
|
| 252 | return pos.offset(dx, 0, dz);
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 |
|