| 1 | package net.minecraft.world.level.biome;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import it.unimi.dsi.fastutil.longs.Long2FloatLinkedOpenHashMap;
|
| 8 | import java.util.Optional;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.HolderSet;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import net.minecraft.util.StringRepresentable;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import net.minecraft.world.attribute.EnvironmentAttribute;
|
| 18 | import net.minecraft.world.attribute.EnvironmentAttributeMap;
|
| 19 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 20 | import net.minecraft.world.attribute.modifier.AttributeModifier;
|
| 21 | import net.minecraft.world.attribute.modifier.MobSpawnSettingsModifier;
|
| 22 | import net.minecraft.world.level.DryFoliageColor;
|
| 23 | import net.minecraft.world.level.FoliageColor;
|
| 24 | import net.minecraft.world.level.GrassColor;
|
| 25 | import net.minecraft.world.level.LevelReader;
|
| 26 | import net.minecraft.world.level.LightLayer;
|
| 27 | import net.minecraft.world.level.block.Blocks;
|
| 28 | import net.minecraft.world.level.block.LiquidBlock;
|
| 29 | import net.minecraft.world.level.block.state.BlockState;
|
| 30 | import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
| 31 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 32 | import net.minecraft.world.level.levelgen.synth.Noise;
|
| 33 | import net.minecraft.world.level.levelgen.synth.NoiseStack;
|
| 34 | import net.minecraft.world.level.levelgen.synth.SimplexNoise;
|
| 35 | import net.minecraft.world.level.material.FluidState;
|
| 36 | import net.minecraft.world.level.material.Fluids;
|
| 37 | import org.jspecify.annotations.Nullable;
|
| 38 |
|
| 39 | public final class Biome {
|
| 40 | public static final Codec<Biome> DIRECT_CODEC = RecordCodecBuilder.create(
|
| 41 | i -> i.group(
|
| 42 | Biome.ClimateSettings.CODEC.forGetter(b -> b.climateSettings),
|
| 43 | EnvironmentAttributeMap.CODEC_ONLY_POSITIONAL.optionalFieldOf("attributes", EnvironmentAttributeMap.EMPTY).forGetter(b -> b.attributes),
|
| 44 | BiomeSpecialEffects.CODEC.fieldOf("effects").forGetter(b -> b.specialEffects),
|
| 45 | BiomeGenerationSettings.CODEC.forGetter(b -> b.generationSettings)
|
| 46 | )
|
| 47 | .apply(i, Biome::new)
|
| 48 | );
|
| 49 | public static final Codec<Biome> NETWORK_CODEC = RecordCodecBuilder.create(
|
| 50 | i -> i.group(
|
| 51 | Biome.ClimateSettings.CODEC.forGetter(b -> b.climateSettings),
|
| 52 | EnvironmentAttributeMap.NETWORK_CODEC.optionalFieldOf("attributes", EnvironmentAttributeMap.EMPTY).forGetter(b -> b.attributes),
|
| 53 | BiomeSpecialEffects.CODEC.fieldOf("effects").forGetter(b -> b.specialEffects)
|
| 54 | )
|
| 55 | .apply(i, (climateSettings, attributes, specialEffects) -> new Biome(climateSettings, attributes, specialEffects, BiomeGenerationSettings.EMPTY))
|
| 56 | );
|
| 57 | public static final Codec<Holder<Biome>> CODEC = RegistryCodecs.holder(Registries.BIOME, DIRECT_CODEC);
|
| 58 | public static final Codec<HolderSet<Biome>> LIST_CODEC = RegistryCodecs.holderSet(Registries.BIOME, DIRECT_CODEC);
|
| 59 | private static final Noise TEMPERATURE_NOISE = new SimplexNoise(new WorldgenRandom(new LegacyRandomSource(1234L)), true);
|
| 60 | @VisibleForTesting
|
| 61 | public static final Noise FROZEN_TEMPERATURE_NOISE = Util.make(
|
| 62 | () -> {
|
| 63 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(3456L));
|
| 64 | return NoiseStack.builder()
|
| 65 | .add(new SimplexNoise(random, true), 1.0, 0.14285715F)
|
| 66 | .add(new SimplexNoise(random, true), 0.5, 0.2857143F)
|
| 67 | .add(new SimplexNoise(random, true), 0.25, 0.5714286F)
|
| 68 | .build();
|
| 69 | }
|
| 70 | );
|
| 71 | @Deprecated(forRemoval = true)
|
| 72 | public static final Noise BIOME_INFO_NOISE = new SimplexNoise(new WorldgenRandom(new LegacyRandomSource(2345L)), true);
|
| 73 | private static final int TEMPERATURE_CACHE_SIZE = 1024;
|
| 74 | private final Biome.ClimateSettings climateSettings;
|
| 75 | private final BiomeGenerationSettings generationSettings;
|
| 76 | private final EnvironmentAttributeMap attributes;
|
| 77 | private final BiomeSpecialEffects specialEffects;
|
| 78 | private final ThreadLocal<Long2FloatLinkedOpenHashMap> temperatureCache = ThreadLocal.withInitial(() -> {
|
| 79 | Long2FloatLinkedOpenHashMap map = new Long2FloatLinkedOpenHashMap(1024, 0.25F) {
|
| 80 | @Override
|
| 81 | protected void rehash(final int newN) {
|
| 82 | }
|
| 83 | };
|
| 84 | map.defaultReturnValue(Float.NaN);
|
| 85 | return map;
|
| 86 | });
|
| 87 |
|
| 88 | private Biome(
|
| 89 | final Biome.ClimateSettings climateSettings,
|
| 90 | final EnvironmentAttributeMap attributes,
|
| 91 | final BiomeSpecialEffects specialEffects,
|
| 92 | final BiomeGenerationSettings generationSettings
|
| 93 | ) {
|
| 94 | this.climateSettings = climateSettings;
|
| 95 | this.attributes = attributes;
|
| 96 | this.specialEffects = specialEffects;
|
| 97 | this.generationSettings = generationSettings;
|
| 98 | }
|
| 99 |
|
| 100 | public boolean hasPrecipitation() {
|
| 101 | return this.climateSettings.hasPrecipitation();
|
| 102 | }
|
| 103 |
|
| 104 | public Biome.Precipitation getPrecipitationAt(final BlockPos pos, final int seaLevel) {
|
| 105 | if (!this.hasPrecipitation()) {
|
| 106 | return Biome.Precipitation.NONE;
|
| 107 | } else {
|
| 108 | return this.coldEnoughToSnow(pos, seaLevel) ? Biome.Precipitation.SNOW : Biome.Precipitation.RAIN;
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private float getHeightAdjustedTemperature(final BlockPos pos, final int seaLevel) {
|
| 113 | float adjustedTemperature = this.climateSettings.temperatureModifier.modifyTemperature(pos, this.getBaseTemperature());
|
| 114 | int snowLevel = seaLevel + 17;
|
| 115 | if (pos.getY() > snowLevel) {
|
| 116 | float v = TEMPERATURE_NOISE.get(pos.getX() / 8.0F, pos.getZ() / 8.0F) * 8.0F;
|
| 117 | return adjustedTemperature - (v + pos.getY() - snowLevel) * 0.05F / 40.0F;
|
| 118 | } else {
|
| 119 | return adjustedTemperature;
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Deprecated
|
| 124 | private float getTemperature(final BlockPos pos, final int seaLevel) {
|
| 125 | long key = pos.asLong();
|
| 126 | Long2FloatLinkedOpenHashMap cache = this.temperatureCache.get();
|
| 127 | float cached = cache.get(key);
|
| 128 | if (!Float.isNaN(cached)) {
|
| 129 | return cached;
|
| 130 | }
|
| 131 |
|
| 132 | float temp = this.getHeightAdjustedTemperature(pos, seaLevel);
|
| 133 | if (cache.size() == 1024) {
|
| 134 | cache.removeFirstFloat();
|
| 135 | }
|
| 136 |
|
| 137 | cache.put(key, temp);
|
| 138 | return temp;
|
| 139 | }
|
| 140 |
|
| 141 | public boolean shouldFreeze(final LevelReader level, final BlockPos pos) {
|
| 142 | return this.shouldFreeze(level, pos, true);
|
| 143 | }
|
| 144 |
|
| 145 | public boolean shouldFreeze(final LevelReader level, final BlockPos pos, final boolean checkNeighbors) {
|
| 146 | if (this.warmEnoughToRain(pos, level.getSeaLevel())) {
|
| 147 | return false;
|
| 148 | }
|
| 149 |
|
| 150 | if (level.isInsideBuildHeight(pos.getY()) && level.getBrightness(LightLayer.BLOCK, pos) < 10) {
|
| 151 | BlockState blockState = level.getBlockState(pos);
|
| 152 | FluidState fluidState = level.getFluidState(pos);
|
| 153 | if (fluidState.is(Fluids.WATER) && blockState.getBlock() instanceof LiquidBlock) {
|
| 154 | if (!checkNeighbors) {
|
| 155 | return true;
|
| 156 | }
|
| 157 |
|
| 158 | boolean surroundedByWater = level.isWaterAt(pos.west()) && level.isWaterAt(pos.east()) && level.isWaterAt(pos.north()) && level.isWaterAt(pos.south());
|
| 159 | if (!surroundedByWater) {
|
| 160 | return true;
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | return false;
|
| 166 | }
|
| 167 |
|
| 168 | public boolean coldEnoughToSnow(final BlockPos pos, final int seaLevel) {
|
| 169 | return !this.warmEnoughToRain(pos, seaLevel);
|
| 170 | }
|
| 171 |
|
| 172 | public boolean warmEnoughToRain(final BlockPos pos, final int seaLevel) {
|
| 173 | return this.getTemperature(pos, seaLevel) >= 0.15F;
|
| 174 | }
|
| 175 |
|
| 176 | public boolean shouldMeltFrozenOceanIcebergSlightly(final BlockPos pos, final int seaLevel) {
|
| 177 | return this.getTemperature(pos, seaLevel) > 0.1F;
|
| 178 | }
|
| 179 |
|
| 180 | public boolean shouldSnow(final LevelReader level, final BlockPos pos) {
|
| 181 | if (this.getPrecipitationAt(pos, level.getSeaLevel()) != Biome.Precipitation.SNOW) {
|
| 182 | return false;
|
| 183 | }
|
| 184 |
|
| 185 | if (level.isInsideBuildHeight(pos.getY()) && level.getBrightness(LightLayer.BLOCK, pos) < 10) {
|
| 186 | BlockState state = level.getBlockState(pos);
|
| 187 | if ((state.isAir() || state.is(Blocks.SNOW)) && Blocks.SNOW.defaultBlockState().canSurvive(level, pos)) {
|
| 188 | return true;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | return false;
|
| 193 | }
|
| 194 |
|
| 195 | public BiomeGenerationSettings getGenerationSettings() {
|
| 196 | return this.generationSettings;
|
| 197 | }
|
| 198 |
|
| 199 | public int getGrassColor(final double x, final double z) {
|
| 200 | int baseGrassColor = this.getBaseGrassColor();
|
| 201 | return this.specialEffects.grassColorModifier().modifyColor(x, z, baseGrassColor);
|
| 202 | }
|
| 203 |
|
| 204 | private int getBaseGrassColor() {
|
| 205 | Optional<Integer> colorOverride = this.specialEffects.grassColorOverride();
|
| 206 | return colorOverride.isPresent() ? colorOverride.get() : this.getGrassColorFromTexture();
|
| 207 | }
|
| 208 |
|
| 209 | private int getGrassColorFromTexture() {
|
| 210 | double temp = Mth.clamp(this.climateSettings.temperature, 0.0F, 1.0F);
|
| 211 | double rain = Mth.clamp(this.climateSettings.downfall, 0.0F, 1.0F);
|
| 212 | return GrassColor.get(temp, rain);
|
| 213 | }
|
| 214 |
|
| 215 | public int getFoliageColor() {
|
| 216 | return this.specialEffects.foliageColorOverride().orElseGet(this::getFoliageColorFromTexture);
|
| 217 | }
|
| 218 |
|
| 219 | private int getFoliageColorFromTexture() {
|
| 220 | double temp = Mth.clamp(this.climateSettings.temperature, 0.0F, 1.0F);
|
| 221 | double rain = Mth.clamp(this.climateSettings.downfall, 0.0F, 1.0F);
|
| 222 | return FoliageColor.get(temp, rain);
|
| 223 | }
|
| 224 |
|
| 225 | public int getDryFoliageColor() {
|
| 226 | return this.specialEffects.dryFoliageColorOverride().orElseGet(this::getDryFoliageColorFromTexture);
|
| 227 | }
|
| 228 |
|
| 229 | private int getDryFoliageColorFromTexture() {
|
| 230 | double temp = Mth.clamp(this.climateSettings.temperature, 0.0F, 1.0F);
|
| 231 | double rain = Mth.clamp(this.climateSettings.downfall, 0.0F, 1.0F);
|
| 232 | return DryFoliageColor.get(temp, rain);
|
| 233 | }
|
| 234 |
|
| 235 | public float getBaseTemperature() {
|
| 236 | return this.climateSettings.temperature;
|
| 237 | }
|
| 238 |
|
| 239 | public EnvironmentAttributeMap getAttributes() {
|
| 240 | return this.attributes;
|
| 241 | }
|
| 242 |
|
| 243 | public BiomeSpecialEffects getSpecialEffects() {
|
| 244 | return this.specialEffects;
|
| 245 | }
|
| 246 |
|
| 247 | public int getWaterColor() {
|
| 248 | return this.specialEffects.waterColor();
|
| 249 | }
|
| 250 |
|
| 251 | public static class BiomeBuilder {
|
| 252 | private boolean hasPrecipitation = true;
|
| 253 | private @Nullable Float temperature;
|
| 254 | private Biome.TemperatureModifier temperatureModifier = Biome.TemperatureModifier.NONE;
|
| 255 | private @Nullable Float downfall;
|
| 256 | private final EnvironmentAttributeMap.Builder attributes = EnvironmentAttributeMap.builder();
|
| 257 | private @Nullable BiomeSpecialEffects specialEffects;
|
| 258 | private @Nullable BiomeGenerationSettings generationSettings;
|
| 259 |
|
| 260 | public Biome.BiomeBuilder hasPrecipitation(final boolean hasPrecipitation) {
|
| 261 | this.hasPrecipitation = hasPrecipitation;
|
| 262 | return this;
|
| 263 | }
|
| 264 |
|
| 265 | public Biome.BiomeBuilder temperature(final float temperature) {
|
| 266 | this.temperature = temperature;
|
| 267 | return this;
|
| 268 | }
|
| 269 |
|
| 270 | public Biome.BiomeBuilder downfall(final float downfall) {
|
| 271 | this.downfall = downfall;
|
| 272 | return this;
|
| 273 | }
|
| 274 |
|
| 275 | public Biome.BiomeBuilder putAttributes(final EnvironmentAttributeMap attributes) {
|
| 276 | this.attributes.putAll(attributes);
|
| 277 | return this;
|
| 278 | }
|
| 279 |
|
| 280 | public Biome.BiomeBuilder putAttributes(final EnvironmentAttributeMap.Builder attributes) {
|
| 281 | return this.putAttributes(attributes.build());
|
| 282 | }
|
| 283 |
|
| 284 | public <Value> Biome.BiomeBuilder setAttribute(final EnvironmentAttribute<Value> attribute, final Value value) {
|
| 285 | this.attributes.set(attribute, value);
|
| 286 | return this;
|
| 287 | }
|
| 288 |
|
| 289 | public <Value, Parameter> Biome.BiomeBuilder modifyAttribute(
|
| 290 | final EnvironmentAttribute<Value> attribute, final AttributeModifier<Value, Parameter> modifier, final Parameter value
|
| 291 | ) {
|
| 292 | this.attributes.modify(attribute, modifier, value);
|
| 293 | return this;
|
| 294 | }
|
| 295 |
|
| 296 | public Biome.BiomeBuilder specialEffects(final BiomeSpecialEffects specialEffects) {
|
| 297 | this.specialEffects = specialEffects;
|
| 298 | return this;
|
| 299 | }
|
| 300 |
|
| 301 | public Biome.BiomeBuilder mobSpawnSettings(final MobSpawnSettings mobSpawnSettings) {
|
| 302 | this.modifyAttribute(EnvironmentAttributes.NATURAL_MOB_SPAWNS, MobSpawnSettingsModifier.overlay(), mobSpawnSettings);
|
| 303 | return this;
|
| 304 | }
|
| 305 |
|
| 306 | public Biome.BiomeBuilder generationSettings(final BiomeGenerationSettings generationSettings) {
|
| 307 | this.generationSettings = generationSettings;
|
| 308 | return this;
|
| 309 | }
|
| 310 |
|
| 311 | public Biome.BiomeBuilder temperatureAdjustment(final Biome.TemperatureModifier temperatureModifier) {
|
| 312 | this.temperatureModifier = temperatureModifier;
|
| 313 | return this;
|
| 314 | }
|
| 315 |
|
| 316 | public Biome build() {
|
| 317 | if (this.temperature != null && this.downfall != null && this.specialEffects != null && this.generationSettings != null) {
|
| 318 | return new Biome(
|
| 319 | new Biome.ClimateSettings(this.hasPrecipitation, this.temperature, this.temperatureModifier, this.downfall),
|
| 320 | this.attributes.build(),
|
| 321 | this.specialEffects,
|
| 322 | this.generationSettings
|
| 323 | );
|
| 324 | } else {
|
| 325 | throw new IllegalStateException("You are missing parameters to build a proper biome\n" + this);
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | @Override
|
| 330 | public String toString() {
|
| 331 | return "BiomeBuilder{\nhasPrecipitation="
|
| 332 | + this.hasPrecipitation
|
| 333 | + ",\ntemperature="
|
| 334 | + this.temperature
|
| 335 | + ",\ntemperatureModifier="
|
| 336 | + this.temperatureModifier
|
| 337 | + ",\ndownfall="
|
| 338 | + this.downfall
|
| 339 | + ",\nspecialEffects="
|
| 340 | + this.specialEffects
|
| 341 | + ",\ngenerationSettings="
|
| 342 | + this.generationSettings
|
| 343 | + ",\n}";
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | private record ClimateSettings(boolean hasPrecipitation, float temperature, Biome.TemperatureModifier temperatureModifier, float downfall) {
|
| 348 | public static final MapCodec<Biome.ClimateSettings> CODEC = RecordCodecBuilder.mapCodec(
|
| 349 | i -> i.group(
|
| 350 | Codec.BOOL.fieldOf("has_precipitation").forGetter(b -> b.hasPrecipitation),
|
| 351 | Codec.FLOAT.fieldOf("temperature").forGetter(b -> b.temperature),
|
| 352 | Biome.TemperatureModifier.CODEC.optionalFieldOf("temperature_modifier", Biome.TemperatureModifier.NONE).forGetter(b -> b.temperatureModifier),
|
| 353 | Codec.FLOAT.fieldOf("downfall").forGetter(b -> b.downfall)
|
| 354 | )
|
| 355 | .apply(i, Biome.ClimateSettings::new)
|
| 356 | );
|
| 357 | }
|
| 358 |
|
| 359 | public enum Precipitation implements StringRepresentable {
|
| 360 | NONE("none"),
|
| 361 | RAIN("rain"),
|
| 362 | SNOW("snow");
|
| 363 |
|
| 364 | public static final Codec<Biome.Precipitation> CODEC = StringRepresentable.fromEnum(Biome.Precipitation::values);
|
| 365 | private final String name;
|
| 366 |
|
| 367 | Precipitation(final String name) {
|
| 368 | this.name = name;
|
| 369 | }
|
| 370 |
|
| 371 | @Override
|
| 372 | public String getSerializedName() {
|
| 373 | return this.name;
|
| 374 | }
|
| 375 | }
|
| 376 |
|
| 377 | public enum TemperatureModifier implements StringRepresentable {
|
| 378 | NONE("none") {
|
| 379 | @Override
|
| 380 | public float modifyTemperature(final BlockPos pos, final float baseTemperature) {
|
| 381 | return baseTemperature;
|
| 382 | }
|
| 383 | },
|
| 384 | FROZEN("frozen") {
|
| 385 | @Override
|
| 386 | public float modifyTemperature(final BlockPos pos, final float baseTemperature) {
|
| 387 | double groundValueLargeVariation = Biome.FROZEN_TEMPERATURE_NOISE.get(pos.getX() * 0.05, pos.getZ() * 0.05) * 7.0F;
|
| 388 | double groundValueEdgeVariation = Biome.BIOME_INFO_NOISE.get(pos.getX() * 0.2, pos.getZ() * 0.2);
|
| 389 | double icePatches = groundValueLargeVariation + groundValueEdgeVariation;
|
| 390 | if (icePatches < 0.3) {
|
| 391 | double groundValueSmallVariation = Biome.BIOME_INFO_NOISE.get(pos.getX() * 0.09, pos.getZ() * 0.09);
|
| 392 | if (groundValueSmallVariation < 0.8) {
|
| 393 | return 0.2F;
|
| 394 | }
|
| 395 | }
|
| 396 |
|
| 397 | return baseTemperature;
|
| 398 | }
|
| 399 | };
|
| 400 |
|
| 401 | private final String name;
|
| 402 | public static final Codec<Biome.TemperatureModifier> CODEC = StringRepresentable.fromEnum(Biome.TemperatureModifier::values);
|
| 403 |
|
| 404 | public abstract float modifyTemperature(final BlockPos pos, final float baseTemperature);
|
| 405 |
|
| 406 | TemperatureModifier(final String name) {
|
| 407 | this.name = name;
|
| 408 | }
|
| 409 |
|
| 410 | public String getName() {
|
| 411 | return this.name;
|
| 412 | }
|
| 413 |
|
| 414 | @Override
|
| 415 | public String getSerializedName() {
|
| 416 | return this.name;
|
| 417 | }
|
| 418 | }
|
| 419 | }
|
| 420 |
|