Skip to content

Commit

Permalink
Fixed config seed
Browse files Browse the repository at this point in the history
  • Loading branch information
ELginas committed Jan 16, 2022
1 parent c47c564 commit 5acbc4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion feather/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn init_world_source(game: &mut Game, config: &Config) {
// world otherwise. This is a placeholder:
// we don't have proper world generation yet.

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

let generator: Arc<dyn WorldGenerator> = match &config.world.generator[..] {
"flat" => Arc::new(SuperflatWorldGenerator::new(
Expand Down
18 changes: 18 additions & 0 deletions feather/worldgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,18 @@ impl BiomeGenerator for StaticBiomeGenerator {
}
}

pub fn seed_from_string(s: &str) -> u64 {
s.parse().unwrap_or_else(|_| {
if s.is_empty() {
rand::random()
} else {
s.chars()
.fold(0u32, |val, ch| val.wrapping_mul(31).wrapping_add(ch as u32))
as u64
}
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -468,4 +480,10 @@ mod tests {
assert_eq!(biomes.get_at_block(-1, 0, -1), Biome::Plains);
assert_eq!(biomes.get_at_block(-1, 0, 0), Biome::BirchForest);
}

#[test]
fn test_seed_from_config() {
assert_eq!(seed_from_string("42"), 42);
assert_eq!(seed_from_string("Hello"), 69609650);
}
}

0 comments on commit 5acbc4d

Please sign in to comment.