| 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.DensitySampler;
|
| 9 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 10 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 11 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.generator.ConstantFunction;
|
| 14 |
|
| 15 | public record PowFunction(DensityFunction base, DensityFunction exponent) implements DensityFunction {
|
| 16 | public static final MapCodec<PowFunction> CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(DensityFunction.CODEC.fieldOf("base").forGetter(PowFunction::base), DensityFunction.CODEC.fieldOf("exponent").forGetter(PowFunction::exponent))
|
| 18 | .apply(i, PowFunction::new)
|
| 19 | );
|
| 20 |
|
| 21 | @Override
|
| 22 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 23 | DensitySampler base = this.base.compileSampler(context);
|
| 24 | DensitySampler exponent = this.exponent.compileSampler(context);
|
| 25 | if (this.base instanceof ConstantFunction(float baseValue)) {
|
| 26 | return new PowFunction.ConstBaseSampler(baseValue, exponent);
|
| 27 | } else {
|
| 28 | return this.exponent instanceof ConstantFunction(float exponentValue) ? compileConstExponent(base, exponentValue) : new PowFunction.Sampler(base, exponent);
|
| 29 | }
|
| 30 | }
|
| 31 |
|
| 32 | private static DensitySampler compileConstExponent(final DensitySampler base, final float exponent) {
|
| 33 | float absExponent = Math.abs(exponent);
|
| 34 | DensitySampler specialSampler;
|
| 35 | if (absExponent == 0.5F) {
|
| 36 | specialSampler = new UnaryFunction.SqrtSampler(base);
|
| 37 | } else if (absExponent == 1.0F) {
|
| 38 | specialSampler = base;
|
| 39 | } else if (absExponent == 2.0F) {
|
| 40 | specialSampler = new UnaryFunction.SquareSampler(base);
|
| 41 | } else {
|
| 42 | if (absExponent != 3.0F) {
|
| 43 | return new PowFunction.ConstExponentSampler(base, exponent);
|
| 44 | }
|
| 45 |
|
| 46 | specialSampler = new UnaryFunction.CubeSampler(base);
|
| 47 | }
|
| 48 |
|
| 49 | return exponent >= 0.0F ? specialSampler : new UnaryFunction.ReciprocalSampler(specialSampler);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 54 | DensityFunction base = rule.rewrite(this.base);
|
| 55 | DensityFunction exponent = rule.rewrite(this.exponent);
|
| 56 | return base == this.base && exponent == this.exponent ? this : new PowFunction(base, exponent);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public Interval range() {
|
| 61 | return Interval.pow(this.base.range(), this.exponent.range());
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public @DensityFunction.Axes int domainAxes() {
|
| 66 | return this.base.domainAxes() | this.exponent.domainAxes();
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public MapCodec<PowFunction> codec() {
|
| 71 | return CODEC;
|
| 72 | }
|
| 73 |
|
| 74 | private record ConstBaseSampler(double base, DensitySampler exponent) implements DensitySampler {
|
| 75 | @Override
|
| 76 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 77 | this.exponent.sampleVolume(context, outputBuffer, volume);
|
| 78 |
|
| 79 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 80 | outputBuffer.set(i, (float)Math.pow(this.base, outputBuffer.get(i)));
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 86 | return (float)Math.pow(this.base, this.exponent.sampleValue(context, blockX, blockY, blockZ));
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private record ConstExponentSampler(DensitySampler base, double exponent) implements DensitySampler {
|
| 91 | @Override
|
| 92 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 93 | this.base.sampleVolume(context, outputBuffer, volume);
|
| 94 |
|
| 95 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 96 | outputBuffer.set(i, (float)Math.pow(outputBuffer.get(i), this.exponent));
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 102 | return (float)Math.pow(this.base.sampleValue(context, blockX, blockY, blockZ), this.exponent);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | private record Sampler(DensitySampler base, DensitySampler exponent) implements DensitySampler {
|
| 107 | @Override
|
| 108 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 109 | this.base.sampleVolume(context, outputBuffer, volume);
|
| 110 |
|
| 111 | try (ScopedDensityBuffer exponentBuffer = context.acquireBuffer(volume)) {
|
| 112 | this.exponent.sampleVolume(context, exponentBuffer, volume);
|
| 113 |
|
| 114 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 115 | float base = outputBuffer.get(i);
|
| 116 | float exponent = exponentBuffer.get(i);
|
| 117 | outputBuffer.set(i, (float)Math.pow(base, exponent));
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 124 | return (float)Math.pow(this.base.sampleValue(context, blockX, blockY, blockZ), this.exponent.sampleValue(context, blockX, blockY, blockZ));
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|