@@ -16,6 +16,7 @@ pub struct GameState {
1616 player : player:: Player ,
1717 world : world:: main:: World , // lol main data storage :)
1818 save_path : std:: path:: PathBuf ,
19+ world_seed : u32 ,
1920 is_running : bool ,
2021}
2122
@@ -52,8 +53,7 @@ pub fn make_world(save_path: PathBuf) {
5253
5354#[ allow( dead_code) ]
5455impl GameState {
55- #[ inline]
56- pub fn new ( worldname : & str ) -> Self {
56+ #[ inline] pub fn new ( worldname : & str ) -> Self {
5757 let state = ptr:: get_state ( ) ;
5858 let offset = Vec3 :: new ( 0. , 1.7 , 0. ) ; let pos = Vec3 :: new ( 0.5 , 0.5 , 0.5 ) ;
5959 let mut player = player:: Player :: new ( CameraConfig :: new ( offset) , pos, state. device ( ) , * state. size ( ) , & state. render_context . layouts [ 1 ] ) ;
@@ -66,16 +66,31 @@ impl GameState {
6666
6767 make_world ( save_path. clone ( ) ) ;
6868
69- match world:: manager:: update_world_data ( & save_path) {
70- Ok ( _) => ( ) , // Everything is fine, do nothing
71- Err ( e) => println ! ( "Error updating world data: {}" , e) ,
72- }
73-
69+ let creation_date: u64 = match world:: manager:: update_world_data ( & save_path) {
70+ Ok ( time) => time. to_unix_timestamp ( ) , // Everything is fine, do nothing
71+ Err ( e) => {
72+ println ! ( "Error updating world data: {}" , e) ;
73+ 0
74+ } ,
75+ } ;
76+ // Combine worldname and creation_date into a seed
77+ let world_seed = {
78+ // Simple but effective hash function
79+ let mut hash: u32 = 0 ;
80+ for ( i, c) in worldname. chars ( ) . enumerate ( ) {
81+ hash = hash. wrapping_add ( c as u32 )
82+ . wrapping_mul ( i as u32 + 1 )
83+ . wrapping_add ( creation_date as u32 )
84+ . rotate_left ( 3 ) ;
85+ }
86+ hash. wrapping_add ( creation_date as u32 )
87+ } ;
7488 Self {
7589 worldname : worldname. to_string ( ) ,
7690 player,
7791 world : world:: main:: World :: empty ( ) ,
7892 save_path,
93+ world_seed,
7994 is_running : false ,
8095 }
8196 }
@@ -103,10 +118,8 @@ impl GameState {
103118 #[ inline] pub const fn running ( & mut self ) -> & mut bool {
104119 & mut self . is_running
105120 }
106-
107-
108121 #[ inline] pub const fn seed ( & self ) -> & u32 {
109- & 0u32
122+ & self . world_seed
110123 }
111124}
112125
0 commit comments