-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathcomposition.rs
More file actions
206 lines (180 loc) · 6.32 KB
/
composition.rs
File metadata and controls
206 lines (180 loc) · 6.32 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Composition generator, used to populate chunks with blocks
//! based on the density and biome values.
use crate::{block_index, util, CompositionGenerator, SEA_LEVEL};
use base::{chunk::BiomeStore, Biome, BlockId, Chunk, ChunkPosition};
use bitvec::order::LocalBits;
use bitvec::slice::BitSlice;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
use std::cmp::min;
/// A composition generator which generates basic
/// terrain based on biome values.
#[derive(Debug, Default)]
pub struct BasicCompositionGenerator;
impl CompositionGenerator for BasicCompositionGenerator {
fn generate_for_chunk(
&self,
chunk: &mut Chunk,
_pos: ChunkPosition,
biomes: &BiomeStore,
density: &BitSlice<LocalBits, u8>,
seed: u64,
) {
// For each column in the chunk, go from top to
// bottom. The first time a block density value is set to `true`,
// set it and the next three blocks to dirt. After that, use
// stone.
for x in 0..16 {
for z in 0..16 {
basic_composition_for_column(
x,
z,
chunk,
density,
seed,
biomes.get_at_block(x, 0, z),
);
}
}
}
}
fn basic_composition_for_column(
x: usize,
z: usize,
chunk: &mut Chunk,
density: &BitSlice<LocalBits, u8>,
seed: u64,
biome: Biome,
) {
basic_composition_for_solid_biome(x, z, chunk, density, seed, biome);
}
fn basic_composition_for_solid_biome(
x: usize,
z: usize,
chunk: &mut Chunk,
density: &BitSlice<LocalBits, u8>,
seed: u64,
biome: Biome,
) {
let mut rng =
XorShiftRng::seed_from_u64(util::shuffle_seed_for_column(seed, chunk.position(), x, z));
let top_soil = top_soil_block(biome);
let mut topsoil_remaining = -1;
let mut water_level = 0; // `level` block data starts at 0 and skips to min(8+n, 15) for each level of water downward
for y in (0..256).rev() {
let mut block = BlockId::air();
let is_solid = density[block_index(x, y, z)];
let mut skip = false;
if biome == Biome::Ocean {
if y <= SEA_LEVEL && !is_solid {
block = BlockId::water().with_water_level(water_level);
if water_level == 0 {
water_level = 8;
} else {
water_level = min(water_level + 1, 15);
}
skip = true;
} else if y >= SEA_LEVEL {
continue; // Leave at air - no blocks above sea level in ocean
}
}
if !skip {
if y <= rng.gen_range(0, 4) {
block = BlockId::bedrock();
} else {
block = if is_solid {
if topsoil_remaining == -1 {
topsoil_remaining = 3;
top_soil
} else if topsoil_remaining > 0 {
let block = underneath_top_soil_block(biome);
topsoil_remaining -= 1;
block
} else {
BlockId::stone()
}
} else {
topsoil_remaining = -1;
BlockId::air()
};
}
}
if !block.is_air() {
chunk.set_block_at(x, y, z, block);
}
}
}
/// Returns the top soil block for the given biome.
fn top_soil_block(biome: Biome) -> BlockId {
match biome {
Biome::SnowyTundra
| Biome::IceSpikes
| Biome::SnowyTaiga
| Biome::SnowyTaigaMountains
| Biome::SnowyBeach => BlockId::grass_block().with_snowy(true),
Biome::GravellyMountains | Biome::ModifiedGravellyMountains => BlockId::gravel(),
Biome::StoneShore => BlockId::stone(),
Biome::Beach | Biome::Desert | Biome::DesertHills | Biome::DesertLakes => BlockId::sand(),
Biome::MushroomFields | Biome::MushroomFieldShore => BlockId::mycelium(),
Biome::Badlands
| Biome::ErodedBadlands
| Biome::WoodedBadlandsPlateau
| Biome::BadlandsPlateau
| Biome::ModifiedBadlandsPlateau
| Biome::ModifiedWoodedBadlandsPlateau => BlockId::red_sand(),
Biome::Ocean => BlockId::sand(),
_ => BlockId::grass_block(),
}
}
/// Returns the block under the top soil block for the given biome.
fn underneath_top_soil_block(biome: Biome) -> BlockId {
match biome {
Biome::SnowyBeach => BlockId::snow_block(),
Biome::GravellyMountains | Biome::ModifiedGravellyMountains => BlockId::gravel(),
Biome::StoneShore => BlockId::stone(),
Biome::Beach | Biome::Desert | Biome::DesertHills | Biome::DesertLakes => {
BlockId::sandstone()
}
Biome::MushroomFields | Biome::MushroomFieldShore => BlockId::dirt(),
Biome::Badlands
| Biome::ErodedBadlands
| Biome::WoodedBadlandsPlateau
| Biome::BadlandsPlateau
| Biome::ModifiedBadlandsPlateau
| Biome::ModifiedWoodedBadlandsPlateau => BlockId::red_sandstone(),
Biome::Ocean => BlockId::sand(),
_ => BlockId::dirt(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use bitvec::vec::BitVec;
#[test]
fn test_basic_composition_for_column() {
let mut density = BitVec::from_vec(vec![0u8; 16 * 256 * 16 / 8]);
let x = 0;
let z = 0;
for y in 0..=32 {
density.set(block_index(x, y, z), true);
}
for y in 40..=64 {
density.set(block_index(x, y, z), true);
}
let mut chunk = Chunk::new(ChunkPosition::new(0, 0));
basic_composition_for_column(x, z, &mut chunk, &density[..], 435, Biome::Plains);
for y in 4..=28 {
assert_eq!(chunk.block_at(x, y, z).unwrap(), BlockId::stone());
}
for y in 29..=31 {
assert_eq!(chunk.block_at(x, y, z).unwrap(), BlockId::dirt());
}
for y in 33..40 {
assert_eq!(chunk.block_at(x, y, z).unwrap(), BlockId::air());
}
for y in 40..=60 {
assert_eq!(chunk.block_at(x, y, z).unwrap(), BlockId::stone());
}
assert_eq!(chunk.block_at(x, 64, z).unwrap(), BlockId::grass_block());
}
}