| 1 | package net.minecraft.world.level.levelgen.densityfunction.op;
|
| 2 |
|
| 3 | import com.google.common.collect.Comparators;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import it.unimi.dsi.fastutil.floats.FloatArrayList;
|
| 10 | import it.unimi.dsi.fastutil.floats.FloatList;
|
| 11 | import java.util.List;
|
| 12 | import java.util.function.Function;
|
| 13 | import net.minecraft.util.Interval;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 15 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 16 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunctions;
|
| 17 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 18 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 19 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 20 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 21 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 22 | import org.apache.commons.lang3.mutable.MutableBoolean;
|
| 23 |
|
| 24 | public record IntervalSelectFunction(DensityFunction input, FloatList thresholds, List<DensityFunction> functions) implements DensityFunction {
|
| 25 | private static final Codec<FloatList> THRESHOLDS_CODEC = DensityFunctions.NOISE_VALUE_CODEC.listOf().xmap(FloatArrayList::new, Function.identity());
|
| 26 | public static final MapCodec<IntervalSelectFunction> CODEC = RecordCodecBuilder.<IntervalSelectFunction>mapCodec(
|
| 27 | i -> i.group(
|
| 28 | DensityFunction.CODEC.fieldOf("input").forGetter(IntervalSelectFunction::input),
|
| 29 | THRESHOLDS_CODEC.fieldOf("thresholds").forGetter(IntervalSelectFunction::thresholds),
|
| 30 | DensityFunction.CODEC.listOf(2, Integer.MAX_VALUE).fieldOf("functions").forGetter(IntervalSelectFunction::functions)
|
| 31 | )
|
| 32 | .apply(i, IntervalSelectFunction::new)
|
| 33 | )
|
| 34 | .validate(IntervalSelectFunction::validate);
|
| 35 |
|
| 36 | private DataResult<IntervalSelectFunction> validate() {
|
| 37 | if (this.thresholds.size() != this.functions.size() - 1) {
|
| 38 | return DataResult.error(
|
| 39 | () -> "Expected " + (this.functions.size() - 1) + " thresholds for " + this.functions.size() + " functions, but got " + this.thresholds.size()
|
| 40 | );
|
| 41 | } else {
|
| 42 | return !Comparators.isInOrder(this.thresholds, Float::compare)
|
| 43 | ? DataResult.error(() -> "Threshold values must be ordered from smallest to largest")
|
| 44 | : DataResult.success(this);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 50 | DensitySampler input = this.input.compileSampler(context);
|
| 51 | if (this.thresholds.size() == 1) {
|
| 52 | float threshold = this.thresholds.getFloat(0);
|
| 53 | DensitySampler ifBelow = this.functions.getFirst().compileSampler(context);
|
| 54 | DensitySampler ifAbove = this.functions.getLast().compileSampler(context);
|
| 55 | return new IntervalSelectFunction.SingleThresholdSampler(input, threshold, ifBelow, ifAbove);
|
| 56 | } else {
|
| 57 | return new IntervalSelectFunction.Sampler(
|
| 58 | input, this.thresholds.toFloatArray(), this.functions.stream().map(function -> function.compileSampler(context)).toArray(DensitySampler[]::new)
|
| 59 | );
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 65 | DensityFunction input = rule.rewrite(this.input);
|
| 66 | MutableBoolean functionsChanged = new MutableBoolean();
|
| 67 | List<DensityFunction> functions = this.functions.stream().map(function -> {
|
| 68 | DensityFunction newFunction = rule.rewrite(function);
|
| 69 | if (newFunction != function) {
|
| 70 | functionsChanged.setTrue();
|
| 71 | }
|
| 72 |
|
| 73 | return newFunction;
|
| 74 | }).toList();
|
| 75 | return input == this.input && !functionsChanged.booleanValue() ? this : new IntervalSelectFunction(input, this.thresholds, functions);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public Interval range() {
|
| 80 | return Interval.encapsulating(Lists.transform(this.functions, DensityFunction::range));
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public @DensityFunction.Axes int domainAxes() {
|
| 85 | int axes = this.input.domainAxes();
|
| 86 |
|
| 87 | for (DensityFunction function : this.functions) {
|
| 88 | axes |= function.domainAxes();
|
| 89 | }
|
| 90 |
|
| 91 | return axes;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public MapCodec<IntervalSelectFunction> codec() {
|
| 96 | return CODEC;
|
| 97 | }
|
| 98 |
|
| 99 | private record Sampler(DensitySampler input, float[] thresholds, DensitySampler[] samplers) implements DensitySampler {
|
| 100 | @Override
|
| 101 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 102 | this.input.sampleVolume(context, outputBuffer, volume);
|
| 103 | ScopedDensityBuffer[] buffers = new ScopedDensityBuffer[this.samplers.length];
|
| 104 |
|
| 105 | try {
|
| 106 | for (int i = 0; i < this.samplers.length; i++) {
|
| 107 | ScopedDensityBuffer buffer = context.acquireBuffer(volume);
|
| 108 | buffers[i] = buffer;
|
| 109 | this.samplers[i].sampleVolume(context, buffer, volume);
|
| 110 | }
|
| 111 |
|
| 112 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 113 | int samplerIndex = this.selectSamplerIndex(outputBuffer.get(i));
|
| 114 | outputBuffer.set(i, buffers[samplerIndex].get(i));
|
| 115 | }
|
| 116 | } finally {
|
| 117 | ScopedDensityBuffer[] var10 = buffers;
|
| 118 | int var11 = var10.length;
|
| 119 | int var12 = 0;
|
| 120 |
|
| 121 | while (true) {
|
| 122 | if (var12 >= var11) {
|
| 123 | ;
|
| 124 | } else {
|
| 125 | ScopedDensityBuffer buffer = var10[var12];
|
| 126 | if (buffer != null) {
|
| 127 | buffer.close();
|
| 128 | }
|
| 129 |
|
| 130 | var12++;
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 138 | float input = this.input.sampleValue(context, blockX, blockY, blockZ);
|
| 139 | return this.samplers[this.selectSamplerIndex(input)].sampleValue(context, blockX, blockY, blockZ);
|
| 140 | }
|
| 141 |
|
| 142 | private int selectSamplerIndex(final float input) {
|
| 143 | for (int i = 0; i < this.thresholds.length; i++) {
|
| 144 | if (input < this.thresholds[i]) {
|
| 145 | return i;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | return this.samplers.length - 1;
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private record SingleThresholdSampler(DensitySampler input, float threshold, DensitySampler ifBelow, DensitySampler ifAbove) implements DensitySampler {
|
| 154 | @Override
|
| 155 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 156 | this.input.sampleVolume(context, outputBuffer, volume);
|
| 157 |
|
| 158 | try (ScopedDensityBuffer ifBelowBuffer = context.acquireBuffer(volume)) {
|
| 159 | this.ifBelow.sampleVolume(context, ifBelowBuffer, volume);
|
| 160 |
|
| 161 | try (ScopedDensityBuffer ifAboveBuffer = context.acquireBuffer(volume)) {
|
| 162 | this.ifAbove.sampleVolume(context, ifAboveBuffer, volume);
|
| 163 |
|
| 164 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 165 | float input = outputBuffer.get(i);
|
| 166 | outputBuffer.set(i, input < this.threshold ? ifBelowBuffer.get(i) : ifAboveBuffer.get(i));
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 174 | float input = this.input.sampleValue(context, blockX, blockY, blockZ);
|
| 175 | return input < this.threshold ? this.ifBelow.sampleValue(context, blockX, blockY, blockZ) : this.ifAbove.sampleValue(context, blockX, blockY, blockZ);
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 |
|