| 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 RoundFunction(RoundFunction.Type type, DensityFunction input, DensityFunction multiple) implements DensityFunction {
|
| 17 | private static float roundToInteger(final float input, final RoundFunction.Type type) {
|
| 18 | return switch (type) {
|
| 19 | case FLOOR -> (float)Math.floor(input);
|
| 20 | case ROUND -> Math.round(input);
|
| 21 | case CEIL -> (float)Math.ceil(input);
|
| 22 | case TRUNCATE -> input > 0.0F ? (float)Math.floor(input) : (float)Math.ceil(input);
|
| 23 | };
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 28 | DensitySampler input = this.input.compileSampler(context);
|
| 29 | return this.multiple instanceof ConstantFunction(float multipleValue) && multipleValue == 1.0F
|
| 30 | ? new RoundFunction.IntegerMultipleSampler(this.type, input)
|
| 31 | : new RoundFunction.Sampler(this.type, input, this.multiple.compileSampler(context));
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 36 | DensityFunction input = rule.rewrite(this.input);
|
| 37 | DensityFunction multiple = rule.rewrite(this.multiple);
|
| 38 | return input == this.input && multiple == this.multiple ? this : new RoundFunction(this.type, input, multiple);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public Interval range() {
|
| 43 | Interval multipleRange = this.multiple.range();
|
| 44 | return Interval.mul(Interval.mapMonotonic(Interval.div(this.input.range(), multipleRange), value -> roundToInteger(value, this.type)), multipleRange);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public @DensityFunction.Axes int domainAxes() {
|
| 49 | return this.input.domainAxes() | this.multiple.domainAxes();
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public MapCodec<RoundFunction> codec() {
|
| 54 | return this.type.codec;
|
| 55 | }
|
| 56 |
|
| 57 | private record IntegerMultipleSampler(RoundFunction.Type type, DensitySampler input) implements DensitySampler {
|
| 58 | @Override
|
| 59 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 60 | this.input.sampleVolume(context, outputBuffer, volume);
|
| 61 |
|
| 62 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 63 | outputBuffer.set(i, RoundFunction.roundToInteger(outputBuffer.get(i), this.type));
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 69 | float input = this.input.sampleValue(context, blockX, blockY, blockZ);
|
| 70 | return RoundFunction.roundToInteger(input, this.type);
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | private record Sampler(RoundFunction.Type type, DensitySampler input, DensitySampler multiple) implements DensitySampler {
|
| 75 | @Override
|
| 76 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 77 | this.input.sampleVolume(context, outputBuffer, volume);
|
| 78 |
|
| 79 | try (ScopedDensityBuffer multipleBuffer = context.acquireBuffer(volume)) {
|
| 80 | this.multiple.sampleVolume(context, multipleBuffer, volume);
|
| 81 |
|
| 82 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 83 | float input = outputBuffer.get(i);
|
| 84 | float multiple = multipleBuffer.get(i);
|
| 85 | outputBuffer.set(i, this.apply(input, multiple));
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 92 | float input = this.input.sampleValue(context, blockX, blockY, blockZ);
|
| 93 | float multiple = this.multiple.sampleValue(context, blockX, blockY, blockZ);
|
| 94 | return this.apply(input, multiple);
|
| 95 | }
|
| 96 |
|
| 97 | private float apply(final float input, final float multiple) {
|
| 98 | return multiple == 0.0F ? input : RoundFunction.roundToInteger(input / multiple, this.type) * multiple;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | public enum Type {
|
| 103 | FLOOR("floor"),
|
| 104 | ROUND("round"),
|
| 105 | CEIL("ceil"),
|
| 106 | TRUNCATE("truncate");
|
| 107 |
|
| 108 | public final String id;
|
| 109 | public final MapCodec<RoundFunction> codec = RecordCodecBuilder.mapCodec(
|
| 110 | i -> i.group(
|
| 111 | DensityFunction.CODEC.fieldOf("input").forGetter(RoundFunction::input),
|
| 112 | DensityFunction.CODEC.optionalFieldOf("multiple", DensityFunctions.constant(1.0F)).forGetter(RoundFunction::multiple)
|
| 113 | )
|
| 114 | .apply(i, (input, multiple) -> new RoundFunction(this, input, multiple))
|
| 115 | );
|
| 116 |
|
| 117 | Type(final String id) {
|
| 118 | this.id = id;
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|