| 1 | package net.minecraft.world.level.levelgen.material;
|
| 2 |
|
| 3 | import java.util.Arrays;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.Set;
|
| 6 | import java.util.function.Function;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.SectionPos;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.level.ChunkPos;
|
| 14 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 15 | import net.minecraft.world.level.biome.Biome;
|
| 16 | import net.minecraft.world.level.biome.BiomeManager;
|
| 17 | import net.minecraft.world.level.biome.Biomes;
|
| 18 | import net.minecraft.world.level.block.Blocks;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.chunk.BlockColumn;
|
| 21 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 22 | import net.minecraft.world.level.dimension.DimensionType;
|
| 23 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 24 | import net.minecraft.world.level.levelgen.NoiseChunk;
|
| 25 | import net.minecraft.world.level.levelgen.Noises;
|
| 26 | import net.minecraft.world.level.levelgen.PositionalRandomFactory;
|
| 27 | import net.minecraft.world.level.levelgen.RandomState;
|
| 28 | import net.minecraft.world.level.levelgen.WorldGenerationContext;
|
| 29 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 30 | import net.minecraft.world.level.levelgen.densityfunction.DensitySamplerSet;
|
| 31 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 32 | import net.minecraft.world.level.levelgen.material.rule.MaterialRule;
|
| 33 | import net.minecraft.world.level.levelgen.material.rule.RuleEvaluator;
|
| 34 | import net.minecraft.world.level.levelgen.synth.Noise;
|
| 35 | import org.jspecify.annotations.Nullable;
|
| 36 |
|
| 37 | public class MaterialSystem {
|
| 38 | private static final BlockState WHITE_TERRACOTTA = Blocks.DYED_TERRACOTTA.white().defaultBlockState();
|
| 39 | private static final BlockState ORANGE_TERRACOTTA = Blocks.DYED_TERRACOTTA.orange().defaultBlockState();
|
| 40 | private static final BlockState TERRACOTTA = Blocks.TERRACOTTA.defaultBlockState();
|
| 41 | private static final BlockState YELLOW_TERRACOTTA = Blocks.DYED_TERRACOTTA.yellow().defaultBlockState();
|
| 42 | private static final BlockState BROWN_TERRACOTTA = Blocks.DYED_TERRACOTTA.brown().defaultBlockState();
|
| 43 | private static final BlockState RED_TERRACOTTA = Blocks.DYED_TERRACOTTA.red().defaultBlockState();
|
| 44 | private static final BlockState LIGHT_GRAY_TERRACOTTA = Blocks.DYED_TERRACOTTA.lightGray().defaultBlockState();
|
| 45 | private static final BlockState PACKED_ICE = Blocks.PACKED_ICE.defaultBlockState();
|
| 46 | private static final BlockState SNOW_BLOCK = Blocks.SNOW_BLOCK.defaultBlockState();
|
| 47 | private final BlockState defaultBlock;
|
| 48 | private final int seaLevel;
|
| 49 | private final DensityFunction preliminarySurfaceFunction;
|
| 50 | private final BlockState[] clayBands;
|
| 51 | private final Noise clayBandsOffsetNoise;
|
| 52 | private final Noise badlandsPillarNoise;
|
| 53 | private final Noise badlandsPillarRoofNoise;
|
| 54 | private final Noise badlandsSurfaceNoise;
|
| 55 | private final Noise icebergPillarNoise;
|
| 56 | private final Noise icebergPillarRoofNoise;
|
| 57 | private final Noise icebergSurfaceNoise;
|
| 58 | private final PositionalRandomFactory noiseRandom;
|
| 59 | private final Noise surfaceNoise;
|
| 60 | private final Noise surfaceSecondaryNoise;
|
| 61 |
|
| 62 | public MaterialSystem(
|
| 63 | final RandomState randomState,
|
| 64 | final BlockState defaultBlock,
|
| 65 | final int seaLevel,
|
| 66 | final DensityFunction preliminarySurfaceFunction,
|
| 67 | final PositionalRandomFactory noiseRandom
|
| 68 | ) {
|
| 69 | this.defaultBlock = defaultBlock;
|
| 70 | this.seaLevel = seaLevel;
|
| 71 | this.preliminarySurfaceFunction = preliminarySurfaceFunction;
|
| 72 | this.noiseRandom = noiseRandom;
|
| 73 | this.clayBandsOffsetNoise = randomState.getOrCreateNoise(Noises.CLAY_BANDS_OFFSET);
|
| 74 | this.clayBands = generateBands(noiseRandom.fromHashOf(Identifier.withDefaultNamespace("clay_bands")));
|
| 75 | this.surfaceNoise = randomState.getOrCreateNoise(Noises.SURFACE);
|
| 76 | this.surfaceSecondaryNoise = randomState.getOrCreateNoise(Noises.SURFACE_SECONDARY);
|
| 77 | this.badlandsPillarNoise = randomState.getOrCreateNoise(Noises.BADLANDS_PILLAR);
|
| 78 | this.badlandsPillarRoofNoise = randomState.getOrCreateNoise(Noises.BADLANDS_PILLAR_ROOF);
|
| 79 | this.badlandsSurfaceNoise = randomState.getOrCreateNoise(Noises.BADLANDS_SURFACE);
|
| 80 | this.icebergPillarNoise = randomState.getOrCreateNoise(Noises.ICEBERG_PILLAR);
|
| 81 | this.icebergPillarRoofNoise = randomState.getOrCreateNoise(Noises.ICEBERG_PILLAR_ROOF);
|
| 82 | this.icebergSurfaceNoise = randomState.getOrCreateNoise(Noises.ICEBERG_SURFACE);
|
| 83 | }
|
| 84 |
|
| 85 | public void buildSurface(
|
| 86 | final RandomState randomState,
|
| 87 | final BiomeManager biomeManager,
|
| 88 | final WorldGenerationContext generationContext,
|
| 89 | final ChunkAccess protoChunk,
|
| 90 | final NoiseChunk noiseChunk,
|
| 91 | final MaterialRule ruleSource,
|
| 92 | final @Nullable Set<Holder<Biome>> possibleBiomes
|
| 93 | ) {
|
| 94 | final BlockPos.MutableBlockPos columnPos = new BlockPos.MutableBlockPos();
|
| 95 | final ChunkPos chunkPos = protoChunk.getPos();
|
| 96 | int minBlockX = chunkPos.getMinBlockX();
|
| 97 | int minBlockZ = chunkPos.getMinBlockZ();
|
| 98 | LevelHeightAccessor heightAccessor = protoChunk.getHeightAccessorForGeneration();
|
| 99 | final int minY = heightAccessor.getMinY();
|
| 100 | final int maxY = heightAccessor.getMaxY();
|
| 101 | BlockColumn column = new BlockColumn() {
|
| 102 | @Override
|
| 103 | public BlockState getBlock(final int blockY) {
|
| 104 | return protoChunk.getBlockState(columnPos.setY(blockY));
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public void setBlock(final int blockY, final BlockState state) {
|
| 109 | if (blockY >= minY && blockY <= maxY) {
|
| 110 | protoChunk.setBlockState(columnPos.setY(blockY), state);
|
| 111 | if (!state.getFluidState().isEmpty()) {
|
| 112 | protoChunk.markPosForPostProcessing(columnPos);
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public String toString() {
|
| 119 | return "ChunkBlockColumn " + chunkPos;
|
| 120 | }
|
| 121 | };
|
| 122 | int highestFilledSectionIndex = protoChunk.getHighestFilledSectionIndex();
|
| 123 | int maxBlockY = protoChunk.getSectionYFromSectionIndex(highestFilledSectionIndex) * 16 + 15;
|
| 124 | DensityVolume fullVolume = noiseChunk.volume();
|
| 125 | DensityVolume narrowedVolume = new DensityVolume(
|
| 126 | fullVolume.sizeX(),
|
| 127 | Math.max(maxBlockY - fullVolume.minBlockY() + 1, 1),
|
| 128 | fullVolume.sizeZ(),
|
| 129 | fullVolume.minBlockX(),
|
| 130 | fullVolume.minBlockY(),
|
| 131 | fullVolume.minBlockZ()
|
| 132 | );
|
| 133 | MaterialRuleContext context = new MaterialRuleContext(
|
| 134 | this, randomState, narrowedVolume, noiseChunk.cachingSamplers(), biomeManager::getBiome, generationContext, possibleBiomes
|
| 135 | );
|
| 136 | RuleEvaluator rule = ruleSource.compile(context);
|
| 137 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 138 |
|
| 139 | for (int x = 0; x < 16; x++) {
|
| 140 | for (int z = 0; z < 16; z++) {
|
| 141 | int blockX = minBlockX + x;
|
| 142 | int blockZ = minBlockZ + z;
|
| 143 | int startingHeight = protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, z) + 1;
|
| 144 | columnPos.setX(blockX).setZ(blockZ);
|
| 145 | Holder<Biome> surfaceBiome = biomeManager.getBiome(blockPos.set(blockX, startingHeight, blockZ));
|
| 146 | if (surfaceBiome.is(Biomes.ERODED_BADLANDS)) {
|
| 147 | this.erodedBadlandsExtension(column, blockX, blockZ, startingHeight, protoChunk);
|
| 148 | }
|
| 149 |
|
| 150 | int height = protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, z) + 1;
|
| 151 | int gradientX = getSurfaceGradientX(protoChunk, x, z);
|
| 152 | int gradientZ = getSurfaceGradientZ(protoChunk, x, z);
|
| 153 | context.updateXZ(blockX, blockZ, gradientX, gradientZ);
|
| 154 | int stoneAboveDepth = 0;
|
| 155 | int waterHeight = Integer.MIN_VALUE;
|
| 156 | int nextCeilingStoneY = Integer.MAX_VALUE;
|
| 157 | int endY = protoChunk.getMinY();
|
| 158 |
|
| 159 | for (int y = height; y >= endY; y--) {
|
| 160 | BlockState old = column.getBlock(y);
|
| 161 | if (old.isAir()) {
|
| 162 | stoneAboveDepth = 0;
|
| 163 | waterHeight = Integer.MIN_VALUE;
|
| 164 | } else if (!old.getFluidState().isEmpty()) {
|
| 165 | if (waterHeight == Integer.MIN_VALUE) {
|
| 166 | waterHeight = y + 1;
|
| 167 | }
|
| 168 | } else {
|
| 169 | if (nextCeilingStoneY >= y) {
|
| 170 | nextCeilingStoneY = DimensionType.WAY_BELOW_MIN_Y;
|
| 171 |
|
| 172 | for (int lookaheadY = y - 1; lookaheadY >= endY - 1; lookaheadY--) {
|
| 173 | BlockState nextState = column.getBlock(lookaheadY);
|
| 174 | if (!this.isStone(nextState)) {
|
| 175 | nextCeilingStoneY = lookaheadY + 1;
|
| 176 | break;
|
| 177 | }
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | stoneAboveDepth++;
|
| 182 | int stoneBelowDepth = y - nextCeilingStoneY + 1;
|
| 183 | context.updateY(stoneAboveDepth, stoneBelowDepth, waterHeight, y);
|
| 184 | if (y >= minY && y <= maxY) {
|
| 185 | BlockState state = rule.tryApply(blockX, y, blockZ);
|
| 186 | if (state != null) {
|
| 187 | column.setBlock(y, state);
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | if (surfaceBiome.is(Biomes.FROZEN_OCEAN) || surfaceBiome.is(Biomes.DEEP_FROZEN_OCEAN)) {
|
| 194 | this.frozenOceanExtension(context.getMinSurfaceLevel(), surfaceBiome.value(), column, blockPos, blockX, blockZ, startingHeight);
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | private static int getSurfaceGradientX(final ChunkAccess protoChunk, final int x, final int z) {
|
| 201 | return protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, Math.min(x + 1, 15), z)
|
| 202 | - protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, Math.max(x - 1, 0), z);
|
| 203 | }
|
| 204 |
|
| 205 | private static int getSurfaceGradientZ(final ChunkAccess protoChunk, final int x, final int z) {
|
| 206 | return protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, Math.min(z + 1, 15))
|
| 207 | - protoChunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, Math.max(z - 1, 0));
|
| 208 | }
|
| 209 |
|
| 210 | protected int getSurfaceDepth(final int blockX, final int blockZ) {
|
| 211 | double noiseValue = this.surfaceNoise.get(blockX, 0.0, blockZ);
|
| 212 | return (int)(noiseValue * 2.75 + 3.0 + this.noiseRandom.at(blockX, 0, blockZ).nextDouble() * 0.25);
|
| 213 | }
|
| 214 |
|
| 215 | protected double getSurfaceSecondary(final int blockX, final int blockZ) {
|
| 216 | return this.surfaceSecondaryNoise.get(blockX, 0.0, blockZ);
|
| 217 | }
|
| 218 |
|
| 219 | private boolean isStone(final BlockState state) {
|
| 220 | return !state.isAir() && state.getFluidState().isEmpty();
|
| 221 | }
|
| 222 |
|
| 223 | public int getSeaLevel() {
|
| 224 | return this.seaLevel;
|
| 225 | }
|
| 226 |
|
| 227 | @Deprecated
|
| 228 | public Optional<BlockState> topMaterial(
|
| 229 | final MaterialRule ruleSource,
|
| 230 | final RandomState randomState,
|
| 231 | final WorldGenerationContext worldGenerationContext,
|
| 232 | final Function<BlockPos, Holder<Biome>> biomeGetter,
|
| 233 | final ChunkAccess chunk,
|
| 234 | final DensitySamplerSet densitySamplers,
|
| 235 | final BlockPos pos,
|
| 236 | final boolean underFluid
|
| 237 | ) {
|
| 238 | DensityVolume volume = new DensityVolume(1, 1, 1, pos.getX(), pos.getY(), pos.getZ());
|
| 239 | MaterialRuleContext context = new MaterialRuleContext(this, randomState, volume, densitySamplers, biomeGetter, worldGenerationContext, null);
|
| 240 | RuleEvaluator rule = ruleSource.compile(context);
|
| 241 | int blockX = pos.getX();
|
| 242 | int blockY = pos.getY();
|
| 243 | int blockZ = pos.getZ();
|
| 244 | int gradientX = getSurfaceGradientX(chunk, SectionPos.sectionRelative(pos.getX()), SectionPos.sectionRelative(pos.getZ()));
|
| 245 | int gradientZ = getSurfaceGradientZ(chunk, SectionPos.sectionRelative(pos.getX()), SectionPos.sectionRelative(pos.getZ()));
|
| 246 | context.updateXZ(blockX, blockZ, gradientX, gradientZ);
|
| 247 | context.updateY(1, 1, underFluid ? blockY + 1 : Integer.MIN_VALUE, blockY);
|
| 248 | BlockState state = rule.tryApply(blockX, blockY, blockZ);
|
| 249 | return Optional.ofNullable(state);
|
| 250 | }
|
| 251 |
|
| 252 | private void erodedBadlandsExtension(final BlockColumn column, final int blockX, final int blockZ, final int height, final LevelHeightAccessor protoChunk) {
|
| 253 | double pillarNoiseScale = 0.2;
|
| 254 | double pillarBuffer = Math.min(
|
| 255 | Math.abs(this.badlandsSurfaceNoise.get(blockX, 0.0, blockZ) * 8.25), this.badlandsPillarNoise.get(blockX * 0.2, 0.0, blockZ * 0.2) * 15.0F
|
| 256 | );
|
| 257 | if (!(pillarBuffer <= 0.0)) {
|
| 258 | double floorNoiseSampleResolution = 0.75;
|
| 259 | double floorAmplitude = 1.5;
|
| 260 | double pillarFloor = Math.abs(this.badlandsPillarRoofNoise.get(blockX * 0.75, 0.0, blockZ * 0.75) * 1.5);
|
| 261 | double extensionTop = 64.0 + Math.min(pillarBuffer * pillarBuffer * 2.5, Math.ceil(pillarFloor * 50.0) + 24.0);
|
| 262 | int startY = Mth.floor(extensionTop);
|
| 263 | if (height <= startY) {
|
| 264 | for (int y = startY; y >= protoChunk.getMinY(); y--) {
|
| 265 | BlockState oldState = column.getBlock(y);
|
| 266 | if (oldState.is(this.defaultBlock.getBlock())) {
|
| 267 | break;
|
| 268 | }
|
| 269 |
|
| 270 | if (oldState.is(Blocks.WATER)) {
|
| 271 | return;
|
| 272 | }
|
| 273 | }
|
| 274 |
|
| 275 | for (int y = startY; y >= protoChunk.getMinY() && column.getBlock(y).isAir(); y--) {
|
| 276 | column.setBlock(y, this.defaultBlock);
|
| 277 | }
|
| 278 | }
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | private void frozenOceanExtension(
|
| 283 | final int minSurfaceLevel,
|
| 284 | final Biome surfaceBiome,
|
| 285 | final BlockColumn column,
|
| 286 | final BlockPos.MutableBlockPos blockPos,
|
| 287 | final int blockX,
|
| 288 | final int blockZ,
|
| 289 | final int height
|
| 290 | ) {
|
| 291 | double pillarScale = 1.28;
|
| 292 | double iceberg = Math.min(
|
| 293 | Math.abs(this.icebergSurfaceNoise.get(blockX, 0.0, blockZ) * 8.25), this.icebergPillarNoise.get(blockX * 1.28, 0.0, blockZ * 1.28) * 15.0F
|
| 294 | );
|
| 295 | if (!(iceberg <= 1.8)) {
|
| 296 | double roofScale = 1.17;
|
| 297 | double roofAmplitude = 1.5;
|
| 298 | double icebergRoof = Math.abs(this.icebergPillarRoofNoise.get(blockX * 1.17, 0.0, blockZ * 1.17) * 1.5);
|
| 299 | double top = Math.min(iceberg * iceberg * 1.2, Math.ceil(icebergRoof * 40.0) + 14.0);
|
| 300 | if (surfaceBiome.shouldMeltFrozenOceanIcebergSlightly(blockPos.set(blockX, this.seaLevel, blockZ), this.seaLevel)) {
|
| 301 | top -= 2.0;
|
| 302 | }
|
| 303 |
|
| 304 | if (!(top <= 2.0)) {
|
| 305 | double extensionBottom = this.seaLevel - top - 7.0;
|
| 306 | top += this.seaLevel;
|
| 307 | double extensionTop = top;
|
| 308 | RandomSource random = this.noiseRandom.at(blockX, 0, blockZ);
|
| 309 | int maxSnowDepth = 2 + random.nextInt(4);
|
| 310 | int minSnowHeight = this.seaLevel + 18 + random.nextInt(10);
|
| 311 | int snowDepth = 0;
|
| 312 |
|
| 313 | for (int y = Math.max(height, (int)extensionTop + 1); y >= minSurfaceLevel; y--) {
|
| 314 | if (column.getBlock(y).isAir() && y < (int)extensionTop && random.nextDouble() > 0.01
|
| 315 | || column.getBlock(y).is(Blocks.WATER) && y > (int)extensionBottom && y < this.seaLevel && random.nextDouble() > 0.15) {
|
| 316 | if (snowDepth <= maxSnowDepth && y > minSnowHeight) {
|
| 317 | column.setBlock(y, SNOW_BLOCK);
|
| 318 | snowDepth++;
|
| 319 | } else {
|
| 320 | column.setBlock(y, PACKED_ICE);
|
| 321 | }
|
| 322 | }
|
| 323 | }
|
| 324 | }
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | private static BlockState[] generateBands(final RandomSource random) {
|
| 329 | BlockState[] clayBands = new BlockState[192];
|
| 330 | Arrays.fill(clayBands, TERRACOTTA);
|
| 331 |
|
| 332 | for (int i = 0; i < clayBands.length; i++) {
|
| 333 | i += random.nextInt(5) + 1;
|
| 334 | if (i < clayBands.length) {
|
| 335 | clayBands[i] = ORANGE_TERRACOTTA;
|
| 336 | }
|
| 337 | }
|
| 338 |
|
| 339 | makeBands(random, clayBands, 1, YELLOW_TERRACOTTA);
|
| 340 | makeBands(random, clayBands, 2, BROWN_TERRACOTTA);
|
| 341 | makeBands(random, clayBands, 1, RED_TERRACOTTA);
|
| 342 | int whiteBandCount = random.nextIntBetweenInclusive(9, 15);
|
| 343 | int i = 0;
|
| 344 |
|
| 345 | for (int start = 0; i < whiteBandCount && start < clayBands.length; start += random.nextInt(16) + 4) {
|
| 346 | clayBands[start] = WHITE_TERRACOTTA;
|
| 347 | if (start - 1 > 0 && random.nextBoolean()) {
|
| 348 | clayBands[start - 1] = LIGHT_GRAY_TERRACOTTA;
|
| 349 | }
|
| 350 |
|
| 351 | if (start + 1 < clayBands.length && random.nextBoolean()) {
|
| 352 | clayBands[start + 1] = LIGHT_GRAY_TERRACOTTA;
|
| 353 | }
|
| 354 |
|
| 355 | i++;
|
| 356 | }
|
| 357 |
|
| 358 | return clayBands;
|
| 359 | }
|
| 360 |
|
| 361 | private static void makeBands(final RandomSource random, final BlockState[] clayBands, final int baseWidth, final BlockState state) {
|
| 362 | int bandCount = random.nextIntBetweenInclusive(6, 15);
|
| 363 |
|
| 364 | for (int i = 0; i < bandCount; i++) {
|
| 365 | int width = baseWidth + random.nextInt(3);
|
| 366 | int start = random.nextInt(clayBands.length);
|
| 367 |
|
| 368 | for (int p = 0; start + p < clayBands.length && p < width; p++) {
|
| 369 | clayBands[start + p] = state;
|
| 370 | }
|
| 371 | }
|
| 372 | }
|
| 373 |
|
| 374 | protected BlockState getBand(final int worldX, final int y, final int worldZ) {
|
| 375 | int offset = Math.round(this.clayBandsOffsetNoise.get(worldX, 0.0, worldZ) * 4.0F);
|
| 376 | return this.clayBands[(y + offset + this.clayBands.length) % this.clayBands.length];
|
| 377 | }
|
| 378 |
|
| 379 | public DensityFunction preliminarySurfaceFunction() {
|
| 380 | return this.preliminarySurfaceFunction;
|
| 381 | }
|
| 382 | }
|
| 383 |
|