FindTopSurfaceFunction .java

net.minecraft.world.level.levelgen.densityfunction.op.FindTopSurfaceFunction (record): members: density, upperBound, lowerBound, cellHeight, Sampler.

net.minecraft.world.level.levelgen.densityfunction.oprecord102 linescommon4.1 KB

Implements

API Surface (6)

DensitySampler compileSampler(final DensityFunction.CompileContext context)
DensityFunction rewriteChildren(final DfRewriteRule rule)
Interval range()
MapCodec<FindTopSurfaceFunction> codec()
void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume)
float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ)

Fields & Constants (1)

CODEC

String literals (5)

"density"
"upper_bound"
"lower_bound"
"cell_height"
"Cannot sample with sizeY="

Full Source

FindTopSurfaceFunction.java102 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.densityfunction.op;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.MapCodec;
5
import com.mojang.serialization.codecs.RecordCodecBuilder;
6
import net.minecraft.util.ExtraCodecs;
7
import net.minecraft.util.Interval;
8
import net.minecraft.util.Mth;
9
import net.minecraft.world.level.dimension.DimensionType;
10
import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
11
import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
12
import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
13
import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
14
import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
15
import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
16
17
public record FindTopSurfaceFunction(DensityFunction density, DensityFunction upperBound, int lowerBound, int cellHeight) implements DensityFunction {
18
	public static final MapCodec<FindTopSurfaceFunction> CODEC = RecordCodecBuilder.mapCodec(
19
		i -> i.group(
20
				DensityFunction.CODEC.fieldOf("density").forGetter(FindTopSurfaceFunction::density),
21
				DensityFunction.CODEC.fieldOf("upper_bound").forGetter(FindTopSurfaceFunction::upperBound),
22
				Codec.intRange(DimensionType.MIN_Y * 2, DimensionType.MAX_Y * 2).fieldOf("lower_bound").forGetter(FindTopSurfaceFunction::lowerBound),
23
				ExtraCodecs.POSITIVE_INT.fieldOf("cell_height").forGetter(FindTopSurfaceFunction::cellHeight)
24
			)
25
			.apply(i, FindTopSurfaceFunction::new)
26
	);
27
28
	@Override
29
	public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
30
		FindTopSurfaceFunction.Sampler sampler = new FindTopSurfaceFunction.Sampler(
31
			this.density.compileSampler(context), this.upperBound.compileSampler(context), this.lowerBound, this.cellHeight
32
		);
33
		return new SliceFunction.YSampler(sampler, 0);
34
	}
35
36
	@Override
37
	public DensityFunction rewriteChildren(final DfRewriteRule rule) {
38
		DensityFunction density = rule.rewrite(this.density);
39
		DensityFunction upperBound = rule.rewrite(this.upperBound);
40
		return density == this.density && upperBound == this.upperBound ? this : new FindTopSurfaceFunction(density, upperBound, this.lowerBound, this.cellHeight);
41
	}
42
43
	@Override
44
	public Interval range() {
45
		return Interval.of(this.lowerBound, Math.max(this.lowerBound, this.upperBound.range().max()));
46
	}
47
48
	@Override
49
	public @DensityFunction.Axes int domainAxes() {
50
		return (this.density.domainAxes() | this.upperBound.domainAxes()) & -3;
51
	}
52
53
	@Override
54
	public MapCodec<FindTopSurfaceFunction> codec() {
55
		return CODEC;
56
	}
57
58
	private record Sampler(DensitySampler density, DensitySampler upperBound, int lowerBound, int cellHeight) implements DensitySampler {
59
		@Override
60
		public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
61
			if (volume.sizeY() != 1) {
62
				throw new IllegalArgumentException("Cannot sample with sizeY=" + volume.sizeY());
63
			}
64
65
			this.upperBound.sampleVolume(context, outputBuffer, volume);
66
			int index = 0;
67
68
			for (int z = 0; z < volume.sizeZ(); z++) {
69
				int blockZ = volume.blockZ(z);
70
71
				for (int x = 0; x < volume.sizeX(); x++) {
72
					int blockX = volume.blockX(x);
73
					float upperBound = outputBuffer.get(index);
74
					outputBuffer.set(index, this.findSurfaceFrom(context, blockX, blockZ, upperBound));
75
					index++;
76
				}
77
			}
78
		}
79
80
		@Override
81
		public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
82
			float upperBound = this.upperBound.sampleValue(context, blockX, blockY, blockZ);
83
			return this.findSurfaceFrom(context, blockX, blockZ, upperBound);
84
		}
85
86
		private int findSurfaceFrom(final SamplerContext context, final int x, final int z, final float upperBound) {
87
			int topY = Mth.floor(upperBound / this.cellHeight) * this.cellHeight;
88
			if (topY <= this.lowerBound) {
89
				return this.lowerBound;
90
			}
91
92
			for (int probeY = topY; probeY >= this.lowerBound; probeY -= this.cellHeight) {
93
				if (this.density.sampleValue(context, x, probeY, z) > 0.0F) {
94
					return probeY;
95
				}
96
			}
97
98
			return this.lowerBound;
99
		}
100
	}
101
}
102

Decompiled Minecraft 26.3 source of FindTopSurfaceFunction (net/minecraft/world/level/levelgen/densityfunction/op/FindTopSurfaceFunction.java) with Mojang official mappings, stored verbatim on the site.