Skip to content

Commit

Permalink
Fix compile time errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Mar 2, 2025
1 parent 4c3115a commit b5ea19f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 41 deletions.
2 changes: 0 additions & 2 deletions pumpkin-data/build/biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::array_to_tokenstream;

pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=../assets/biome.json");

Expand Down
43 changes: 33 additions & 10 deletions pumpkin-world/src/biome/mod.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
use std::sync::LazyLock;
use std::{cell::RefCell, sync::LazyLock};

use enum_dispatch::enum_dispatch;
use multi_noise::{BiomeEntries, SearchTree};
use multi_noise::{BiomeEntries, SearchTree, TreeLeafNode};
use pumpkin_data::chunk::Biome;
mod multi_noise;

use crate::{
coordinates::BlockCoordinates, generation::noise_router::multi_noise_sampler::MultiNoiseSampler,
};
pub mod multi_noise;

pub static BIOME_ENTRIES: LazyLock<SearchTree<Biome>> = LazyLock::new(|| {
SearchTree::create(
serde_json::from_str::<BiomeEntries>(include_str!("../../../assets/multi_noise.json"))
.expect("Could not parse synced_registries.json registry.")
.nodes,
.expect("Could not parse multi_noise.json.")
.nodes
.into_iter()
.flat_map(|(_, biome_map)| biome_map.into_iter())
.collect(),
)
.expect("entries cannot be empty")
});

thread_local! {
static LAST_RESULT_NODE: RefCell<Option<TreeLeafNode<Biome>>> = RefCell::new(None);
}

#[enum_dispatch]
pub trait BiomeSupplier {
fn biome(&self, x: i32, y: i32, z: i32, noise: &MultiNoiseSampler) -> Biome;
fn biome(&mut self, at: BlockCoordinates) -> Biome;
}

#[derive(Clone)]
pub struct DebugBiomeSupplier {}
pub struct DebugBiomeSupplier;

impl BiomeSupplier for DebugBiomeSupplier {
fn biome(&self, _x: i32, _y: i32, _z: i32, _noise: &MultiNoiseSampler) -> Biome {
fn biome(&mut self, _at: BlockCoordinates) -> Biome {
Biome::Plains
}
}

// TODO: Implement
pub struct MultiNoiseSampler {}
pub struct MultiNoiseBiomeSupplier<'a> {
noise: MultiNoiseSampler<'a>,
}

impl BiomeSupplier for MultiNoiseBiomeSupplier<'_> {
fn biome(&mut self, at: BlockCoordinates) -> Biome {
let point = self.noise.sample(at.x, at.y.0 as i32, at.z);
LAST_RESULT_NODE.with_borrow_mut(|last_result| {
BIOME_ENTRIES
.get(&point, last_result)
.expect("failed to get biome entry")
})
}
}
38 changes: 18 additions & 20 deletions pumpkin-world/src/biome/multi_noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use pumpkin_data::chunk::Biome;
use serde::{Deserialize, Deserializer, Serialize};

