Climate .java

net.minecraft.world.level.biome.Climate (class, 486 lines): members: DEBUG_SLOW_BIOME_SEARCH, QUANTIZATION_FACTOR, PARAMETER_COUNT, unquantizeCoord, RTree.

net.minecraft.world.level.biomeclass486 linescommon16.8 KB

API Surface (31)

Climate.TargetPoint target(final float temperature, final float humidity, final float continentalness, final float erosion, final float depth, final float weirdness)
Climate.ParameterPoint parameters(final float temperature, final float humidity, final float continentalness, final float erosion, final float depth, final float weirdness, final float offset)
Climate.ParameterPoint parameters(final Climate.Parameter temperature, final Climate.Parameter humidity, final Climate.Parameter continentalness, final Climate.Parameter erosion, final Climate.Parameter depth, final Climate.Parameter weirdness, final float offset)
long quantizeCoord(final float coord)
float unquantizeCoord(final long coord)
record Parameter(long min, long max)
Climate.Parameter point(final float min)
Climate.Parameter span(final float min, final float max)
Climate.Parameter span(final Climate.Parameter min, final Climate.Parameter max)
String toString()
long distance(final long target)
Climate.Parameter span(final Climate.@Nullable Parameter other)
<T> Codec<Climate.ParameterList<T>> codec(final MapCodec<T> valueCodec)
Climate.ParameterList<T> rebuildWithChildrenPerNode(final int childrenPerNode)
List<Pair<Climate.ParameterPoint, T>> values()
T findValue(final Climate.TargetPoint target)
T findValueBruteForce(final Climate.TargetPoint target)
T findValueIndex(final Climate.TargetPoint target)
T findValueIndex(final Climate.TargetPoint target, final Climate.DistanceMetric<T> distanceMetric)
record ParameterPoint(Climate.Parameter temperature, Climate.Parameter humidity, Climate.Parameter continentalness, Climate.Parameter erosion, Climate.Parameter depth, Climate.Parameter weirdness, long offset)
long fitness(final Climate.TargetPoint target)
<T> Climate.RTree<T> create(final List<Pair<Climate.ParameterPoint, T>> values)
<T> Climate.RTree<T> create(final List<Pair<Climate.ParameterPoint, T>> values, final int childrenPerNode)
T search(final Climate.TargetPoint target, final Climate.DistanceMetric<T> distanceMetric)
Climate.RTree.Leaf<T> search(final long[] target, final Climate.RTree.@Nullable Leaf<T> candidate, final Climate.DistanceMetric<T> distanceMetric)
long distance(final long[] target)
String toString()
Climate.RTree.Leaf<T> search(final long[] target, final Climate.RTree.@Nullable Leaf<T> candidate, final Climate.DistanceMetric<T> distanceMetric)
record Sampler(DensitySampler.Bound temperature, DensitySampler.Bound humidity, DensitySampler.Bound continentalness, DensitySampler.Bound erosion, DensitySampler.Bound depth, DensitySampler.Bound weirdness)
Climate.TargetPoint sample(final int quartX, final int quartY, final int quartZ)
record TargetPoint(long temperature, long humidity, long continentalness, long erosion, long depth, long weirdness)

Fields & Constants (9)

DEBUG_SLOW_BIOME_SEARCH
QUANTIZATION_FACTOR
PARAMETER_COUNT
depth
weirdness
temperature
humidity
continentalness
erosion

String literals (13)

