| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.text.DecimalFormat;
|
| 8 | import java.text.DecimalFormatSymbols;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Locale;
|
| 11 | import java.util.OptionalInt;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.concurrent.CompletableFuture;
|
| 14 | import java.util.function.Function;
|
| 15 | import java.util.function.Predicate;
|
| 16 | import java.util.function.Supplier;
|
| 17 | import net.minecraft.SharedConstants;
|
| 18 | import net.minecraft.core.BlockPos;
|
| 19 | import net.minecraft.core.Direction;
|
| 20 | import net.minecraft.core.Holder;
|
| 21 | import net.minecraft.core.QuartPos;
|
| 22 | import net.minecraft.core.SectionPos;
|
| 23 | import net.minecraft.resources.ResourceKey;
|
| 24 | import net.minecraft.server.level.WorldGenRegion;
|
| 25 | import net.minecraft.tags.BlockTags;
|
| 26 | import net.minecraft.util.Mth;
|
| 27 | import net.minecraft.util.Util;
|
| 28 | import net.minecraft.util.profiling.Profiler;
|
| 29 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 30 | import net.minecraft.util.profiling.Zone;
|
| 31 | import net.minecraft.world.level.ChunkPos;
|
| 32 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 33 | import net.minecraft.world.level.NaturalSpawner;
|
| 34 | import net.minecraft.world.level.NoiseColumn;
|
| 35 | import net.minecraft.world.level.StructureManager;
|
| 36 | import net.minecraft.world.level.biome.Biome;
|
| 37 | import net.minecraft.world.level.biome.BiomeGenerationSettings;
|
| 38 | import net.minecraft.world.level.biome.BiomeManager;
|
| 39 | import net.minecraft.world.level.biome.BiomeResolver;
|
| 40 | import net.minecraft.world.level.biome.BiomeSource;
|
| 41 | import net.minecraft.world.level.block.Blocks;
|
| 42 | import net.minecraft.world.level.block.state.BlockState;
|
| 43 | import net.minecraft.world.level.chunk.CarvingMask;
|
| 44 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 45 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 46 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 47 | import net.minecraft.world.level.dimension.DimensionType;
|
| 48 | import net.minecraft.world.level.levelgen.blending.Blender;
|
| 49 | import net.minecraft.world.level.levelgen.carver.WorldCarver;
|
| 50 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 51 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 52 | import net.minecraft.world.level.levelgen.densityfunction.DensitySamplerSet;
|
| 53 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 54 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 55 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 56 | import net.minecraft.world.level.levelgen.material.rule.MaterialRule;
|
| 57 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 58 | import org.jspecify.annotations.Nullable;
|
| 59 |
|
| 60 | public final class NoiseBasedChunkGenerator extends ChunkGenerator {
|
| 61 | public static final MapCodec<NoiseBasedChunkGenerator> CODEC = RecordCodecBuilder.mapCodec(
|
| 62 | i -> i.group(
|
| 63 | BiomeSource.CODEC.fieldOf("biome_source").forGetter(g -> g.biomeSource), NoiseGeneratorSettings.CODEC.fieldOf("settings").forGetter(g -> g.settings)
|
| 64 | )
|
| 65 | .apply(i, i.stable(NoiseBasedChunkGenerator::new))
|
| 66 | );
|
| 67 | private static final BlockState AIR = Blocks.AIR.defaultBlockState();
|
| 68 | private static final DecimalFormat DEBUG_DENSITY_FORMAT = new DecimalFormat("0.000", DecimalFormatSymbols.getInstance(Locale.ROOT));
|
| 69 | private final Holder<NoiseGeneratorSettings> settings;
|
| 70 | private final Supplier<Aquifer.FluidPicker> globalFluidPicker;
|
| 71 |
|
| 72 | public NoiseBasedChunkGenerator(final BiomeSource biomeSource, final Holder<NoiseGeneratorSettings> settings) {
|
| 73 | super(biomeSource);
|
| 74 | this.settings = settings;
|
| 75 | this.globalFluidPicker = Suppliers.memoize(() -> createFluidPicker(settings.value()));
|
| 76 | }
|
| 77 |
|
| 78 | private static Aquifer.FluidPicker createFluidPicker(final NoiseGeneratorSettings settings) {
|
| 79 | Aquifer.FluidStatus lavaStatus = new Aquifer.FluidStatus(-54, Blocks.LAVA.defaultBlockState());
|
| 80 | int seaLevel = settings.seaLevel();
|
| 81 | Aquifer.FluidStatus seaStatus = new Aquifer.FluidStatus(seaLevel, settings.defaultFluid());
|
| 82 | Aquifer.FluidStatus emptyStatus = new Aquifer.FluidStatus(DimensionType.MIN_Y * 2, Blocks.AIR.defaultBlockState());
|
| 83 | return (x, y, z) -> {
|
| 84 | if (SharedConstants.DEBUG_DISABLE_FLUID_GENERATION) {
|
| 85 | return emptyStatus;
|
| 86 | } else {
|
| 87 | return y < Math.min(-54, seaLevel) ? lavaStatus : seaStatus;
|
| 88 | }
|
| 89 | };
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected BiomeResolver decorateBiomeResolver(final Blender blender, final ChunkAccess protoChunk, BiomeResolver biomeResolver) {
|
| 94 | biomeResolver = blender.getBiomeResolver(biomeResolver);
|
| 95 | return BelowZeroRetrogen.getBiomeResolver(biomeResolver, protoChunk);
|
| 96 | }
|
| 97 |
|
| 98 | private NoiseChunk createNoiseChunk(
|
| 99 | final ChunkAccess chunk, final StructureManager structureManager, final Blender blender, final RandomState randomState, final NoiseSettings noiseSettings
|
| 100 | ) {
|
| 101 | Beardifier beardifier = Beardifier.forStructuresInChunk(structureManager, chunk.getPos());
|
| 102 | return new NoiseChunk(randomState, beardifier, this.settings.value(), this.globalFluidPicker.get(), blender, chunkVolume(chunk, noiseSettings));
|
| 103 | }
|
| 104 |
|
| 105 | private static DensityVolume chunkVolume(final ChunkAccess chunk, final NoiseSettings noiseSettings) {
|
| 106 | ChunkPos pos = chunk.getPos();
|
| 107 | return new DensityVolume(16, noiseSettings.height(), 16, pos.getMinBlockX(), noiseSettings.minY(), pos.getMinBlockZ());
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public ChunkPos getOrigin(final RandomState randomState) {
|
| 112 | List<SpawnTargetPoint> spawnTarget = this.settings.value().spawnTarget();
|
| 113 | if (spawnTarget.isEmpty()) {
|
| 114 | return super.getOrigin(randomState);
|
| 115 | }
|
| 116 |
|
| 117 | SamplerContext samplerContext = SamplerContext.builder().enableCaches().build();
|
| 118 | return ChunkPos.containing(NoiseSpawnFinder.findSpawnPosition(spawnTarget, randomState.samplersWithContext(samplerContext)));
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | protected MapCodec<? extends ChunkGenerator> codec() {
|
| 123 | return CODEC;
|
| 124 | }
|
| 125 |
|
| 126 | public Holder<NoiseGeneratorSettings> generatorSettings() {
|
| 127 | return this.settings;
|
| 128 | }
|
| 129 |
|
| 130 | public boolean stable(final ResourceKey<NoiseGeneratorSettings> expectedPreset) {
|
| 131 | return this.settings.is(expectedPreset);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public int getBaseHeight(final int x, final int z, final Heightmap.Types type, final LevelHeightAccessor heightAccessor, final RandomState randomState) {
|
| 136 | return this.iterateNoiseColumn(heightAccessor, randomState, x, z, null, type.isOpaque()).orElse(heightAccessor.getMinY());
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public NoiseColumn getBaseColumn(final int x, final int z, final LevelHeightAccessor heightAccessor, final RandomState randomState) {
|
| 141 | MutableObject<NoiseColumn> result = new MutableObject<>();
|
| 142 | this.iterateNoiseColumn(heightAccessor, randomState, x, z, result, null);
|
| 143 | return result.get();
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public void addDebugScreenInfo(final List<String> result, final RandomState randomState, final BlockPos feetPos, final SamplerContext samplerContext) {
|
| 148 | List<NoiseGeneratorSettings.DebugFunctionEntry> functions = this.settings.value().debugFunctions().functions();
|
| 149 | if (!functions.isEmpty()) {
|
| 150 | DensitySamplerSet samplers = randomState.samplersWithContext(samplerContext);
|
| 151 | StringBuilder builder = new StringBuilder("Density ");
|
| 152 |
|
| 153 | for (NoiseGeneratorSettings.DebugFunctionEntry entry : functions) {
|
| 154 | builder.append(entry.label()).append(": ");
|
| 155 | builder.append(DEBUG_DENSITY_FORMAT.format(samplers.sampleValue(entry.function(), feetPos.getX(), feetPos.getY(), feetPos.getZ())));
|
| 156 | builder.append(' ');
|
| 157 | }
|
| 158 |
|
| 159 | builder.deleteCharAt(builder.length() - 1);
|
| 160 | result.add(builder.toString());
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | private OptionalInt iterateNoiseColumn(
|
| 165 | final LevelHeightAccessor heightAccessor,
|
| 166 | final RandomState randomState,
|
| 167 | final int blockX,
|
| 168 | final int blockZ,
|
| 169 | final @Nullable MutableObject<NoiseColumn> columnReference,
|
| 170 | final @Nullable Predicate<BlockState> tester
|
| 171 | ) {
|
| 172 | NoiseSettings noiseSettings = this.settings.value().noiseSettings().clampToHeightAccessor(heightAccessor);
|
| 173 | if (noiseSettings.height() <= 0) {
|
| 174 | return OptionalInt.empty();
|
| 175 | }
|
| 176 |
|
| 177 | DensityVolume volume = new DensityVolume(1, noiseSettings.height(), 1, blockX, noiseSettings.minY(), blockZ);
|
| 178 | BlockState[] writeTo;
|
| 179 | if (columnReference == null) {
|
| 180 | writeTo = null;
|
| 181 | } else {
|
| 182 | writeTo = new BlockState[volume.sizeY()];
|
| 183 | columnReference.setValue(new NoiseColumn(volume.minBlockY(), writeTo));
|
| 184 | }
|
| 185 |
|
| 186 | try (NoiseChunk noiseChunk = new NoiseChunk(randomState, null, this.settings.value(), this.globalFluidPicker.get(), Blender.empty(), volume)) {
|
| 187 | Aquifer aquifer = noiseChunk.aquifer();
|
| 188 | BlockState defaultState = this.settings.value().defaultBlock();
|
| 189 | DensitySampler.Bound finalDensity = noiseChunk.cachingSamplers().get(this.settings.value().noiseRouter().finalDensity());
|
| 190 |
|
| 191 | try (ScopedDensityBuffer densityBuffer = finalDensity.sampleVolume(noiseChunk.volume())) {
|
| 192 | for (int y = volume.sizeY() - 1; y >= 0; y--) {
|
| 193 | float density = densityBuffer.get(volume.indexUnchecked(0, y, 0));
|
| 194 | int blockY = volume.blockY(y);
|
| 195 | BlockState baseState = aquifer.computeSubstance(blockX, blockY, blockZ, density);
|
| 196 | BlockState state = baseState == null ? defaultState : baseState;
|
| 197 | if (writeTo != null) {
|
| 198 | writeTo[y] = state;
|
| 199 | }
|
| 200 |
|
| 201 | if (tester != null && tester.test(state)) {
|
| 202 | return OptionalInt.of(blockY + 1);
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | return OptionalInt.empty();
|
| 209 | }
|
| 210 |
|
| 211 | private void buildSurface(
|
| 212 | final ChunkAccess protoChunk,
|
| 213 | final NoiseChunk noiseChunk,
|
| 214 | final RandomState randomState,
|
| 215 | final BiomeManager biomeManager,
|
| 216 | final Set<Holder<Biome>> possibleBiomes,
|
| 217 | final MaterialRule materialRule
|
| 218 | ) {
|
| 219 | if (!SharedConstants.debugVoidTerrain(protoChunk.getPos()) && !SharedConstants.DEBUG_DISABLE_SURFACE) {
|
| 220 | WorldGenerationContext context = new WorldGenerationContext(this, protoChunk.getHeightAccessorForGeneration());
|
| 221 | randomState.surfaceSystem().buildSurface(randomState, biomeManager, context, protoChunk, noiseChunk, materialRule, possibleBiomes);
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | private void generateCarvers(
|
| 226 | final ChunkAccess chunk,
|
| 227 | final Blender blender,
|
| 228 | final NoiseChunk noiseChunk,
|
| 229 | final RandomState randomState,
|
| 230 | final BiomeManager biomeManager,
|
| 231 | final @Nullable WorldGenRegion carverBiomeRegion,
|
| 232 | final MaterialRule materialRule
|
| 233 | ) {
|
| 234 | if (!SharedConstants.DEBUG_DISABLE_CARVERS && !SharedConstants.debugVoidTerrain(chunk.getPos())) {
|
| 235 | BiomeResolver biomeResolver = this.biomeSource.createUncachedResolver(randomState);
|
| 236 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(RandomSupport.generateUniqueSeed()));
|
| 237 | int range = 8;
|
| 238 | ChunkPos pos = chunk.getPos();
|
| 239 | WorldGenerationContext context = new WorldGenerationContext(this, chunk.getHeightAccessorForGeneration());
|
| 240 | int protectedBlocksOnTop = chunk.isUpgrading() ? 0 : 7;
|
| 241 | int maxY = context.getMinGenY() + context.getGenDepth() - 1 - protectedBlocksOnTop;
|
| 242 | CarvingMask mask = new CarvingMask(context.getMinGenY() + 1, maxY);
|
| 243 |
|
| 244 | for (int dx = -8; dx <= 8; dx++) {
|
| 245 | for (int dz = -8; dz <= 8; dz++) {
|
| 246 | ChunkPos sourcePos = new ChunkPos(pos.x() + dx, pos.z() + dz);
|
| 247 | BiomeGenerationSettings sourceBiomeGenerationSettings;
|
| 248 | if (carverBiomeRegion != null) {
|
| 249 | ChunkAccess carverCenterChunk = carverBiomeRegion.getChunk(sourcePos.x(), sourcePos.z());
|
| 250 | sourceBiomeGenerationSettings = carverCenterChunk.carverBiome(() -> this.getBiomeGenerationSettingsForCarver(biomeResolver, sourcePos));
|
| 251 | } else {
|
| 252 | sourceBiomeGenerationSettings = this.getBiomeGenerationSettingsForCarver(biomeResolver, sourcePos);
|
| 253 | }
|
| 254 |
|
| 255 | Iterable<Holder<WorldCarver>> carvers = sourceBiomeGenerationSettings.getCarvers();
|
| 256 | int index = 0;
|
| 257 |
|
| 258 | for (Holder<WorldCarver> carverHolder : carvers) {
|
| 259 | WorldCarver carver = carverHolder.value();
|
| 260 | random.setLargeFeatureSeed(randomState.seed() + index, sourcePos.x(), sourcePos.z());
|
| 261 | if (carver.isStartChunk(random)) {
|
| 262 | carver.carve(context, random, chunk.getPos(), sourcePos, mask);
|
| 263 | }
|
| 264 |
|
| 265 | index++;
|
| 266 | }
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | if (!mask.isEmpty()) {
|
| 271 | try (Zone var27 = Profiler.get().zone("applyCarvingMask")) {
|
| 272 | BiomeManager correctBiomeManager = biomeManager.withDifferentSource(biomeResolver);
|
| 273 | this.applyCarvingMask(chunk, mask, randomState, materialRule, context, noiseChunk, correctBiomeManager::getBiome, blender.getCarvingFilter());
|
| 274 | }
|
| 275 | }
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | private void applyCarvingMask(
|
| 280 | final ChunkAccess chunk,
|
| 281 | final CarvingMask mask,
|
| 282 | final RandomState randomState,
|
| 283 | final MaterialRule materialRule,
|
| 284 | final WorldGenerationContext context,
|
| 285 | final NoiseChunk noiseChunk,
|
| 286 | final Function<BlockPos, Holder<Biome>> biomeGetter,
|
| 287 | final CarvingMask.@Nullable Filter filter
|
| 288 | ) {
|
| 289 | ChunkPos chunkPos = chunk.getPos();
|
| 290 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 291 | BlockPos.MutableBlockPos helperPos = new BlockPos.MutableBlockPos();
|
| 292 | Aquifer aquifer = noiseChunk.aquifer();
|
| 293 | mask.visit(
|
| 294 | (x, z, bottomY, topY) -> {
|
| 295 | boolean hasGrass = false;
|
| 296 | int worldX = chunkPos.getBlockX(x);
|
| 297 | int worldZ = chunkPos.getBlockZ(z);
|
| 298 |
|
| 299 | for (int worldY = topY; worldY >= bottomY; worldY--) {
|
| 300 | if (filter == null || filter.test(x, worldY, z)) {
|
| 301 | blockPos.set(worldX, worldY, worldZ);
|
| 302 | BlockState blockState = chunk.getBlockState(blockPos);
|
| 303 | if (!blockState.is(BlockTags.UNCARVABLE)) {
|
| 304 | if (blockState.is(Blocks.GRASS_BLOCK) || blockState.is(Blocks.MYCELIUM)) {
|
| 305 | hasGrass = true;
|
| 306 | }
|
| 307 |
|
| 308 | BlockState state = aquifer.computeSubstance(worldX, worldY, worldZ, 0.0);
|
| 309 | if (state != null) {
|
| 310 | chunk.setBlockState(blockPos, state);
|
| 311 | if (aquifer.shouldScheduleFluidUpdate() && !state.getFluidState().isEmpty()) {
|
| 312 | chunk.markPosForPostProcessing(blockPos);
|
| 313 | }
|
| 314 |
|
| 315 | if (hasGrass) {
|
| 316 | helperPos.setWithOffset(blockPos, Direction.DOWN);
|
| 317 | if (chunk.getBlockState(helperPos).is(Blocks.DIRT)) {
|
| 318 | randomState.surfaceSystem()
|
| 319 | .topMaterial(materialRule, randomState, context, biomeGetter, chunk, noiseChunk.cachingSamplers(), helperPos, !state.getFluidState().isEmpty())
|
| 320 | .ifPresent(topMaterial -> {
|
| 321 | chunk.setBlockState(helperPos, topMaterial);
|
| 322 | if (!topMaterial.getFluidState().isEmpty()) {
|
| 323 | chunk.markPosForPostProcessing(helperPos);
|
| 324 | }
|
| 325 | });
|
| 326 | }
|
| 327 | }
|
| 328 | }
|
| 329 | }
|
| 330 | }
|
| 331 | }
|
| 332 | }
|
| 333 | );
|
| 334 | }
|
| 335 |
|
| 336 | private BiomeGenerationSettings getBiomeGenerationSettingsForCarver(final BiomeResolver biomeResolver, final ChunkPos sourcePos) {
|
| 337 | int quartX = QuartPos.fromBlock(sourcePos.getMinBlockX());
|
| 338 | int quartZ = QuartPos.fromBlock(sourcePos.getMinBlockZ());
|
| 339 | return this.getBiomeGenerationSettings(biomeResolver.getNoiseBiome(quartX, 0, quartZ));
|
| 340 | }
|
| 341 |
|
| 342 | @Override
|
| 343 | public CompletableFuture<ChunkAccess> buildTerrain(
|
| 344 | final ChunkAccess chunk,
|
| 345 | final Blender blender,
|
| 346 | final RandomState randomState,
|
| 347 | final StructureManager structureManager,
|
| 348 | final BiomeManager biomeManager,
|
| 349 | final @Nullable WorldGenRegion carverBiomeRegion,
|
| 350 | final Set<Holder<Biome>> possibleBiomes
|
| 351 | ) {
|
| 352 | NoiseSettings noiseSettings = this.settings.value().noiseSettings().clampToHeightAccessor(chunk.getHeightAccessorForGeneration());
|
| 353 | return noiseSettings.height() > 0 && !SharedConstants.debugVoidTerrain(chunk.getPos()) ? CompletableFuture.supplyAsync(() -> {
|
| 354 | ProfilerFiller profiler = Profiler.get();
|
| 355 |
|
| 356 | try (NoiseChunk noiseChunk = this.createNoiseChunk(chunk, structureManager, blender, randomState, noiseSettings)) {
|
| 357 | DensityVolume volume = noiseChunk.volume();
|
| 358 | int topSectionIndex = chunk.getSectionIndex(volume.maxBlockY());
|
| 359 | int bottomSectionIndex = chunk.getSectionIndex(volume.minBlockY());
|
| 360 | Set<LevelChunkSection> sections = Sets.newHashSet();
|
| 361 |
|
| 362 | for (int sectionIndex = topSectionIndex; sectionIndex >= bottomSectionIndex; sectionIndex--) {
|
| 363 | LevelChunkSection section = chunk.getSection(sectionIndex);
|
| 364 | section.acquire();
|
| 365 | sections.add(section);
|
| 366 | }
|
| 367 |
|
| 368 | try (Zone var39 = profiler.zone("doFill")) {
|
| 369 | this.doFill(noiseChunk, chunk);
|
| 370 | } finally {
|
| 371 | for (LevelChunkSection section : sections) {
|
| 372 | section.release();
|
| 373 | }
|
| 374 | }
|
| 375 |
|
| 376 | MaterialRule materialRule = this.settings.value().materialRule().value();
|
| 377 |
|
| 378 | try (Zone var41 = profiler.zone("buildSurface")) {
|
| 379 | this.buildSurface(chunk, noiseChunk, randomState, biomeManager, possibleBiomes, materialRule);
|
| 380 | }
|
| 381 |
|
| 382 | try (Zone var42 = profiler.zone("generateCarvers")) {
|
| 383 | this.generateCarvers(chunk, blender, noiseChunk, randomState, biomeManager, carverBiomeRegion, materialRule);
|
| 384 | }
|
| 385 |
|
| 386 | return chunk;
|
| 387 | }
|
| 388 | }, Util.backgroundExecutor().forName("buildTerrain")) : CompletableFuture.completedFuture(chunk);
|
| 389 | }
|
| 390 |
|
| 391 | private void doFill(final NoiseChunk noiseChunk, final ChunkAccess chunk) {
|
| 392 | Heightmap oceanFloor = chunk.getOrCreateHeightmapUnprimed(Heightmap.Types.OCEAN_FLOOR_WG);
|
| 393 | Heightmap worldSurface = chunk.getOrCreateHeightmapUnprimed(Heightmap.Types.WORLD_SURFACE_WG);
|
| 394 | Aquifer aquifer = noiseChunk.aquifer();
|
| 395 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 396 | DensityVolume volume = noiseChunk.volume();
|
| 397 | DensitySampler.Bound finalDensity = noiseChunk.cachingSamplers().get(this.settings.value().noiseRouter().finalDensity());
|
| 398 |
|
| 399 | try (ScopedDensityBuffer densityBuffer = finalDensity.sampleVolume(volume)) {
|
| 400 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 401 | int blockZ = volume.blockZ(z);
|
| 402 |
|
| 403 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 404 | int blockX = volume.blockX(x);
|
| 405 |
|
| 406 | for (int y = volume.sizeY() - 1; y >= 0; y--) {
|
| 407 | int blockY = volume.blockY(y);
|
| 408 | LevelChunkSection section = chunk.getSection(chunk.getSectionIndex(blockY));
|
| 409 | float density = densityBuffer.get(volume.indexUnchecked(x, y, z));
|
| 410 | BlockState state = aquifer.computeSubstance(blockX, blockY, blockZ, density);
|
| 411 | if (state == null) {
|
| 412 | state = this.settings.value().defaultBlock();
|
| 413 | }
|
| 414 |
|
| 415 | state = this.debugPreliminarySurfaceLevel(noiseChunk, blockX, blockY, blockZ, state);
|
| 416 | if (state != AIR) {
|
| 417 | section.setBlockState(x, SectionPos.sectionRelative(blockY), z, state, false);
|
| 418 | oceanFloor.update(x, blockY, z, state);
|
| 419 | worldSurface.update(x, blockY, z, state);
|
| 420 | if (aquifer.shouldScheduleFluidUpdate() && !state.getFluidState().isEmpty()) {
|
| 421 | blockPos.set(blockX, blockY, blockZ);
|
| 422 | chunk.markPosForPostProcessing(blockPos);
|
| 423 | }
|
| 424 | }
|
| 425 | }
|
| 426 | }
|
| 427 | }
|
| 428 | }
|
| 429 | }
|
| 430 |
|
| 431 | private BlockState debugPreliminarySurfaceLevel(final NoiseChunk noiseChunk, final int posX, final int posY, final int posZ, BlockState state) {
|
| 432 | if (SharedConstants.DEBUG_AQUIFERS && posZ >= 0 && posZ % 4 == 0) {
|
| 433 | DensityFunction surfaceLevelFunction = this.settings.value().aquifers().map(Aquifer.Config::surfaceLevel).orElse(null);
|
| 434 | if (surfaceLevelFunction == null) {
|
| 435 | return state;
|
| 436 | }
|
| 437 |
|
| 438 | int preliminarySurfaceLevel = Mth.floor(noiseChunk.cachingSamplers().sampleValue(surfaceLevelFunction, posX, 0, posZ));
|
| 439 | int adjustedSurfaceLevel = preliminarySurfaceLevel + 8;
|
| 440 | if (posY == adjustedSurfaceLevel) {
|
| 441 | state = adjustedSurfaceLevel < this.getSeaLevel() ? Blocks.SLIME_BLOCK.defaultBlockState() : Blocks.HONEY_BLOCK.defaultBlockState();
|
| 442 | }
|
| 443 | }
|
| 444 |
|
| 445 | return state;
|
| 446 | }
|
| 447 |
|
| 448 | @Override
|
| 449 | public int getGenDepth() {
|
| 450 | return this.settings.value().noiseSettings().height();
|
| 451 | }
|
| 452 |
|
| 453 | @Override
|
| 454 | public int getSeaLevel() {
|
| 455 | return this.settings.value().seaLevel();
|
| 456 | }
|
| 457 |
|
| 458 | @Override
|
| 459 | public int getMinY() {
|
| 460 | return this.settings.value().noiseSettings().minY();
|
| 461 | }
|
| 462 |
|
| 463 | @Override
|
| 464 | public void spawnOriginalMobs(final WorldGenRegion worldGenRegion) {
|
| 465 | if (!this.settings.value().disableMobGeneration()) {
|
| 466 | ChunkPos center = worldGenRegion.getCenter();
|
| 467 | BlockPos sourcePos = center.getWorldPosition().atY(worldGenRegion.getMaxY());
|
| 468 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(RandomSupport.generateUniqueSeed()));
|
| 469 | random.setDecorationSeed(worldGenRegion.getSeed(), center.getMinBlockX(), center.getMinBlockZ());
|
| 470 | NaturalSpawner.spawnMobsForChunkGeneration(worldGenRegion, sourcePos, center, random);
|
| 471 | }
|
| 472 | }
|
| 473 | }
|
| 474 |
|