| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.level.BlockGetter;
|
| 9 | import net.minecraft.world.level.LevelAccessor;
|
| 10 | import net.minecraft.world.level.WorldGenLevel;
|
| 11 | import net.minecraft.world.level.block.Blocks;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 14 |
|
| 15 | public record IcebergFeature(BlockState state) implements Feature {
|
| 16 | public static final MapCodec<IcebergFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(BlockState.CODEC.fieldOf("state").forGetter(IcebergFeature::state)).apply(i, IcebergFeature::new)
|
| 18 | );
|
| 19 |
|
| 20 | @Override
|
| 21 | public MapCodec<IcebergFeature> codec() {
|
| 22 | return CODEC;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, BlockPos origin) {
|
| 27 | origin = new BlockPos(origin.getX(), chunkGenerator.getSeaLevel(), origin.getZ());
|
| 28 | boolean snowOnTop = random.nextDouble() > 0.7;
|
| 29 | BlockState mainBlockState = this.state;
|
| 30 | double shapeAngle = random.nextDouble() * 2.0 * Math.PI;
|
| 31 | int shapeEllipseA = 11 - random.nextInt(5);
|
| 32 | int shapeEllipseC = 3 + random.nextInt(3);
|
| 33 | boolean isEllipse = random.nextDouble() > 0.7;
|
| 34 | int maxWidthRoundIceberg = 11;
|
| 35 | int overWaterHeight = isEllipse ? random.nextInt(6) + 6 : random.nextInt(15) + 3;
|
| 36 | if (!isEllipse && random.nextDouble() > 0.9) {
|
| 37 | overWaterHeight += random.nextInt(19) + 7;
|
| 38 | }
|
| 39 |
|
| 40 | int underWaterHeight = Math.min(overWaterHeight + random.nextInt(11), 18);
|
| 41 | int width = Math.min(overWaterHeight + random.nextInt(7) - random.nextInt(5), 11);
|
| 42 | int a = isEllipse ? shapeEllipseA : 11;
|
| 43 |
|
| 44 | for (int xo = -a; xo < a; xo++) {
|
| 45 | for (int zo = -a; zo < a; zo++) {
|
| 46 | for (int yOff = 0; yOff < overWaterHeight; yOff++) {
|
| 47 | int radius = isEllipse
|
| 48 | ? this.heightDependentRadiusEllipse(yOff, overWaterHeight, width)
|
| 49 | : this.heightDependentRadiusRound(random, yOff, overWaterHeight, width);
|
| 50 | if (isEllipse || xo < radius) {
|
| 51 | this.generateIcebergBlock(
|
| 52 | level, random, origin, overWaterHeight, xo, yOff, zo, radius, a, isEllipse, shapeEllipseC, shapeAngle, snowOnTop, mainBlockState
|
| 53 | );
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | this.smooth(level, origin, width, overWaterHeight, isEllipse, shapeEllipseA);
|
| 60 |
|
| 61 | for (int xo = -a; xo < a; xo++) {
|
| 62 | for (int zo = -a; zo < a; zo++) {
|
| 63 | for (int yOff = -1; yOff > -underWaterHeight; yOff--) {
|
| 64 | int newA = isEllipse ? Mth.ceil(a * (1.0F - (float)Math.pow(yOff, 2.0) / (underWaterHeight * 8.0F))) : a;
|
| 65 | int radius = this.heightDependentRadiusSteep(random, -yOff, underWaterHeight, width);
|
| 66 | if (xo < radius) {
|
| 67 | this.generateIcebergBlock(
|
| 68 | level, random, origin, underWaterHeight, xo, yOff, zo, radius, newA, isEllipse, shapeEllipseC, shapeAngle, snowOnTop, mainBlockState
|
| 69 | );
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | boolean doCutOut = isEllipse ? random.nextDouble() > 0.1 : random.nextDouble() > 0.7;
|
| 76 | if (doCutOut) {
|
| 77 | this.generateCutOut(random, level, width, overWaterHeight, origin, isEllipse, shapeEllipseA, shapeAngle, shapeEllipseC);
|
| 78 | }
|
| 79 |
|
| 80 | return true;
|
| 81 | }
|
| 82 |
|
| 83 | private void generateCutOut(
|
| 84 | final RandomSource random,
|
| 85 | final LevelAccessor level,
|
| 86 | final int width,
|
| 87 | final int height,
|
| 88 | final BlockPos globalOrigin,
|
| 89 | final boolean isEllipse,
|
| 90 | final int shapeEllipseA,
|
| 91 | final double shapeAngle,
|
| 92 | final int shapeEllipseC
|
| 93 | ) {
|
| 94 | int randomSignX = random.nextBoolean() ? -1 : 1;
|
| 95 | int randomSignZ = random.nextBoolean() ? -1 : 1;
|
| 96 | int xOff = random.nextInt(Math.max(width / 2 - 2, 1));
|
| 97 | if (random.nextBoolean()) {
|
| 98 | xOff = width / 2 + 1 - random.nextInt(Math.max(width - width / 2 - 1, 1));
|
| 99 | }
|
| 100 |
|
| 101 | int zOff = random.nextInt(Math.max(width / 2 - 2, 1));
|
| 102 | if (random.nextBoolean()) {
|
| 103 | zOff = width / 2 + 1 - random.nextInt(Math.max(width - width / 2 - 1, 1));
|
| 104 | }
|
| 105 |
|
| 106 | if (isEllipse) {
|
| 107 | xOff = zOff = random.nextInt(Math.max(shapeEllipseA - 5, 1));
|
| 108 | }
|
| 109 |
|
| 110 | BlockPos localOrigin = new BlockPos(randomSignX * xOff, 0, randomSignZ * zOff);
|
| 111 | double angle = isEllipse ? shapeAngle + (Math.PI / 2) : random.nextDouble() * 2.0 * Math.PI;
|
| 112 |
|
| 113 | for (int yOff = 0; yOff < height - 3; yOff++) {
|
| 114 | int radius = this.heightDependentRadiusRound(random, yOff, height, width);
|
| 115 | this.carve(radius, yOff, globalOrigin, level, false, angle, localOrigin, shapeEllipseA, shapeEllipseC);
|
| 116 | }
|
| 117 |
|
| 118 | for (int yOff = -1; yOff > -height + random.nextInt(5); yOff--) {
|
| 119 | int radius = this.heightDependentRadiusSteep(random, -yOff, height, width);
|
| 120 | this.carve(radius, yOff, globalOrigin, level, true, angle, localOrigin, shapeEllipseA, shapeEllipseC);
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private void carve(
|
| 125 | final int radius,
|
| 126 | final int yOff,
|
| 127 | final BlockPos globalOrigin,
|
| 128 | final LevelAccessor level,
|
| 129 | final boolean underWater,
|
| 130 | final double angle,
|
| 131 | final BlockPos localOrigin,
|
| 132 | final int shapeEllipseA,
|
| 133 | final int shapeEllipseC
|
| 134 | ) {
|
| 135 | int a = radius + 1 + shapeEllipseA / 3;
|
| 136 | int c = Math.min(radius - 3, 3) + shapeEllipseC / 2 - 1;
|
| 137 |
|
| 138 | for (int xo = -a; xo < a; xo++) {
|
| 139 | for (int zo = -a; zo < a; zo++) {
|
| 140 | double signedDist = this.signedDistanceEllipse(xo, zo, localOrigin, a, c, angle);
|
| 141 | if (signedDist < 0.0) {
|
| 142 | BlockPos pos = globalOrigin.offset(xo, yOff, zo);
|
| 143 | BlockState state = level.getBlockState(pos);
|
| 144 | if (isIcebergState(state) || state.is(Blocks.SNOW_BLOCK)) {
|
| 145 | if (underWater) {
|
| 146 | this.setBlock(level, pos, Blocks.WATER.defaultBlockState());
|
| 147 | } else {
|
| 148 | this.setBlock(level, pos, Blocks.AIR.defaultBlockState());
|
| 149 | this.removeFloatingSnowLayer(level, pos);
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | private void removeFloatingSnowLayer(final LevelAccessor level, final BlockPos pos) {
|
| 158 | if (level.getBlockState(pos.above()).is(Blocks.SNOW)) {
|
| 159 | this.setBlock(level, pos.above(), Blocks.AIR.defaultBlockState());
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | private void generateIcebergBlock(
|
| 164 | final LevelAccessor level,
|
| 165 | final RandomSource random,
|
| 166 | final BlockPos origin,
|
| 167 | final int height,
|
| 168 | final int xo,
|
| 169 | final int yOff,
|
| 170 | final int zo,
|
| 171 | final int radius,
|
| 172 | final int a,
|
| 173 | final boolean isEllipse,
|
| 174 | final int shapeEllipseC,
|
| 175 | final double shapeAngle,
|
| 176 | final boolean snowOnTop,
|
| 177 | final BlockState mainBlockState
|
| 178 | ) {
|
| 179 | double signedDist = isEllipse
|
| 180 | ? this.signedDistanceEllipse(xo, zo, BlockPos.ZERO, a, this.getEllipseC(yOff, height, shapeEllipseC), shapeAngle)
|
| 181 | : this.signedDistanceCircle(xo, zo, BlockPos.ZERO, radius, random);
|
| 182 | if (signedDist < 0.0) {
|
| 183 | BlockPos pos = origin.offset(xo, yOff, zo);
|
| 184 | double compareVal = isEllipse ? -0.5 : -6 - random.nextInt(3);
|
| 185 | if (signedDist > compareVal && random.nextDouble() > 0.9) {
|
| 186 | return;
|
| 187 | }
|
| 188 |
|
| 189 | this.setIcebergBlock(pos, level, random, height - yOff, height, isEllipse, snowOnTop, mainBlockState);
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | private void setIcebergBlock(
|
| 194 | final BlockPos pos,
|
| 195 | final LevelAccessor level,
|
| 196 | final RandomSource random,
|
| 197 | final int hDiff,
|
| 198 | final int height,
|
| 199 | final boolean isEllipse,
|
| 200 | final boolean snowOnTop,
|
| 201 | final BlockState mainBlockState
|
| 202 | ) {
|
| 203 | BlockState state = level.getBlockState(pos);
|
| 204 | if (state.isAir() || state.is(Blocks.SNOW_BLOCK) || state.is(Blocks.ICE) || state.is(Blocks.WATER)) {
|
| 205 | boolean randomness = !isEllipse || random.nextDouble() > 0.05;
|
| 206 | int divisor = isEllipse ? 3 : 2;
|
| 207 | if (snowOnTop && !state.is(Blocks.WATER) && hDiff <= random.nextInt(Math.max(1, height / divisor)) + height * 0.6 && randomness) {
|
| 208 | this.setBlock(level, pos, Blocks.SNOW_BLOCK.defaultBlockState());
|
| 209 | } else {
|
| 210 | this.setBlock(level, pos, mainBlockState);
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | private int getEllipseC(final int yOff, final int height, final int shapeEllipseC) {
|
| 216 | int c = shapeEllipseC;
|
| 217 | if (yOff > 0 && height - yOff <= 3) {
|
| 218 | c -= 4 - (height - yOff);
|
| 219 | }
|
| 220 |
|
| 221 | return c;
|
| 222 | }
|
| 223 |
|
| 224 | private double signedDistanceCircle(final int xo, final int zo, final BlockPos origin, final int radius, final RandomSource random) {
|
| 225 | float off = 10.0F * Mth.clamp(random.nextFloat(), 0.2F, 0.8F) / radius;
|
| 226 | return off + Math.pow(xo - origin.getX(), 2.0) + Math.pow(zo - origin.getZ(), 2.0) - Math.pow(radius, 2.0);
|
| 227 | }
|
| 228 |
|
| 229 | private double signedDistanceEllipse(final int xo, final int zo, final BlockPos origin, final int a, final int c, final double angle) {
|
| 230 | return Math.pow(((xo - origin.getX()) * Math.cos(angle) - (zo - origin.getZ()) * Math.sin(angle)) / a, 2.0)
|
| 231 | + Math.pow(((xo - origin.getX()) * Math.sin(angle) + (zo - origin.getZ()) * Math.cos(angle)) / c, 2.0)
|
| 232 | - 1.0;
|
| 233 | }
|
| 234 |
|
| 235 | private int heightDependentRadiusRound(final RandomSource random, final int yOff, final int height, final int width) {
|
| 236 | float k = 3.5F - random.nextFloat();
|
| 237 | float scale = (1.0F - (float)Math.pow(yOff, 2.0) / (height * k)) * width;
|
| 238 | if (height > 15 + random.nextInt(5)) {
|
| 239 | int tempYOff = yOff < 3 + random.nextInt(6) ? yOff / 2 : yOff;
|
| 240 | scale = (1.0F - tempYOff / (height * k * 0.4F)) * width;
|
| 241 | }
|
| 242 |
|
| 243 | return Mth.ceil(scale / 2.0F);
|
| 244 | }
|
| 245 |
|
| 246 | private int heightDependentRadiusEllipse(final int yOff, final int height, final int width) {
|
| 247 | float k = 1.0F;
|
| 248 | float scale = (1.0F - (float)Math.pow(yOff, 2.0) / (height * 1.0F)) * width;
|
| 249 | return Mth.ceil(scale / 2.0F);
|
| 250 | }
|
| 251 |
|
| 252 | private int heightDependentRadiusSteep(final RandomSource random, final int yOff, final int height, final int width) {
|
| 253 | float k = 1.0F + random.nextFloat() / 2.0F;
|
| 254 | float scale = (1.0F - yOff / (height * k)) * width;
|
| 255 | return Mth.ceil(scale / 2.0F);
|
| 256 | }
|
| 257 |
|
| 258 | private static boolean isIcebergState(final BlockState state) {
|
| 259 | return state.is(Blocks.PACKED_ICE) || state.is(Blocks.SNOW_BLOCK) || state.is(Blocks.BLUE_ICE);
|
| 260 | }
|
| 261 |
|
| 262 | private boolean belowIsAir(final BlockGetter level, final BlockPos pos) {
|
| 263 | return level.getBlockState(pos.below()).isAir();
|
| 264 | }
|
| 265 |
|
| 266 | private void smooth(final LevelAccessor level, final BlockPos origin, final int width, final int height, final boolean isEllipse, final int shapeEllipseA) {
|
| 267 | int a = isEllipse ? shapeEllipseA : width / 2;
|
| 268 |
|
| 269 | for (int x = -a; x <= a; x++) {
|
| 270 | for (int z = -a; z <= a; z++) {
|
| 271 | for (int yOff = 0; yOff <= height; yOff++) {
|
| 272 | BlockPos pos = origin.offset(x, yOff, z);
|
| 273 | BlockState state = level.getBlockState(pos);
|
| 274 | if (isIcebergState(state) || state.is(Blocks.SNOW)) {
|
| 275 | if (this.belowIsAir(level, pos)) {
|
| 276 | this.setBlock(level, pos, Blocks.AIR.defaultBlockState());
|
| 277 | this.setBlock(level, pos.above(), Blocks.AIR.defaultBlockState());
|
| 278 | } else if (isIcebergState(state)) {
|
| 279 | BlockState[] sides = new BlockState[]{
|
| 280 | level.getBlockState(pos.west()), level.getBlockState(pos.east()), level.getBlockState(pos.north()), level.getBlockState(pos.south())
|
| 281 | };
|
| 282 | int counter = 0;
|
| 283 |
|
| 284 | for (BlockState side : sides) {
|
| 285 | if (!isIcebergState(side)) {
|
| 286 | counter++;
|
| 287 | }
|
| 288 | }
|
| 289 |
|
| 290 | if (counter >= 3) {
|
| 291 | this.setBlock(level, pos, Blocks.AIR.defaultBlockState());
|
| 292 | }
|
| 293 | }
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 | }
|
| 298 | }
|
| 299 | }
|
| 300 |
|