-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmountain_islands.rs
202 lines (173 loc) · 7.8 KB
/
mountain_islands.rs
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
use infinigen_common::blocks::Palette;
use infinigen_common::chunks::{Array3Chunk, CHUNK_SIZE, CHUNK_SIZE_F64, CHUNK_USIZE};
use infinigen_common::world::{
BlockPosition, ChunkPosition, MappedBlockID, WorldGen, WorldPosition,
};
use infinigen_common::zoom::ZoomLevel;
use noise::{Fbm, MultiFractal, NoiseFn, Perlin};
use splines::{Interpolation, Key, Spline};
use crate::blocks::{
DIRT_BLOCK_ID, GRASS_BLOCK_ID, GRAVEL_BLOCK_ID, SAND_BLOCK_ID, SNOW_BLOCK_ID, STONE_BLOCK_ID,
WATER_BLOCK_ID,
};
#[derive(Debug, Clone)]
pub struct MountainIslands {
/// The world height at any given (x, z)
heightmap: Fbm<Perlin>,
verticality: Perlin,
terrain_variance: Fbm<Perlin>,
vspline: Spline<f64, f64>,
/// max mountain size without zoom is roughly double this value
vertical_scale: f64,
horizontal_smoothness: f64,
water: MappedBlockID,
snow: MappedBlockID,
gravel: MappedBlockID,
sand: MappedBlockID,
dirt: MappedBlockID,
grass: MappedBlockID,
stone: MappedBlockID,
}
impl MountainIslands {
pub fn new(seed: u32, palette: Palette) -> Self {
let vspline = Spline::from_vec(vec![
Key::new(-1., 0.6, Interpolation::Cosine),
Key::new(-0.9, 0.7, Interpolation::Cosine),
Key::new(0., 0.8, Interpolation::Cosine),
Key::new(0.5, 0.85, Interpolation::Cosine),
Key::new(0.8, 0.9, Interpolation::Cosine),
Key::new(0.9, 1., Interpolation::Cosine),
Key::new(1.1, 1.5, Interpolation::default()), // this last one must be strictly greater than 1 because sometime we may sample with exactly the value 1.
]);
let wgen = Self {
heightmap: default_heightmap(seed),
verticality: Perlin::new(seed),
terrain_variance: default_terrain_variance(seed),
vspline,
vertical_scale: CHUNK_SIZE_F64 * 4.,
horizontal_smoothness: CHUNK_SIZE_F64 * 0.1,
water: *palette.inner.get(WATER_BLOCK_ID).unwrap(),
snow: *palette.inner.get(SNOW_BLOCK_ID).unwrap(),
gravel: *palette.inner.get(GRAVEL_BLOCK_ID).unwrap(),
sand: *palette.inner.get(SAND_BLOCK_ID).unwrap(),
dirt: *palette.inner.get(DIRT_BLOCK_ID).unwrap(),
grass: *palette.inner.get(GRASS_BLOCK_ID).unwrap(),
stone: *palette.inner.get(STONE_BLOCK_ID).unwrap(),
};
tracing::debug!(?wgen.heightmap.octaves, wgen.heightmap.frequency, wgen.heightmap.lacunarity, wgen.heightmap.persistence, "MountainIslands initialized");
wgen
}
}
fn default_heightmap(seed: u32) -> Fbm<Perlin> {
Fbm::<Perlin>::new(seed).set_octaves(6)
}
pub fn default_terrain_variance(seed: u32) -> Fbm<Perlin> {
Fbm::<Perlin>::new(seed).set_octaves(8).set_persistence(0.7)
}
const SEA_LEVEL: f64 = 0.;
// we still bound the worldgen on the Y axis to improve performance
// for an infinitely deep world, we would not have a MIN_Y_HEIGHT maybe
const MIN_Y_HEIGHT: i32 = -6;
/// Based on <https://www.youtube.com/watch?v=CSa5O6knuwI>
impl WorldGen for MountainIslands {
fn get(&self, pos: &ChunkPosition, zoom_level: ZoomLevel) -> Option<Array3Chunk> {
if pos.y < MIN_Y_HEIGHT {
return None;
}
let zoom = zoom_level.as_f64();
let sand_level = (SEA_LEVEL + (1. / zoom)).floor();
// let snow_level: f64 = (SEA_LEVEL + self.vertical_scale) * zoom;
let block_ranges = [
(SEA_LEVEL + (-3. * zoom + 1.), self.sand),
(SEA_LEVEL + (9. * zoom + 1.), self.dirt),
(SEA_LEVEL + (285. * zoom + 1.), self.grass),
(SEA_LEVEL + (300. * zoom + 1.), self.stone),
(f64::INFINITY, self.snow),
];
let mut chunk = Array3Chunk::default();
let offset: WorldPosition = pos.into();
let zoomed_offset = [
offset.x as f64 / zoom,
offset.y as f64 / zoom,
offset.z as f64 / zoom,
];
// needed for every column
let mut wheights = [[0.; CHUNK_USIZE]; CHUNK_USIZE];
let mut nxzs = [[(0., 0.); CHUNK_USIZE]; CHUNK_USIZE];
let mut is_empty = true;
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
let wx = x as f64 / zoom + zoomed_offset[0];
let wz = z as f64 / zoom + zoomed_offset[2];
let nx = wx / (self.horizontal_smoothness * self.vertical_scale);
let nz = wz / (self.horizontal_smoothness * self.vertical_scale);
let mut wheight = self.heightmap.get([nx, nz]);
let verticality = self.verticality.get([nx, nz]);
wheight *= self.vertical_scale * self.vspline.sample(verticality).unwrap();
// short circuit if bottom-most layer (y=0) is empty as this world doesn't have things in the sky
let wy = zoomed_offset[1];
if wy <= wheight || wy <= SEA_LEVEL {
is_empty = false;
}
wheights[x as usize][z as usize] = wheight;
nxzs[x as usize][z as usize] = (nx, nz);
}
}
if is_empty {
return None;
}
let mut terrain_variances = [[0.; CHUNK_USIZE]; CHUNK_USIZE];
{
let _span = tracing::debug_span!("worldgen{stage = terrain}").entered();
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
let wheight = wheights[x as usize][z as usize];
for y in 0..CHUNK_SIZE {
let wy = y as f64 / zoom + zoomed_offset[1];
// wheight is sunken, so we're in a body of water
if wheight <= wy && wy <= SEA_LEVEL {
is_empty = false;
chunk.insert(&BlockPosition { x, y, z }, self.water);
continue;
}
// ensure we fill blocks up to the wheight
if wy <= wheight {
is_empty = false;
if wy < SEA_LEVEL {
// always gravel under sea
chunk.insert(&BlockPosition { x, y, z }, self.gravel);
continue;
} else if wy.floor() <= sand_level {
// sand always borders water
chunk.insert(&BlockPosition { x, y, z }, self.sand);
continue;
}
let next_band_chance = {
let val = &mut terrain_variances[x as usize][z as usize];
// exactly float zero means it (probably?) wasn't calculated before
if *val == 0. {
let (nx, nz) = nxzs[x as usize][z as usize];
*val = self.terrain_variance.get([nx, nz]) / 2.0;
}
*val
};
// Assign block type based on the height and noise.
let mut block_id = block_ranges[0].1;
for &(threshold, id) in &block_ranges {
if wy + next_band_chance * self.vertical_scale < threshold {
block_id = id;
break;
}
}
chunk.insert(&BlockPosition { x, y, z }, block_id);
}
}
}
}
if is_empty {
return None;
}
}
Some(chunk)
}
}