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

implement density functions for terrain generation #98

Merged
merged 11 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2,312 changes: 0 additions & 2,312 deletions Cargo.lock

This file was deleted.

5 changes: 5 additions & 0 deletions pumpkin-core/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod boundingbox;
pub mod position;
pub mod vector2;
pub mod vector3;
pub mod voxel_shape;

pub fn wrap_degrees(var: f32) -> f32 {
let mut var1 = var % 360.0;
Expand All @@ -20,6 +21,10 @@ pub fn squared_magnitude(a: f64, b: f64, c: f64) -> f64 {
a * a + b * b + c * c
}

pub fn magnitude(a: f64, b: f64, c: f64) -> f64 {
squared_magnitude(a, b, c).sqrt()
}

/// Converts a world coordinate to the corresponding chunk-section coordinate.
// TODO: This proberbly should place not here
pub const fn get_section_cord(coord: i32) -> i32 {
Expand Down
9 changes: 9 additions & 0 deletions pumpkin-core/src/math/voxel_shape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub struct VoxelShape {
// TODO
}

impl VoxelShape {
pub fn is_empty() -> bool {
unimplemented!()
}
}
11 changes: 11 additions & 0 deletions pumpkin-core/src/random/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ pub mod xoroshiro128;
pub enum RandomGenerator {
Xoroshiro(Xoroshiro),
Legacy(LegacyRand),
LegacyXoroshiro(Xoroshiro),
}

impl RandomGenerator {
#[inline]
pub fn split(&mut self) -> Self {
match self {
Self::Xoroshiro(rand) => Self::Xoroshiro(rand.split()),
Self::LegacyXoroshiro(rand) => Self::LegacyXoroshiro(rand.split()),
Self::Legacy(rand) => Self::Legacy(rand.split()),
}
}
Expand All @@ -23,6 +25,7 @@ impl RandomGenerator {
pub fn next_splitter(&mut self) -> RandomDeriver {
match self {
Self::Xoroshiro(rand) => RandomDeriver::Xoroshiro(rand.next_splitter()),
Self::LegacyXoroshiro(rand) => RandomDeriver::Xoroshiro(rand.next_splitter()),
Self::Legacy(rand) => RandomDeriver::Legacy(rand.next_splitter()),
}
}
Expand All @@ -31,6 +34,7 @@ impl RandomGenerator {
pub fn next(&mut self, bits: u64) -> u64 {
match self {
Self::Xoroshiro(rand) => rand.next(bits),
Self::LegacyXoroshiro(rand) => rand.next(bits),
Self::Legacy(rand) => rand.next(bits),
}
}
Expand All @@ -39,6 +43,7 @@ impl RandomGenerator {
pub fn next_i32(&mut self) -> i32 {
match self {
Self::Xoroshiro(rand) => rand.next_i32(),
Self::LegacyXoroshiro(rand) => rand.next_i32(),
Self::Legacy(rand) => rand.next_i32(),
}
}
Expand All @@ -47,6 +52,7 @@ impl RandomGenerator {
pub fn next_bounded_i32(&mut self, bound: i32) -> i32 {
match self {
Self::Xoroshiro(rand) => rand.next_bounded_i32(bound),
Self::LegacyXoroshiro(rand) => rand.next_bounded_i32(bound),
Self::Legacy(rand) => rand.next_bounded_i32(bound),
}
}
Expand All @@ -60,6 +66,7 @@ impl RandomGenerator {
pub fn next_i64(&mut self) -> i64 {
match self {
Self::Xoroshiro(rand) => rand.next_i64(),
Self::LegacyXoroshiro(rand) => rand.next_i64(),
Self::Legacy(rand) => rand.next_i64(),
}
}
Expand All @@ -68,6 +75,7 @@ impl RandomGenerator {
pub fn next_bool(&mut self) -> bool {
match self {
Self::Xoroshiro(rand) => rand.next_bool(),
Self::LegacyXoroshiro(rand) => rand.next_bool(),
Self::Legacy(rand) => rand.next_bool(),
}
}
Expand All @@ -76,6 +84,7 @@ impl RandomGenerator {
pub fn next_f32(&mut self) -> f32 {
match self {
Self::Xoroshiro(rand) => rand.next_f32(),
Self::LegacyXoroshiro(rand) => rand.next_f32(),
Self::Legacy(rand) => rand.next_f32(),
}
}
Expand All @@ -84,6 +93,7 @@ impl RandomGenerator {
pub fn next_f64(&mut self) -> f64 {
match self {
Self::Xoroshiro(rand) => rand.next_f64(),
Self::LegacyXoroshiro(rand) => rand.next_f64(),
Self::Legacy(rand) => rand.next_f64(),
}
}
Expand All @@ -92,6 +102,7 @@ impl RandomGenerator {
pub fn next_gaussian(&mut self) -> f64 {
match self {
Self::Xoroshiro(rand) => rand.next_gaussian(),
Self::LegacyXoroshiro(rand) => rand.next_gaussian(),
Self::Legacy(rand) => rand.next_gaussian(),
}
}
Expand Down
3 changes: 3 additions & 0 deletions pumpkin-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ lz4 = "1.11.1"

serde.workspace = true
serde_json = "1.0"
enum_dispatch = "0.3.13"
derive-getters = "0.5.0"

log.workspace = true

parking_lot.workspace = true
Expand Down
24 changes: 24 additions & 0 deletions pumpkin-world/src/biome.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use enum_dispatch::enum_dispatch;
use serde::{Deserialize, Serialize};

// TODO make this work with the protocol
Expand All @@ -9,3 +10,26 @@ pub enum Biome {
SnowyTiga,
// TODO list all Biomes
}

#[derive(Clone)]
#[enum_dispatch(BiomeSupplierImpl)]
pub enum BiomeSupplier {
Debug(DebugBiomeSupplier),
}

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

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

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

// TODO: Implement
pub struct MultiNoiseSampler {}
11 changes: 11 additions & 0 deletions pumpkin-world/src/world_gen/blender/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::noise::density::NoisePos;

pub struct Blender {
// TODO
}

impl Blender {
pub fn apply_blend_density(&self, _pos: &NoisePos, _density: f64) -> f64 {
todo!()
}
}
66 changes: 66 additions & 0 deletions pumpkin-world/src/world_gen/height_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use enum_dispatch::enum_dispatch;

#[enum_dispatch]
pub enum HeightLimitView {
Standard(StandardHeightLimitView),
}

#[enum_dispatch(HeightLimitView)]
pub trait HeightLimitViewImpl {
fn height(&self) -> i32;

fn bottom_y(&self) -> i32;

fn top_y(&self) -> i32 {
self.bottom_y() + self.height()
}

fn vertical_section_count(&self) -> i32 {
self.top_section_coord() - self.bottom_section_coord()
}

fn bottom_section_coord(&self) -> i32 {
self.bottom_y() >> 4
}

fn top_section_coord(&self) -> i32 {
((self.top_y() - 1) >> 4) + 1
}

fn out_of_height(&self, height: i32) -> bool {
height < self.bottom_y() || height >= self.top_y()
}

fn section_index(&self, y: i32) -> i32 {
self.section_coord_to_index(y >> 4)
}

fn section_coord_to_index(&self, coord: i32) -> i32 {
coord - self.bottom_section_coord()
}

fn section_index_to_coord(&self, index: i32) -> i32 {
index + self.bottom_section_coord()
}
}

pub struct StandardHeightLimitView {
height: i32,
bottom_y: i32,
}

impl StandardHeightLimitView {
pub fn new(height: i32, bottom_y: i32) -> Self {
Self { height, bottom_y }
}
}

impl HeightLimitViewImpl for StandardHeightLimitView {
fn height(&self) -> i32 {
self.height
}

fn bottom_y(&self) -> i32 {
self.bottom_y
}
}
55 changes: 55 additions & 0 deletions pumpkin-world/src/world_gen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#![allow(dead_code)]

mod blender;
mod generator;
mod generic_generator;
pub mod height_limit;
mod implementation;
mod noise;
mod positions;
mod proto_chunk;
mod sampler;
mod seed;

pub use generator::WorldGenerator;
Expand All @@ -14,3 +21,51 @@ pub fn get_world_gen(seed: Seed) -> Box<dyn WorldGenerator> {
// TODO decide which WorldGenerator to pick based on config.
Box::new(PlainsGenerator::new(seed))
}

pub mod biome_coords {
use num_traits::PrimInt;

#[inline]
pub fn from_block<T>(coord: T) -> T
where
T: PrimInt,
{
coord >> 2
}

#[inline]
pub fn to_block<T>(coord: T) -> T
where
T: PrimInt,
{
coord << 2
}

#[inline]
pub fn from_chunk<T>(coord: T) -> T
where
T: PrimInt,
{
coord << 2
}

#[inline]
pub fn to_chunk<T>(coord: T) -> T
where
T: PrimInt,
{
coord >> 2
}
}

#[derive(PartialEq)]
pub enum Direction {
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West,
NorthWest,
}
Loading