| 1 | package net.minecraft.world.level.biome;
|
| 2 |
|
| 3 | import com.google.common.hash.Hashing;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.QuartPos;
|
| 7 | import net.minecraft.util.LinearCongruentialGenerator;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 |
|
| 10 | public class BiomeManager {
|
| 11 | public static final int CHUNK_CENTER_QUART = QuartPos.fromBlock(8);
|
| 12 | private static final int ZOOM_BITS = 2;
|
| 13 | private static final int ZOOM = 4;
|
| 14 | private static final int ZOOM_MASK = 3;
|
| 15 | private final BiomeResolver noiseBiomeSource;
|
| 16 | private final long biomeZoomSeed;
|
| 17 |
|
| 18 | public BiomeManager(final BiomeResolver noiseBiomeSource, final long seed) {
|
| 19 | this.noiseBiomeSource = noiseBiomeSource;
|
| 20 | this.biomeZoomSeed = seed;
|
| 21 | }
|
| 22 |
|
| 23 | public static long obfuscateSeed(final long seed) {
|
| 24 | return Hashing.sha256().hashLong(seed).asLong();
|
| 25 | }
|
| 26 |
|
| 27 | public BiomeManager withDifferentSource(final BiomeResolver biomeSource) {
|
| 28 | return new BiomeManager(biomeSource, this.biomeZoomSeed);
|
| 29 | }
|
| 30 |
|
| 31 | public Holder<Biome> getBiome(final BlockPos pos) {
|
| 32 | return this.getBiome(pos.getX(), pos.getY(), pos.getZ());
|
| 33 | }
|
| 34 |
|
| 35 | public Holder<Biome> getBiome(final int x, final int y, final int z) {
|
| 36 | int absX = x - 2;
|
| 37 | int absY = y - 2;
|
| 38 | int absZ = z - 2;
|
| 39 | int parentX = absX >> 2;
|
| 40 | int parentY = absY >> 2;
|
| 41 | int parentZ = absZ >> 2;
|
| 42 | double fractX = (absX & 3) / 4.0;
|
| 43 | double fractY = (absY & 3) / 4.0;
|
| 44 | double fractZ = (absZ & 3) / 4.0;
|
| 45 | int minI = 0;
|
| 46 | double minFiddledDistance = Double.POSITIVE_INFINITY;
|
| 47 |
|
| 48 | for (int i = 0; i < 8; i++) {
|
| 49 | boolean xEven = (i & 4) == 0;
|
| 50 | boolean yEven = (i & 2) == 0;
|
| 51 | boolean zEven = (i & 1) == 0;
|
| 52 | int cornerX = xEven ? parentX : parentX + 1;
|
| 53 | int cornerY = yEven ? parentY : parentY + 1;
|
| 54 | int cornerZ = zEven ? parentZ : parentZ + 1;
|
| 55 | double distanceX = xEven ? fractX : fractX - 1.0;
|
| 56 | double distanceY = yEven ? fractY : fractY - 1.0;
|
| 57 | double distanceZ = zEven ? fractZ : fractZ - 1.0;
|
| 58 | double next = getFiddledDistance(this.biomeZoomSeed, cornerX, cornerY, cornerZ, distanceX, distanceY, distanceZ);
|
| 59 | if (minFiddledDistance > next) {
|
| 60 | minI = i;
|
| 61 | minFiddledDistance = next;
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | int biomeX = (minI & 4) == 0 ? parentX : parentX + 1;
|
| 66 | int biomeY = (minI & 2) == 0 ? parentY : parentY + 1;
|
| 67 | int biomeZ = (minI & 1) == 0 ? parentZ : parentZ + 1;
|
| 68 | return this.noiseBiomeSource.getNoiseBiome(biomeX, biomeY, biomeZ);
|
| 69 | }
|
| 70 |
|
| 71 | public Holder<Biome> getNoiseBiomeAtPosition(final double x, final double y, final double z) {
|
| 72 | int quartX = QuartPos.fromBlock(Mth.floor(x));
|
| 73 | int quartY = QuartPos.fromBlock(Mth.floor(y));
|
| 74 | int quartZ = QuartPos.fromBlock(Mth.floor(z));
|
| 75 | return this.getNoiseBiomeAtQuart(quartX, quartY, quartZ);
|
| 76 | }
|
| 77 |
|
| 78 | public Holder<Biome> getNoiseBiomeAtPosition(final BlockPos blockPos) {
|
| 79 | int quartX = QuartPos.fromBlock(blockPos.getX());
|
| 80 | int quartY = QuartPos.fromBlock(blockPos.getY());
|
| 81 | int quartZ = QuartPos.fromBlock(blockPos.getZ());
|
| 82 | return this.getNoiseBiomeAtQuart(quartX, quartY, quartZ);
|
| 83 | }
|
| 84 |
|
| 85 | public Holder<Biome> getNoiseBiomeAtQuart(final int quartX, final int quartY, final int quartZ) {
|
| 86 | return this.noiseBiomeSource.getNoiseBiome(quartX, quartY, quartZ);
|
| 87 | }
|
| 88 |
|
| 89 | private static double getFiddledDistance(
|
| 90 | final long seed, final int xRandom, final int yRandom, final int zRandom, final double distanceX, final double distanceY, final double distanceZ
|
| 91 | ) {
|
| 92 | long rval = seed;
|
| 93 | rval = LinearCongruentialGenerator.next(rval, xRandom);
|
| 94 | rval = LinearCongruentialGenerator.next(rval, yRandom);
|
| 95 | rval = LinearCongruentialGenerator.next(rval, zRandom);
|
| 96 | rval = LinearCongruentialGenerator.next(rval, xRandom);
|
| 97 | rval = LinearCongruentialGenerator.next(rval, yRandom);
|
| 98 | rval = LinearCongruentialGenerator.next(rval, zRandom);
|
| 99 | double fiddleX = getFiddle(rval);
|
| 100 | rval = LinearCongruentialGenerator.next(rval, seed);
|
| 101 | double fiddleY = getFiddle(rval);
|
| 102 | rval = LinearCongruentialGenerator.next(rval, seed);
|
| 103 | double fiddleZ = getFiddle(rval);
|
| 104 | return Mth.square(distanceZ + fiddleZ) + Mth.square(distanceY + fiddleY) + Mth.square(distanceX + fiddleX);
|
| 105 | }
|
| 106 |
|
| 107 | private static double getFiddle(final long rval) {
|
| 108 | double uniform = Math.floorMod(rval >> 24, 1024) / 1024.0;
|
| 109 | return (uniform - 0.5) * 0.9;
|
| 110 | }
|
| 111 | }
|
| 112 |
|