BlendingData .java

net.minecraft.world.level.levelgen.blending.BlendingData (class): members: CELL_HORIZONTAL_MAX_INDEX_OUTSIDE, CELL_HORIZONTAL_MAX_INDEX_INSIDE, heights.

net.minecraft.world.level.levelgen.blendingclass426 linescommon16.1 KB

API Surface (9)

BlendingData.Packed pack()
Set<CompositeDirection.Direction8> sideByGenerationAge(final WorldGenLevel region, final int chunkX, final int chunkZ, final boolean wantedOldGen)
float getHeight(final int cellX, final int cellY, final int cellZ)
float getDensity(final int cellX, final int cellY, final int cellZ)
void iterateBiomes(final int minCellX, final int quartY, final int minCellZ, final BlendingData.BiomeConsumer biomeConsumer)
void iterateHeights(final int minCellX, final int minCellZ, final BlendingData.HeightConsumer heightConsumer)
void iterateDensities(final int minCellX, final int minCellZ, final int fromCellY, final int toCellY, final BlendingData.DensityConsumer densityConsumer)
LevelHeightAccessor getAreaWithOldGeneration()
record Packed(int minSection, int maxSection, Optional<float[]> heights)

Fields & Constants (22)

BLENDING_DENSITY_FACTOR
CELL_WIDTH
CELL_HEIGHT
CELL_RATIO
SOLID_DENSITY
AIR_DENSITY
CELLS_PER_SECTION_Y
QUARTS_PER_SECTION
CELL_HORIZONTAL_MAX_INDEX_INSIDE
CELL_HORIZONTAL_MAX_INDEX_OUTSIDE
CELL_COLUMN_INSIDE_COUNT
CELL_COLUMN_OUTSIDE_COUNT
CELL_COLUMN_COUNT
SURFACE_BLOCKS
NO_VALUE
areaWithOldGeneration
hasCalculatedData
heights
biomes
densities
chunkZ
toCellY

String literals (4)

"min_section"
"max_section"
"heights"
"heights has to be of length "

Same Package (2)

Full Source