use crate::dimension::Dimension;
fn to_long(float: f64) -> i64 {
pub fn to_long(float: f32) -> i64 {
(float * 1000.0) as i64
}
#[derive(Clone, Serialize, Deserialize)]
pub struct NoiseValuePoint {
pub temperature: f64,
pub erosion: f64,
pub depth: f64,
pub continents: f64,
pub weirdness: f64,
pub humidity: f64,
pub temperature: i64,
pub humidity: i64,
pub continentalness: i64,
pub erosion: i64,
pub depth: i64,
pub weirdness: i64,
}

#[derive(Clone, Deserialize)]
Expand Down Expand Up @@ -56,7 +56,7 @@ impl<'de> Deserialize<'de> for ParameterRange {
where
D: Deserializer<'de>,
{
let arr: [f64; 2] = Deserialize::deserialize(deserializer)?;
let arr: [f32; 2] = Deserialize::deserialize(deserializer)?;
Ok(ParameterRange {
min: to_long(arr[0]),
max: to_long(arr[1]),
Expand All @@ -68,7 +68,7 @@ impl ParameterRange {
fn get_distance(&self, noise: i64) -> i64 {
let l = noise - self.max;
let m = self.min - noise;
return if l > 0 { l } else { m.max(0) };
if l > 0 { l } else { m.max(0) }
}

pub fn combine(&self, other: &Self) -> Self {
Expand All @@ -85,6 +85,7 @@ pub struct BiomeEntries {
}

#[derive(Clone)]
/// T = Biome
pub struct SearchTree<T: Clone> {
root: TreeNode<T>,
}
Expand Down Expand Up @@ -133,10 +134,7 @@ fn create_node<T: Clone>(sub_tree: Vec<TreeNode<T>>) -> TreeNode<T> {
sub_tree.into_iter().next().unwrap()
} else if sub_tree.len() <= 6 {
let mut sorted_sub_tree = sub_tree;
sorted_sub_tree.sort_by_key(|a| {
let sum = calculate_midpoint_sum(a);
sum
});
sorted_sub_tree.sort_by_key(|a| calculate_midpoint_sum(a));
let bounds = calculate_bounds(&sorted_sub_tree);
TreeNode::Branch {
children: sorted_sub_tree,
Expand Down Expand Up @@ -170,7 +168,7 @@ fn create_node<T: Clone>(sub_tree: Vec<TreeNode<T>>) -> TreeNode<T> {
}
}

fn sort_tree<T: Clone>(sub_tree: &mut Vec<TreeNode<T>>, parameter_offset: usize, abs: bool) {
fn sort_tree<T: Clone>(sub_tree: &mut [TreeNode<T>], parameter_offset: usize, abs: bool) {
sub_tree.sort_by(|a, b| {
for i in 0..7 {
// Calculate the parameter index in cyclic order
Expand Down Expand Up @@ -238,7 +236,7 @@ fn get_batched_tree<T: Clone>(nodes: Vec<TreeNode<T>>) -> Vec<TreeNode<T>> {
}

fn calculate_bounds<T: Clone>(nodes: &[TreeNode<T>]) -> [ParameterRange; 7] {
let mut bounds = nodes[0].bounds().clone();
let mut bounds = *nodes[0].bounds();

for node in nodes.iter().skip(1) {
for (i, range) in node.bounds().iter().enumerate() {
Expand Down Expand Up @@ -273,9 +271,9 @@ impl<T: Clone> TreeNode<T> {
TreeNode::Leaf(TreeLeafNode { value, point })
}

pub fn new_branch(children: Vec<TreeNode<T>>, bounds: [ParameterRange; 7]) -> Self {
TreeNode::Branch { children, bounds }
}
// pub fn new_branch(children: Vec<TreeNode<T>>, bounds: [ParameterRange; 7]) -> Self {
// TreeNode::Branch { children, bounds }
// }

pub fn get_node(
&self,
Expand Down Expand Up @@ -311,8 +309,8 @@ impl<T: Clone> TreeNode<T> {

pub fn bounds(&self) -> &[ParameterRange; 7] {
match self {
TreeNode::Leaf(TreeLeafNode { point, .. }) => &point,
TreeNode::Branch { bounds, .. } => &bounds,
TreeNode::Leaf(TreeLeafNode { point, .. }) => point,
TreeNode::Branch { bounds, .. } => bounds,
}
}

Expand Down
27 changes: 18 additions & 9 deletions pumpkin-world/src/generation/noise_router/multi_noise_sampler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
GlobalProtoNoiseRouter, generation::biome_coords,
GlobalProtoNoiseRouter,
biome::multi_noise::{NoiseValuePoint, to_long},
generation::biome_coords,
noise_router::density_function_ast::WrapperType,
};

Expand Down Expand Up @@ -45,7 +47,7 @@ pub struct MultiNoiseSampler<'a> {
}

impl<'a> MultiNoiseSampler<'a> {
pub fn sample(&mut self, biome_x: i32, biome_y: i32, biome_z: i32) {
pub fn sample(&mut self, biome_x: i32, biome_y: i32, biome_z: i32) -> NoiseValuePoint {
let block_x = biome_coords::to_block(biome_x);
let block_y = biome_coords::to_block(biome_y);
let block_z = biome_coords::to_block(biome_z);
Expand All @@ -54,43 +56,50 @@ impl<'a> MultiNoiseSampler<'a> {
let sample_options =
ChunkNoiseFunctionSampleOptions::new(false, SampleAction::SkipCellCaches, 0, 0, 0);

let _temperature = ChunkNoiseFunctionComponent::sample_from_stack(
let temperature = ChunkNoiseFunctionComponent::sample_from_stack(
&mut self.component_stack[..=self.temperature],
&pos,
&sample_options,
) as f32;

let _humidity = ChunkNoiseFunctionComponent::sample_from_stack(
let humidity = ChunkNoiseFunctionComponent::sample_from_stack(
&mut self.component_stack[..=self.vegetation],
&pos,
&sample_options,
) as f32;

let _continentalness = ChunkNoiseFunctionComponent::sample_from_stack(
let continentalness = ChunkNoiseFunctionComponent::sample_from_stack(
&mut self.component_stack[..=self.continents],
&pos,
&sample_options,
) as f32;

let _erosion = ChunkNoiseFunctionComponent::sample_from_stack(
let erosion = ChunkNoiseFunctionComponent::sample_from_stack(
&mut self.component_stack[..=self.erosion],
&pos,
&sample_options,
) as f32;

let _depth = ChunkNoiseFunctionComponent::sample_from_stack(
let depth = ChunkNoiseFunctionComponent::sample_from_stack(
&mut self.component_stack[..=self.depth],
&pos,
&sample_options,
) as f32;

let _weirdness = ChunkNoiseFunctionComponent::sample_from_stack(
let weirdness = ChunkNoiseFunctionComponent::sample_from_stack(
&mut self.component_stack[..=self.ridges],
&pos,
&sample_options,
) as f32;

// TODO: Multi noise value here
NoiseValuePoint {
temperature: to_long(temperature),
humidity: to_long(humidity),
continentalness: to_long(continentalness),
erosion: to_long(erosion),
depth: to_long(depth),
weirdness: to_long(weirdness),
}
}

pub fn generate(
Expand Down

0 comments on commit b5ea19f

Please sign in to comment.