chore: move to crates folder
This commit is contained in:
1 parent
5d10c0d8a8
commit
cb3660bcd2
3393 files changed
+1270
-741
No files matched your search
@@ -0,0 +1,45 @@
|
||||
use pumpkin_data::chunk::Biome;
|
||||
|
||||
use crate::{
|
||||
biome::BiomeSupplier,
|
||||
generation::{
|
||||
biome_coords, noise::router::multi_noise_sampler::MultiNoiseSampler, section_coords,
|
||||
},
|
||||
};
|
||||
|
||||
pub struct TheEndBiomeSupplier;
|
||||
|
||||
impl TheEndBiomeSupplier {
|
||||
const CENTER_BIOME: Biome = Biome::THE_END;
|
||||
const HIGHLANDS_BIOME: Biome = Biome::END_HIGHLANDS;
|
||||
const MIDLANDS_BIOME: Biome = Biome::END_MIDLANDS;
|
||||
const SMALL_ISLANDS_BIOME: Biome = Biome::SMALL_END_ISLANDS;
|
||||
const BARRENS_BIOME: Biome = Biome::END_BARRENS;
|
||||
}
|
||||
|
||||
impl BiomeSupplier for TheEndBiomeSupplier {
|
||||
fn biome(&self, x: i32, y: i32, z: i32, noise: &mut MultiNoiseSampler<'_>) -> &'static Biome {
|
||||
let x = biome_coords::to_block(x);
|
||||
let y = biome_coords::to_block(y);
|
||||
let z = biome_coords::to_block(z);
|
||||
let section_x = section_coords::block_to_section(x);
|
||||
let section_z = section_coords::block_to_section(z);
|
||||
if section_x * section_x + section_z * section_z <= 4096 {
|
||||
return &Self::CENTER_BIOME;
|
||||
}
|
||||
let x = (section_x * 2 + 1) * 8;
|
||||
let z = (section_z * 2 + 1) * 8;
|
||||
let noise = noise.sample_erosion(x, y, z);
|
||||
if noise > 0.25 {
|
||||
return &Self::HIGHLANDS_BIOME;
|
||||
}
|
||||
if noise >= -0.0625 {
|
||||
return &Self::MIDLANDS_BIOME;
|
||||
}
|
||||
if noise < -0.21875 {
|
||||
return &Self::SMALL_ISLANDS_BIOME;
|
||||
}
|
||||
|
||||
&Self::BARRENS_BIOME
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::cell::RefCell;
|
||||
|
||||
use pumpkin_data::chunk::{Biome, BiomeTree, NETHER_BIOME_SOURCE, OVERWORLD_BIOME_SOURCE};
|
||||
|
||||
use crate::generation::noise::router::multi_noise_sampler::MultiNoiseSampler;
|
||||
pub mod end;
|
||||
pub mod multi_noise;
|
||||
pub mod position_finder;
|
||||
|
||||
thread_local! {
|
||||
/// A shortcut; check if last used biome is what we should use
|
||||
static LAST_RESULT_NODE: RefCell<Option<&'static BiomeTree>> = const {RefCell::new(None) };
|
||||
}
|
||||
|
||||
pub trait BiomeSupplier {
|
||||
fn biome(&self, x: i32, y: i32, z: i32, noise: &mut MultiNoiseSampler<'_>) -> &'static Biome;
|
||||
}
|
||||
|
||||
pub struct MultiNoiseBiomeSupplier {
|
||||
source: &'static BiomeTree,
|
||||
}
|
||||
|
||||
impl MultiNoiseBiomeSupplier {
|
||||
pub const OVERWORLD: Self = Self::new(&OVERWORLD_BIOME_SOURCE);
|
||||
pub const NETHER: Self = Self::new(&NETHER_BIOME_SOURCE);
|
||||
|
||||
const fn new(source: &'static BiomeTree) -> Self {
|
||||
Self { source }
|
||||
}
|
||||
}
|
||||
|
||||
impl BiomeSupplier for MultiNoiseBiomeSupplier {
|
||||
fn biome(&self, x: i32, y: i32, z: i32, noise: &mut MultiNoiseSampler<'_>) -> &'static Biome {
|
||||
let point = noise.sample(x, y, z);
|
||||
let point_list = point.convert_to_list();
|
||||
LAST_RESULT_NODE.with_borrow_mut(|last_result| self.source.get(&point_list, last_result))
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn hash_seed(seed: u64) -> i64 {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(seed.to_le_bytes());
|
||||
let result = hasher.finalize();
|
||||
i64::from_le_bytes(result[..8].try_into().unwrap())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pumpkin_data::{chunk::Biome, dimension::Dimension};
|
||||
use pumpkin_util::read_data_from_file;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
ProtoChunk,
|
||||
chunk::palette::BIOME_NETWORK_MAX_BITS,
|
||||
generation::noise::router::multi_noise_sampler::{
|
||||
MultiNoiseSampler, MultiNoiseSamplerBuilderOptions,
|
||||
},
|
||||
};
|
||||
|
||||
use super::{BiomeSupplier, MultiNoiseBiomeSupplier, hash_seed};
|
||||
|
||||
#[test]
|
||||
fn biome_desert() {
|
||||
use crate::generation::generator::{GeneratorInit, VanillaGenerator};
|
||||
use pumpkin_util::world_seed::Seed;
|
||||
let seed = 13579;
|
||||
let generator = VanillaGenerator::new(Seed(seed as u64), Dimension::OVERWORLD);
|
||||
let multi_noise_config = MultiNoiseSamplerBuilderOptions::new(1, 1, 1);
|
||||
let mut sampler =
|
||||
MultiNoiseSampler::generate(&generator.base_router.multi_noise, &multi_noise_config);
|
||||
let biome = MultiNoiseBiomeSupplier::OVERWORLD.biome(-24, 1, 8, &mut sampler);
|
||||
assert_eq!(biome, &Biome::DESERT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_area_surface() {
|
||||
use crate::generation::generator::{GeneratorInit, VanillaGenerator, WorldGenerator};
|
||||
use crate::generation::noise::router::multi_noise_sampler::{
|
||||
MultiNoiseSampler, MultiNoiseSamplerBuilderOptions,
|
||||
};
|
||||
use crate::generation::{biome_coords, positions::chunk_pos};
|
||||
use pumpkin_util::world_seed::Seed;
|
||||
#[derive(Deserialize)]
|
||||
struct BiomeData {
|
||||
x: i32,
|
||||
z: i32,
|
||||
data: Vec<(i32, i32, i32, u8)>,
|
||||
}
|
||||
|
||||
let expected_data: Vec<BiomeData> =
|
||||
read_data_from_file!("../../../assets/tests/biome_no_blend_no_beard_0.json");
|
||||
|
||||
let seed = 0;
|
||||
let world_gen = WorldGenerator::Noise(Box::new(VanillaGenerator::new(
|
||||
Seed(seed as u64),
|
||||
Dimension::OVERWORLD,
|
||||
)));
|
||||
let WorldGenerator::Noise(generator) = &world_gen else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
for data in expected_data {
|
||||
let chunk_x = data.x;
|
||||
let chunk_z = data.z;
|
||||
|
||||
let mut chunk = ProtoChunk::new(chunk_x, chunk_z, &world_gen);
|
||||
|
||||
// Create MultiNoiseSampler for populate_biomes
|
||||
|
||||
let start_x = chunk_pos::start_block_x(chunk_x);
|
||||
let start_z = chunk_pos::start_block_z(chunk_z);
|
||||
|
||||
let horizontal_biome_end = biome_coords::from_block(16);
|
||||
let multi_noise_config = MultiNoiseSamplerBuilderOptions::new(
|
||||
biome_coords::from_block(start_x),
|
||||
biome_coords::from_block(start_z),
|
||||
horizontal_biome_end as usize,
|
||||
);
|
||||
let mut multi_noise_sampler = MultiNoiseSampler::generate(
|
||||
&generator.base_router.multi_noise,
|
||||
&multi_noise_config,
|
||||
);
|
||||
|
||||
chunk.populate_biomes(generator, &mut multi_noise_sampler);
|
||||
|
||||
for (biome_x, biome_y, biome_z, biome_id) in data.data {
|
||||
let calculated_biome = chunk.get_biome(biome_x, biome_y, biome_z);
|
||||
|
||||
assert_eq!(
|
||||
biome_id,
|
||||
calculated_biome.id,
|
||||
"Expected {:?} was {:?} at {},{},{} ({},{})",
|
||||
Biome::from_id(biome_id),
|
||||
calculated_biome,
|
||||
biome_x,
|
||||
biome_y,
|
||||
biome_z,
|
||||
data.x,
|
||||
data.z
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_seed_test() {
|
||||
let hashed_seed = hash_seed(0);
|
||||
assert_eq!(8794265229978523055, hashed_seed);
|
||||
|
||||
let hashed_seed = hash_seed((-777i64) as u64);
|
||||
assert_eq!(-1087248400229165450, hashed_seed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proper_network_bits_per_entry() {
|
||||
let id_to_test = 1 << BIOME_NETWORK_MAX_BITS;
|
||||
assert!(
|
||||
Biome::from_id(id_to_test).is_none(),
|
||||
"We need to update our constants!"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[must_use]
|
||||
pub const fn to_long(float: f32) -> i64 {
|
||||
(float * 10000f32) as i64
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct NoiseValuePoint {
|
||||
pub temperature: i64,
|
||||
pub humidity: i64,
|
||||
pub continentalness: i64,
|
||||
pub erosion: i64,
|
||||
pub depth: i64,
|
||||
pub weirdness: i64,
|
||||
}
|
||||
|
||||
impl NoiseValuePoint {
|
||||
#[must_use]
|
||||
pub const fn convert_to_list(&self) -> [i64; 7] {
|
||||
[
|
||||
self.temperature,
|
||||
self.humidity,
|
||||
self.continentalness,
|
||||
self.erosion,
|
||||
self.depth,
|
||||
self.weirdness,
|
||||
0,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pumpkin_data::dimension::Dimension;
|
||||
use pumpkin_util::read_data_from_file;
|
||||
|
||||
use crate::ProtoChunk;
|
||||
|
||||
#[test]
|
||||
fn sample_value() {
|
||||
use crate::generation::generator::{GeneratorInit, VanillaGenerator, WorldGenerator};
|
||||
use crate::generation::noise::router::multi_noise_sampler::{
|
||||
MultiNoiseSampler, MultiNoiseSamplerBuilderOptions,
|
||||
};
|
||||
use crate::generation::{biome_coords, positions::chunk_pos};
|
||||
use pumpkin_util::world_seed::Seed;
|
||||
type PosToPoint = (i32, i32, i32, i64, i64, i64, i64, i64, i64);
|
||||
let expected_data: Vec<PosToPoint> =
|
||||
read_data_from_file!("../../../assets/multi_noise_sample_no_blend_no_beard_0_0_0.json");
|
||||
|
||||
let seed = 0;
|
||||
let chunk_x = 0;
|
||||
let chunk_z = 0;
|
||||
|
||||
let generator = Box::new(VanillaGenerator::new(
|
||||
Seed(seed as u64),
|
||||
Dimension::OVERWORLD,
|
||||
));
|
||||
let world_gen = WorldGenerator::Noise(generator);
|
||||
let WorldGenerator::Noise(generator) = &world_gen else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
let _chunk = ProtoChunk::new(chunk_x, chunk_z, &world_gen);
|
||||
|
||||
let start_x = chunk_pos::start_block_x(chunk_x);
|
||||
let start_z = chunk_pos::start_block_z(chunk_z);
|
||||
let horizontal_biome_end = biome_coords::from_block(16);
|
||||
let multi_noise_config = MultiNoiseSamplerBuilderOptions::new(
|
||||
biome_coords::from_block(start_x),
|
||||
biome_coords::from_block(start_z),
|
||||
horizontal_biome_end as usize,
|
||||
);
|
||||
let mut multi_noise_sampler =
|
||||
MultiNoiseSampler::generate(&generator.base_router.multi_noise, &multi_noise_config);
|
||||
|
||||
for (x, y, z, tem, hum, con, ero, dep, wei) in expected_data {
|
||||
let point = multi_noise_sampler.sample(x, y, z);
|
||||
assert_eq!(point.temperature, tem);
|
||||
assert_eq!(point.humidity, hum);
|
||||
assert_eq!(point.continentalness, con);
|
||||
assert_eq!(point.erosion, ero);
|
||||
assert_eq!(point.depth, dep);
|
||||
assert_eq!(point.weirdness, wei);
|
||||
}
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn sample_multinoise_biome() {
|
||||
// use crate::generation::generator::{GeneratorInit, VanillaGenerator};
|
||||
// use pumpkin_util::world_seed::Seed;
|
||||
|
||||
// let expected_data: Vec<(i32, i32, i32, u8)> =
|
||||
// read_data_from_file!("../../../assets/multi_noise_biome_source_test.json");
|
||||
|
||||
// let seed = 0;
|
||||
// let generator = VanillaGenerator::new(Seed(seed as u64), Dimension::OVERWORLD);
|
||||
|
||||
// let mut sampler = MultiNoiseSampler::generate(
|
||||
// &generator.base_router.multi_noise,
|
||||
// &MultiNoiseSamplerBuilderOptions::new(0, 0, 4),
|
||||
// );
|
||||
|
||||
// for (x, y, z, biome_id) in expected_data {
|
||||
// let calculated_biome = MultiNoiseBiomeSupplier::OVERWORLD.biome(x, y, z, &mut sampler);
|
||||
|
||||
// assert_eq!(
|
||||
// biome_id,
|
||||
// calculated_biome.id,
|
||||
// "Expected {:?} was {:?} at {},{},{}",
|
||||
// Biome::from_id(biome_id),
|
||||
// calculated_biome,
|
||||
// x,
|
||||
// y,
|
||||
// z
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
use pumpkin_data::chunk::ParameterRange;
|
||||
use pumpkin_util::math::vector2::Vector2;
|
||||
|
||||
pub struct FittestPositionFinderResult {
|
||||
pub location: Vector2<i32>,
|
||||
pub fitness: i64,
|
||||
}
|
||||
|
||||
pub struct FittestPositionFinder;
|
||||
|
||||
impl FittestPositionFinder {
|
||||
pub fn find_best_spawn_position(
|
||||
target_noises: &[[ParameterRange; 7]],
|
||||
sampler: &dyn Fn(i32, i32) -> [i64; 7],
|
||||
) -> Vector2<i32> {
|
||||
let mut best_result = Self::calculate_fitness(target_noises, sampler, 0, 0);
|
||||
|
||||
Self::find_fittest(target_noises, sampler, &mut best_result, 2048.0, 512.0);
|
||||
Self::find_fittest(target_noises, sampler, &mut best_result, 512.0, 32.0);
|
||||
|
||||
best_result.location
|
||||
}
|
||||
|
||||
fn find_fittest(
|
||||
noises: &[[ParameterRange; 7]],
|
||||
sampler: &dyn Fn(i32, i32) -> [i64; 7],
|
||||
best_result: &mut FittestPositionFinderResult,
|
||||
max_distance: f32,
|
||||
step: f32,
|
||||
) {
|
||||
let mut angle = 0.0f32;
|
||||
let mut distance = step;
|
||||
let center = best_result.location;
|
||||
|
||||
while distance <= max_distance {
|
||||
let x = center.x + (angle.sin() * distance) as i32;
|
||||
let z = center.y + (angle.cos() * distance) as i32;
|
||||
|
||||
let result = Self::calculate_fitness(noises, sampler, x, z);
|
||||
if result.fitness < best_result.fitness {
|
||||
*best_result = result;
|
||||
}
|
||||
|
||||
angle += step / distance;
|
||||
if angle > std::f32::consts::TAU {
|
||||
angle = 0.0;
|
||||
distance += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn calculate_fitness(
|
||||
noises: &[[ParameterRange; 7]],
|
||||
sampler: &dyn Fn(i32, i32) -> [i64; 7],
|
||||
x: i32,
|
||||
z: i32,
|
||||
) -> FittestPositionFinderResult {
|
||||
let sampled_noise = sampler(x, z);
|
||||
let mut min_squared_dist = i64::MAX;
|
||||
|
||||
for noise_ranges in noises {
|
||||
let mut current_dist = 0i64;
|
||||
for i in 0..7 {
|
||||
current_dist += noise_ranges[i].calc_distance(sampled_noise[i]);
|
||||
}
|
||||
min_squared_dist = min_squared_dist.min(current_dist);
|
||||
}
|
||||
|
||||
let origin_dist_sq = (x as i64 * x as i64) + (z as i64 * z as i64);
|
||||
let fitness = min_squared_dist * (2048 * 2048) + origin_dist_sq;
|
||||
|
||||
FittestPositionFinderResult {
|
||||
location: Vector2::new(x, z),
|
||||
fitness,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user