| 1 | package net.minecraft.world.level.levelgen.heightproviders;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.levelgen.VerticalAnchor;
|
| 10 | import net.minecraft.world.level.levelgen.WorldGenerationContext;
|
| 11 | import org.slf4j.Logger;
|
| 12 |
|
| 13 | public class TrapezoidHeight extends HeightProvider {
|
| 14 | public static final MapCodec<TrapezoidHeight> CODEC = RecordCodecBuilder.mapCodec(
|
| 15 | i -> i.group(
|
| 16 | VerticalAnchor.CODEC.fieldOf("min_inclusive").forGetter(u -> u.minInclusive),
|
| 17 | VerticalAnchor.CODEC.fieldOf("max_inclusive").forGetter(u -> u.maxInclusive),
|
| 18 | Codec.INT.optionalFieldOf("plateau", 0).forGetter(u -> u.plateau)
|
| 19 | )
|
| 20 | .apply(i, TrapezoidHeight::new)
|
| 21 | );
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private final VerticalAnchor minInclusive;
|
| 24 | private final VerticalAnchor maxInclusive;
|
| 25 | private final int plateau;
|
| 26 |
|
| 27 | private TrapezoidHeight(final VerticalAnchor minInclusive, final VerticalAnchor maxInclusive, final int plateau) {
|
| 28 | this.minInclusive = minInclusive;
|
| 29 | this.maxInclusive = maxInclusive;
|
| 30 | this.plateau = plateau;
|
| 31 | }
|
| 32 |
|
| 33 | public static TrapezoidHeight of(final VerticalAnchor minInclusive, final VerticalAnchor maxInclusive, final int plateau) {
|
| 34 | return new TrapezoidHeight(minInclusive, maxInclusive, plateau);
|
| 35 | }
|
| 36 |
|
| 37 | public static TrapezoidHeight of(final VerticalAnchor minInclusive, final VerticalAnchor maxInclusive) {
|
| 38 | return of(minInclusive, maxInclusive, 0);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public int sample(final RandomSource random, final WorldGenerationContext context) {
|
| 43 | int min = this.minInclusive.resolveY(context);
|
| 44 | int max = this.maxInclusive.resolveY(context);
|
| 45 | if (min > max) {
|
| 46 | LOGGER.warn("Empty height range: {}", this);
|
| 47 | return min;
|
| 48 | }
|
| 49 |
|
| 50 | int range = max - min;
|
| 51 | if (this.plateau >= range) {
|
| 52 | return Mth.randomBetweenInclusive(random, min, max);
|
| 53 | }
|
| 54 |
|
| 55 | int plateauStart = (range - this.plateau) / 2;
|
| 56 | int plateauEnd = range - plateauStart;
|
| 57 | return min + Mth.randomBetweenInclusive(random, 0, plateauEnd) + Mth.randomBetweenInclusive(random, 0, plateauStart);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public HeightProviderType<?> getType() {
|
| 62 | return HeightProviderType.TRAPEZOID;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public String toString() {
|
| 67 | return this.plateau == 0
|
| 68 | ? "triangle (" + this.minInclusive + "-" + this.maxInclusive + ")"
|
| 69 | : "trapezoid(" + this.plateau + ") in [" + this.minInclusive + "-" + this.maxInclusive + "]";
|
| 70 | }
|
| 71 | }
|
| 72 |
|