| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 |
|
| 8 | public class XoroshiroRandomSource implements RandomSource {
|
| 9 | private static final float FLOAT_UNIT = 5.9604645E-8F;
|
| 10 | private static final double DOUBLE_UNIT = 1.110223E-16F;
|
| 11 | public static final Codec<XoroshiroRandomSource> CODEC = Xoroshiro128PlusPlus.CODEC
|
| 12 | .xmap(generator -> new XoroshiroRandomSource(generator), source -> source.randomNumberGenerator);
|
| 13 | private Xoroshiro128PlusPlus randomNumberGenerator;
|
| 14 | private final MarsagliaPolarGaussian gaussianSource = new MarsagliaPolarGaussian(this);
|
| 15 |
|
| 16 | public XoroshiroRandomSource(final long seed) {
|
| 17 | this.randomNumberGenerator = new Xoroshiro128PlusPlus(RandomSupport.upgradeSeedTo128bit(seed));
|
| 18 | }
|
| 19 |
|
| 20 | public XoroshiroRandomSource(final RandomSupport.Seed128bit seed) {
|
| 21 | this.randomNumberGenerator = new Xoroshiro128PlusPlus(seed);
|
| 22 | }
|
| 23 |
|
| 24 | public XoroshiroRandomSource(final long seedLo, final long seedHi) {
|
| 25 | this.randomNumberGenerator = new Xoroshiro128PlusPlus(seedLo, seedHi);
|
| 26 | }
|
| 27 |
|
| 28 | private XoroshiroRandomSource(final Xoroshiro128PlusPlus randomNumberGenerator) {
|
| 29 | this.randomNumberGenerator = randomNumberGenerator;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public RandomSource fork() {
|
| 34 | return new XoroshiroRandomSource(this.randomNumberGenerator.nextLong(), this.randomNumberGenerator.nextLong());
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public PositionalRandomFactory forkPositional() {
|
| 39 | return new XoroshiroRandomSource.XoroshiroPositionalRandomFactory(this.randomNumberGenerator.nextLong(), this.randomNumberGenerator.nextLong());
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public void setSeed(final long seed) {
|
| 44 | this.randomNumberGenerator = new Xoroshiro128PlusPlus(RandomSupport.upgradeSeedTo128bit(seed));
|
| 45 | this.gaussianSource.reset();
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public int nextInt() {
|
| 50 | return (int)this.randomNumberGenerator.nextLong();
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public int nextInt(final int bound) {
|
| 55 | if (bound <= 0) {
|
| 56 | throw new IllegalArgumentException("Bound must be positive");
|
| 57 | }
|
| 58 |
|
| 59 | long randomBits = Integer.toUnsignedLong(this.nextInt());
|
| 60 | long multipliedRandomBits = randomBits * bound;
|
| 61 | long fractionalPart = multipliedRandomBits & 4294967295L;
|
| 62 | if (fractionalPart < bound) {
|
| 63 | for (int unbiasedBucketsStartIndex = Integer.remainderUnsigned(~bound + 1, bound);
|
| 64 | fractionalPart < unbiasedBucketsStartIndex;
|
| 65 | fractionalPart = multipliedRandomBits & 4294967295L
|
| 66 | ) {
|
| 67 | randomBits = Integer.toUnsignedLong(this.nextInt());
|
| 68 | multipliedRandomBits = randomBits * bound;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | long integerPart = multipliedRandomBits >> 32;
|
| 73 | return (int)integerPart;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public long nextLong() {
|
| 78 | return this.randomNumberGenerator.nextLong();
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public boolean nextBoolean() {
|
| 83 | return (this.randomNumberGenerator.nextLong() & 1L) != 0L;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public float nextFloat() {
|
| 88 | return (float)this.nextBits(24) * 5.9604645E-8F;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public double nextDouble() {
|
| 93 | return this.nextBits(53) * 1.110223E-16F;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public double nextGaussian() {
|
| 98 | return this.gaussianSource.nextGaussian();
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public void consumeCount(final int rounds) {
|
| 103 | for (int i = 0; i < rounds; i++) {
|
| 104 | this.randomNumberGenerator.nextLong();
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private long nextBits(final int bits) {
|
| 109 | return this.randomNumberGenerator.nextLong() >>> 64 - bits;
|
| 110 | }
|
| 111 |
|
| 112 | public static class XoroshiroPositionalRandomFactory implements PositionalRandomFactory {
|
| 113 | private final long seedLo;
|
| 114 | private final long seedHi;
|
| 115 |
|
| 116 | public XoroshiroPositionalRandomFactory(final long seedLo, final long seedHi) {
|
| 117 | this.seedLo = seedLo;
|
| 118 | this.seedHi = seedHi;
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public RandomSource at(final int x, final int y, final int z) {
|
| 123 | long positionalSeed = Mth.getSeed(x, y, z);
|
| 124 | long randomSeed = positionalSeed ^ this.seedLo;
|
| 125 | return new XoroshiroRandomSource(randomSeed, this.seedHi);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public RandomSource fromHashOf(final String name) {
|
| 130 | RandomSupport.Seed128bit seed = RandomSupport.seedFromHashOf(name);
|
| 131 | return new XoroshiroRandomSource(seed.xor(this.seedLo, this.seedHi));
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public RandomSource fromSeed(final long seed) {
|
| 136 | return new XoroshiroRandomSource(seed ^ this.seedLo, seed ^ this.seedHi);
|
| 137 | }
|
| 138 |
|
| 139 | @VisibleForTesting
|
| 140 | @Override
|
| 141 | public void parityConfigString(final StringBuilder sb) {
|
| 142 | sb.append("seedLo: ").append(this.seedLo).append(", seedHi: ").append(this.seedHi);
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|