"Cannon construct interval, min > max ("
"min > max: "
"parameters"
"temperature"
"humidity"
"continentalness"
"erosion"
"depth"
"weirdness"
"offset"
"Expecting parameter space to be 7, got "
"Need at least one child to build a node"
"SubTree needs at least one child"

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Climate.java486 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.biome;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.Lists;
6
import com.mojang.datafixers.util.Pair;
7
import com.mojang.serialization.Codec;
8
import com.mojang.serialization.DataResult;
9
import com.mojang.serialization.MapCodec;
10
import com.mojang.serialization.codecs.RecordCodecBuilder;
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.Comparator;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Locale;
17
import java.util.stream.Collectors;
18
import net.minecraft.core.QuartPos;
19
import net.minecraft.util.ExtraCodecs;
20
import net.minecraft.util.Mth;
21
import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
22
import org.jspecify.annotations.Nullable;
23
24
public class Climate {
25
	private static final boolean DEBUG_SLOW_BIOME_SEARCH = false;
26
	private static final float QUANTIZATION_FACTOR = 10000.0F;
27
	@VisibleForTesting
28
	protected static final int PARAMETER_COUNT = 7;
29
30
	public static Climate.TargetPoint target(
31
		final float temperature, final float humidity, final float continentalness, final float erosion, final float depth, final float weirdness
32
	) {
33
		return new Climate.TargetPoint(
34
			quantizeCoord(temperature), quantizeCoord(humidity), quantizeCoord(continentalness), quantizeCoord(erosion), quantizeCoord(depth), quantizeCoord(weirdness)
35
		);
36
	}
37
38
	public static Climate.ParameterPoint parameters(
39
		final float temperature, final float humidity, final float continentalness, final float erosion, final float depth, final float weirdness, final float offset
40
	) {
41
		return new Climate.ParameterPoint(
42
			Climate.Parameter.point(temperature),
43
			Climate.Parameter.point(humidity),
44
			Climate.Parameter.point(continentalness),
45
			Climate.Parameter.point(erosion),
46
			Climate.Parameter.point(depth),
47
			Climate.Parameter.point(weirdness),
48
			quantizeCoord(offset)
49
		);
50
	}
51
52
	public static Climate.ParameterPoint parameters(
53
		final Climate.Parameter temperature,
54
		final Climate.Parameter humidity,
55
		final Climate.Parameter continentalness,
56
		final Climate.Parameter erosion,
57
		final Climate.Parameter depth,
58
		final Climate.Parameter weirdness,
59
		final float offset
60
	) {
61
		return new Climate.ParameterPoint(temperature, humidity, continentalness, erosion, depth, weirdness, quantizeCoord(offset));
62
	}
63
64
	public static long quantizeCoord(final float coord) {
65
		return (long)(coord * 10000.0F);
66
	}
67
68
	public static float unquantizeCoord(final long coord) {
69
		return (float)coord / 10000.0F;
70
	}
71
72
	@VisibleForTesting
73
	interface DistanceMetric<T> {
74
		long distance(Climate.RTree.Node<T> node, long[] target);
75
	}
76
77
	public record Parameter(long min, long max) {
78
		public static final Codec<Climate.Parameter> CODEC = ExtraCodecs.intervalCodec(
79
			Codec.floatRange(-2.0F, 2.0F),
80
			"min",
81
			"max",
82
			(min, max) -> min.compareTo(max) > 0
83
				? DataResult.error(() -> "Cannon construct interval, min > max (" + min + " > " + max + ")")
84
				: DataResult.success(new Climate.Parameter(Climate.quantizeCoord(min), Climate.quantizeCoord(max))),
85
			p -> Climate.unquantizeCoord(p.min()),
86
			p -> Climate.unquantizeCoord(p.max())
87
		);
88
89
		public static Climate.Parameter point(final float min) {
90
			return span(min, min);
91
		}
92
93
		public static Climate.Parameter span(final float min, final float max) {
94
			if (min > max) {
95
				throw new IllegalArgumentException("min > max: " + min + " " + max);
96
			} else {
97
				return new Climate.Parameter(Climate.quantizeCoord(min), Climate.quantizeCoord(max));
98
			}
99
		}
100
101
		public static Climate.Parameter span(final Climate.Parameter min, final Climate.Parameter max) {
102
			if (min.min() > max.max()) {
103
				throw new IllegalArgumentException("min > max: " + min + " " + max);
104
			} else {
105
				return new Climate.Parameter(min.min(), max.max());
106
			}
107
		}
108
109
		@Override
110
		public String toString() {
111
			return this.min == this.max ? String.format(Locale.ROOT, "%d", this.min) : String.format(Locale.ROOT, "[%d-%d]", this.min, this.max);
112
		}
113
114
		public long distance(final long target) {
115
			long above = target - this.max;
116
			long below = this.min - target;
117
			return above > 0L ? above : Math.max(below, 0L);
118
		}
119
120
		public Climate.Parameter span(final Climate.@Nullable Parameter other) {
121
			return other == null ? this : new Climate.Parameter(Math.min(this.min, other.min()), Math.max(this.max, other.max()));
122
		}
123
	}
124
125
	public static class ParameterList<T> {
126
		private final List<Pair<Climate.ParameterPoint, T>> values;
127
		private final Climate.RTree<T> index;
128
129
		public static <T> Codec<Climate.ParameterList<T>> codec(final MapCodec<T> valueCodec) {
130
			return ExtraCodecs.nonEmptyList(
131
					RecordCodecBuilder.<Pair<Climate.ParameterPoint, T>>create(
132
							i -> i.group(Climate.ParameterPoint.CODEC.fieldOf("parameters").forGetter(Pair::getFirst), valueCodec.forGetter(Pair::getSecond)).apply(i, Pair::of)
133
						)
134
						.listOf()
135
				)
136
				.xmap(Climate.ParameterList::new, Climate.ParameterList::values);
137
		}
138
139
		public ParameterList(final List<Pair<Climate.ParameterPoint, T>> values) {
140
			this(values, 19);
141
		}
142
143
		private ParameterList(final List<Pair<Climate.ParameterPoint, T>> values, final int childrenPerNode) {
144
			this.values = values;
145
			this.index = Climate.RTree.create(values, childrenPerNode);
146
		}
147
148
		@VisibleForTesting
149
		public Climate.ParameterList<T> rebuildWithChildrenPerNode(final int childrenPerNode) {
150
			return new Climate.ParameterList<>(this.values, childrenPerNode);
151
		}
152
153
		public List<Pair<Climate.ParameterPoint, T>> values() {
154
			return this.values;
155
		}
156
157
		public T findValue(final Climate.TargetPoint target) {
158
			return this.findValueIndex(target);
159
		}
160
161
		@VisibleForTesting
162
		public T findValueBruteForce(final Climate.TargetPoint target) {
163
			Iterator<Pair<Climate.ParameterPoint, T>> iterator = this.values().iterator();
164
			Pair<Climate.ParameterPoint, T> first = iterator.next();
165
			long bestFitness = first.getFirst().fitness(target);
166
			T best = first.getSecond();
167
168
			while (iterator.hasNext()) {
169
				Pair<Climate.ParameterPoint, T> parameter = iterator.next();
170
				long fitness = parameter.getFirst().fitness(target);
171
				if (fitness < bestFitness) {
172
					bestFitness = fitness;
173
					best = parameter.getSecond();
174
				}
175
			}
176
177
			return best;
178
		}
179
180
		public T findValueIndex(final Climate.TargetPoint target) {
181
			return this.findValueIndex(target, Climate.RTree.Node::distance);
182
		}
183
184
		protected T findValueIndex(final Climate.TargetPoint target, final Climate.DistanceMetric<T> distanceMetric) {
185
			return this.index.search(target, distanceMetric);
186
		}
187
	}
188
189
	public record ParameterPoint(
190
		Climate.Parameter temperature,
191
		Climate.Parameter humidity,
192
		Climate.Parameter continentalness,
193
		Climate.Parameter erosion,
194
		Climate.Parameter depth,
195
		Climate.Parameter weirdness,
196
		long offset
197
	) {
198
		public static final Codec<Climate.ParameterPoint> CODEC = RecordCodecBuilder.create(
199
			i -> i.group(
200
					Climate.Parameter.CODEC.fieldOf("temperature").forGetter(p -> p.temperature),
201
					Climate.Parameter.CODEC.fieldOf("humidity").forGetter(p -> p.humidity),
202
					Climate.Parameter.CODEC.fieldOf("continentalness").forGetter(p -> p.continentalness),
203
					Climate.Parameter.CODEC.fieldOf("erosion").forGetter(p -> p.erosion),
204
					Climate.Parameter.CODEC.fieldOf("depth").forGetter(p -> p.depth),
205
					Climate.Parameter.CODEC.fieldOf("weirdness").forGetter(p -> p.weirdness),
206
					Codec.floatRange(0.0F, 1.0F).fieldOf("offset").xmap(Climate::quantizeCoord, Climate::unquantizeCoord).forGetter(p -> p.offset)
207
				)
208
				.apply(i, Climate.ParameterPoint::new)
209
		);
210
211
		public long fitness(final Climate.TargetPoint target) {
212
			return Mth.square(this.temperature.distance(target.temperature))
213
				+ Mth.square(this.humidity.distance(target.humidity))
214
				+ Mth.square(this.continentalness.distance(target.continentalness))
215
				+ Mth.square(this.erosion.distance(target.erosion))
216
				+ Mth.square(this.depth.distance(target.depth))
217
				+ Mth.square(this.weirdness.distance(target.weirdness))
218
				+ Mth.square(this.offset);
219
		}
220
221
		List<Climate.Parameter> parameterSpace() {
222
			return ImmutableList.of(
223
				this.temperature, this.humidity, this.continentalness, this.erosion, this.depth, this.weirdness, new Climate.Parameter(this.offset, this.offset)
224
			);
225
		}
226
	}
227
228
	protected static final class RTree<T> {
229
		private static final int CHILDREN_PER_NODE = 19;
230
		private final Climate.RTree.Node<T> root;
231
		private final ThreadLocal<Climate.RTree.@Nullable Leaf<T>> lastResult = new ThreadLocal<>();
232
233
		private RTree(final Climate.RTree.Node<T> root) {
234
			this.root = root;
235
		}
236
237
		public static <T> Climate.RTree<T> create(final List<Pair<Climate.ParameterPoint, T>> values) {
238
			return create(values, 19);
239
		}
240
241
		public static <T> Climate.RTree<T> create(final List<Pair<Climate.ParameterPoint, T>> values, final int childrenPerNode) {
242
			if (values.isEmpty()) {
243
				throw new IllegalArgumentException("Need at least one value to build the search tree.");
244
			}
245
246
			int dimensions = values.get(0).getFirst().parameterSpace().size();
247
			if (dimensions != 7) {
248
				throw new IllegalStateException("Expecting parameter space to be 7, got " + dimensions);
249
			}
250
251
			List<Climate.RTree.Leaf<T>> leaves = values.stream()
252
				.map(p -> new Climate.RTree.Leaf(p.getFirst(), p.getSecond()))
253
				.collect(Collectors.toCollection(ArrayList::new));
254
			return new Climate.RTree<>(build(dimensions, leaves, childrenPerNode));
255
		}
256
257
		private static <T> Climate.RTree.Node<T> build(final int dimensions, final List<? extends Climate.RTree.Node<T>> children, final int childrenPerNode) {
258
			if (children.isEmpty()) {
259
				throw new IllegalStateException("Need at least one child to build a node");
260
			}
261
262
			if (children.size() == 1) {
263
				return (Climate.RTree.Node<T>)children.get(0);
264
			}
265
266
			if (children.size() <= childrenPerNode) {
267
				children.sort(Comparator.comparingLong(leaf -> {
268
					long totalMagnitude = 0L;
269
270
					for (int dx = 0; dx < dimensions; dx++) {
271
						Climate.Parameter parameter = leaf.parameterSpace[dx];
272
						totalMagnitude += Math.abs((parameter.min() + parameter.max()) / 2L);
273
					}
274
275
					return totalMagnitude;
276
				}));
277
				return new Climate.RTree.SubTree<>(children);
278
			}
279
280
			long minCost = Long.MAX_VALUE;
281
			int minDimension = -1;
282
			List<Climate.RTree.SubTree<T>> minBuckets = null;
283
284
			for (int d = 0; d < dimensions; d++) {
285
				sort(children, dimensions, d, false);
286
				List<Climate.RTree.SubTree<T>> buckets = bucketize(children, childrenPerNode);
287
				long totalCost = 0L;
288
289
				for (Climate.RTree.SubTree<T> bucket : buckets) {
290
					totalCost += cost(bucket.parameterSpace);
291
				}
292
293
				if (minCost > totalCost) {
294
					minCost = totalCost;
295
					minDimension = d;
296
					minBuckets = buckets;
297
				}
298
			}
299
300
			sort(minBuckets, dimensions, minDimension, true);
301
			return new Climate.RTree.SubTree<>(minBuckets.stream().map(b -> build(dimensions, Arrays.asList(b.children), childrenPerNode)).collect(Collectors.toList()));
302
		}
303
304
		private static <T> void sort(final List<? extends Climate.RTree.Node<T>> children, final int dimensions, final int dimension, final boolean absolute) {
305
			Comparator<Climate.RTree.Node<T>> comparator = comparator(dimension, absolute);
306
307
			for (int d = 1; d < dimensions; d++) {
308
				comparator = comparator.thenComparing(comparator((dimension + d) % dimensions, absolute));
309
			}
310
311
			children.sort(comparator);
312
		}
313
314
		private static <T> Comparator<Climate.RTree.Node<T>> comparator(final int dimension, final boolean absolute) {
315
			return Comparator.comparingLong(leaf -> {
316
				Climate.Parameter parameter = leaf.parameterSpace[dimension];
317
				long center = (parameter.min() + parameter.max()) / 2L;
318
				return absolute ? Math.abs(center) : center;
319
			});
320
		}
321
322
		private static <T> List<Climate.RTree.SubTree<T>> bucketize(final List<? extends Climate.RTree.Node<T>> nodes, final int childrenPerNode) {
323
			List<Climate.RTree.SubTree<T>> buckets = Lists.newArrayList();
324
			List<Climate.RTree.Node<T>> children = Lists.newArrayList();
325
			int expectedChildrenCount = (int)Math.pow(childrenPerNode, Math.floor(Math.log(nodes.size() - 0.01) / Math.log(childrenPerNode)));
326
327
			for (Climate.RTree.Node<T> child : nodes) {
328
				children.add(child);
329
				if (children.size() >= expectedChildrenCount) {
330
					buckets.add(new Climate.RTree.SubTree<>(children));
331
					children = Lists.newArrayList();
332
				}
333
			}
334
335
			if (!children.isEmpty()) {
336
				buckets.add(new Climate.RTree.SubTree<>(children));
337
			}
338
339
			return buckets;
340
		}
341
342
		private static long cost(final Climate.Parameter[] parameterSpace) {
343
			long result = 0L;
344
345
			for (Climate.Parameter parameter : parameterSpace) {
346
				result += Math.abs(parameter.max() - parameter.min());
347
			}
348
349
			return result;
350
		}
351
352
		private static <T> List<Climate.Parameter> buildParameterSpace(final List<? extends Climate.RTree.Node<T>> children) {
353
			if (children.isEmpty()) {
354
				throw new IllegalArgumentException("SubTree needs at least one child");
355
			}
356
357
			int dimensions = 7;
358
			List<Climate.Parameter> bounds = Lists.newArrayList();
359
360
			for (int d = 0; d < 7; d++) {
361
				bounds.add(null);
362
			}
363
364
			for (Climate.RTree.Node<T> child : children) {
365
				for (int d = 0; d < 7; d++) {
366
					bounds.set(d, child.parameterSpace[d].span(bounds.get(d)));
367
				}
368
			}
369
370
			return bounds;
371
		}
372
373
		public T search(final Climate.TargetPoint target, final Climate.DistanceMetric<T> distanceMetric) {
374
			long[] targetArray = target.toParameterArray();
375
			Climate.RTree.Leaf<T> leaf = this.root.search(targetArray, this.lastResult.get(), distanceMetric);
376
			this.lastResult.set(leaf);
377
			return leaf.value;
378
		}
379
380
		private static final class Leaf<T> extends Climate.RTree.Node<T> {
381
			private final T value;
382
383
			private Leaf(final Climate.ParameterPoint parameterPoint, final T value) {
384
				super(parameterPoint.parameterSpace());
385
				this.value = value;
386
			}
387
388
			@Override
389
			protected Climate.RTree.Leaf<T> search(final long[] target, final Climate.RTree.@Nullable Leaf<T> candidate, final Climate.DistanceMetric<T> distanceMetric) {
390
				return this;
391
			}
392
		}
393
394
		@VisibleForTesting
395
		abstract static class Node<T> {
396
			protected final Climate.Parameter[] parameterSpace;
397
398
			protected Node(final List<Climate.Parameter> parameterSpace) {
399
				this.parameterSpace = parameterSpace.toArray(new Climate.Parameter[0]);
400
			}
401
402
			protected abstract Climate.RTree.Leaf<T> search(
403
				final long[] target, final Climate.RTree.@Nullable Leaf<T> candidate, final Climate.DistanceMetric<T> distanceMetric
404
			);
405
406
			protected long distance(final long[] target) {
407
				long distance = 0L;
408
409
				for (int i = 0; i < 7; i++) {
410
					distance += Mth.square(this.parameterSpace[i].distance(target[i]));
411
				}
412
413
				return distance;
414
			}
415
416
			@Override
417
			public String toString() {
418
				return Arrays.toString(this.parameterSpace);
419
			}
420
		}
421
422
		private static final class SubTree<T> extends Climate.RTree.Node<T> {
423
			private final Climate.RTree.Node<T>[] children;
424
425
			public SubTree(final List<? extends Climate.RTree.Node<T>> children) {
426
				this(Climate.RTree.buildParameterSpace(children), children);
427
			}
428
429
			public SubTree(final List<Climate.Parameter> parameterSpace, final List<? extends Climate.RTree.Node<T>> children) {
430
				super(parameterSpace);
431
				this.children = children.toArray(new Climate.RTree.Node[0]);
432
			}
433
434
			@Override
435
			protected Climate.RTree.Leaf<T> search(final long[] target, final Climate.RTree.@Nullable Leaf<T> candidate, final Climate.DistanceMetric<T> distanceMetric) {
436
				long minDistance = candidate == null ? Long.MAX_VALUE : distanceMetric.distance(candidate, target);
437
				Climate.RTree.Leaf<T> closestLeaf = candidate;
438
439
				for (Climate.RTree.Node<T> child : this.children) {
440
					long childDistance = distanceMetric.distance(child, target);
441
					if (minDistance > childDistance) {
442
						Climate.RTree.Leaf<T> leaf = child.search(target, closestLeaf, distanceMetric);
443
						long leafDistance = child == leaf ? childDistance : distanceMetric.distance(leaf, target);
444
						if (minDistance > leafDistance) {
445
							minDistance = leafDistance;
446
							closestLeaf = leaf;
447
						}
448
					}
449
				}
450
451
				return closestLeaf;
452
			}
453
		}
454
	}
455
456
	public record Sampler(
457
		DensitySampler.Bound temperature,
458
		DensitySampler.Bound humidity,
459
		DensitySampler.Bound continentalness,
460
		DensitySampler.Bound erosion,
461
		DensitySampler.Bound depth,
462
		DensitySampler.Bound weirdness
463
	) {
464
		public Climate.TargetPoint sample(final int quartX, final int quartY, final int quartZ) {
465
			int blockX = QuartPos.toBlock(quartX);
466
			int blockY = QuartPos.toBlock(quartY);
467
			int blockZ = QuartPos.toBlock(quartZ);
468
			return Climate.target(
469
				this.temperature.sampleValue(blockX, blockY, blockZ),
470
				this.humidity.sampleValue(blockX, blockY, blockZ),
471
				this.continentalness.sampleValue(blockX, blockY, blockZ),
472
				this.erosion.sampleValue(blockX, blockY, blockZ),
473
				this.depth.sampleValue(blockX, blockY, blockZ),
474
				this.weirdness.sampleValue(blockX, blockY, blockZ)
475
			);
476
		}
477
	}
478
479
	public record TargetPoint(long temperature, long humidity, long continentalness, long erosion, long depth, long weirdness) {
480
		@VisibleForTesting
481
		long[] toParameterArray() {
482
			return new long[]{this.temperature, this.humidity, this.continentalness, this.erosion, this.depth, this.weirdness, 0L};
483
		}
484
	}
485
}
486

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