File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed
Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff 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 ( 0u32 , |val, ch| val. wrapping_mul ( 31 ) . wrapping_add ( ch as u32 ) )
374+ as u64
375+ }
376+ } )
377+ }
378+
367379#[ cfg( test) ]
368380mod 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}
You can’t perform that action at this time.
0 commit comments