Skip to content

Commit 9153481

Browse files
Refactor chunk generation
1 parent 93545c4 commit 9153481

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/server/terrain/util/generator.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,8 @@ use terrain_resources::{Generator, NoiseFunctionParams, TerrainGeneratorParams};
22

33
use crate::prelude::*;
44

5-
impl Generator {
6-
pub fn new(seed: u32) -> Generator {
7-
Self::new_with_params(seed, TerrainGeneratorParams::default())
8-
}
9-
10-
pub fn new_with_params(seed: u32, params: TerrainGeneratorParams) -> Generator {
11-
Generator {
12-
seed,
13-
perlin: Perlin::new(seed),
14-
params,
15-
}
16-
}
17-
18-
pub fn generate_chunk(&self, chunk: &mut Chunk) {
19-
if chunk.position.y < 0.0 {
20-
return;
21-
}
22-
5+
macro_rules! for_each_chunk_coordinate {
6+
($chunk:expr, $body:expr) => {
237
for x in 0..CHUNK_SIZE + 2 {
248
for y in 0..CHUNK_SIZE + 2 {
259
for z in 0..CHUNK_SIZE + 2 {
@@ -34,14 +18,40 @@ impl Generator {
3418
continue;
3519
}
3620

37-
let chunk_origin = chunk.position * CHUNK_SIZE as f32;
21+
let chunk_origin = $chunk.position * CHUNK_SIZE as f32;
3822
let local_position = Vec3::new(x as f32, y as f32, z as f32);
3923
let block_position = chunk_origin + local_position;
40-
let block = self.generate_block(block_position);
41-
chunk.set_unpadded(x, y, z, block);
24+
25+
$body(x, y, z, block_position);
4226
}
4327
}
4428
}
29+
};
30+
}
31+
32+
33+
impl Generator {
34+
pub fn new(seed: u32) -> Generator {
35+
Self::new_with_params(seed, TerrainGeneratorParams::default())
36+
}
37+
38+
pub fn new_with_params(seed: u32, params: TerrainGeneratorParams) -> Generator {
39+
Generator {
40+
seed,
41+
perlin: Perlin::new(seed),
42+
params,
43+
}
44+
}
45+
46+
pub fn generate_chunk(&self, chunk: &mut Chunk) {
47+
if chunk.position.y < 0.0 {
48+
return;
49+
}
50+
51+
for_each_chunk_coordinate!(chunk, |x, y, z, block_position| {
52+
let block = self.generate_block(block_position);
53+
chunk.set_unpadded(x, y, z, block);
54+
});
4555
}
4656

4757
fn generate_block(&self, position: Vec3) -> BlockId {

0 commit comments

Comments
 (0)