From eef0fc6c0bb259113a3f2ad9f6e1f666afbedd6e Mon Sep 17 00:00:00 2001 From: klangner Date: Mon, 14 Sep 2020 20:36:07 +0200 Subject: [PATCH] correct room positions. no coridors yet. --- src/common/random.rs | 6 +++--- src/dungeon/random_rooms.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/common/random.rs b/src/common/random.rs index dd3c2fe..c77c70c 100644 --- a/src/common/random.rs +++ b/src/common/random.rs @@ -3,9 +3,9 @@ use rand::prelude::*; -/// Generate random number between start and end inclusive on both ends +/// Generate random number between start (inclusive) and end (exclusive). pub fn random_range(rng: &mut StdRng, start: usize, end: usize) -> usize { - let max = (end - start + 1) as u32; + let max = (end - start) as u32; ((rng.next_u32() % max) + start as u32) as usize } @@ -23,7 +23,7 @@ mod tests { let system_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Can't access system time"); let mut rng = StdRng::seed_from_u64(system_time.as_millis() as u64); let x = random_range(&mut rng, 5, 8); - assert!(x >= 5 && x <= 8); + assert!(x >= 5 && x < 8); } } \ No newline at end of file diff --git a/src/dungeon/random_rooms.rs b/src/dungeon/random_rooms.rs index e60118a..65dc527 100644 --- a/src/dungeon/random_rooms.rs +++ b/src/dungeon/random_rooms.rs @@ -56,8 +56,8 @@ impl RandomRoomsGen { for _ in 0..self.max_rooms { let w = random::random_range(rng, self.min_room_size, self.max_room_size); let h = random::random_range(rng, self.min_room_size, self.max_room_size); - let x = random::random_range(rng, 0, width - w); - let y = random::random_range(rng, 0, height - h); + let x = random::random_range(rng, 1, width - w); + let y = random::random_range(rng, 1, height - h); let new_room = Rect::new(x as i32, y as i32, w as i32, h as i32); let intersects = rooms.iter().any(|r| new_room.intersect(r)); if !intersects {