Skip to content

Commit d9ce6c8

Browse files
committed
random seed
but deterministic
1 parent be5fe14 commit d9ce6c8

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

src/event_handler.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ impl<'a> crate::State<'a> {
120120
return true
121121
}
122122
},
123+
Key::KeyL => {
124+
if is_pressed {
125+
extra::add_full_chunk();
126+
return true
127+
}
128+
},
123129
Key::KeyI => {
124130
if is_pressed {
125131
let state = ptr::get_state();
@@ -166,12 +172,6 @@ impl<'a> crate::State<'a> {
166172
return true
167173
}
168174
},
169-
Key::KeyL => {
170-
if is_pressed {
171-
extra::add_full_chunk();
172-
return true
173-
}
174-
},
175175
_ => { },
176176
};
177177
}
@@ -296,8 +296,17 @@ impl<'a> crate::State<'a> {
296296
MouseScrollDelta::LineDelta(_, y) => y * -0.5,
297297
MouseScrollDelta::PixelDelta(pos) => pos.y as f32 * -0.01,
298298
}; // delta is reversed for some reason ... might need to look into it more (maybe it's different for platforms so yeah)
299-
ptr::get_gamestate().player_mut().inventory_mut().step_select_slot(delta);
300-
ptr::get_state().ui_manager.setup_ui();
299+
let state = ptr::get_state();
300+
match state.ui_manager.state.clone() {
301+
manager::UIState::Inventory(_) => {
302+
// item interaction i guess
303+
},
304+
manager::UIState::InGame => {
305+
ptr::get_gamestate().player_mut().inventory_mut().step_select_slot(delta);
306+
ptr::get_state().ui_manager.setup_ui();
307+
}
308+
_ => return false,
309+
}
301310
}
302311
true
303312
}

src/game/state.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
5455
impl 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

src/world/manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn save_world_data(path: &Path, data: &WorldData) -> Result<()> {
208208
Ok(())
209209
}
210210

211-
pub fn update_world_data(path: &PathBuf) -> Result<()> {
211+
pub fn update_world_data(path: &PathBuf) -> Result<time::Time> {
212212
let mut world_data = load_world_data(path)?;
213213
let current_version = std::env!("CARGO_PKG_VERSION");
214214

@@ -220,7 +220,7 @@ pub fn update_world_data(path: &PathBuf) -> Result<()> {
220220
save_world_data(path, &world_data)?;
221221
println!("World data updated");
222222

223-
Ok(())
223+
Ok(world_data.creation_date)
224224
}
225225

226226
//

0 commit comments

Comments
 (0)