BlendingData.java426 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.blending;
2
3
import com.google.common.primitives.Floats;
4
import com.mojang.serialization.Codec;
5
import com.mojang.serialization.DataResult;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import it.unimi.dsi.fastutil.floats.FloatArrays;
8
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
9
import java.util.Arrays;
10
import java.util.EnumSet;
11
import java.util.List;
12
import java.util.Optional;
13
import java.util.Set;
14
import net.minecraft.core.BlockPos;
15
import net.minecraft.core.CompositeDirection;
16
import net.minecraft.core.Direction;
17
import net.minecraft.core.Holder;
18
import net.minecraft.core.QuartPos;
19
import net.minecraft.core.SectionPos;
20
import net.minecraft.server.level.WorldGenRegion;
21
import net.minecraft.tags.BlockTags;
22
import net.minecraft.util.Mth;
23
import net.minecraft.util.Util;
24
import net.minecraft.world.level.LevelHeightAccessor;
25
import net.minecraft.world.level.WorldGenLevel;
26
import net.minecraft.world.level.biome.Biome;
27
import net.minecraft.world.level.block.Block;
28
import net.minecraft.world.level.block.Blocks;
29
import net.minecraft.world.level.block.state.BlockState;
30
import net.minecraft.world.level.chunk.ChunkAccess;
31
import net.minecraft.world.level.chunk.status.ChunkStatus;
32
import net.minecraft.world.level.levelgen.Heightmap;
33
import org.jspecify.annotations.Nullable;
34
35
public class BlendingData {
36
	private static final float BLENDING_DENSITY_FACTOR = 0.1F;
37
	protected static final int CELL_WIDTH = 4;
38
	protected static final int CELL_HEIGHT = 8;
39
	protected static final int CELL_RATIO = 2;
40
	private static final float SOLID_DENSITY = 1.0F;
41
	private static final float AIR_DENSITY = -1.0F;
42
	private static final int CELLS_PER_SECTION_Y = 2;
43
	private static final int QUARTS_PER_SECTION = QuartPos.fromBlock(16);
44
	private static final int CELL_HORIZONTAL_MAX_INDEX_INSIDE = QUARTS_PER_SECTION - 1;
45
	private static final int CELL_HORIZONTAL_MAX_INDEX_OUTSIDE = QUARTS_PER_SECTION;
46
	private static final int CELL_COLUMN_INSIDE_COUNT = 2 * CELL_HORIZONTAL_MAX_INDEX_INSIDE + 1;
47
	private static final int CELL_COLUMN_OUTSIDE_COUNT = 2 * CELL_HORIZONTAL_MAX_INDEX_OUTSIDE + 1;
48
	private static final int CELL_COLUMN_COUNT = CELL_COLUMN_INSIDE_COUNT + CELL_COLUMN_OUTSIDE_COUNT;
49
	private final LevelHeightAccessor areaWithOldGeneration;
50
	private static final List<Block> SURFACE_BLOCKS = List.of(
51
		Blocks.PODZOL,
52
		Blocks.GRAVEL,
53
		Blocks.GRASS_BLOCK,
54
		Blocks.STONE,
55
		Blocks.COARSE_DIRT,
56
		Blocks.SAND,
57
		Blocks.RED_SAND,
58
		Blocks.MYCELIUM,
59
		Blocks.SNOW_BLOCK,
60
		Blocks.TERRACOTTA,
61
		Blocks.DIRT
62
	);
63
	protected static final float NO_VALUE = Float.MAX_VALUE;
64
	private boolean hasCalculatedData;
65
	private final float[] heights;
66
	private final List<@Nullable List<@Nullable Holder<Biome>>> biomes;
67
	private final transient float[][] densities;
68
69
	private BlendingData(final int minSection, final int maxSection, final Optional<float[]> heights) {
70
		this.heights = heights.orElseGet(() -> Util.make(new float[CELL_COLUMN_COUNT], i -> Arrays.fill(i, Float.MAX_VALUE)));
71
		this.densities = new float[CELL_COLUMN_COUNT][];
72
		ObjectArrayList<List<Holder<Biome>>> biomes = new ObjectArrayList<>(CELL_COLUMN_COUNT);
73
		biomes.size(CELL_COLUMN_COUNT);
74
		this.biomes = biomes;
75
		int minY = SectionPos.sectionToBlockCoord(minSection);
76
		int height = SectionPos.sectionToBlockCoord(maxSection) - minY;
77
		this.areaWithOldGeneration = LevelHeightAccessor.create(minY, height);
78
	}
79
80
	public static @Nullable BlendingData unpack(final BlendingData.@Nullable Packed packed) {
81
		return packed == null ? null : new BlendingData(packed.minSection(), packed.maxSection(), packed.heights());
82
	}
83
84
	public BlendingData.Packed pack() {
85
		boolean hasHeight = false;
86
87
		for (float height : this.heights) {
88
			if (height != Float.MAX_VALUE) {
89
				hasHeight = true;
90
				break;
91
			}
92
		}
93
94
		return new BlendingData.Packed(
95
			this.areaWithOldGeneration.getMinSectionY(),
96
			this.areaWithOldGeneration.getMaxSectionY() + 1,
97
			hasHeight ? Optional.of(FloatArrays.copy(this.heights)) : Optional.empty()
98
		);
99
	}
100
101
	public static @Nullable BlendingData getOrUpdateBlendingData(final WorldGenRegion region, final int chunkX, final int chunkZ) {
102
		ChunkAccess chunk = region.getChunk(chunkX, chunkZ);
103
		BlendingData blendingData = chunk.getBlendingData();
104
		if (blendingData != null && !chunk.getHighestGeneratedStatus().isBefore(ChunkStatus.BIOMES)) {
105
			blendingData.calculateData(chunk, sideByGenerationAge(region, chunkX, chunkZ, false));
106
			return blendingData;
107
		} else {
108
			return null;
109
		}
110
	}
111
112
	public static Set<CompositeDirection.Direction8> sideByGenerationAge(
113
		final WorldGenLevel region, final int chunkX, final int chunkZ, final boolean wantedOldGen
114
	) {
115
		Set<CompositeDirection.Direction8> sides = EnumSet.noneOf(CompositeDirection.Direction8.class);
116
117
		for (CompositeDirection.Direction8 direction8 : CompositeDirection.Direction8.values()) {
118
			int testChunkX = chunkX + direction8.getStepX();
119
			int testChunkZ = chunkZ + direction8.getStepZ();
120
			if (region.getChunk(testChunkX, testChunkZ).isOldNoiseGeneration() == wantedOldGen) {
121
				sides.add(direction8);
122
			}
123
		}
124
125
		return sides;
126
	}
127
128
	private void calculateData(final ChunkAccess chunk, final Set<CompositeDirection.Direction8> newSides) {
129
		if (!this.hasCalculatedData) {
130
			if (newSides.contains(CompositeDirection.Direction8.NORTH)
131
				|| newSides.contains(CompositeDirection.Direction8.WEST)
132
				|| newSides.contains(CompositeDirection.Direction8.NORTH_WEST)) {
133
				this.addValuesForColumn(getInsideIndex(0, 0), chunk, 0, 0);
134
			}
135
136
			if (newSides.contains(CompositeDirection.Direction8.NORTH)) {
137
				for (int i = 1; i < QUARTS_PER_SECTION; i++) {
138
					this.addValuesForColumn(getInsideIndex(i, 0), chunk, 4 * i, 0);
139
				}
140
			}
141
142
			if (newSides.contains(CompositeDirection.Direction8.WEST)) {
143
				for (int i = 1; i < QUARTS_PER_SECTION; i++) {
144
					this.addValuesForColumn(getInsideIndex(0, i), chunk, 0, 4 * i);
145
				}
146
			}
147
148
			if (newSides.contains(CompositeDirection.Direction8.EAST)) {
149
				for (int i = 1; i < QUARTS_PER_SECTION; i++) {
150
					this.addValuesForColumn(getOutsideIndex(CELL_HORIZONTAL_MAX_INDEX_OUTSIDE, i), chunk, 15, 4 * i);
151
				}
152
			}
153
154
			if (newSides.contains(CompositeDirection.Direction8.SOUTH)) {
155
				for (int i = 0; i < QUARTS_PER_SECTION; i++) {
156
					this.addValuesForColumn(getOutsideIndex(i, CELL_HORIZONTAL_MAX_INDEX_OUTSIDE), chunk, 4 * i, 15);
157
				}
158
			}
159
160
			if (newSides.contains(CompositeDirection.Direction8.EAST) && newSides.contains(CompositeDirection.Direction8.NORTH_EAST)) {
161
				this.addValuesForColumn(getOutsideIndex(CELL_HORIZONTAL_MAX_INDEX_OUTSIDE, 0), chunk, 15, 0);
162
			}
163
164
			if (newSides.contains(CompositeDirection.Direction8.EAST)
165
				&& newSides.contains(CompositeDirection.Direction8.SOUTH)
166
				&& newSides.contains(CompositeDirection.Direction8.SOUTH_EAST)) {
167
				this.addValuesForColumn(getOutsideIndex(CELL_HORIZONTAL_MAX_INDEX_OUTSIDE, CELL_HORIZONTAL_MAX_INDEX_OUTSIDE), chunk, 15, 15);
168
			}
169
170
			this.hasCalculatedData = true;
171
		}
172
	}
173
174
	private void addValuesForColumn(final int index, final ChunkAccess chunk, final int blockX, final int blockZ) {
175
		if (this.heights[index] == Float.MAX_VALUE) {
176
			this.heights[index] = this.getHeightAtXZ(chunk, blockX, blockZ);
177
		}
178
179
		this.densities[index] = this.getDensityColumn(chunk, blockX, blockZ, Mth.floor(this.heights[index]));
180
		this.biomes.set(index, this.getBiomeColumn(chunk, blockX, blockZ));
181
	}
182
183
	private int getHeightAtXZ(final ChunkAccess chunk, final int blockX, final int blockZ) {
184
		int height;
185
		if (chunk.hasPrimedHeightmap(Heightmap.Types.WORLD_SURFACE_WG)) {
186
			height = Math.min(chunk.getHeight(Heightmap.Types.WORLD_SURFACE_WG, blockX, blockZ), this.areaWithOldGeneration.getMaxY());
187
		} else {
188
			height = this.areaWithOldGeneration.getMaxY();
189
		}
190
191
		int minY = this.areaWithOldGeneration.getMinY();
192
		BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(blockX, height, blockZ);
193
194
		while (pos.getY() > minY) {
195
			if (SURFACE_BLOCKS.contains(chunk.getBlockState(pos).getBlock())) {
196
				return pos.getY();
197
			}
198
199
			pos.move(Direction.DOWN);
200
		}
201
202
		return minY;
203
	}
204
205
	private static float read1(final ChunkAccess chunk, final BlockPos.MutableBlockPos pos) {
206
		return isGround(chunk, pos.move(Direction.DOWN)) ? 1.0F : -1.0F;
207
	}
208
209
	private static float read7(final ChunkAccess chunk, final BlockPos.MutableBlockPos pos) {
210
		float sum = 0.0F;
211
212
		for (int i = 0; i < 7; i++) {
213
			sum += read1(chunk, pos);
214
		}
215
216
		return sum;
217
	}
218
219
	private float[] getDensityColumn(final ChunkAccess chunk, final int x, final int z, final int height) {
220
		float[] densities = new float[this.cellCountPerColumn()];
221
		Arrays.fill(densities, -1.0F);
222
		BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(x, this.areaWithOldGeneration.getMaxY() + 1, z);
223
		float last7 = read7(chunk, pos);
224
225
		for (int cellIndex = densities.length - 2; cellIndex >= 0; cellIndex--) {
226
			float one = read1(chunk, pos);
227
			float current7 = read7(chunk, pos);
228
			densities[cellIndex] = (last7 + one + current7) / 15.0F;
229
			last7 = current7;
230
		}
231
232
		int highestCellWithSurfaceIndex = this.getCellYIndex(Mth.floorDiv(height, 8));
233
		if (highestCellWithSurfaceIndex >= 0 && highestCellWithSurfaceIndex < densities.length - 1) {
234
			float inCellIndex = (height + 0.5F) % 8.0F / 8.0F;
235
			float amplitudeAboveToMakeSurfaceBeAtHeight = (1.0F - inCellIndex) / inCellIndex;
236
			float max = Math.max(amplitudeAboveToMakeSurfaceBeAtHeight, 1.0F) * 0.25F;
237
			densities[highestCellWithSurfaceIndex + 1] = -amplitudeAboveToMakeSurfaceBeAtHeight / max;
238
			densities[highestCellWithSurfaceIndex] = 1.0F / max;
239
		}
240
241
		return densities;
242
	}
243
244
	private List<Holder<Biome>> getBiomeColumn(final ChunkAccess chunk, final int blockX, final int blockZ) {
245
		ObjectArrayList<Holder<Biome>> biomes = new ObjectArrayList<>(this.quartCountPerColumn());
246
		biomes.size(this.quartCountPerColumn());
247
248
		for (int quartIndex = 0; quartIndex < biomes.size(); quartIndex++) {
249
			int quartY = quartIndex + QuartPos.fromBlock(this.areaWithOldGeneration.getMinY());
250
			biomes.set(quartIndex, chunk.getNoiseBiome(QuartPos.fromBlock(blockX), quartY, QuartPos.fromBlock(blockZ)));
251
		}
252
253
		return biomes;
254
	}
255
256
	private static boolean isGround(final ChunkAccess chunk, final BlockPos pos) {
257
		BlockState state = chunk.getBlockState(pos);
258
		if (state.isAir()) {
259
			return false;
260
		} else if (state.is(BlockTags.LEAVES)) {
261
			return false;
262
		} else if (state.is(BlockTags.LOGS)) {
263
			return false;
264
		} else {
265
			return state.is(Blocks.BROWN_MUSHROOM_BLOCK) || state.is(Blocks.RED_MUSHROOM_BLOCK) ? false : !state.getCollisionShape(chunk, pos).isEmpty();
266
		}
267
	}
268
269
	protected float getHeight(final int cellX, final int cellY, final int cellZ) {
270
		if (cellX == CELL_HORIZONTAL_MAX_INDEX_OUTSIDE || cellZ == CELL_HORIZONTAL_MAX_INDEX_OUTSIDE) {
271
			return this.heights[getOutsideIndex(cellX, cellZ)];
272
		} else {
273
			return cellX != 0 && cellZ != 0 ? Float.MAX_VALUE : this.heights[getInsideIndex(cellX, cellZ)];
274
		}
275
	}
276
277
	private float getDensity(final float @Nullable [] densityColumn, final int cellY) {
278
		if (densityColumn == null) {
279
			return Float.MAX_VALUE;
280
		}
281
282
		int yIndex = this.getCellYIndex(cellY);
283
		return yIndex >= 0 && yIndex < densityColumn.length ? densityColumn[yIndex] * 0.1F : Float.MAX_VALUE;
284
	}
285
286
	protected float getDensity(final int cellX, final int cellY, final int cellZ) {
287
		if (cellY == this.getMinY()) {
288
			return 0.1F;
289
		} else if (cellX == CELL_HORIZONTAL_MAX_INDEX_OUTSIDE || cellZ == CELL_HORIZONTAL_MAX_INDEX_OUTSIDE) {
290
			return this.getDensity(this.densities[getOutsideIndex(cellX, cellZ)], cellY);
291
		} else {
292
			return cellX != 0 && cellZ != 0 ? Float.MAX_VALUE : this.getDensity(this.densities[getInsideIndex(cellX, cellZ)], cellY);
293
		}
294
	}
295
296
	protected void iterateBiomes(final int minCellX, final int quartY, final int minCellZ, final BlendingData.BiomeConsumer biomeConsumer) {
297
		if (quartY >= QuartPos.fromBlock(this.areaWithOldGeneration.getMinY()) && quartY <= QuartPos.fromBlock(this.areaWithOldGeneration.getMaxY())) {
298
			int quartIndex = quartY - QuartPos.fromBlock(this.areaWithOldGeneration.getMinY());
299
300
			for (int i = 0; i < this.biomes.size(); i++) {
301
				List<Holder<Biome>> biomeCell = this.biomes.get(i);
302
				if (biomeCell != null) {
303
					Holder<Biome> value = biomeCell.get(quartIndex);
304
					if (value != null) {
305
						biomeConsumer.consume(minCellX + getX(i), minCellZ + getZ(i), value);
306
					}
307
				}
308
			}
309
		}
310
	}
311
312
	protected void iterateHeights(final int minCellX, final int minCellZ, final BlendingData.HeightConsumer heightConsumer) {
313
		for (int i = 0; i < this.heights.length; i++) {
314
			float value = this.heights[i];
315
			if (value != Float.MAX_VALUE) {
316
				heightConsumer.consume(minCellX + getX(i), minCellZ + getZ(i), value);
317
			}
318
		}
319
	}
320
321
	protected void iterateDensities(
322
		final int minCellX, final int minCellZ, final int fromCellY, final int toCellY, final BlendingData.DensityConsumer densityConsumer
323
	) {
324
		int minCellY = this.getColumnMinY();
325
		int minYIndex = Math.max(0, fromCellY - minCellY);
326
		int maxYIndex = Math.min(this.cellCountPerColumn(), toCellY - minCellY);
327
328
		for (int i = 0; i < this.densities.length; i++) {
329
			float[] densityColumn = this.densities[i];
330
			if (densityColumn != null) {
331
				int testCellX = minCellX + getX(i);
332
				int testCellZ = minCellZ + getZ(i);
333
334
				for (int yIndex = minYIndex; yIndex < maxYIndex; yIndex++) {
335
					densityConsumer.consume(testCellX, yIndex + minCellY, testCellZ, densityColumn[yIndex] * 0.1F);
336
				}
337
			}
338
		}
339
	}
340
341
	private int cellCountPerColumn() {
342
		return this.areaWithOldGeneration.getSectionsCount() * 2;
343
	}
344
345
	private int quartCountPerColumn() {
346
		return QuartPos.fromSection(this.areaWithOldGeneration.getSectionsCount());
347
	}
348
349
	private int getColumnMinY() {
350
		return this.getMinY() + 1;
351
	}
352
353
	private int getMinY() {
354
		return this.areaWithOldGeneration.getMinSectionY() * 2;
355
	}
356
357
	private int getCellYIndex(final int cellY) {
358
		return cellY - this.getColumnMinY();
359
	}
360
361
	private static int getInsideIndex(final int x, final int z) {
362
		return CELL_HORIZONTAL_MAX_INDEX_INSIDE - x + z;
363
	}
364
365
	private static int getOutsideIndex(final int x, final int z) {
366
		return CELL_COLUMN_INSIDE_COUNT + x + CELL_HORIZONTAL_MAX_INDEX_OUTSIDE - z;
367
	}
368
369
	private static int getX(final int index) {
370
		if (index < CELL_COLUMN_INSIDE_COUNT) {
371
			return zeroIfNegative(CELL_HORIZONTAL_MAX_INDEX_INSIDE - index);
372
		}
373
374
		int offsetIndex = index - CELL_COLUMN_INSIDE_COUNT;
375
		return CELL_HORIZONTAL_MAX_INDEX_OUTSIDE - zeroIfNegative(CELL_HORIZONTAL_MAX_INDEX_OUTSIDE - offsetIndex);
376
	}
377
378
	private static int getZ(final int index) {
379
		if (index < CELL_COLUMN_INSIDE_COUNT) {
380
			return zeroIfNegative(index - CELL_HORIZONTAL_MAX_INDEX_INSIDE);
381
		}
382
383
		int offsetIndex = index - CELL_COLUMN_INSIDE_COUNT;
384
		return CELL_HORIZONTAL_MAX_INDEX_OUTSIDE - zeroIfNegative(offsetIndex - CELL_HORIZONTAL_MAX_INDEX_OUTSIDE);
385
	}
386
387
	private static int zeroIfNegative(final int value) {
388
		return value & ~(value >> 31);
389
	}
390
391
	public LevelHeightAccessor getAreaWithOldGeneration() {
392
		return this.areaWithOldGeneration;
393
	}
394
395
	protected interface BiomeConsumer {
396
		void consume(final int cellX, final int cellZ, final Holder<Biome> biome);
397
	}
398
399
	protected interface DensityConsumer {
400
		void consume(final int cellX, final int cellY, final int cellZ, final float density);
401
	}
402
403
	protected interface HeightConsumer {
404
		void consume(final int cellX, final int cellZ, final float height);
405
	}
406
407
	public record Packed(int minSection, int maxSection, Optional<float[]> heights) {
408
		private static final Codec<float[]> FLOAT_ARRAY_CODEC = Codec.FLOAT.listOf().xmap(Floats::toArray, Floats::asList);
409
		public static final Codec<BlendingData.Packed> CODEC = RecordCodecBuilder.<BlendingData.Packed>create(
410
				i -> i.group(
411
						Codec.INT.fieldOf("min_section").forGetter(BlendingData.Packed::minSection),
412
						Codec.INT.fieldOf("max_section").forGetter(BlendingData.Packed::maxSection),
413
						FLOAT_ARRAY_CODEC.lenientOptionalFieldOf("heights").forGetter(BlendingData.Packed::heights)
414
					)
415
					.apply(i, BlendingData.Packed::new)
416
			)
417
			.validate(BlendingData.Packed::validateArraySize);
418
419
		private static DataResult<BlendingData.Packed> validateArraySize(final BlendingData.Packed blendingData) {
420
			return blendingData.heights.isPresent() && ((float[])blendingData.heights.get()).length != BlendingData.CELL_COLUMN_COUNT
421
				? DataResult.error(() -> "heights has to be of length " + BlendingData.CELL_COLUMN_COUNT)
422
				: DataResult.success(blendingData);
423
		}
424
	}
425
}
426

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