Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"populate noise" step of chunk generation #319

Merged
merged 27 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
96b79a2
implement populate noise step of chunk generation
kralverde Nov 23, 2024
c841a41
fix gitignore
kralverde Nov 23, 2024
44ccf62
pass thru seed
kralverde Nov 23, 2024
c0dd6d0
add counter for edge case
kralverde Nov 23, 2024
96915ba
add entire chunk test case
kralverde Nov 23, 2024
b4f2141
remove eclipse file
kralverde Nov 23, 2024
a49427f
remove packet timing
kralverde Nov 23, 2024
a3c77af
precompute float divisions
kralverde Nov 24, 2024
f9cf387
unsafe speed up
kralverde Nov 24, 2024
68949ec
slight aquifer optimization
kralverde Nov 24, 2024
fa0411d
tweak block pos packing
kralverde Nov 24, 2024
828cb67
optimize perlin map
kralverde Nov 25, 2024
014dfae
precompute aquifer random positions
kralverde Nov 25, 2024
74a1fe6
fix aquifer regression
kralverde Nov 25, 2024
c120b54
refactor samplers, add tests to aquifer, and move chunk extractor to …
kralverde Nov 26, 2024
f25e9aa
Merge branch 'master' into basic_chunk_generation
Snowiiii Nov 26, 2024
2fc5ddd
fix base density function error and add a bunch of test cases
kralverde Nov 28, 2024
487b3c1
ignore expensive tests by default
kralverde Nov 28, 2024
2a11487
remove un-needed lifetimes
kralverde Nov 28, 2024
4713a8c
Merge branch 'master' into basic_chunk_generation
kralverde Nov 28, 2024
6732a77
remove minecraft prefix from block lookups
kralverde Nov 28, 2024
69b516b
fix populate noise bench
kralverde Nov 29, 2024
6f0842f
slight optimizations
kralverde Nov 30, 2024
675ff9f
read files at runtime to speed up test compilation speed and remove i…
kralverde Nov 30, 2024
c13e22d
remove new lines from test data so people stop freaking out about lin…
kralverde Nov 30, 2024
04b1fce
make extractor the same
kralverde Dec 1, 2024
c8ac364
leverage branch prediction
kralverde Dec 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions extractor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ dependencies {
modImplementation("net.fabricmc:fabric-language-kotlin:${project.property("kotlin_loader_version")}")

modImplementation("net.fabricmc.fabric-api:fabric-api:${project.property("fabric_version")}")

// To allow for reflection
implementation(kotlin("reflect"))
}

