Skip to content

Commit c111d6f

Browse files
authored
Merge pull request Pumpkin-MC#98 from kralverde/plains_generation
implement density functions for terrain generation
2 parents c5fdb39 + 30fdb14 commit c111d6f

26 files changed

Lines changed: 4703 additions & 2323 deletions

‎Cargo.lock‎

Lines changed: 0 additions & 2312 deletions
This file was deleted.

‎pumpkin-core/src/math/mod.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod boundingbox;
22
pub mod position;
33
pub mod vector2;
44
pub mod vector3;
5+
pub mod voxel_shape;
56

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

24+
pub fn magnitude(a: f64, b: f64, c: f64) -> f64 {
25+
squared_magnitude(a, b, c).sqrt()
26+
}
27+
2328
/// Converts a world coordinate to the corresponding chunk-section coordinate.
2429
// TODO: This proberbly should place not here
2530
pub const fn get_section_cord(coord: i32) -> i32 {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub struct VoxelShape {
2+
// TODO
3+
}
4+
5+
impl VoxelShape {
6+
pub fn is_empty() -> bool {
7+
unimplemented!()
8+
}
9+
}

‎pumpkin-core/src/random/mod.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ pub mod xoroshiro128;
88
pub enum RandomGenerator {
99
Xoroshiro(Xoroshiro),
1010
Legacy(LegacyRand),
11+
LegacyXoroshiro(Xoroshiro),
1112
}
1213

1314
impl RandomGenerator {
1415
#[inline]
1516
pub fn split(&mut self) -> Self {
1617
match self {
1718
Self::Xoroshiro(rand) => Self::Xoroshiro(rand.split()),
19+
Self::LegacyXoroshiro(rand) => Self::LegacyXoroshiro(rand.split()),
1820
Self::Legacy(rand) => Self::Legacy(rand.split()),
1921
}
2022
}
@@ -23,6 +25,7 @@ impl RandomGenerator {
2325
pub fn next_splitter(&mut self) -> RandomDeriver {
2426
match self {
2527
Self::Xoroshiro(rand) => RandomDeriver::Xoroshiro(rand.next_splitter()),
28+
Self::LegacyXoroshiro(rand) => RandomDeriver::Xoroshiro(rand.next_splitter()),
2629
Self::Legacy(rand) => RandomDeriver::Legacy(rand.next_splitter()),
2730
}
2831
}
@@ -31,6 +34,7 @@ impl RandomGenerator {
3134
pub fn next(&mut self, bits: u64) -> u64 {
3235
match self {
3336
Self::Xoroshiro(rand) => rand.next(bits),
37+
Self::LegacyXoroshiro(rand) => rand.next(bits),
3438
Self::Legacy(rand) => rand.next(bits),
3539
}
3640
}
@@ -39,6 +43,7 @@ impl RandomGenerator {
3943
pub fn next_i32(&mut self) -> i32 {
4044
match self {
4145
Self::Xoroshiro(rand) => rand.next_i32(),
46+
Self::LegacyXoroshiro(rand) => rand.next_i32(),
4247
Self::Legacy(rand) => rand.next_i32(),
4348
}
4449
}
@@ -47,6 +52,7 @@ impl RandomGenerator {
4752
pub fn next_bounded_i32(&mut self, bound: i32) -> i32 {
4853
match self {
4954
Self::Xoroshiro(rand) => rand.next_bounded_i32(bound),
55+
Self::LegacyXoroshiro(rand) => rand.next_bounded_i32(bound),
5056
Self::Legacy(rand) => rand.next_bounded_i32(bound),
5157
}
5258
}
@@ -60,6 +66,7 @@ impl RandomGenerator {
6066
pub fn next_i64(&mut self) -> i64 {
6167
match self {
6268
Self::Xoroshiro(rand) => rand.next_i64(),
69+
Self::LegacyXoroshiro(rand) => rand.next_i64(),
6370
Self::Legacy(rand) => rand.next_i64(),
6471
}
6572
}
@@ -68,6 +75,7 @@ impl RandomGenerator {
6875
pub fn next_bool(&mut self) -> bool {
6976
match self {
7077
Self::Xoroshiro(rand) => rand.next_bool(),
78+
Self::LegacyXoroshiro(rand) => rand.next_bool(),
7179
Self::Legacy(rand) => rand.next_bool(),
7280
}
7381
}
@@ -76,6 +84,7 @@ impl RandomGenerator {
7684
pub fn next_f32(&mut self) -> f32 {
7785
match self {
7886
Self::Xoroshiro(rand) => rand.next_f32(),
87+
Self::LegacyXoroshiro(rand) => rand.next_f32(),
7988
Self::Legacy(rand) => rand.next_f32(),
8089
}
8190
}
@@ -84,6 +93,7 @@ impl RandomGenerator {
8493
pub fn next_f64(&mut self) -> f64 {
8594
match self {
8695
Self::Xoroshiro(rand) => rand.next_f64(),
96+
Self::LegacyXoroshiro(rand) => rand.next_f64(),
8797
Self::Legacy(rand) => rand.next_f64(),
8898
}
8999
}
@@ -92,6 +102,7 @@ impl RandomGenerator {
92102
pub fn next_gaussian(&mut self) -> f64 {
93103
match self {
94104
Self::Xoroshiro(rand) => rand.next_gaussian(),
105+
Self::LegacyXoroshiro(rand) => rand.next_gaussian(),
95106
Self::Legacy(rand) => rand.next_gaussian(),
96107
}
97108
}

‎pumpkin-world/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ lz4 = "1.11.1"
2222

2323
serde.workspace = true
2424
serde_json = "1.0"
25+
enum_dispatch = "0.3.13"
26+
derive-getters = "0.5.0"
27+
2528
log.workspace = true
2629

2730
parking_lot.workspace = true

‎pumpkin-world/src/biome.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use enum_dispatch::enum_dispatch;
12
use serde::{Deserialize, Serialize};
23

34
// TODO make this work with the protocol
@@ -9,3 +10,26 @@ pub enum Biome {
910
SnowyTiga,
1011
// TODO list all Biomes
1112
}
13+
14+
#[derive(Clone)]
15+
#[enum_dispatch(BiomeSupplierImpl)]
16+
pub enum BiomeSupplier {
17+
Debug(DebugBiomeSupplier),
18+
}
19+
20+
#[enum_dispatch]
21+
pub trait BiomeSupplierImpl {
22+
fn biome(&self, x: i32, y: i32, z: i32, noise: &MultiNoiseSampler) -> Biome;
23+
}
24+
25+
#[derive(Clone)]
26+
pub struct DebugBiomeSupplier {}
27+
28+
impl BiomeSupplierImpl for DebugBiomeSupplier {
29+
fn biome(&self, _x: i32, _y: i32, _z: i32, _noise: &MultiNoiseSampler) -> Biome {
30+
Biome::Plains
31+
}
32+
}
33+
34+
// TODO: Implement
35+
pub struct MultiNoiseSampler {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use super::noise::density::NoisePos;
2+
3+
pub struct Blender {
4+
// TODO
5+
}
6+
7+
impl Blender {
8+
pub fn apply_blend_density(&self, _pos: &NoisePos, _density: f64) -> f64 {
9+
todo!()
10+
}
11+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use enum_dispatch::enum_dispatch;
2+
3+
#[enum_dispatch]
4+
pub enum HeightLimitView {
5+
Standard(StandardHeightLimitView),
6+
}
7+
8+
#[enum_dispatch(HeightLimitView)]
9+
pub trait HeightLimitViewImpl {
10+
fn height(&self) -> i32;
11+
12+
fn bottom_y(&self) -> i32;
13+
14+
fn top_y(&self) -> i32 {
15+
self.bottom_y() + self.height()
16+
}
17+
18+
fn vertical_section_count(&self) -> i32 {
19+
self.top_section_coord() - self.bottom_section_coord()
20+
}
21+
22+
fn bottom_section_coord(&self) -> i32 {
23+
self.bottom_y() >> 4
24+
}
25+
26+
fn top_section_coord(&self) -> i32 {
27+
((self.top_y() - 1) >> 4) + 1
28+
}
29+
30+
fn out_of_height(&self, height: i32) -> bool {
31+
height < self.bottom_y() || height >= self.top_y()
32+
}
33+
34+
fn section_index(&self, y: i32) -> i32 {
35+
self.section_coord_to_index(y >> 4)
36+
}
37+
38+
fn section_coord_to_index(&self, coord: i32) -> i32 {
39+
coord - self.bottom_section_coord()
40+
}
41+
42+
fn section_index_to_coord(&self, index: i32) -> i32 {
43+
index + self.bottom_section_coord()
44+
}
45+
}
46+
47+
pub struct StandardHeightLimitView {
48+
height: i32,
49+
bottom_y: i32,
50+
}
51+
52+
impl StandardHeightLimitView {
53+
pub fn new(height: i32, bottom_y: i32) -> Self {
54+
Self { height, bottom_y }
55+
}
56+
}
57+
58+
impl HeightLimitViewImpl for StandardHeightLimitView {
59+
fn height(&self) -> i32 {
60+
self.height
61+
}
62+
63+
fn bottom_y(&self) -> i32 {
64+
self.bottom_y
65+
}
66+
}

‎pumpkin-world/src/world_gen/mod.rs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
#![allow(dead_code)]
2+
3+
mod blender;
14
mod generator;
25
mod generic_generator;
6+
pub mod height_limit;
37
mod implementation;
48
mod noise;
9+
mod positions;
10+
mod proto_chunk;
11+
mod sampler;
512
mod seed;
613

714
pub use generator::WorldGenerator;
@@ -14,3 +21,51 @@ pub fn get_world_gen(seed: Seed) -> Box<dyn WorldGenerator> {
1421
// TODO decide which WorldGenerator to pick based on config.
1522
Box::new(PlainsGenerator::new(seed))
1623
}
24+
25+
pub mod biome_coords {
26+
use num_traits::PrimInt;
27+
28+
#[inline]
29+
pub fn from_block<T>(coord: T) -> T
30+
where
31+
T: PrimInt,
32+
{
33+
coord >> 2
34+
}
35+
36+
#[inline]
37+
pub fn to_block<T>(coord: T) -> T
38+
where
39+
T: PrimInt,
40+
{
41+
coord << 2
42+
}
43+
44+
#[inline]
45+
pub fn from_chunk<T>(coord: T) -> T
46+
where
47+
T: PrimInt,
48+
{
49+
coord << 2
50+
}
51+
52+
#[inline]
53+
pub fn to_chunk<T>(coord: T) -> T
54+
where
55+
T: PrimInt,
56+
{
57+
coord >> 2
58+
}
59+
}
60+
61+
#[derive(PartialEq)]
62+
pub enum Direction {
63+
North,
64+
NorthEast,
65+
East,
66+
SouthEast,
67+
South,
68+
SouthWest,
69+
West,
70+
NorthWest,
71+
}

0 commit comments

Comments
 (0)