2025-11-24 22:52:51 +03:00

98 lines
3.1 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.google.common.annotations.VisibleForTesting
*/
package net.minecraft.world.level.levelgen;
import com.google.common.annotations.VisibleForTesting;
import java.util.concurrent.atomic.AtomicLong;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.util.ThreadingDetector;
import net.minecraft.world.level.levelgen.BitRandomSource;
import net.minecraft.world.level.levelgen.MarsagliaPolarGaussian;
import net.minecraft.world.level.levelgen.PositionalRandomFactory;
public class LegacyRandomSource
implements BitRandomSource {
private static final int MODULUS_BITS = 48;
private static final long MODULUS_MASK = 0xFFFFFFFFFFFFL;
private static final long MULTIPLIER = 25214903917L;
private static final long INCREMENT = 11L;
private final AtomicLong seed = new AtomicLong();
private final MarsagliaPolarGaussian gaussianSource = new MarsagliaPolarGaussian(this);
public LegacyRandomSource(long seed) {
this.setSeed(seed);
}
@Override
public RandomSource fork() {
return new LegacyRandomSource(this.nextLong());
}
@Override
public PositionalRandomFactory forkPositional() {
return new LegacyPositionalRandomFactory(this.nextLong());
}
@Override
public void setSeed(long seed) {
if (!this.seed.compareAndSet(this.seed.get(), (seed ^ 0x5DEECE66DL) & 0xFFFFFFFFFFFFL)) {
throw ThreadingDetector.makeThreadingException("LegacyRandomSource", null);
}
this.gaussianSource.reset();
}
@Override
public int next(int bits) {
long newSeed;
long oldSeed = this.seed.get();
if (!this.seed.compareAndSet(oldSeed, newSeed = oldSeed * 25214903917L + 11L & 0xFFFFFFFFFFFFL)) {
throw ThreadingDetector.makeThreadingException("LegacyRandomSource", null);
}
return (int)(newSeed >> 48 - bits);
}
@Override
public double nextGaussian() {
return this.gaussianSource.nextGaussian();
}
public static class LegacyPositionalRandomFactory
implements PositionalRandomFactory {
private final long seed;
public LegacyPositionalRandomFactory(long seed) {
this.seed = seed;
}
@Override
public RandomSource at(int x, int y, int z) {
long positionalSeed = Mth.getSeed(x, y, z);
long randomSeed = positionalSeed ^ this.seed;
return new LegacyRandomSource(randomSeed);
}
@Override
public RandomSource fromHashOf(String name) {
int positionalSeed = name.hashCode();
return new LegacyRandomSource((long)positionalSeed ^ this.seed);
}
@Override
public RandomSource fromSeed(long seed) {
return new LegacyRandomSource(seed);
}
@Override
@VisibleForTesting
public void parityConfigString(StringBuilder sb) {
sb.append("LegacyPositionalRandomFactory{").append(this.seed).append("}");
}
}
}