tasks.processResources {
Expand Down
8 changes: 7 additions & 1 deletion extractor/src/main/kotlin/de/snowii/extractor/Extractor.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.snowii.extractor

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonElement
import de.snowii.extractor.extractors.*
Expand Down Expand Up @@ -32,6 +33,7 @@ class Extractor : ModInitializer {
Tags(),
Items(),
Blocks(),
Tests(),
)

val outputDirectory: Path
Expand All @@ -49,7 +51,7 @@ class Extractor : ModInitializer {
try {
val out = outputDirectory.resolve(ext.fileName())
val fileWriter = FileWriter(out.toFile(), StandardCharsets.UTF_8)
gson.toJson(ext.extract(server), fileWriter)
(ext.gson_builder() ?: gson).toJson(ext.extract(server), fileWriter)
fileWriter.close()
logger.info("Wrote " + out.toAbsolutePath())
} catch (e: java.lang.Exception) {
Expand All @@ -64,5 +66,9 @@ class Extractor : ModInitializer {

@Throws(Exception::class)
fun extract(server: MinecraftServer): JsonElement

fun gson_builder(): Gson? {
return null
}
}
}
146 changes: 146 additions & 0 deletions extractor/src/main/kotlin/de/snowii/extractor/extractors/Tests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@

package de.snowii.extractor.extractors

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import de.snowii.extractor.Extractor
import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.block.Blocks
import net.minecraft.registry.BuiltinRegistries
import net.minecraft.registry.RegistryKeys
import net.minecraft.registry.RegistryWrapper
import net.minecraft.registry.RegistryWrapper.WrapperLookup
import net.minecraft.registry.entry.RegistryEntry.Reference
import net.minecraft.server.MinecraftServer
import net.minecraft.util.math.noise.DoublePerlinNoiseSampler.NoiseParameters
import net.minecraft.util.math.ChunkPos
import net.minecraft.world.gen.chunk.AquiferSampler
import net.minecraft.world.gen.chunk.Blender
import net.minecraft.world.gen.chunk.ChunkGeneratorSettings
import net.minecraft.world.gen.chunk.ChunkNoiseSampler
import net.minecraft.world.gen.chunk.GenerationShapeConfig
import net.minecraft.world.gen.densityfunction.DensityFunction.NoisePos;
import net.minecraft.world.gen.densityfunction.DensityFunction.EachApplier;
import net.minecraft.world.gen.densityfunction.DensityFunctionTypes
import net.minecraft.world.gen.noise.NoiseConfig

import java.lang.reflect.Method
import java.util.Arrays
import kotlin.reflect.full.createType
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.jvm.javaMethod
import kotlin.reflect.KFunction

class Tests : Extractor.Extractor {
override fun fileName(): String = "chunk.json"

private fun createFluidLevelSampler(settings: ChunkGeneratorSettings): AquiferSampler.FluidLevelSampler {
val fluidLevel = AquiferSampler.FluidLevel(-54, Blocks.LAVA.getDefaultState());
val i = settings.seaLevel();
val fluidLevel2 = AquiferSampler.FluidLevel(i, settings.defaultFluid());
return AquiferSampler.FluidLevelSampler {_, y, _ -> if (y < Math.min(-54, i)) fluidLevel else fluidLevel2};
}

private fun get_index(config: GenerationShapeConfig, x: Int, y: Int, z: Int): Int {
if (x < 0 || y < 0 || z < 0) {
System.err.println("Bad local pos");
System.exit(1);
}
return config.height() * 16 * x + 16 * y + z
}

// This is basically just what NoiseChunkGenerator is doing
private fun populate_noise(start_x: Int, start_z: Int, sampler: ChunkNoiseSampler, config: GenerationShapeConfig, settings: ChunkGeneratorSettings): IntArray? {
val result = IntArray(16 * 16 * config.height())

for (method: KFunction<*> in sampler::class.declaredFunctions) {
if (method.name.equals("sampleBlockState")) {
sampler.sampleStartDensity()
val k = config.horizontalCellBlockCount()
val l = config.verticalCellBlockCount()

val m = 16 / k
val n = 16 / k

val cellHeight = config.height() / l
val minimumCellY = config.minimumY() / l

for (o in 0..<m) {
sampler.sampleEndDensity(o)
for (p in 0..<n) {
for (r in (0..<cellHeight).reversed()) {
sampler.onSampledCellCorners(r, p)
for (s in (0..<l).reversed()) {
val t = (minimumCellY + r) * l + s
val d = s.toDouble() / l.toDouble()
sampler.interpolateY(t, d)
for (w in 0..<k) {
val x = start_x + o * k + w
val y = x and 15
val e = w.toDouble() / k.toDouble()
sampler.interpolateX(x, e)
for (z in 0..<k) {
val aa = start_z + p * k + z
val ab = aa and 15
val f = z.toDouble() / k.toDouble()
sampler.interpolateZ(aa, f)
var blockstate = method.call(sampler) as BlockState?
if (blockstate == null) {
blockstate = settings.defaultBlock()
}
val index = this.get_index(config, y, t - config.minimumY(), ab)
result[index] = Block.getRawIdFromState(blockstate)
}
}
}
}
}
sampler.swapBuffers()
}
sampler.stopInterpolation()
return result
}
}
System.err.println("No valid method found for block state sampler!");
return null;
}

override fun gson_builder(): Gson {
return GsonBuilder().create()
}
kralverde marked this conversation as resolved.
Show resolved Hide resolved

// Dumps a chunk to an array of block state ids
override fun extract(server: MinecraftServer): JsonElement {
val topLevelJson = JsonArray()
val seed = 0L
val chunk_pos = ChunkPos(7, 4)

val lookup = BuiltinRegistries.createWrapperLookup()
val wrapper = lookup.getOrThrow(RegistryKeys.CHUNK_GENERATOR_SETTINGS)
val noise_params = lookup.getOrThrow(RegistryKeys.NOISE_PARAMETERS)

val ref = wrapper.getOrThrow(ChunkGeneratorSettings.OVERWORLD)
val settings = ref.value()
val config = NoiseConfig.create(settings, noise_params, seed)

// Overworld shape config
val shape = GenerationShapeConfig(-64, 384, 1, 2)
val test_sampler = ChunkNoiseSampler(16 / shape.horizontalCellBlockCount(), config, chunk_pos.startX, chunk_pos.startZ,
shape, object: DensityFunctionTypes.Beardifying{
override fun maxValue(): Double = 0.0
override fun minValue(): Double = 0.0
override fun sample(pos: NoisePos): Double = 0.0
override fun fill(densities: DoubleArray, applier: EachApplier) {densities.fill(0.0)}
}, settings, createFluidLevelSampler(settings), Blender.getNoBlending())

val data = populate_noise(chunk_pos.startX, chunk_pos.startZ, test_sampler, shape, settings)
data?.forEach { state ->
topLevelJson.add(state)
}

return topLevelJson
}
}
158 changes: 0 additions & 158 deletions pumpkin-world/assets/no_bench_no_blend_0_0.java

This file was deleted.

2 changes: 1 addition & 1 deletion pumpkin-world/assets/no_blend_no_beard_0_0.chunk

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pumpkin-world/assets/no_blend_no_beard_7_4.chunk

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion pumpkin-world/src/block/block_state.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use super::block_registry::{get_block, get_state_by_state_id};

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq)]
pub struct BlockState {
pub state_id: u16,
pub block_id: u16,
}

impl PartialEq for BlockState {
fn eq(&self, other: &Self) -> bool {
self.state_id == other.state_id
}
}

impl BlockState {
pub const AIR: BlockState = BlockState {
state_id: 0,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use block::BlockState;
use world_gen::{
aquifer_sampler::{FluidLevel, FluidLevelSampler},
chunk_noise::{ChunkNoiseGenerator, LAVA_BLOCK, WATER_BLOCK},
generation_shapes::GenerationShape,
noise::{config::NoiseConfig, router::OVERWORLD_NOISE_ROUTER},
proto_chunk::StandardChunkFluidLevelSampler,
sampler::{FluidLevel, FluidLevelSampler},
};

pub mod biome;
Expand Down
Loading