-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.rs
More file actions
346 lines (277 loc) · 10.7 KB
/
Copy pathgenerator.rs
File metadata and controls
346 lines (277 loc) · 10.7 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
use fastrand::Rng;
use crate::{
prelude::*,
terrain::{
config::NoiseFunctionParams,
resources::{Generator, Noise, NoiseSample},
},
};
macro_rules! for_each_chunk_coordinate {
($chunk:expr, $body:expr) => {
for x in 0..CHUNK_SIZE + 2 {
for y in 0..CHUNK_SIZE + 2 {
for z in 0..CHUNK_SIZE + 2 {
#[cfg(feature = "skip_chunk_padding")]
if x == 0
|| x == CHUNK_SIZE + 1
|| y == 0
|| y == CHUNK_SIZE + 1
|| z == 0
|| z == CHUNK_SIZE + 1
{
continue;
}
let chunk_origin = $chunk.position * CHUNK_SIZE as i32;
let local_position = IVec3::new(x as i32, y as i32, z as i32);
let world_position = chunk_origin + local_position;
$body(x, y, z, world_position);
}
}
}
};
}
impl Generator {
pub fn new(seed: u32) -> Generator {
Generator {
noise: Noise::new(seed),
params: CONFIG.generator.clone(),
}
}
pub fn generate_chunk(&self, chunk: &mut Chunk) {
let mut rng = fastrand::Rng::new();
rng.seed(chunk.rng_seed());
for_each_chunk_coordinate!(chunk, |x, y, z, world_position| {
let block = self.generate_block(world_position);
chunk.set_unpadded(x, y, z, block);
});
for_each_chunk_coordinate!(chunk, |x, y, z, _| {
let pos = IVec3::new(x as i32, y as i32, z as i32);
self.decorate_block(&mut rng, chunk, pos);
});
for _ in 0..self.params.tree.spawn_attempts_per_chunk {
self.attempt_spawn_tree(&mut rng, chunk);
}
}
fn attempt_spawn_tree(&self, rng: &mut Rng, chunk: &mut Chunk) {
let proposal = self.propose_tree_blocks(rng);
struct Bounds {
min: IVec3,
max: IVec3,
}
let proposal_bounds = proposal.iter().fold(
Bounds {
min: IVec3::ZERO,
max: IVec3::ZERO,
},
|bounds, (relative_pos, _block_id)| Bounds {
min: IVec3 {
x: bounds.min.x.min(relative_pos.x),
y: bounds.min.y.min(relative_pos.y),
z: bounds.min.z.min(relative_pos.z),
},
max: IVec3 {
x: bounds.max.x.max(relative_pos.x),
y: bounds.max.y.max(relative_pos.y),
z: bounds.max.z.max(relative_pos.z),
},
},
);
let sapling_x =
rng.i32(proposal_bounds.min.x.abs()..(CHUNK_SIZE as i32 - proposal_bounds.max.x));
let sapling_y =
rng.i32(proposal_bounds.min.y.abs()..(CHUNK_SIZE as i32 - proposal_bounds.max.y));
let sapling_z =
rng.i32(proposal_bounds.min.z.abs()..(CHUNK_SIZE as i32 - proposal_bounds.max.z));
if chunk.get(sapling_x, sapling_y, sapling_z) != BlockId::Grass {
return;
}
let proposal_valid = proposal.iter().all(|(relative_pos, _block)| {
let IVec3 { x, y, z } = relative_pos;
Chunk::is_within_padded_bounds(
sapling_x + { *x },
sapling_y + { *y },
sapling_z + { *z },
) && chunk.get(sapling_x + { *x }, sapling_y + { *y }, sapling_z + { *z })
== BlockId::Air
});
if !proposal_valid {
return;
}
proposal.iter().for_each(|(relative_pos, block_id)| {
let IVec3 { x, y, z } = relative_pos;
chunk.set(
sapling_x + { *x },
sapling_y + { *y },
sapling_z + { *z },
*block_id,
);
});
}
fn propose_tree_blocks(&self, rng: &mut Rng) -> Vec<(IVec3, BlockId)> {
let mut blocks = Vec::new();
let min_tree_stump_height = self.params.tree.min_stump_height;
let max_tree_stump_height = self.params.tree.max_stump_height;
let tree_stump_height = rng.u32(min_tree_stump_height..max_tree_stump_height) as i32;
let bush_radius: i32 =
rng.u32(self.params.tree.min_bush_radius..self.params.tree.max_bush_radius) as i32;
for dx in -bush_radius..bush_radius {
for dz in -bush_radius..bush_radius {
for dy in -bush_radius..bush_radius {
let distance_from_center = dx * dx + dy * dy + dz * dz;
if distance_from_center < bush_radius * bush_radius {
blocks.push((
IVec3 {
x: dx,
y: tree_stump_height + dy,
z: dz,
},
BlockId::OakLeaves,
));
}
}
}
}
for dy in 1..tree_stump_height {
blocks.push((IVec3 { x: 0, y: dy, z: 0 }, BlockId::OakLog));
}
blocks
}
fn decorate_block(&self, rng: &mut Rng, chunk: &mut Chunk, position: IVec3) {
let x = position.x as usize;
let y = position.y as usize;
let z = position.z as usize;
let block = chunk.get_unpadded(x, y, z);
if block == BlockId::Air {
if y > 0
&& Chunk::valid_unpadded(x, y - 1, z)
&& chunk.get_unpadded(x, y - 1, z) == BlockId::Grass
{
let random_number = rng.u32(0..=self.params.grass.frequency);
if random_number == 0 {
chunk.set_unpadded(x, y, z, BlockId::Tallgrass);
}
}
return;
}
let mut depth_below_nearest_air = 0;
let depth_check = 3;
for delta_height in 0..depth_check {
if !Chunk::valid_unpadded(x, y + delta_height, z) {
break;
}
let block = chunk.get_unpadded(x, y + delta_height, z);
if block == BlockId::Air {
break;
}
depth_below_nearest_air += 1;
}
let block = match depth_below_nearest_air {
0_i32..=1_i32 => BlockId::Grass,
2..3 => BlockId::Dirt,
_ => BlockId::Stone,
};
chunk.set_unpadded(x, y, z, block);
}
fn generate_block(&self, position: IVec3) -> BlockId {
if self.is_inside_cave(position.as_vec3()) {
return BlockId::Air;
}
if (position.y as f64) < self.determine_terrain_height(position.as_vec3()) {
return BlockId::Stone;
}
if self.determine_terrain_density(position.as_vec3()) > 0.0 {
return BlockId::Stone;
}
BlockId::Air
}
fn is_inside_cave(&self, position: Vec3) -> bool {
let density = self.sample_3d(position, &self.params.cave.noise);
let upper_bound = self.params.cave.base_value - self.params.cave.threshold;
let lower_bound = self.params.cave.base_value + self.params.cave.threshold;
lower_bound <= density && density >= upper_bound
}
fn determine_terrain_height(&self, position: Vec3) -> f64 {
let noise_value = self
.sample_2d(
Vec2 {
x: position.x,
y: position.z,
},
&self.params.height.noise,
)
.abs();
self.spline_lerp(noise_value)
}
fn determine_terrain_density(&self, position: Vec3) -> f64 {
let density = self.sample_3d(position, &self.params.density.noise);
let density_falloff = (position.y as f64 + self.params.density.height_offset)
* self.params.density.squash_factor;
density - density_falloff
}
pub fn normalized_spline_terrain_sample(&self, position: Vec2) -> f64 {
let noise_value = self.sample_2d(position, &self.params.height.noise);
let min_height = self.params.height.splines[0].y as f64;
let max_height = self.params.height.splines[self.params.height.splines.len() - 1].y as f64;
let splined_value = self.spline_lerp(noise_value);
(splined_value - min_height) / (max_height - min_height)
}
fn spline_lerp(&self, x: f64) -> f64 {
let x: f32 = x as f32;
assert!(self.params.height.splines.len() >= 2);
let min_x = self.params.height.splines[0].x;
let max_x = self.params.height.splines[self.params.height.splines.len() - 1].x;
assert!(min_x == -1.0);
assert!(max_x == 1.0);
for i in 0..self.params.height.splines.len() - 1 {
let current = self.params.height.splines[i];
let next = self.params.height.splines[i + 1];
if x >= current.x && x <= next.x {
return self.lerp(current, x, next);
}
}
panic!("Could not find matching spline points for x value {}", x);
}
fn lerp(&self, point0: Vec2, x: f32, point1: Vec2) -> f64 {
((point0.y * (point1.x - x) + point1.y * (x - point0.x)) / (point1.x - point0.x)) as f64
}
pub fn sample_2d(&self, position: Vec2, params: &NoiseFunctionParams) -> f64 {
let mut sample = 0.0;
let mut frequency = params.frequency;
let mut weight = 1.0;
let mut weight_sum = 0.0;
for _ in 0..params.octaves {
let new_sample = self.noise.get(position.as_dvec2() * frequency);
frequency *= params.lacuranity;
sample += new_sample * weight;
weight_sum += weight;
weight *= params.persistence;
}
sample / weight_sum
}
pub fn sample_3d(&self, position: Vec3, params: &NoiseFunctionParams) -> f64 {
let mut sample = 0.0;
let mut frequency = params.frequency;
let mut weight = 1.0;
let mut weight_sum = 0.0;
for _ in 0..params.octaves {
let new_sample = self.noise.get(position.as_dvec3() * frequency);
frequency *= params.lacuranity;
sample += new_sample * weight;
weight_sum += weight;
weight *= params.persistence;
}
sample / weight_sum
}
}
#[cfg(test)]
mod tests {
use super::*;
use terrain_resources::Generator;
#[test]
fn test_generate_chunk() {
let generator = Generator::default();
let mut chunk = Chunk::new(IVec3::ZERO);
generator.generate_chunk(&mut chunk);
assert_ne!(chunk.get(0, 0, 0), BlockId::Air);
}
}