| 1 | package net.minecraft.world.level.levelgen.densityfunction.op;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.HashMap;
|
| 7 | import java.util.Map;
|
| 8 | import net.minecraft.util.BoundedFloatFunction;
|
| 9 | import net.minecraft.util.CubicSpline;
|
| 10 | import net.minecraft.util.Interval;
|
| 11 | import net.minecraft.util.Unit;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 15 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 16 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 17 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 18 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 19 | import org.apache.commons.lang3.mutable.MutableBoolean;
|
| 20 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public record SplineFunction(CubicSpline<SplineFunction.Coordinate> spline) implements DensityFunction {
|
| 24 | private static final Codec<CubicSpline<SplineFunction.Coordinate>> SPLINE_CODEC = CubicSpline.codec(SplineFunction.Coordinate.CODEC);
|
| 25 | public static final MapCodec<SplineFunction> CODEC = SPLINE_CODEC.fieldOf("spline").xmap(SplineFunction::new, SplineFunction::spline);
|
| 26 |
|
| 27 | @Override
|
| 28 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 29 | return new SplineFunction.Sampler(context, this.spline);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public Interval range() {
|
| 34 | return this.spline.range();
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public @DensityFunction.Axes int domainAxes() {
|
| 39 | MutableInt axes = new MutableInt(0);
|
| 40 | this.spline.forEachCoordinate(coordinate -> axes.setValue(axes.intValue() | coordinate.function().domainAxes()));
|
| 41 | return axes.intValue();
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 46 | MutableBoolean changed = new MutableBoolean();
|
| 47 | CubicSpline<SplineFunction.Coordinate> newSpline = this.spline.mapCoordinates(coordinate -> {
|
| 48 | DensityFunction function = coordinate.function();
|
| 49 | DensityFunction newFunction = rule.rewrite(function);
|
| 50 | if (newFunction != function) {
|
| 51 | changed.setTrue();
|
| 52 | return new SplineFunction.Coordinate(newFunction);
|
| 53 | } else {
|
| 54 | return coordinate;
|
| 55 | }
|
| 56 | });
|
| 57 | return changed.isFalse() ? this : new SplineFunction(newSpline);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public MapCodec<SplineFunction> codec() {
|
| 62 | return CODEC;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public String toString() {
|
| 67 | return this.spline.toString();
|
| 68 | }
|
| 69 |
|
| 70 | private static class BufferSplineInput implements SplineFunction.SplineInput, AutoCloseable {
|
| 71 | private final SamplerContext context;
|
| 72 | private final DensityVolume volume;
|
| 73 | private final @Nullable ScopedDensityBuffer[] buffers;
|
| 74 | private int index;
|
| 75 |
|
| 76 | private BufferSplineInput(final SamplerContext context, final DensityVolume volume, final int coordinateCount) {
|
| 77 | this.context = context;
|
| 78 | this.volume = volume;
|
| 79 | this.buffers = new ScopedDensityBuffer[coordinateCount];
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public float sampleCoordinate(final DensitySampler coordinateSampler, final int coordinateIndex) {
|
| 84 | ScopedDensityBuffer buffer = this.buffers[coordinateIndex];
|
| 85 | if (buffer == null) {
|
| 86 | buffer = this.context.acquireBuffer(this.volume);
|
| 87 | coordinateSampler.sampleVolume(this.context, buffer, this.volume);
|
| 88 | this.buffers[coordinateIndex] = buffer;
|
| 89 | }
|
| 90 |
|
| 91 | return buffer.get(this.index);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void close() {
|
| 96 | for (ScopedDensityBuffer buffer : this.buffers) {
|
| 97 | if (buffer != null) {
|
| 98 | buffer.close();
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | public record Coordinate(DensityFunction function) implements BoundedFloatFunction<Unit> {
|
| 105 | public static final Codec<SplineFunction.Coordinate> CODEC = DensityFunction.CODEC.xmap(SplineFunction.Coordinate::new, SplineFunction.Coordinate::function);
|
| 106 |
|
| 107 | public float apply(final Unit point) {
|
| 108 | return 0.0F;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public Interval range() {
|
| 113 | return this.function.range();
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | private static class PointSplineInput implements SplineFunction.SplineInput {
|
| 118 | private final SamplerContext context;
|
| 119 | private final int blockX;
|
| 120 | private final int blockY;
|
| 121 | private final int blockZ;
|
| 122 | private final float[] cachedValues;
|
| 123 |
|
| 124 | private PointSplineInput(final SamplerContext context, final int blockX, final int blockY, final int blockZ, final int coordinateCount) {
|
| 125 | this.context = context;
|
| 126 | this.blockX = blockX;
|
| 127 | this.blockY = blockY;
|
| 128 | this.blockZ = blockZ;
|
| 129 | this.cachedValues = new float[coordinateCount];
|
| 130 | Arrays.fill(this.cachedValues, Float.NaN);
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public float sampleCoordinate(final DensitySampler coordinateSampler, final int coordinateIndex) {
|
| 135 | float cachedValue = this.cachedValues[coordinateIndex];
|
| 136 | if (!Float.isNaN(cachedValue)) {
|
| 137 | return cachedValue;
|
| 138 | }
|
| 139 |
|
| 140 | float value = coordinateSampler.sampleValue(this.context, this.blockX, this.blockY, this.blockZ);
|
| 141 | this.cachedValues[coordinateIndex] = value;
|
| 142 | return value;
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | private static class Sampler implements DensitySampler {
|
| 147 | private final BoundedFloatFunction<SplineFunction.SplineInput> sampler;
|
| 148 | private final int coordinateCount;
|
| 149 |
|
| 150 | public Sampler(final DensityFunction.CompileContext context, final CubicSpline<SplineFunction.Coordinate> spline) {
|
| 151 | Map<DensityFunction, SplineFunction.SamplerCoordinate> coordinates = new HashMap<>();
|
| 152 | this.sampler = CubicSpline.asSampler(spline.mapCoordinates(coordinate -> coordinates.computeIfAbsent(coordinate.function(), function -> {
|
| 153 | int index = coordinates.size();
|
| 154 | DensitySampler sampler = function.compileSampler(context);
|
| 155 | return new SplineFunction.SamplerCoordinate(sampler, index, function.range());
|
| 156 | })));
|
| 157 | this.coordinateCount = coordinates.size();
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 162 | try (SplineFunction.BufferSplineInput input = new SplineFunction.BufferSplineInput(context, volume, this.coordinateCount)) {
|
| 163 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 164 | input.index = i;
|
| 165 | outputBuffer.set(i, this.sampler.apply(input));
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 172 | return this.sampler.apply(new SplineFunction.PointSplineInput(context, blockX, blockY, blockZ, this.coordinateCount));
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private record SamplerCoordinate(DensitySampler sampler, int index, Interval range) implements BoundedFloatFunction<SplineFunction.SplineInput> {
|
| 177 | public float apply(final SplineFunction.SplineInput input) {
|
| 178 | return input.sampleCoordinate(this.sampler, this.index);
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | @FunctionalInterface
|
| 183 | private interface SplineInput {
|
| 184 | float sampleCoordinate(DensitySampler coordinateSampler, int coordinateIndex);
|
| 185 | }
|
| 186 | }
|
| 187 |
|