-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathsuperflat.rs
More file actions
85 lines (73 loc) · 2.82 KB
/
superflat.rs
File metadata and controls
85 lines (73 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use base::{anvil::level::SuperflatGeneratorOptions, Biome, BlockId, Chunk, ChunkPosition};
use crate::WorldGenerator;
pub struct SuperflatWorldGenerator {
pub options: SuperflatGeneratorOptions,
}
impl SuperflatWorldGenerator {
pub fn new(options: SuperflatGeneratorOptions) -> Self {
Self { options }
}
}
impl WorldGenerator for SuperflatWorldGenerator {
fn generate_chunk(&self, position: ChunkPosition) -> Chunk {
let biome = Biome::from_name(self.options.biome.as_str()).unwrap_or(Biome::Plains);
let mut chunk = Chunk::new_with_default_biome(position, biome);
let mut y_counter = 0;
for layer in self.options.clone().layers {
if layer.height == 0 {
continue;
}
// FIXME: get rid of this hack by having a consistent naming convention - Item::name() returns `stone` but BlockId::from_identifier requires `minecraft:stone`
let layer_block = BlockId::from_identifier(&format!("minecraft:{}", layer.block));
if let Some(layer_block) = layer_block {
for y in y_counter..(y_counter + layer.height) {
for x in 0..16 {
for z in 0..16 {
chunk.set_block_at(x as usize, y as usize, z as usize, layer_block);
}
}
}
} else {
// Skip this layer
log::warn!("Failed to generate layer: unknown block {}", layer.block);
}
y_counter += layer.height;
}
chunk.recalculate_heightmaps();
chunk
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_worldgen_flat() {
let options = SuperflatGeneratorOptions {
biome: Biome::Mountains.name().to_owned(),
..Default::default()
};
let chunk_pos = ChunkPosition { x: 1, z: 2 };
let generator = SuperflatWorldGenerator { options };
let chunk = generator.generate_chunk(chunk_pos);
assert_eq!(chunk.position(), chunk_pos);
for x in 0usize..16 {
for z in 0usize..16 {
for (y, block) in &[
(0usize, BlockId::bedrock()),
(1usize, BlockId::dirt()),
(2usize, BlockId::dirt()),
(3usize, BlockId::grass_block()),
] {
assert_eq!(chunk.block_at(x, *y, z).unwrap(), *block);
}
for y in 4..256 {
assert_eq!(
chunk.block_at(x as usize, y as usize, z as usize).unwrap(),
BlockId::air()
);
}
assert_eq!(chunk.biomes().get_at_block(x, 0, z), Biome::Mountains);
}
}
}
}