| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import java.util.function.Consumer;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.HolderSet;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.level.LevelAccessor;
|
| 9 | import net.minecraft.world.level.WorldGenLevel;
|
| 10 | import net.minecraft.world.level.block.Block;
|
| 11 | import net.minecraft.world.level.block.Blocks;
|
| 12 | import net.minecraft.world.level.block.PointedDripstoneBlock;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.block.state.properties.SpeleothemThickness;
|
| 15 |
|
| 16 | public class SpeleothemUtils {
|
| 17 | protected static double getSpeleothemHeight(double xzDistanceFromCenter, final double speleothemRadius, final double scale, final double bluntness) {
|
| 18 | if (xzDistanceFromCenter < bluntness) {
|
| 19 | xzDistanceFromCenter = bluntness;
|
| 20 | }
|
| 21 |
|
| 22 | double cutoff = 0.384;
|
| 23 | double r = xzDistanceFromCenter / speleothemRadius * 0.384;
|
| 24 | double part1 = 0.75 * Math.pow(r, 1.3333333333333333);
|
| 25 | double part2 = Math.pow(r, 0.6666666666666666);
|
| 26 | double part3 = 0.3333333333333333 * Math.log(r);
|
| 27 | double heightRelativeToMaxRadius = scale * (part1 - part2 - part3);
|
| 28 | heightRelativeToMaxRadius = Math.max(heightRelativeToMaxRadius, 0.0);
|
| 29 | return heightRelativeToMaxRadius / 0.384 * speleothemRadius;
|
| 30 | }
|
| 31 |
|
| 32 | protected static boolean isCircleMostlyEmbeddedInStone(final WorldGenLevel level, final BlockPos center, final int xzRadius) {
|
| 33 | if (isEmptyOrWaterOrLava(level, center)) {
|
| 34 | return false;
|
| 35 | }
|
| 36 |
|
| 37 | float arcLength = 6.0F;
|
| 38 | float angleIncrement = 6.0F / xzRadius;
|
| 39 |
|
| 40 | for (float angle = 0.0F; angle < (float) (Math.PI * 2); angle += angleIncrement) {
|
| 41 | int dx = (int)(Mth.cos(angle) * xzRadius);
|
| 42 | int dz = (int)(Mth.sin(angle) * xzRadius);
|
| 43 | if (isEmptyOrWaterOrLava(level, center.offset(dx, 0, dz))) {
|
| 44 | return false;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | return true;
|
| 49 | }
|
| 50 |
|
| 51 | protected static boolean isEmptyOrWater(final LevelAccessor level, final BlockPos pos) {
|
| 52 | return level.isStateAtPosition(pos, SpeleothemUtils::isEmptyOrWater);
|
| 53 | }
|
| 54 |
|
| 55 | protected static boolean isEmptyOrWaterOrLava(final LevelAccessor level, final BlockPos pos) {
|
| 56 | return level.isStateAtPosition(pos, SpeleothemUtils::isEmptyOrWaterOrLava);
|
| 57 | }
|
| 58 |
|
| 59 | protected static void buildBaseToTipColumn(
|
| 60 | final Direction direction, final int totalLength, final boolean mergedTip, final Consumer<BlockState> consumer, final Block pointedBlock
|
| 61 | ) {
|
| 62 | if (totalLength >= 3) {
|
| 63 | consumer.accept(createPointedBlock(direction, SpeleothemThickness.BASE, pointedBlock));
|
| 64 |
|
| 65 | for (int i = 0; i < totalLength - 3; i++) {
|
| 66 | consumer.accept(createPointedBlock(direction, SpeleothemThickness.MIDDLE, pointedBlock));
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | if (totalLength >= 2) {
|
| 71 | consumer.accept(createPointedBlock(direction, SpeleothemThickness.FRUSTUM, pointedBlock));
|
| 72 | }
|
| 73 |
|
| 74 | if (totalLength >= 1) {
|
| 75 | consumer.accept(createPointedBlock(direction, mergedTip ? SpeleothemThickness.TIP_MERGE : SpeleothemThickness.TIP, pointedBlock));
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | protected static void growSpeleothem(
|
| 80 | final LevelAccessor level,
|
| 81 | final BlockPos startPos,
|
| 82 | final Direction tipDirection,
|
| 83 | final int height,
|
| 84 | final boolean mergedTip,
|
| 85 | final Block baseBlock,
|
| 86 | final Block pointedBlock,
|
| 87 | final HolderSet<Block> replaceableBlocks
|
| 88 | ) {
|
| 89 | if (isBase(level.getBlockState(startPos.relative(tipDirection.getOpposite())), baseBlock, replaceableBlocks)) {
|
| 90 | BlockPos.MutableBlockPos pos = startPos.mutable();
|
| 91 | buildBaseToTipColumn(tipDirection, height, mergedTip, state -> {
|
| 92 | if (state.is(pointedBlock)) {
|
| 93 | state = state.setValue(PointedDripstoneBlock.WATERLOGGED, level.isWaterAt(pos));
|
| 94 | }
|
| 95 |
|
| 96 | level.setBlock(pos, state, 2);
|
| 97 | pos.move(tipDirection);
|
| 98 | }, pointedBlock);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | protected static boolean placeBaseBlockIfPossible(
|
| 103 | final LevelAccessor level, final BlockPos pos, final Block baseBlock, final HolderSet<Block> replaceableBlocks
|
| 104 | ) {
|
| 105 | BlockState state = level.getBlockState(pos);
|
| 106 | if (state.is(replaceableBlocks)) {
|
| 107 | level.setBlock(pos, baseBlock.defaultBlockState(), 2);
|
| 108 | return true;
|
| 109 | } else {
|
| 110 | return false;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private static BlockState createPointedBlock(final Direction direction, final SpeleothemThickness thickness, final Block pointedBlock) {
|
| 115 | return pointedBlock.defaultBlockState().setValue(PointedDripstoneBlock.TIP_DIRECTION, direction).setValue(PointedDripstoneBlock.THICKNESS, thickness);
|
| 116 | }
|
| 117 |
|
| 118 | public static boolean isBaseOrLava(final BlockState state, final Block baseBlock, final HolderSet<Block> replaceableBlocks) {
|
| 119 | return isBase(state, baseBlock, replaceableBlocks) || state.is(Blocks.LAVA);
|
| 120 | }
|
| 121 |
|
| 122 | public static boolean isBase(final BlockState state, final Block baseBlock, final HolderSet<Block> replaceableBlocks) {
|
| 123 | return state.is(baseBlock) || state.is(replaceableBlocks);
|
| 124 | }
|
| 125 |
|
| 126 | public static boolean isEmptyOrWater(final BlockState state) {
|
| 127 | return state.isAir() || state.is(Blocks.WATER);
|
| 128 | }
|
| 129 |
|
| 130 | public static boolean isNeitherEmptyNorWater(final BlockState state) {
|
| 131 | return !state.isAir() && !state.is(Blocks.WATER);
|
| 132 | }
|
| 133 |
|
| 134 | public static boolean isEmptyOrWaterOrLava(final BlockState state) {
|
| 135 | return state.isAir() || state.is(Blocks.WATER) || state.is(Blocks.LAVA);
|
| 136 | }
|
| 137 | }
|
| 138 |
|