Skip to content

Commit d33eb7e

Browse files
Uopdate generatror grass placement
1 parent 45f87c4 commit d33eb7e

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/server/terrain/util/generator.rs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ macro_rules! for_each_chunk_coordinate {
1010
#[cfg(feature = "skip_chunk_padding")]
1111
if x == 0
1212
|| x == CHUNK_SIZE + 1
13-
|| y == 0
14-
|| y == CHUNK_SIZE + 1
15-
|| z == 0
16-
|| z == CHUNK_SIZE + 1
13+
|| y == 0
14+
|| y == CHUNK_SIZE + 1
15+
|| z == 0
16+
|| z == CHUNK_SIZE + 1
1717
{
1818
continue;
1919
}
2020

2121
let chunk_origin = $chunk.position * CHUNK_SIZE as f32;
2222
let local_position = Vec3::new(x as f32, y as f32, z as f32);
23-
let block_position = chunk_origin + local_position;
23+
let world_position = chunk_origin + local_position;
2424

25-
$body(x, y, z, block_position);
25+
$body(x, y, z, world_position);
2626
}
2727
}
2828
}
@@ -47,23 +47,58 @@ impl Generator {
4747
return;
4848
}
4949

50-
for_each_chunk_coordinate!(chunk, |x, y, z, block_position| {
51-
let block = self.generate_block(block_position);
50+
for_each_chunk_coordinate!(chunk, |x, y, z, world_position| {
51+
let block = self.generate_block(world_position);
5252
chunk.set_unpadded(x, y, z, block);
5353
});
5454

5555
for_each_chunk_coordinate!(chunk, |x, y, z, _| {
56-
let block = chunk.get_unpadded(x, y, z);
56+
let pos = Vec3 {
57+
x: x as f32,
58+
y: y as f32,
59+
z: z as f32,
60+
};
5761

58-
if block == BlockId::Stone && Chunk::valid_unpadded(x, y + 1, z) {
59-
let top_block = chunk.get_unpadded(x, y + 1, z);
60-
if top_block == BlockId::Air {
61-
chunk.set_unpadded(x, y, z, BlockId::Grass);
62-
}
63-
}
62+
let block = self.decorate_block(chunk, pos);
63+
chunk.set_unpadded(x, y, z, block);
6464
});
6565
}
6666

67+
fn decorate_block(&self, chunk: &Chunk, position: Vec3) -> BlockId {
68+
let x = position.x as usize;
69+
let y = position.y as usize;
70+
let z = position.z as usize;
71+
72+
let block = chunk.get_unpadded(x,y,z);
73+
if block == BlockId::Air {
74+
return block;
75+
}
76+
77+
78+
let mut depth_below_nearest_air = 0;
79+
let depth_check = 5;
80+
81+
for delta_height in 0..depth_check {
82+
if !Chunk::valid_unpadded(x, y + delta_height, z) {
83+
break
84+
}
85+
86+
let block = chunk.get_unpadded(x,y + delta_height,z);
87+
88+
if block == BlockId::Air {
89+
break
90+
}
91+
92+
depth_below_nearest_air += 1;
93+
}
94+
95+
match depth_below_nearest_air {
96+
0_i32..=1_i32 => BlockId::Grass,
97+
2..5 => BlockId::Dirt,
98+
_ => BlockId::Stone
99+
}
100+
}
101+
67102
fn generate_block(&self, position: Vec3) -> BlockId {
68103
let terrain_height = self.determine_terrain_height(position);
69104
let terrain_density = self.determine_terrain_density(position);

0 commit comments

Comments
 (0)