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) {
64
64
// world otherwise. This is a placeholder:
65
65
// we don't have proper world generation yet.
66
66
67
- let seed = 42 ; // FIXME: load from the level file
67
+ let seed = worldgen :: seed_from_string ( & config . world . seed ) ;
68
68
69
69
let generator: Arc < dyn WorldGenerator > = match & config. world . generator [ ..] {
70
70
"flat" => Arc :: new ( SuperflatWorldGenerator :: new (
Original file line number Diff line number Diff line change @@ -364,6 +364,18 @@ impl BiomeGenerator for StaticBiomeGenerator {
364
364
}
365
365
}
366
366
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
+
367
379
#[ cfg( test) ]
368
380
mod tests {
369
381
use super :: * ;
@@ -468,4 +480,10 @@ mod tests {
468
480
assert_eq ! ( biomes. get_at_block( -1 , 0 , -1 ) , Biome :: Plains ) ;
469
481
assert_eq ! ( biomes. get_at_block( -1 , 0 , 0 ) , Biome :: BirchForest ) ;
470
482
}
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
+ }
471
489
}
You can’t perform that action at this time.
0 commit comments