| 1 | package net.minecraft.world.level.levelgen.densityfunction.op;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.util.Interval;
|
| 6 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunctions;
|
| 9 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 10 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 11 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.generator.ConstantFunction;
|
| 15 |
|
| 16 | public record RangeChoiceFunction(DensityFunction input, float minInclusive, float maxExclusive, DensityFunction whenInRange, DensityFunction whenOutOfRange)
|
| 17 | implements DensityFunction {
|
| 18 | public static final MapCodec<RangeChoiceFunction> CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(
|
| 20 | DensityFunction.CODEC.fieldOf("input").forGetter(RangeChoiceFunction::input),
|
| 21 | DensityFunctions.NOISE_VALUE_CODEC.fieldOf("min_inclusive").forGetter(RangeChoiceFunction::minInclusive),
|
| 22 | DensityFunctions.NOISE_VALUE_CODEC.fieldOf("max_exclusive").forGetter(RangeChoiceFunction::maxExclusive),
|
| 23 | DensityFunction.CODEC.fieldOf("when_in_range").forGetter(RangeChoiceFunction::whenInRange),
|
| 24 | DensityFunction.CODEC.fieldOf("when_out_of_range").forGetter(RangeChoiceFunction::whenOutOfRange)
|
| 25 | )
|
| 26 | .apply(i, RangeChoiceFunction::new)
|
| 27 | );
|
| 28 |
|
| 29 | @Override
|
| 30 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 31 | DensitySampler input = this.input.compileSampler(context);
|
| 32 | return this.whenInRange instanceof ConstantFunction(float whenInRangeValue) && this.whenOutOfRange instanceof ConstantFunction(float whenOutOfRangeValue)
|
| 33 | ? new RangeChoiceFunction.ConstSampler(input, this.minInclusive, this.maxExclusive, whenInRangeValue, whenOutOfRangeValue)
|
| 34 | : new RangeChoiceFunction.Sampler(
|
| 35 | input, this.minInclusive, this.maxExclusive, this.whenInRange.compileSampler(context), this.whenOutOfRange.compileSampler(context)
|
| 36 | );
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 41 | DensityFunction input = rule.rewrite(this.input);
|
| 42 | DensityFunction whenInRange = rule.rewrite(this.whenInRange);
|
| 43 | DensityFunction whenOutOfRange = rule.rewrite(this.whenOutOfRange);
|
| 44 | return input == this.input && whenInRange == this.whenInRange && whenOutOfRange == this.whenOutOfRange
|
| 45 | ? this
|
| 46 | : new RangeChoiceFunction(input, this.minInclusive, this.maxExclusive, whenInRange, whenOutOfRange);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public Interval range() {
|
| 51 | return Interval.encapsulating(this.whenInRange.range(), this.whenOutOfRange.range());
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public @DensityFunction.Axes int domainAxes() {
|
| 56 | return this.input.domainAxes() | this.whenInRange.domainAxes() | this.whenOutOfRange.domainAxes();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public MapCodec<RangeChoiceFunction> codec() {
|
| 61 | return CODEC;
|
| 62 | }
|
| 63 |
|
| 64 | private record ConstSampler(DensitySampler input, float minInclusive, float maxExclusive, float whenInRange, float whenOutOfRange) implements DensitySampler {
|
| 65 | private float choose(final float input) {
|
| 66 | return input >= this.minInclusive && input < this.maxExclusive ? this.whenInRange : this.whenOutOfRange;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 71 | this.input.sampleVolume(context, outputBuffer, volume);
|
| 72 |
|
| 73 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 74 | outputBuffer.set(i, this.choose(outputBuffer.get(i)));
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 80 | return this.choose(this.input.sampleValue(context, blockX, blockY, blockZ));
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | private record Sampler(DensitySampler input, float minInclusive, float maxExclusive, DensitySampler whenInRange, DensitySampler whenOutOfRange)
|
| 85 | implements DensitySampler {
|
| 86 | @Override
|
| 87 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 88 | this.whenInRange.sampleVolume(context, outputBuffer, volume);
|
| 89 |
|
| 90 | try (ScopedDensityBuffer inputBuffer = context.acquireBuffer(volume)) {
|
| 91 | this.input.sampleVolume(context, inputBuffer, volume);
|
| 92 |
|
| 93 | try (ScopedDensityBuffer whenOutOfRangeBuffer = context.acquireBuffer(volume)) {
|
| 94 | this.whenOutOfRange.sampleVolume(context, whenOutOfRangeBuffer, volume);
|
| 95 |
|
| 96 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 97 | float input = inputBuffer.get(i);
|
| 98 | if (!(input >= this.minInclusive) || !(input < this.maxExclusive)) {
|
| 99 | outputBuffer.set(i, whenOutOfRangeBuffer.get(i));
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 108 | float inputValue = this.input.sampleValue(context, blockX, blockY, blockZ);
|
| 109 | return inputValue >= this.minInclusive && inputValue < this.maxExclusive
|
| 110 | ? this.whenInRange.sampleValue(context, blockX, blockY, blockZ)
|
| 111 | : this.whenOutOfRange.sampleValue(context, blockX, blockY, blockZ);
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|