From 1ca2348adf88fc693406d28e6c64d8860e164010 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 8 Mar 2025 17:18:45 +0700 Subject: [PATCH] Provide PCG32 wrapper of RandomSource --- gradle.properties | 2 +- .../mc/otm/core/util/LCG64RandomSource.kt | 5 -- .../mc/otm/core/util/PCG32RandomSource.kt | 53 +++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt diff --git a/gradle.properties b/gradle.properties index fa23fe2bf..4b7460417 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,7 +22,7 @@ mixin_version=0.8.5 neogradle.subsystems.parchment.minecraftVersion=1.21.1 neogradle.subsystems.parchment.mappingsVersion=2024.11.17 -kommons_version=3.2.1 +kommons_version=3.3.0 caffeine_cache_version=3.1.5 jei_version=19.16.4.171 diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/LCG64RandomSource.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/LCG64RandomSource.kt index af4f1bdec..60f07343c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/LCG64RandomSource.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/LCG64RandomSource.kt @@ -56,9 +56,4 @@ class LCG64RandomSource(seed: Long = RandomSupport.generateUniqueSeed()) : LCG64 throw UnsupportedOperationException() } } - - companion object { - const val MULTIPLIER = 6364136223846793005 - const val INCREMENT = 1442695040888963407 - } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt new file mode 100644 index 000000000..da61dbff2 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt @@ -0,0 +1,53 @@ +package ru.dbotthepony.mc.otm.core.util + +import net.minecraft.util.Mth +import net.minecraft.util.RandomSource +import net.minecraft.world.level.levelgen.LegacyRandomSource +import net.minecraft.world.level.levelgen.MarsagliaPolarGaussian +import net.minecraft.world.level.levelgen.PositionalRandomFactory +import net.minecraft.world.level.levelgen.RandomSupport +import ru.dbotthepony.kommons.random.LCG64Random +import ru.dbotthepony.kommons.random.PCG32Random +import java.lang.StringBuilder + +/** + * @see PCG32Random + */ +class PCG32RandomSource(seed: Long = RandomSupport.generateUniqueSeed()) : PCG32Random(seed), IRandomSourceGenerator { + private val gaussian = MarsagliaPolarGaussian(this) + + override fun setSeed(seed: Long) { + this.seed = seed + gaussian.reset() + } + + override fun nextGaussian(): Double { + return gaussian.nextGaussian() + } + + override fun fork(): RandomSource { + return PCG32RandomSource(nextLong()) + } + + override fun forkPositional(): PositionalRandomFactory { + return Positional(nextLong()) + } + + class Positional(val seed: Long) : PositionalRandomFactory { + override fun at(x: Int, y: Int, z: Int): RandomSource { + return PCG32RandomSource(Mth.getSeed(x, y, z).xor(seed)) + } + + override fun fromHashOf(name: String): RandomSource { + return PCG32RandomSource(name.hashCode().toLong().xor(seed)) + } + + override fun fromSeed(seed: Long): RandomSource { + return PCG32RandomSource(seed) + } + + override fun parityConfigString(builder: StringBuilder) { + throw UnsupportedOperationException() + } + } +}