| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.concurrent.atomic.AtomicLong;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.util.ThreadingDetector;
|
| 8 |
|
| 9 | public class LegacyRandomSource implements BitRandomSource {
|
| 10 | private static final int MODULUS_BITS = 48;
|
| 11 | private static final long MODULUS_MASK = 281474976710655L;
|
| 12 | private static final long MULTIPLIER = 25214903917L;
|
| 13 | private static final long INCREMENT = 11L;
|
| 14 | private final AtomicLong seed = new AtomicLong();
|
| 15 | private final MarsagliaPolarGaussian gaussianSource = new MarsagliaPolarGaussian(this);
|
| 16 |
|
| 17 | public LegacyRandomSource(final long seed) {
|
| 18 | this.setSeed(seed);
|
| 19 | }
|
| 20 |
|
| 21 | @Override
|
| 22 | public RandomSource fork() {
|
| 23 | return new LegacyRandomSource(this.nextLong());
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public PositionalRandomFactory forkPositional() {
|
| 28 | return new LegacyRandomSource.LegacyPositionalRandomFactory(this.nextLong());
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public void setSeed(final long seed) {
|
| 33 | if (!this.seed.compareAndSet(this.seed.get(), (seed ^ 25214903917L) & 281474976710655L)) {
|
| 34 | throw ThreadingDetector.makeThreadingException("LegacyRandomSource", null);
|
| 35 | }
|
| 36 |
|
| 37 | this.gaussianSource.reset();
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public int next(final int bits) {
|
| 42 | long oldSeed = this.seed.get();
|
| 43 | long newSeed = oldSeed * 25214903917L + 11L & 281474976710655L;
|
| 44 | if (!this.seed.compareAndSet(oldSeed, newSeed)) {
|
| 45 | throw ThreadingDetector.makeThreadingException("LegacyRandomSource", null);
|
| 46 | } else {
|
| 47 | return (int)(newSeed >> 48 - bits);
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public double nextGaussian() {
|
| 53 | return this.gaussianSource.nextGaussian();
|
| 54 | }
|
| 55 |
|
| 56 | public static class LegacyPositionalRandomFactory implements PositionalRandomFactory {
|
| 57 | private final long seed;
|
| 58 |
|
| 59 | public LegacyPositionalRandomFactory(final long seed) {
|
| 60 | this.seed = seed;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public RandomSource at(final int x, final int y, final int z) {
|
| 65 | long positionalSeed = Mth.getSeed(x, y, z);
|
| 66 | long randomSeed = positionalSeed ^ this.seed;
|
| 67 | return new LegacyRandomSource(randomSeed);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public RandomSource fromHashOf(final String name) {
|
| 72 | int positionalSeed = name.hashCode();
|
| 73 | return new LegacyRandomSource(positionalSeed ^ this.seed);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public RandomSource fromSeed(final long seed) {
|
| 78 | return new LegacyRandomSource(seed);
|
| 79 | }
|
| 80 |
|
| 81 | @VisibleForTesting
|
| 82 | @Override
|
| 83 | public void parityConfigString(final StringBuilder sb) {
|
| 84 | sb.append("LegacyPositionalRandomFactory{").append(this.seed).append("}");
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 |
|