-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathlib.rs
More file actions
471 lines (411 loc) · 14.3 KB
/
lib.rs
File metadata and controls
471 lines (411 loc) · 14.3 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#![forbid(unsafe_code)]
//! World generation for Feather.
//!
//! Generation is primarily based around the `ComposableGenerator`,
//! which allows configuration of a world generator pipeline.
mod biomes;
mod composition;
mod density_map;
mod finishers;
pub mod noise;
mod superflat;
mod util;
pub mod voronoi;
use base::chunk::BiomeStore;
use base::{Biome, BlockId, Chunk, ChunkPosition};
pub use biomes::{DistortedVoronoiBiomeGenerator, TwoLevelBiomeGenerator};
use bitvec::vec::BitVec;
use bitvec::{order::LocalBits, slice::BitSlice};
pub use composition::BasicCompositionGenerator;
pub use density_map::{DensityMapGeneratorImpl, HeightMapGenerator};
use finishers::{ClumpedFoliageFinisher, SingleFoliageFinisher, SnowFinisher};
pub use noise::NoiseLerper;
use num_traits::ToPrimitive;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
use smallvec::SmallVec;
pub use superflat::SuperflatWorldGenerator;
/// Sea-level height.
pub const SEA_LEVEL: usize = 64;
/// Sky limit.
pub const SKY_LIMIT: usize = 255;
/// Depth of an ocean.
const OCEAN_DEPTH: usize = 30;
pub trait WorldGenerator: Send + Sync {
/// Generates the chunk at the given position.
fn generate_chunk(&self, position: ChunkPosition) -> Chunk;
}
pub struct VoidWorldGenerator;
impl WorldGenerator for VoidWorldGenerator {
fn generate_chunk(&self, position: ChunkPosition) -> Chunk {
Chunk::new(position)
}
}
/// A "composable" world generator.
///
/// This generator will generate the world based
/// on a pipeline, and each step in the pipeline passes
/// data to the next stage.
///
/// The pipeline stages are as follows:
/// * Biomes - generates a biome grid.
/// * Terrain density - generates the terrain density values using Perlin noise.
/// * Terrain composition - sets the correct block types based on the biome and terrain density.
/// * Finishing generators - generates final elements, such as grass, snow, and trees.
///
/// This generator is based on [this document](http://cuberite.xoft.cz/docs/Generator.html).
pub struct ComposableGenerator {
/// The biome generator.
biome: Box<dyn BiomeGenerator>,
/// The height map generator.
density_map: Box<dyn DensityMapGenerator>,
/// The composition generator.
composition: Box<dyn CompositionGenerator>,
/// A vector of finishing generators used
/// by this composable generator.
finishers: SmallVec<[Box<dyn FinishingGenerator>; 8]>,
/// The world seed.
seed: u64,
}
impl ComposableGenerator {
/// Creates a new `ComposableGenerator` with the given stages.
pub fn new<B, D, C, F>(
biome: B,
density_map: D,
composition: C,
finishers: F,
seed: u64,
) -> Self
where
B: BiomeGenerator + 'static,
D: DensityMapGenerator + 'static,
C: CompositionGenerator + 'static,
F: IntoIterator<Item = Box<dyn FinishingGenerator>>,
{
Self {
biome: Box::new(biome),
density_map: Box::new(density_map),
composition: Box::new(composition),
finishers: finishers.into_iter().collect(),
seed,
}
}
/// A default composable generator, used
/// for worlds with "default" world type.
pub fn default_with_seed(seed: u64) -> Self {
let finishers: Vec<Box<dyn FinishingGenerator>> = vec![
Box::new(SnowFinisher::default()),
Box::new(SingleFoliageFinisher::default()),
Box::new(ClumpedFoliageFinisher::default()),
];
Self::new(
TwoLevelBiomeGenerator::default(),
DensityMapGeneratorImpl::default(),
BasicCompositionGenerator::default(),
finishers,
seed,
)
}
}
impl WorldGenerator for ComposableGenerator {
fn generate_chunk(&self, position: ChunkPosition) -> Chunk {
let mut seed_shuffler = XorShiftRng::seed_from_u64(self.seed);
// Generate biomes for 3x3 grid of chunks around current chunk.
let biome_seed = seed_shuffler.gen();
let mut biomes = vec![];
for z in -1..=1 {
for x in -1..=1 {
let pos = ChunkPosition::new(position.x + x, position.z + z);
biomes.push(self.biome.generate_for_chunk(pos, biome_seed));
}
}
let biomes = NearbyBiomes::from_slice(&biomes[..]).unwrap();
let density_map =
self.density_map
.generate_for_chunk(position, &biomes, seed_shuffler.gen());
let mut chunk = Chunk::new(position);
*chunk.biomes_mut() = *biomes.center();
self.composition.generate_for_chunk(
&mut chunk,
position,
&biomes.biome_stores[4], // Center chunk
density_map.as_bitslice(),
seed_shuffler.gen(),
);
// Calculate top blocks in chunk.
// TODO: perhaps this should be moved to `Chunk`?
let mut top_blocks = TopBlocks::new();
for x in 0..16 {
for z in 0..16 {
for y in (0..256).rev() {
if chunk.block_at(x, y, z).unwrap() != BlockId::air() {
top_blocks.set_top_block_at(x, z, y);
break;
}
}
}
}
chunk.recalculate_heightmaps();
// Finishers.
for finisher in &self.finishers {
finisher.generate_for_chunk(
&mut chunk,
&biomes.biome_stores[4],
&top_blocks,
seed_shuffler.gen(),
);
}
chunk
}
}
/// A generator which generates the biome grid for a `ComposableGenerator`.
pub trait BiomeGenerator: Send + Sync {
/// Generates the biomes for a given chunk.
/// This function should be deterministic.
fn generate_for_chunk(&self, chunk: ChunkPosition, seed: u64) -> BiomeStore;
}
/// A generator which generates the density map for a chunk.
/// Used in the `ComposableGenerator` pipeline.
pub trait DensityMapGenerator: Send + Sync {
/// Generates the density map for a given chunk.
/// A compact array of booleans is returned, indexable
/// by (y << 8) | (x << 4) | z. Those set to `true` will
/// contain solid blacks; those set to `false` will be air.
fn generate_for_chunk(
&self,
chunk: ChunkPosition,
biomes: &NearbyBiomes,
seed: u64,
) -> BitVec<LocalBits, u8>;
}
/// A generator which populates the given chunk using blocks
/// based on the given density map and biomes.
pub trait CompositionGenerator: Send + Sync {
/// Populates the given chunk with blocks based on the given
/// biomes and density map.
fn generate_for_chunk(
&self,
chunk: &mut Chunk,
pos: ChunkPosition,
biomes: &BiomeStore,
density: &BitSlice<LocalBits, u8>,
seed: u64,
);
}
/// A generator, run after composition,
/// which can add finishing elements to chunks,
/// such as grass, trees, and snow.
pub trait FinishingGenerator: Send + Sync {
/// Populates the given chunk with any
/// finishing blocks.
fn generate_for_chunk(
&self,
chunk: &mut Chunk,
biomes: &BiomeStore,
top_blocks: &TopBlocks,
seed: u64,
);
}
/// Returns an index into a one-dimensional array
/// for the given x, y, and z values.
pub fn block_index(x: usize, y: usize, z: usize) -> usize {
assert!(x < 16 && y < 256 && z < 16);
(y << 8) | (x << 4) | z
}
/// Represents the highest solid blocks in a chunk.
#[derive(Default)]
pub struct TopBlocks {
top_blocks: Vec<u8>,
}
impl TopBlocks {
pub fn new() -> Self {
Self {
top_blocks: vec![0; 16 * 16],
}
}
/// Fetches the highest solid blocks for the
/// given column coordinates (chunk-local).
pub fn top_block_at(&self, x: usize, z: usize) -> usize {
self.top_blocks[x + (z << 4)] as usize
}
pub fn set_top_block_at(&mut self, x: usize, z: usize, top: usize) {
self.top_blocks[x + (z << 4)] = top as u8;
}
}
/// Represents the biomes in a 3x3 grid of chunks,
/// centered on the chunk currently being generated.
pub struct NearbyBiomes {
/// 2D array of chunk biomes. The chunk biomes
/// for a given chunk position relative to the center
/// chunk can be obtained using (x + 1) + (z + 1) * 3.
pub biome_stores: [BiomeStore; 3 * 3],
}
impl NearbyBiomes {
pub fn from_slice(biome_store_slice: &[BiomeStore]) -> Option<Self> {
let mut biome_stores = [BiomeStore::new(Biome::Badlands); 9];
if biome_store_slice.len() != biome_stores.len() {
return None;
}
biome_stores.clone_from_slice(biome_store_slice);
Some(Self { biome_stores })
}
/// Gets the biome at the given coordinates.
///
/// # Panics
/// Panics if `x >= 16`, `z >= 16`, or `y >= 256`.
pub fn get_at_block<N: ToPrimitive>(&self, x: N, y: N, z: N) -> Biome {
let (index, local_x, local_y, local_z) = self.index(x, y, z);
self.biome_stores[index].get_at_block(local_x, local_y, local_z)
}
/// Gets the biome at the given coordinates, in multiples
/// of 4 blocks.
///
/// # Panics
/// Panics if `x >= 4`, `z >= 4`, or `y >= 64`.
pub fn get<N: ToPrimitive>(&self, x: N, y: N, z: N) -> Biome {
let (index, local_x, local_y, local_z) = self.index(
x.to_isize().unwrap() * 4,
y.to_isize().unwrap() * 4,
z.to_isize().unwrap() * 4,
);
self.biome_stores[index].get(local_x / 4, local_y / 4, local_z / 4)
}
/// Sets the biome at the given coordinates, in multiples
/// of 4 blocks.
///
/// # Panics
/// Panics if `x >= 4`, `z >= 4`, or `y >= 64`.
pub fn set<N: ToPrimitive>(&mut self, x: N, y: N, z: N, biome: Biome) {
let (index, local_x, local_y, local_z) = self.index(
x.to_isize().unwrap() * 4,
y.to_isize().unwrap() * 4,
z.to_isize().unwrap() * 4,
);
self.biome_stores[index].set(local_x / 4, local_y / 4, local_z / 4, biome);
}
pub fn center(&self) -> &BiomeStore {
&self.biome_stores[4]
}
pub fn center_mut(&mut self) -> &mut BiomeStore {
&mut self.biome_stores[4]
}
/// Returns a tuple of (chunk_index, local_x, local_y, local_z)
fn index<N: ToPrimitive>(&self, ox: N, oy: N, oz: N) -> (usize, usize, usize, usize) {
// FIXME: Does this function need to so complicated?
let ox = ox.to_isize().unwrap();
let oy = oy.to_isize().unwrap();
let oz = oz.to_isize().unwrap();
let x = ox + 16;
let z = oz + 16;
let chunk_x = (x / 16) as usize;
let chunk_z = (z / 16) as usize;
let mut local_x = (ox % 16).unsigned_abs();
let local_y = (oy % 16).unsigned_abs();
let mut local_z = (oz % 16).unsigned_abs();
if ox < 0 {
local_x = 16 - local_x;
}
if oz < 0 {
local_z = 16 - local_z;
}
(chunk_x + chunk_z * 3, local_x, local_y, local_z)
}
}
/// A biome generator which always generates plains.
#[derive(Debug, Default)]
pub struct StaticBiomeGenerator;
impl BiomeGenerator for StaticBiomeGenerator {
fn generate_for_chunk(&self, _chunk: ChunkPosition, _seed: u64) -> BiomeStore {
BiomeStore::new(Biome::Plains)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_reproducability() {
let seeds: [u64; 4] = [std::u64::MAX, 3243, 0, 100];
let chunks = [
ChunkPosition::new(0, 0),
ChunkPosition::new(-1, -1),
ChunkPosition::new(1, 1),
];
for seed in seeds.iter() {
let gen = ComposableGenerator::default_with_seed(*seed);
for chunk in chunks.iter() {
let first = gen.generate_chunk(*chunk);
let second = gen.generate_chunk(*chunk);
test_chunks_eq(&first, &second);
}
}
}
fn test_chunks_eq(a: &Chunk, b: &Chunk) {
for x in 0..16 {
for z in 0..16 {
for y in 0..256 {
assert_eq!(a.block_at(x, y, z), b.block_at(x, y, z));
}
}
}
for x in 0..4 {
for z in 0..4 {
for y in 0..64 {
assert_eq!(a.biomes().get(x, y, z), b.biomes().get(x, y, z));
}
}
}
}
#[test]
pub fn test_worldgen_void() {
let chunk_pos = ChunkPosition { x: 1, z: 2 };
let generator = VoidWorldGenerator;
let chunk = generator.generate_chunk(chunk_pos);
// No sections have been generated
assert!(chunk.sections().iter().all(|sec| sec.is_none()));
assert_eq!(chunk_pos, chunk.position());
}
#[test]
fn test_chunk_biomes() {
let mut biomes = BiomeStore::new(Biome::Plains);
for x in 0..4 {
for z in 0..4 {
for y in 0..64 {
assert_eq!(biomes.get(x, y, z), Biome::Plains);
biomes.set(x, y, z, Biome::Ocean);
assert_eq!(biomes.get(x, y, z), Biome::Ocean);
}
}
}
}
#[test]
fn test_static_biome_generator() {
let gen = StaticBiomeGenerator::default();
let biomes = gen.generate_for_chunk(ChunkPosition::new(0, 0), 0);
for x in 0..4 {
for z in 0..4 {
for y in 0..64 {
assert_eq!(biomes.get(x, y, z), Biome::Plains);
}
}
}
}
#[test]
fn test_nearby_biomes() {
let biomes = vec![
BiomeStore::new(Biome::Plains),
BiomeStore::new(Biome::Swamp),
BiomeStore::new(Biome::Savanna),
BiomeStore::new(Biome::BirchForest),
BiomeStore::new(Biome::DarkForest),
BiomeStore::new(Biome::Mountains),
BiomeStore::new(Biome::Ocean),
BiomeStore::new(Biome::Desert),
BiomeStore::new(Biome::Taiga),
];
let biomes = NearbyBiomes::from_slice(&biomes[..]).unwrap();
assert_eq!(biomes.get_at_block(0, 0, 0), Biome::DarkForest);
assert_eq!(biomes.get_at_block(16, 0, 16), Biome::Taiga);
assert_eq!(biomes.get_at_block(-1, 0, -1), Biome::Plains);
assert_eq!(biomes.get_at_block(-1, 0, 0), Biome::BirchForest);
}
}