Browse Source

Add rng seed

master
Garrit Franke 3 years ago
parent
commit
74ca838893
  1. 11
      src/main.rs
  2. 11
      src/map.rs

11
src/main.rs

@ -10,6 +10,7 @@ struct State {
view: Chunk,
loaded_chunks: Box<HashMap<(i32, i32), Chunk>>,
world_pos: (i32, i32),
seed: u64,
player_position: usize,
selected_object: Option<TileType>,
visible: Vec<bool>,
@ -17,6 +18,10 @@ struct State {
impl State {
pub fn new() -> State {
let seed = RandomNumberGenerator::new().rand::<u64>();
Self::seeded(seed)
}
pub fn seeded(seed: u64) -> State {
let mut chunks: HashMap<(i32, i32), Chunk> = HashMap::new();
// Coordinates of the current and surrounding chunks
@ -33,8 +38,7 @@ impl State {
]
.into();
for key in keys {
let seed = RandomNumberGenerator::new().rand::<u64>();
let chunk = map::new_chunk(seed);
let chunk = map::new_chunk(seed, Point::from(key));
chunks.insert(key, chunk);
}
@ -42,6 +46,7 @@ impl State {
world_pos: (0, 0),
view: chunks.get(&(0, 0)).unwrap().to_vec(),
loaded_chunks: Box::new(chunks),
seed,
player_position: (25 * 80) + 40, // Equivalent to point2d_to_index(40, 25) but we haven't initialized it yet
visible: vec![false; 80 * 50],
selected_object: None,
@ -58,7 +63,7 @@ impl State {
let chunk = match self.loaded_chunks.get(&self.world_pos) {
Some(chunk) => chunk,
None => {
let new_chunk = map::new_chunk(crypto::hash_to_number(&self.world_pos));
let new_chunk = map::new_chunk(self.seed, Point::from(self.world_pos));
self.loaded_chunks.insert(self.world_pos, new_chunk);
self.loaded_chunks
.get(&self.world_pos)

11
src/map.rs

@ -1,3 +1,5 @@
use crate::crypto;
use rltk::Point;
use rltk::RandomNumberGenerator;
use std::fmt::Display;
@ -18,12 +20,15 @@ impl Display for TileType {
}
}
pub fn new_chunk(seed: u64) -> Chunk {
pub fn new_chunk(seed: u64, pos: Point) -> Chunk {
let mut map = vec![TileType::Floor; 80 * 50];
let count = map.len();
// TODO: Lift seed to game state and pass to this method
let mut rng = RandomNumberGenerator::seeded(seed);
let pos_hash = crypto::hash_to_number(pos);
// Feed seed and world position into random generator.
// There might be a better way to do this, but I think this works for now.
let mut rng = RandomNumberGenerator::seeded(seed.max(pos_hash) - seed.min(pos_hash));
// Place obstacles
for _ in 0..20 {

Loading…
Cancel
Save