Added examples and tests

This commit is contained in:
klangner 2020-10-17 17:31:32 +02:00
parent 3e40de60a5
commit 4434a35022
4 changed files with 73 additions and 0 deletions

10
examples/bsp_interior.rs Normal file
View File

@ -0,0 +1,10 @@
use rand::prelude::*;
use mapgen::*;
fn main() {
let mut rng = StdRng::seed_from_u64(907647352);
let gen = BspInterior::new();
let map = gen.modify_map(&mut rng, &Map::new(80, 50));
println!("{:}", &map);
}

12
examples/bsp_rooms.rs Normal file
View File

@ -0,0 +1,12 @@
use std::time::{SystemTime, UNIX_EPOCH};
use rand::prelude::*;
use mapgen::*;
fn main() {
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 gen = BspRooms::new();
let map = gen.modify_map(&mut rng, &Map::new(80, 50));
println!("{:}", &map);
}

View File

@ -105,4 +105,30 @@ impl BspInterior {
if half_height > self.min_room_size { self.add_subrects(v2, rng, rects); } if half_height > self.min_room_size { self.add_subrects(v2, rng, rects); }
} }
} }
}
/// ------------------------------------------------------------------------------------------------
/// Module unit tests
/// ------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use crate::{TileType, Map};
#[test]
fn no_corridors_on_borders() {
let mut rng = StdRng::seed_from_u64(907647352);
let gen = BspInterior::new();
let map = gen.modify_map(&mut rng, &Map::new(80, 50));
for i in 0..80 {
assert_eq!(map.at(i, 0), TileType::Wall);
assert_eq!(map.at(i, 49), TileType::Wall);
}
for j in 0..50 {
assert_eq!(map.at(0, j), TileType::Wall);
assert_eq!(map.at(79, j), TileType::Wall);
}
}
} }

View File

@ -131,4 +131,29 @@ impl BspRooms {
can_build can_build
} }
}
/// ------------------------------------------------------------------------------------------------
/// Module unit tests
/// ------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use crate::map::Map;
#[test]
fn no_corridors_on_borders() {
let mut rng = StdRng::seed_from_u64(907647352);
let gen = BspRooms::new();
let map = gen.modify_map(&mut rng, &Map::new(80, 50));
for i in 0..80 {
assert_eq!(map.at(i, 0), TileType::Wall);
assert_eq!(map.at(i, 49), TileType::Wall);
}
for j in 0..50 {
assert_eq!(map.at(0, j), TileType::Wall);
assert_eq!(map.at(79, j), TileType::Wall);
}
}
} }