package net.minecraft.world.level.levelgen; import java.util.Arrays; import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Function; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; import net.minecraft.resources.Identifier; import net.minecraft.util.Mth; import net.minecraft.util.RandomSource; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.LevelHeightAccessor; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.biome.BiomeManager; import net.minecraft.world.level.biome.Biomes; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.chunk.BlockColumn; import net.minecraft.world.level.chunk.ChunkAccess; import net.minecraft.world.level.dimension.DimensionType; import net.minecraft.world.level.levelgen.carver.CarvingContext; import net.minecraft.world.level.levelgen.synth.NormalNoise; import org.jspecify.annotations.Nullable; public class SurfaceSystem { private static final BlockState WHITE_TERRACOTTA = Blocks.DYED_TERRACOTTA.white().defaultBlockState(); private static final BlockState ORANGE_TERRACOTTA = Blocks.DYED_TERRACOTTA.orange().defaultBlockState(); private static final BlockState TERRACOTTA = Blocks.TERRACOTTA.defaultBlockState(); private static final BlockState YELLOW_TERRACOTTA = Blocks.DYED_TERRACOTTA.yellow().defaultBlockState(); private static final BlockState BROWN_TERRACOTTA = Blocks.DYED_TERRACOTTA.brown().defaultBlockState(); private static final BlockState RED_TERRACOTTA = Blocks.DYED_TERRACOTTA.red().defaultBlockState(); private static final BlockState LIGHT_GRAY_TERRACOTTA = Blocks.DYED_TERRACOTTA.lightGray().defaultBlockState(); private static final BlockState PACKED_ICE = Blocks.PACKED_ICE.defaultBlockState(); private static final BlockState SNOW_BLOCK = Blocks.SNOW_BLOCK.defaultBlockState(); private final BlockState defaultBlock; private final int seaLevel; private final BlockState[] clayBands; private final NormalNoise clayBandsOffsetNoise; private final NormalNoise badlandsPillarNoise; private final NormalNoise badlandsPillarRoofNoise; private final NormalNoise badlandsSurfaceNoise; private final NormalNoise icebergPillarNoise; private final NormalNoise icebergPillarRoofNoise; private final NormalNoise icebergSurfaceNoise; private final PositionalRandomFactory noiseRandom; private final NormalNoise surfaceNoise; private final NormalNoise surfaceSecondaryNoise; public SurfaceSystem(final RandomState randomState, final BlockState defaultBlock, final int seaLevel, final PositionalRandomFactory noiseRandom) { this.defaultBlock = defaultBlock; this.seaLevel = seaLevel; this.noiseRandom = noiseRandom; this.clayBandsOffsetNoise = randomState.getOrCreateNoise(Noises.CLAY_BANDS_OFFSET); this.clayBands = generateBands(noiseRandom.fromHashOf(Identifier.withDefaultNamespace("clay_bands"))); this.surfaceNoise = randomState.getOrCreateNoise(Noises.SURFACE); this.surfaceSecondaryNoise = randomState.getOrCreateNoise(Noises.SURFACE_SECONDARY); this.badlandsPillarNoise = randomState.getOrCreateNoise(Noises.BADLANDS_PILLAR); this.badlandsPillarRoofNoise = randomState.getOrCreateNoise(Noises.BADLANDS_PILLAR_ROOF); this.badlandsSurfaceNoise = randomState.getOrCreateNoise(Noises.BADLANDS_SURFACE); this.icebergPillarNoise = randomState.getOrCreateNoise(Noises.ICEBERG_PILLAR); this.icebergPillarRoofNoise = randomState.getOrCreateNoise(Noises.ICEBERG_PILLAR_ROOF); this.icebergSurfaceNoise = randomState.getOrCreateNoise(Noises.ICEBERG_SURFACE); } public void buildSurface( final RandomState randomState, final BiomeManager biomeManager, final boolean useLegacyRandom, final WorldGenerationContext generationContext, final ChunkAccess protoChunk, final NoiseChunk noiseChunk, final SurfaceRules.RuleSource ruleSource, @Nullable final Set> possibleBiomes ) { final BlockPos.MutableBlockPos columnPos = new BlockPos.MutableBlockPos(); final ChunkPos chunkPos = protoChunk.getPos(); int minBlockX = chunkPos.getMinBlockX(); int minBlockZ = chunkPos.getMinBlockZ(); BlockColumn column = new BlockColumn() { { Objects.requireNonNull(SurfaceSystem.this); } @Override public BlockState getBlock(final int blockY) { return protoChunk.getBlockState(columnPos.setY(blockY)); } @Override public void setBlock(final int blockY, final BlockState state) { LevelHeightAccessor heightAccessor = protoChunk.getHeightAccessorForGeneration(); if (heightAccessor.isInsideBuildHeight(blockY)) { protoChunk.setBlockState(columnPos.setY(blockY), state); if (!state.getFluidState().isEmpty()) { protoChunk.markPosForPostProcessing(columnPos); } } } public String toString() { return "ChunkBlockColumn " + chunkPos; } }; SurfaceRules.Context context = new SurfaceRules.Context(this, randomState, protoChunk, noiseChunk, biomeManager::getBiome, generationContext, possibleBiomes); SurfaceRules.SurfaceRule rule = (SurfaceRules.SurfaceRule)ruleSource.apply(context); BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos(); for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { int blockX = minBlockX + x; int blockZ = minBlockZ + z; int startingHeight = protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, z) + 1; columnPos.setX(blockX).setZ(blockZ); Holder surfaceBiome = biomeManager.getBiome(blockPos.set(blockX, useLegacyRandom ? 0 : startingHeight, blockZ)); if (surfaceBiome.is(Biomes.ERODED_BADLANDS)) { this.erodedBadlandsExtension(column, blockX, blockZ, startingHeight, protoChunk); } int height = protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, z) + 1; context.updateXZ(blockX, blockZ); int stoneAboveDepth = 0; int waterHeight = Integer.MIN_VALUE; int nextCeilingStoneY = Integer.MAX_VALUE; int endY = protoChunk.getMinY(); for (int y = height; y >= endY; y--) { BlockState old = column.getBlock(y); if (old.isAir()) { stoneAboveDepth = 0; waterHeight = Integer.MIN_VALUE; } else if (!old.getFluidState().isEmpty()) { if (waterHeight == Integer.MIN_VALUE) { waterHeight = y + 1; } } else { if (nextCeilingStoneY >= y) { nextCeilingStoneY = DimensionType.WAY_BELOW_MIN_Y; for (int lookaheadY = y - 1; lookaheadY >= endY - 1; lookaheadY--) { BlockState nextState = column.getBlock(lookaheadY); if (!this.isStone(nextState)) { nextCeilingStoneY = lookaheadY + 1; break; } } } stoneAboveDepth++; int stoneBelowDepth = y - nextCeilingStoneY + 1; context.updateY(stoneAboveDepth, stoneBelowDepth, waterHeight, y); if (old == this.defaultBlock) { BlockState state = rule.tryApply(blockX, y, blockZ); if (state != null) { column.setBlock(y, state); } } } } if (surfaceBiome.is(Biomes.FROZEN_OCEAN) || surfaceBiome.is(Biomes.DEEP_FROZEN_OCEAN)) { this.frozenOceanExtension(context.getMinSurfaceLevel(), surfaceBiome.value(), column, blockPos, blockX, blockZ, startingHeight); } } } } protected int getSurfaceDepth(final int blockX, final int blockZ) { double noiseValue = this.surfaceNoise.getValue(blockX, 0.0, blockZ); return (int)(noiseValue * 2.75 + 3.0 + this.noiseRandom.at(blockX, 0, blockZ).nextDouble() * 0.25); } protected double getSurfaceSecondary(final int blockX, final int blockZ) { return this.surfaceSecondaryNoise.getValue(blockX, 0.0, blockZ); } private boolean isStone(final BlockState state) { return !state.isAir() && state.getFluidState().isEmpty(); } public int getSeaLevel() { return this.seaLevel; } @Deprecated public Optional topMaterial( final SurfaceRules.RuleSource ruleSource, final CarvingContext carvingContext, final Function> biomeGetter, final ChunkAccess chunk, final NoiseChunk noiseChunk, final BlockPos pos, final boolean underFluid ) { SurfaceRules.Context context = new SurfaceRules.Context(this, carvingContext.randomState(), chunk, noiseChunk, biomeGetter, carvingContext, null); SurfaceRules.SurfaceRule rule = (SurfaceRules.SurfaceRule)ruleSource.apply(context); int blockX = pos.getX(); int blockY = pos.getY(); int blockZ = pos.getZ(); context.updateXZ(blockX, blockZ); context.updateY(1, 1, underFluid ? blockY + 1 : Integer.MIN_VALUE, blockY); BlockState state = rule.tryApply(blockX, blockY, blockZ); return Optional.ofNullable(state); } private void erodedBadlandsExtension(final BlockColumn column, final int blockX, final int blockZ, final int height, final LevelHeightAccessor protoChunk) { double pillarNoiseScale = 0.2; double pillarBuffer = Math.min( Math.abs(this.badlandsSurfaceNoise.getValue(blockX, 0.0, blockZ) * 8.25), this.badlandsPillarNoise.getValue(blockX * 0.2, 0.0, blockZ * 0.2) * 15.0 ); if (!(pillarBuffer <= 0.0)) { double floorNoiseSampleResolution = 0.75; double floorAmplitude = 1.5; double pillarFloor = Math.abs(this.badlandsPillarRoofNoise.getValue(blockX * 0.75, 0.0, blockZ * 0.75) * 1.5); double extensionTop = 64.0 + Math.min(pillarBuffer * pillarBuffer * 2.5, Math.ceil(pillarFloor * 50.0) + 24.0); int startY = Mth.floor(extensionTop); if (height <= startY) { for (int y = startY; y >= protoChunk.getMinY(); y--) { BlockState oldState = column.getBlock(y); if (oldState.is(this.defaultBlock.getBlock())) { break; } if (oldState.is(Blocks.WATER)) { return; } } for (int y = startY; y >= protoChunk.getMinY() && column.getBlock(y).isAir(); y--) { column.setBlock(y, this.defaultBlock); } } } } private void frozenOceanExtension( final int minSurfaceLevel, final Biome surfaceBiome, final BlockColumn column, final BlockPos.MutableBlockPos blockPos, final int blockX, final int blockZ, final int height ) { double pillarScale = 1.28; double iceberg = Math.min( Math.abs(this.icebergSurfaceNoise.getValue(blockX, 0.0, blockZ) * 8.25), this.icebergPillarNoise.getValue(blockX * 1.28, 0.0, blockZ * 1.28) * 15.0 ); if (!(iceberg <= 1.8)) { double roofScale = 1.17; double roofAmplitude = 1.5; double icebergRoof = Math.abs(this.icebergPillarRoofNoise.getValue(blockX * 1.17, 0.0, blockZ * 1.17) * 1.5); double top = Math.min(iceberg * iceberg * 1.2, Math.ceil(icebergRoof * 40.0) + 14.0); if (surfaceBiome.shouldMeltFrozenOceanIcebergSlightly(blockPos.set(blockX, this.seaLevel, blockZ), this.seaLevel)) { top -= 2.0; } double extensionBottom; if (top > 2.0) { extensionBottom = this.seaLevel - top - 7.0; top += this.seaLevel; } else { top = 0.0; extensionBottom = 0.0; } double extensionTop = top; RandomSource random = this.noiseRandom.at(blockX, 0, blockZ); int maxSnowDepth = 2 + random.nextInt(4); int minSnowHeight = this.seaLevel + 18 + random.nextInt(10); int snowDepth = 0; for (int y = Math.max(height, (int)top + 1); y >= minSurfaceLevel; y--) { if (column.getBlock(y).isAir() && y < (int)extensionTop && random.nextDouble() > 0.01 || column.getBlock(y).is(Blocks.WATER) && y > (int)extensionBottom && y < this.seaLevel && extensionBottom != 0.0 && random.nextDouble() > 0.15) { if (snowDepth <= maxSnowDepth && y > minSnowHeight) { column.setBlock(y, SNOW_BLOCK); snowDepth++; } else { column.setBlock(y, PACKED_ICE); } } } } } private static BlockState[] generateBands(final RandomSource random) { BlockState[] clayBands = new BlockState[192]; Arrays.fill(clayBands, TERRACOTTA); for (int i = 0; i < clayBands.length; i++) { i += random.nextInt(5) + 1; if (i < clayBands.length) { clayBands[i] = ORANGE_TERRACOTTA; } } makeBands(random, clayBands, 1, YELLOW_TERRACOTTA); makeBands(random, clayBands, 2, BROWN_TERRACOTTA); makeBands(random, clayBands, 1, RED_TERRACOTTA); int whiteBandCount = random.nextIntBetweenInclusive(9, 15); int ix = 0; for (int start = 0; ix < whiteBandCount && start < clayBands.length; start += random.nextInt(16) + 4) { clayBands[start] = WHITE_TERRACOTTA; if (start - 1 > 0 && random.nextBoolean()) { clayBands[start - 1] = LIGHT_GRAY_TERRACOTTA; } if (start + 1 < clayBands.length && random.nextBoolean()) { clayBands[start + 1] = LIGHT_GRAY_TERRACOTTA; } ix++; } return clayBands; } private static void makeBands(final RandomSource random, final BlockState[] clayBands, final int baseWidth, final BlockState state) { int bandCount = random.nextIntBetweenInclusive(6, 15); for (int i = 0; i < bandCount; i++) { int width = baseWidth + random.nextInt(3); int start = random.nextInt(clayBands.length); for (int p = 0; start + p < clayBands.length && p < width; p++) { clayBands[start + p] = state; } } } protected BlockState getBand(final int worldX, final int y, final int worldZ) { int offset = (int)Math.round(this.clayBandsOffsetNoise.getValue(worldX, 0.0, worldZ) * 4.0); return this.clayBands[(y + offset + this.clayBands.length) % this.clayBands.length]; } }