diff --git a/src/level_generator.rs b/src/level_generator.rs index 46959c7..3e83bfa 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -310,11 +310,11 @@ impl LevelGenerator { for row in 0..ROOMS_VERTICAL { if let Some(connection) = self.rooms[col][row].connection_down { // println!("down"); - connection.render(&mut structure); + connection.render(&mut self.rng, &mut structure); } if let Some(connection) = self.rooms[col][row].connection_right { // println!("right"); - connection.render(&mut structure); + connection.render(&mut self.rng,&mut structure); } } } @@ -331,20 +331,22 @@ impl LevelGenerator { } } -// #[test] -// fn test_level_gen() { -// for _ in 0..1000 { -// LevelGenerator::generate(0, true, false).render(); -// } -// } +#[test] +fn test_level_gen() { + let mut rng = rand::thread_rng(); + for _ in 0..1000 { + LevelGenerator::generate(0, true, false).render(); + } +} -// #[test] -// fn test_level_gen_respects_level() { -// let level = LevelGenerator::generate(0, true, false).render(); -// assert_eq!(0, level.level); -// let level = LevelGenerator::generate(1, true, false).render(); -// assert_eq!(1, level.level); -// } +#[test] +fn test_level_gen_respects_level() { + let mut rng = rand::thread_rng(); + let level = LevelGenerator::generate(0, true, false).render(); + assert_eq!(0, level.level); + let level = LevelGenerator::generate(1, true, false).render(); + assert_eq!(1, level.level); +} #[cfg(test)] fn find_room_types(rooms: &Vec) -> (bool, bool, bool, bool) { diff --git a/src/room.rs b/src/room.rs index 03901a1..a332c6d 100644 --- a/src/room.rs +++ b/src/room.rs @@ -1,4 +1,4 @@ -use std::cmp::{max, min}; +use std::{cmp::{max, min}, ops::RangeInclusive}; use crate::{ constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH}, @@ -26,7 +26,14 @@ pub struct Connection { } impl Connection { - pub fn render(&self, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) { + pub fn render(&self, rng: &mut ThreadRng, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) { + fn calc_range(a: usize, b:usize) -> RangeInclusive{ + if a > b { + b..=a + } else { + a..=b + } + } // the tuples are (col, row) let abs_d_col = max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0); @@ -35,22 +42,11 @@ impl Connection { max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1); if abs_d_col == 0 { - let range = if self.end_pos.1 > self.start_pos.1 { - self.start_pos.1..=self.end_pos.1 - } else { - self.end_pos.1..=self.start_pos.1 - }; - - for row in range { + for row in calc_range(self.end_pos.1, self.start_pos.1 ) { tgt[self.end_pos.0][row] = StructureElement::Floor; } } else if abs_d_row == 0 { - let range = if self.end_pos.0 > self.start_pos.0 { - self.start_pos.0..=self.end_pos.0 - } else { - self.end_pos.0..=self.start_pos.0 - }; - for col in range { + for col in calc_range(self.end_pos.0, self.start_pos.0) { tgt[col][self.end_pos.1] = StructureElement::Floor; } } else { @@ -58,18 +54,16 @@ impl Connection { if d_row > 0 { // more up/down let d_row = self.end_pos.1 - self.start_pos.1; - for r in self.start_pos.1..=(self.start_pos.1 + d_row / 2) { + let vert_offset = if d_row > 3 { + rng.gen_range(1..=d_row-1) + } else {d_row/2}; + for r in self.start_pos.1..=(self.start_pos.1 + vert_offset) { tgt[self.start_pos.0][r] = StructureElement::Floor; } - let range = if self.end_pos.0 > self.start_pos.0 { - self.start_pos.0..=self.end_pos.0 - } else { - self.end_pos.0..=self.start_pos.0 - }; - for c in range { - tgt[c][self.start_pos.1 + d_row / 2] = StructureElement::Floor; + for c in calc_range(self.end_pos.0, self.start_pos.0) { + tgt[c][self.start_pos.1 + vert_offset] = StructureElement::Floor; } - for r in (self.start_pos.1 + d_row / 2)..=self.end_pos.1 { + for r in (self.start_pos.1 + vert_offset)..=self.end_pos.1 { tgt[self.end_pos.0][r] = StructureElement::Floor; } } else { @@ -78,12 +72,7 @@ impl Connection { for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) { tgt_col[self.start_pos.1] = StructureElement::Floor; } - let range = if self.end_pos.1 > self.start_pos.1 { - self.start_pos.1..=self.end_pos.1 - } else { - self.end_pos.1..=self.start_pos.1 - }; - for r in range { + for r in calc_range(self.end_pos.1, self.start_pos.1) { tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor; } for tgt_col in tgt