| 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.Vec3i;
|
| 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.DistanceMetric;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 14 |
|
| 15 | public record DistanceToPointFunction(Vec3i point, DistanceMetric metric) implements DensityFunction {
|
| 16 | public static final MapCodec<DistanceToPointFunction> CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(
|
| 18 | Vec3i.CODEC.fieldOf("point").forGetter(DistanceToPointFunction::point), DistanceMetric.CODEC.fieldOf("metric").forGetter(DistanceToPointFunction::metric)
|
| 19 | )
|
| 20 | .apply(i, DistanceToPointFunction::new)
|
| 21 | );
|
| 22 |
|
| 23 | @Override
|
| 24 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 25 | return new DistanceToPointFunction.Sampler(this.point.getX(), this.point.getY(), this.point.getZ(), this.metric);
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public Interval range() {
|
| 30 | return Interval.of(0.0F, Float.POSITIVE_INFINITY);
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public @DensityFunction.Axes int domainAxes() {
|
| 35 | return 7;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public MapCodec<DistanceToPointFunction> codec() {
|
| 40 | return CODEC;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 45 | return this;
|
| 46 | }
|
| 47 |
|
| 48 | private record Sampler(int x, int y, int z, DistanceMetric metric) implements DensitySampler {
|
| 49 | @Override
|
| 50 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 51 | int index = 0;
|
| 52 |
|
| 53 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 54 | int blockZ = volume.blockZ(z);
|
| 55 |
|
| 56 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 57 | int blockX = volume.blockX(x);
|
| 58 |
|
| 59 | for (int y = 0; y < volume.sizeY(); y++) {
|
| 60 | int blockY = volume.blockY(y);
|
| 61 | outputBuffer.set(index++, this.sampleValue(context, blockX, blockY, blockZ));
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 69 | return this.metric.compute(this.x - blockX, this.y - blockY, this.z - blockZ);
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|