| 1 | package net.minecraft.world.level.levelgen.densityfunction.generator;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.util.Interval;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 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.op.BinaryFunction;
|
| 15 | import net.minecraft.world.level.levelgen.synth.Noise;
|
| 16 | import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
| 17 |
|
| 18 | public interface ShiftNoiseFunction extends DensityFunction {
|
| 19 | double COORDINATE_FACTOR = 0.25;
|
| 20 | float VALUE_FACTOR = 4.0F;
|
| 21 |
|
| 22 | Holder<NormalNoise> offsetNoise();
|
| 23 |
|
| 24 | @Override
|
| 25 | default Interval range() {
|
| 26 | return Interval.mul(this.offsetNoise().value().range(), Interval.ofExact(4.0F));
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | MapCodec<? extends ShiftNoiseFunction> codec();
|
| 31 |
|
| 32 | record Shift(Holder<NormalNoise> offsetNoise) implements ShiftNoiseFunction {
|
| 33 | public static final MapCodec<ShiftNoiseFunction.Shift> CODEC = RecordCodecBuilder.mapCodec(
|
| 34 | i -> i.group(NormalNoise.CODEC.fieldOf("noise").forGetter(ShiftNoiseFunction.Shift::offsetNoise)).apply(i, ShiftNoiseFunction.Shift::new)
|
| 35 | );
|
| 36 |
|
| 37 | @Override
|
| 38 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 39 | Noise noise = context.createNoiseSampler(this.offsetNoise);
|
| 40 | return new BinaryFunction.ConstMulSampler(new NoiseFunction.Sampler(noise, 0.25, 0.25), 4.0F);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 45 | return new ShiftNoiseFunction.Shift(this.offsetNoise);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public @DensityFunction.Axes int domainAxes() {
|
| 50 | return 7;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public MapCodec<ShiftNoiseFunction.Shift> codec() {
|
| 55 | return CODEC;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | record ShiftA(Holder<NormalNoise> offsetNoise) implements ShiftNoiseFunction {
|
| 60 | public static final MapCodec<ShiftNoiseFunction.ShiftA> CODEC = RecordCodecBuilder.mapCodec(
|
| 61 | i -> i.group(NormalNoise.CODEC.fieldOf("noise").forGetter(ShiftNoiseFunction.ShiftA::offsetNoise)).apply(i, ShiftNoiseFunction.ShiftA::new)
|
| 62 | );
|
| 63 |
|
| 64 | @Override
|
| 65 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 66 | Noise noise = context.createNoiseSampler(this.offsetNoise);
|
| 67 | return new BinaryFunction.ConstMulSampler(new NoiseFunction.Sampler(noise, 0.25, 0.0), 4.0F);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 72 | return new ShiftNoiseFunction.ShiftA(this.offsetNoise);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public @DensityFunction.Axes int domainAxes() {
|
| 77 | return 5;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public MapCodec<ShiftNoiseFunction.ShiftA> codec() {
|
| 82 | return CODEC;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | record ShiftB(Holder<NormalNoise> offsetNoise) implements ShiftNoiseFunction {
|
| 87 | public static final MapCodec<ShiftNoiseFunction.ShiftB> CODEC = RecordCodecBuilder.mapCodec(
|
| 88 | i -> i.group(NormalNoise.CODEC.fieldOf("noise").forGetter(ShiftNoiseFunction.ShiftB::offsetNoise)).apply(i, ShiftNoiseFunction.ShiftB::new)
|
| 89 | );
|
| 90 |
|
| 91 | @Override
|
| 92 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 93 | final Noise noise = context.createNoiseSampler(this.offsetNoise);
|
| 94 | return new DensitySampler() {
|
| 95 | @Override
|
| 96 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 97 | DensityVolume transposedVolume = new DensityVolume(
|
| 98 | volume.sizeZ(), volume.sizeX(), 1, volume.minBlockZ(), volume.minBlockX(), 0, volume.stepBlockZ(), volume.stepBlockX(), 1
|
| 99 | );
|
| 100 |
|
| 101 | try (ScopedDensityBuffer transposedBuffer = context.acquireBuffer(transposedVolume)) {
|
| 102 | transposedBuffer.fill(0.0F);
|
| 103 | noise.addToVolume(transposedBuffer, transposedVolume, 0.25, 0.25, 4.0F);
|
| 104 |
|
| 105 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 106 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 107 | float value = transposedBuffer.get(transposedVolume.indexUnchecked(z, x, 0));
|
| 108 | outputBuffer.setRange(volume.indexUnchecked(x, 0, z), volume.sizeY(), value);
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 116 | return noise.get(blockZ * 0.25, blockX * 0.25, 0.0) * 4.0F;
|
| 117 | }
|
| 118 | };
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 123 | return new ShiftNoiseFunction.ShiftB(this.offsetNoise);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public @DensityFunction.Axes int domainAxes() {
|
| 128 | return 5;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public MapCodec<ShiftNoiseFunction.ShiftB> codec() {
|
| 133 | return CODEC;
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 |
|