correct room positions. no coridors yet.

This commit is contained in:
klangner 2020-09-14 20:36:07 +02:00
parent 0e40877eab
commit eef0fc6c0b
2 changed files with 5 additions and 5 deletions

View File

@ -3,9 +3,9 @@
use rand::prelude::*; 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 { 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 ((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 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 mut rng = StdRng::seed_from_u64(system_time.as_millis() as u64);
let x = random_range(&mut rng, 5, 8); let x = random_range(&mut rng, 5, 8);
assert!(x >= 5 && x <= 8); assert!(x >= 5 && x < 8);
} }
} }

View File

@ -56,8 +56,8 @@ impl RandomRoomsGen {
for _ in 0..self.max_rooms { for _ in 0..self.max_rooms {
let w = random::random_range(rng, self.min_room_size, self.max_room_size); 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 h = random::random_range(rng, self.min_room_size, self.max_room_size);
let x = random::random_range(rng, 0, width - w); let x = random::random_range(rng, 1, width - w);
let y = random::random_range(rng, 0, height - h); 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 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)); let intersects = rooms.iter().any(|r| new_room.intersect(r));
if !intersects { if !intersects {