| 1 | package net.minecraft.world.level.levelgen.feature.stateproviders;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.util.ExtraCodecs;
|
| 10 | import net.minecraft.util.InclusiveRange;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.level.LevelAccessor;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
| 16 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 17 | import net.minecraft.world.level.levelgen.synth.Noise;
|
| 18 | import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
| 19 |
|
| 20 | public class DualNoiseProvider extends NoiseProvider {
|
| 21 | public static final MapCodec<DualNoiseProvider> CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> i.group(
|
| 23 | InclusiveRange.codec(Codec.INT, 1, 64).fieldOf("variety").forGetter(p -> p.variety),
|
| 24 | NormalNoise.DIRECT_CODEC.fieldOf("slow_noise").forGetter(p -> p.slowNoiseParameters),
|
| 25 | ExtraCodecs.POSITIVE_FLOAT.fieldOf("slow_scale").forGetter(p -> p.slowScale)
|
| 26 | )
|
| 27 | .and(noiseProviderCodec(i))
|
| 28 | .apply(i, DualNoiseProvider::new)
|
| 29 | );
|
| 30 | private final InclusiveRange<Integer> variety;
|
| 31 | private final NormalNoise slowNoiseParameters;
|
| 32 | private final float slowScale;
|
| 33 | private final Noise slowNoise;
|
| 34 |
|
| 35 | public DualNoiseProvider(
|
| 36 | final InclusiveRange<Integer> variety,
|
| 37 | final NormalNoise slowNoiseParameters,
|
| 38 | final float slowScale,
|
| 39 | final long seed,
|
| 40 | final NormalNoise parameters,
|
| 41 | final float scale,
|
| 42 | final List<BlockState> states
|
| 43 | ) {
|
| 44 | super(seed, parameters, scale, states);
|
| 45 | this.variety = variety;
|
| 46 | this.slowNoiseParameters = slowNoiseParameters;
|
| 47 | this.slowScale = slowScale;
|
| 48 | this.slowNoise = slowNoiseParameters.create(new WorldgenRandom(new LegacyRandomSource(seed)));
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public MapCodec<DualNoiseProvider> codec() {
|
| 53 | return CODEC;
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public BlockState getState(final LevelAccessor level, final RandomSource random, final BlockPos pos) {
|
| 58 | double varietyNoise = this.getSlowNoiseValue(pos);
|
| 59 | int localVariety = (int)Mth.clampedMap(varietyNoise, -1.0, 1.0, this.variety.minInclusive().intValue(), this.variety.maxInclusive() + 1);
|
| 60 | List<BlockState> possibleStates = Lists.newArrayListWithCapacity(localVariety);
|
| 61 |
|
| 62 | for (int i = 0; i < localVariety; i++) {
|
| 63 | possibleStates.add(this.getRandomState(this.states, this.getSlowNoiseValue(pos.offset(i * 54545, 0, i * 34234))));
|
| 64 | }
|
| 65 |
|
| 66 | return this.getRandomState(possibleStates, pos, this.scale);
|
| 67 | }
|
| 68 |
|
| 69 | protected float getSlowNoiseValue(final BlockPos pos) {
|
| 70 | return this.slowNoise.get(pos.getX() * this.slowScale, pos.getY() * this.slowScale, pos.getZ() * this.slowScale);
|
| 71 | }
|
| 72 | }
|
| 73 |
|