Skip to content

Commit 4e0e7a4

Browse files
committed
Fixed config seed
1 parent c47c564 commit 4e0e7a4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

feather/server/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn init_world_source(game: &mut Game, config: &Config) {
6464
// world otherwise. This is a placeholder:
6565
// we don't have proper world generation yet.
6666

67-
let seed = 42; // FIXME: load from the level file
67+
let seed = worldgen::seed_from_string(&config.world.seed);
6868

6969
let generator: Arc<dyn WorldGenerator> = match &config.world.generator[..] {
7070
"flat" => Arc::new(SuperflatWorldGenerator::new(

feather/worldgen/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ impl BiomeGenerator for StaticBiomeGenerator {
364364
}
365365
}
366366

367+
pub fn seed_from_string(s: &str) -> u64 {
368+
s.parse().unwrap_or_else(|_| {
369+
if s.is_empty() {
370+
rand::random()
371+
} else {
372+
s.chars()
373+
.fold(0i32, |val, ch| val.wrapping_mul(31).wrapping_add(ch as i32))
374+
as i64
375+
}
376+
}) as u64
377+
}
378+
367379
#[cfg(test)]
368380
mod tests {
369381
use super::*;
@@ -468,4 +480,10 @@ mod tests {
468480
assert_eq!(biomes.get_at_block(-1, 0, -1), Biome::Plains);
469481
assert_eq!(biomes.get_at_block(-1, 0, 0), Biome::BirchForest);
470482
}
483+
484+
#[test]
485+
fn test_seed_from_config() {
486+
assert_eq!(seed_from_string("42"), 42);
487+
assert_eq!(seed_from_string("Hello"), 69609650);
488+
}
471489
}

0 commit comments

Comments
 (0)