| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.concurrent.ConcurrentHashMap;
|
| 7 | import java.util.concurrent.locks.ReentrantLock;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.HolderGetter;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.resources.ResourceKey;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.util.VisibleForDebug;
|
| 14 | import net.minecraft.world.level.biome.Climate;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.levelgen.densityfunction.DensityBufferPool;
|
| 17 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 18 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunctionCompiler;
|
| 19 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 20 | import net.minecraft.world.level.levelgen.densityfunction.DensitySamplerSet;
|
| 21 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 22 | import net.minecraft.world.level.levelgen.material.MaterialSystem;
|
| 23 | import net.minecraft.world.level.levelgen.synth.BlendedNoise;
|
| 24 | import net.minecraft.world.level.levelgen.synth.Noise;
|
| 25 | import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
| 26 |
|
| 27 | public final class RandomState {
|
| 28 | private static final int MAX_BUFFER_POOLS = 16;
|
| 29 | private static final int MAX_BUFFER_AGE_TICKS = 20;
|
| 30 | private final long seed;
|
| 31 | private final PositionalRandomFactory random;
|
| 32 | private final HolderGetter<NormalNoise> noises;
|
| 33 | private final NoiseRouter router;
|
| 34 | private final MaterialSystem materialSystem;
|
| 35 | private final Map<ResourceKey<NormalNoise>, Noise> noiseInstances;
|
| 36 | private final Map<Identifier, PositionalRandomFactory> positionalRandoms;
|
| 37 | private final DensityFunctionCompiler densityFunctionCompiler;
|
| 38 | private final ReentrantLock densityBufferPoolLock = new ReentrantLock();
|
| 39 | private final List<DensityBufferPool> densityBufferPools = new ArrayList<>(16);
|
| 40 |
|
| 41 | public static RandomState create(final HolderGetter<NormalNoise> noises, final long seed, final NoiseGeneratorSettings settings) {
|
| 42 | return create(noises, seed, settings.useLegacyRandomSource(), settings.defaultBlock(), settings.seaLevel(), settings.noiseRouter());
|
| 43 | }
|
| 44 |
|
| 45 | public static RandomState create(
|
| 46 | final HolderGetter<NormalNoise> noises,
|
| 47 | final long seed,
|
| 48 | final boolean useLegacyRandom,
|
| 49 | final BlockState defaultBlock,
|
| 50 | final int seaLevel,
|
| 51 | final NoiseRouter noiseRouter
|
| 52 | ) {
|
| 53 | return new RandomState(noises, seed, useLegacyRandom, defaultBlock, seaLevel, noiseRouter);
|
| 54 | }
|
| 55 |
|
| 56 | private RandomState(
|
| 57 | final HolderGetter<NormalNoise> noises,
|
| 58 | final long seed,
|
| 59 | final boolean useLegacyRandom,
|
| 60 | final BlockState defaultBlock,
|
| 61 | final int seaLevel,
|
| 62 | final NoiseRouter router
|
| 63 | ) {
|
| 64 | WorldgenRandom.Algorithm randomAlgorithm = useLegacyRandom ? WorldgenRandom.Algorithm.LEGACY : WorldgenRandom.Algorithm.XOROSHIRO;
|
| 65 | this.seed = seed;
|
| 66 | this.random = randomAlgorithm.newInstance(seed).forkPositional();
|
| 67 | this.noises = noises;
|
| 68 | this.router = router;
|
| 69 | this.noiseInstances = new ConcurrentHashMap<>();
|
| 70 | this.positionalRandoms = new ConcurrentHashMap<>();
|
| 71 | this.materialSystem = new MaterialSystem(this, defaultBlock, seaLevel, router.chunkSurfaceLevel(), this.random);
|
| 72 | this.densityFunctionCompiler = new DensityFunctionCompiler(
|
| 73 | new DensityFunction.CompileContext() {
|
| 74 | private RandomSource newLegacyInstance(final long seedOffset) {
|
| 75 | return new LegacyRandomSource(seed + seedOffset);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public Noise createNoiseSampler(final Holder<NormalNoise> parameters) {
|
| 80 | if (parameters.is(Noises.TEMPERATURE_NETHER)) {
|
| 81 | return parameters.value().createForLegacyNetherBiome(this.newLegacyInstance(0L));
|
| 82 | } else {
|
| 83 | return parameters.is(Noises.VEGETATION_NETHER)
|
| 84 | ? parameters.value().createForLegacyNetherBiome(this.newLegacyInstance(1L))
|
| 85 | : RandomState.this.getOrCreateNoise(parameters.unwrapKey().orElseThrow());
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public RandomSource createRandom(final Identifier seed) {
|
| 91 | return useLegacyRandom && seed.equals(BlendedNoise.NOISE_SEED) ? this.newLegacyInstance(0L) : RandomState.this.random.fromHashOf(seed);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public RandomSource createEndIslandRandom() {
|
| 96 | return new LegacyRandomSource(seed);
|
| 97 | }
|
| 98 | }
|
| 99 | );
|
| 100 | }
|
| 101 |
|
| 102 | public DensitySamplerSet samplersWithContext(final SamplerContext context) {
|
| 103 | return new DensitySamplerSet() {
|
| 104 | @Override
|
| 105 | public DensitySampler.Bound get(final DensityFunction function) {
|
| 106 | return RandomState.this.getSampler(function).bind(context);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public float sampleValue(final DensityFunction function, final int blockX, final int blockY, final int blockZ) {
|
| 111 | return RandomState.this.getSampler(function).sampleValue(context, blockX, blockY, blockZ);
|
| 112 | }
|
| 113 | };
|
| 114 | }
|
| 115 |
|
| 116 | public Climate.Sampler createClimateSampler(final SamplerContext context) {
|
| 117 | return this.router.createClimateSampler(this.samplersWithContext(context));
|
| 118 | }
|
| 119 |
|
| 120 | public Noise getOrCreateNoise(final ResourceKey<NormalNoise> noise) {
|
| 121 | return this.noiseInstances.computeIfAbsent(noise, key -> Noises.instantiate(this.noises, this.random, noise));
|
| 122 | }
|
| 123 |
|
| 124 | public PositionalRandomFactory getOrCreateRandomFactory(final Identifier name) {
|
| 125 | return this.positionalRandoms.computeIfAbsent(name, key -> this.random.fromHashOf(name).forkPositional());
|
| 126 | }
|
| 127 |
|
| 128 | public MaterialSystem surfaceSystem() {
|
| 129 | return this.materialSystem;
|
| 130 | }
|
| 131 |
|
| 132 | public DensitySampler getSampler(final DensityFunction function) {
|
| 133 | return this.densityFunctionCompiler.getSampler(function);
|
| 134 | }
|
| 135 |
|
| 136 | @VisibleForDebug
|
| 137 | public float sampleBlockValueUncached(final DensityFunction function, final int x, final int y, final int z) {
|
| 138 | return this.getSampler(function).sampleValue(SamplerContext.EMPTY_UNCACHED, x, y, z);
|
| 139 | }
|
| 140 |
|
| 141 | public DensityBufferPool acquireDensityBufferPool() {
|
| 142 | this.densityBufferPoolLock.lock();
|
| 143 |
|
| 144 | try {
|
| 145 | return this.densityBufferPools.isEmpty() ? new DensityBufferPool(20) : this.densityBufferPools.removeLast();
|
| 146 | } finally {
|
| 147 | this.densityBufferPoolLock.unlock();
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | public void releaseDensityBufferPool(final DensityBufferPool pool) {
|
| 152 | this.densityBufferPoolLock.lock();
|
| 153 |
|
| 154 | try {
|
| 155 | if (this.densityBufferPools.size() < 16) {
|
| 156 | this.densityBufferPools.add(pool);
|
| 157 | }
|
| 158 | } finally {
|
| 159 | this.densityBufferPoolLock.unlock();
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | public void garbageCollect() {
|
| 164 | this.densityBufferPoolLock.lock();
|
| 165 |
|
| 166 | try {
|
| 167 | this.densityBufferPools.removeIf(pool -> {
|
| 168 | pool.garbageCollect();
|
| 169 | return pool.isEmpty();
|
| 170 | });
|
| 171 | } finally {
|
| 172 | this.densityBufferPoolLock.unlock();
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | @Deprecated
|
| 177 | public long seed() {
|
| 178 | return this.seed;
|
| 179 | }
|
| 180 | }
|
| 181 |
|