From 77821768bcbadd2492bc70b599e353f346f7e2cd Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 19 Jan 2025 13:22:56 +0700 Subject: [PATCH] Better tritanium ore placement --- .../ru/dbotthepony/mc/otm/datagen/WorldGen.kt | 12 ++-- .../mc/otm/OverdriveThatMatters.kt | 1 + .../kotlin/ru/dbotthepony/mc/otm/core/Ext.kt | 44 +++++++++++++ .../world/StandardDeviationHeightProvider.kt | 64 +++++++++++++++++++ .../mc/otm/registry/MHeightProviders.kt | 16 +++++ 5 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/data/world/StandardDeviationHeightProvider.kt create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/registry/MHeightProviders.kt diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/WorldGen.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/WorldGen.kt index 09e1328a8..38d45dc92 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/WorldGen.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/WorldGen.kt @@ -11,6 +11,7 @@ import net.minecraft.world.level.levelgen.VerticalAnchor import net.minecraft.world.level.levelgen.feature.ConfiguredFeature import net.minecraft.world.level.levelgen.feature.Feature import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration +import net.minecraft.world.level.levelgen.heightproviders.VeryBiasedToBottomHeight import net.minecraft.world.level.levelgen.placement.CountPlacement import net.minecraft.world.level.levelgen.placement.HeightRangePlacement import net.minecraft.world.level.levelgen.placement.InSquarePlacement @@ -19,6 +20,7 @@ import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest import net.neoforged.neoforge.common.world.BiomeModifier import net.neoforged.neoforge.registries.NeoForgeRegistries import ru.dbotthepony.mc.otm.core.math.Decimal +import ru.dbotthepony.mc.otm.data.world.StandardDeviationHeightProvider import ru.dbotthepony.mc.otm.registry.MBlocks import ru.dbotthepony.mc.otm.registry.MWorldGenFeatures import ru.dbotthepony.mc.otm.worldgen.feature.BlackHolePlacerFeature @@ -63,18 +65,18 @@ fun registerPlacedFeatures(context: BootstrapContext) { context.register(PlacedFeatures.NORMAL_TRITANIUM, PlacedFeature( ore, listOf( - CountPlacement.of(8), + CountPlacement.of(6), InSquarePlacement.spread(), - HeightRangePlacement.triangle(VerticalAnchor.absolute(0), VerticalAnchor.absolute(50)) + HeightRangePlacement.of(StandardDeviationHeightProvider(VerticalAnchor.absolute(10), 15.0)) ) )) context.register(PlacedFeatures.DEEP_TRITANIUM, PlacedFeature( ore, listOf( - CountPlacement.of(10), + CountPlacement.of(12), InSquarePlacement.spread(), - HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(8), VerticalAnchor.absolute(0)) + HeightRangePlacement.of(VeryBiasedToBottomHeight.of(VerticalAnchor.aboveBottom(4), VerticalAnchor.absolute(0), 16)) ) )) @@ -83,7 +85,7 @@ fun registerPlacedFeatures(context: BootstrapContext) { context.register(PlacedFeatures.BLACK_HOLE, PlacedFeature( blackHole, listOf( - CountPlacement.of(1), + InSquarePlacement.spread(), HeightRangePlacement.uniform(VerticalAnchor.absolute(64), VerticalAnchor.absolute(128)) ) )) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt index c7e3df6af..61a08961e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt @@ -98,6 +98,7 @@ object OverdriveThatMatters { MStats.register(MOD_BUS) MWorldGenFeatures.register(MOD_BUS) CommandArgumentTypes.register(MOD_BUS) + MHeightProviders.register(MOD_BUS) StorageStack.Companion.register(MOD_BUS) MatteryChestMenu.Companion.register(MOD_BUS) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt index f990a69fb..5e44c3d69 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt @@ -65,6 +65,8 @@ import java.util.stream.Stream import java.util.stream.StreamSupport import kotlin.NoSuchElementException import kotlin.jvm.optionals.getOrNull +import kotlin.math.ln +import kotlin.math.sqrt import kotlin.reflect.KProperty operator fun RecipeInput.get(index: Int): ItemStack = getItem(index) @@ -592,3 +594,45 @@ infix fun FluidStack.isSameAs(other: FluidStack): Boolean { infix fun FluidStack.isNotSameAs(other: FluidStack): Boolean { return !FluidStack.isSameFluidSameComponents(this, other) && amount == other.amount } + +data class DoublePair(val first: Double, val second: Double) + +// normal distribution via Box-Muller +fun RandomGenerator.nextNormalDouble(stddev: Double, mean: Double): DoublePair { + var rand1: Double + var rand2: Double + var distSqr: Double + + do { + rand1 = 2.0 * nextDouble() - 1.0 + rand2 = 2.0 * nextDouble() - 1.0 + distSqr = rand1 * rand1 + rand2 * rand2 + } while (distSqr >= 1) + + val mapping = sqrt(-2.0 * ln(distSqr) / distSqr) + + return DoublePair( + rand1 * mapping * stddev + mean, + rand2 * mapping * stddev + mean + ) +} + +// this kinda exists in form of nextGaussian, but it doesn't allow to specify deviation and mean +fun RandomSource.nextNormalDouble(stddev: Double, mean: Double): DoublePair { + var rand1: Double + var rand2: Double + var distSqr: Double + + do { + rand1 = 2.0 * nextDouble() - 1.0 + rand2 = 2.0 * nextDouble() - 1.0 + distSqr = rand1 * rand1 + rand2 * rand2 + } while (distSqr >= 1) + + val mapping = sqrt(-2.0 * ln(distSqr) / distSqr) + + return DoublePair( + rand1 * mapping * stddev + mean, + rand2 * mapping * stddev + mean + ) +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/StandardDeviationHeightProvider.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/StandardDeviationHeightProvider.kt new file mode 100644 index 000000000..860d76a75 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/StandardDeviationHeightProvider.kt @@ -0,0 +1,64 @@ +package ru.dbotthepony.mc.otm.data.world + +import com.mojang.serialization.Codec +import com.mojang.serialization.MapCodec +import com.mojang.serialization.codecs.RecordCodecBuilder +import net.minecraft.util.RandomSource +import net.minecraft.world.level.levelgen.VerticalAnchor +import net.minecraft.world.level.levelgen.WorldGenerationContext +import net.minecraft.world.level.levelgen.heightproviders.HeightProvider +import net.minecraft.world.level.levelgen.heightproviders.HeightProviderType +import org.apache.logging.log4j.LogManager +import ru.dbotthepony.mc.otm.core.nextNormalDouble +import ru.dbotthepony.mc.otm.data.minRange +import ru.dbotthepony.mc.otm.registry.MHeightProviders + +/** + * Doubles generated are rounded into integers towards negative infinity + */ +data class StandardDeviationHeightProvider( + val mean: VerticalAnchor, + val deviation: Double, +) : HeightProvider() { + override fun sample(random: RandomSource, context: WorldGenerationContext): Int { + val y = mean.resolveY(context) + val yd = y.toDouble() + var i = 100 + + while (i-- > 0) { + val (r0, r1) = random.nextNormalDouble(deviation, yd) + val i0 = r0.toInt() + val i1 = r1.toInt() + + if (i0 in context.minGenY .. context.genDepth) { + return i0 + } else if (i1 in context.minGenY .. context.genDepth) { + return i1 + } + } + + if (LOGGER.isDebugEnabled) { + LOGGER.debug("StandardDeviationHeightProvider couldn't find proper height value for mean $mean with deviation of $deviation") + } + + // failsafe + return y + } + + override fun getType(): HeightProviderType<*> { + return MHeightProviders.STANDARD_DEVIATION + } + + companion object { + private val LOGGER = LogManager.getLogger() + + val CODEC: MapCodec by lazy { + RecordCodecBuilder.mapCodec { + it.group( + VerticalAnchor.CODEC.fieldOf("mean").forGetter(StandardDeviationHeightProvider::mean), + Codec.DOUBLE.minRange(1.0).fieldOf("deviation").forGetter(StandardDeviationHeightProvider::deviation), + ).apply(it, ::StandardDeviationHeightProvider) + } + } + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MHeightProviders.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MHeightProviders.kt new file mode 100644 index 000000000..907cebd23 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MHeightProviders.kt @@ -0,0 +1,16 @@ +package ru.dbotthepony.mc.otm.registry + +import net.minecraft.core.registries.BuiltInRegistries +import net.minecraft.world.level.levelgen.heightproviders.HeightProviderType +import net.neoforged.bus.api.IEventBus +import ru.dbotthepony.mc.otm.data.world.StandardDeviationHeightProvider + +object MHeightProviders { + private val registry = MDeferredRegister(BuiltInRegistries.HEIGHT_PROVIDER_TYPE) + + fun register(bus: IEventBus) { + registry.register(bus) + } + + val STANDARD_DEVIATION by registry.register("standard_deviation") { HeightProviderType { StandardDeviationHeightProvider.CODEC } } +}