@@ -10,19 +10,19 @@ macro_rules! for_each_chunk_coordinate {
10
10
#[ cfg( feature = "skip_chunk_padding" ) ]
11
11
if x == 0
12
12
|| 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
17
17
{
18
18
continue ;
19
19
}
20
20
21
21
let chunk_origin = $chunk. position * CHUNK_SIZE as f32 ;
22
22
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;
24
24
25
- $body( x, y, z, block_position ) ;
25
+ $body( x, y, z, world_position ) ;
26
26
}
27
27
}
28
28
}
@@ -47,23 +47,58 @@ impl Generator {
47
47
return ;
48
48
}
49
49
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 ) ;
52
52
chunk. set_unpadded( x, y, z, block) ;
53
53
} ) ;
54
54
55
55
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
+ } ;
57
61
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) ;
64
64
} ) ;
65
65
}
66
66
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
+
67
102
fn generate_block ( & self , position : Vec3 ) -> BlockId {
68
103
let terrain_height = self . determine_terrain_height ( position) ;
69
104
let terrain_density = self . determine_terrain_density ( position) ;
0 commit comments