2021-12-01 10:47:41 -05:00
|
|
|
use rltk::RandomNumberGenerator;
|
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
use crate::map_builders::common::{
|
|
|
|
apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel,
|
|
|
|
};
|
|
|
|
use crate::map_builders::{BuilderMap, InitialMapBuilder};
|
2021-12-15 12:08:23 -05:00
|
|
|
use crate::Rect;
|
2021-12-10 20:16:48 -05:00
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
pub struct SimpleMapBuilder {}
|
2021-12-10 11:30:58 -05:00
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
impl InitialMapBuilder for SimpleMapBuilder {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
|
|
|
self.rooms_and_corridors(rng, build_data);
|
2021-12-10 16:34:11 -05:00
|
|
|
}
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SimpleMapBuilder {
|
2021-12-14 16:29:36 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn new() -> Box<SimpleMapBuilder> {
|
|
|
|
Box::new(SimpleMapBuilder {})
|
2021-12-01 12:03:49 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
fn rooms_and_corridors(
|
|
|
|
&mut self,
|
|
|
|
rng: &mut RandomNumberGenerator,
|
|
|
|
build_data: &mut BuilderMap,
|
|
|
|
) {
|
2021-12-01 10:47:41 -05:00
|
|
|
const MAX_ROOMS: i32 = 30;
|
|
|
|
const MIN_SIZE: i32 = 6;
|
|
|
|
const MAX_SIZE: i32 = 10;
|
2021-12-14 16:29:36 -05:00
|
|
|
let mut rooms: Vec<Rect> = Vec::new();
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
for _ in 0..MAX_ROOMS {
|
|
|
|
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
|
|
|
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
2021-12-14 16:29:36 -05:00
|
|
|
let x = rng.roll_dice(1, build_data.map.width - w - 1) - 1;
|
|
|
|
let y = rng.roll_dice(1, build_data.map.height - h - 1) - 1;
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
let new_room = Rect::new(x, y, w, h);
|
|
|
|
let mut ok = true;
|
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
for other_room in rooms.iter() {
|
2021-12-01 10:47:41 -05:00
|
|
|
if new_room.intersect(other_room) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2021-12-14 16:29:36 -05:00
|
|
|
apply_room_to_map(&mut build_data.map, &new_room);
|
|
|
|
build_data.take_snapshot();
|
2021-12-01 10:47:41 -05:00
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
if !rooms.is_empty() {
|
2021-12-01 10:47:41 -05:00
|
|
|
let (new_x, new_y) = new_room.center();
|
2021-12-14 16:29:36 -05:00
|
|
|
let (prev_x, prev_y) = rooms[rooms.len() - 1].center();
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
if rng.range(0, 2) == 1 {
|
2021-12-14 16:29:36 -05:00
|
|
|
apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, prev_y);
|
|
|
|
apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, new_x);
|
2021-12-01 10:47:41 -05:00
|
|
|
} else {
|
2021-12-14 16:29:36 -05:00
|
|
|
apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, prev_x);
|
|
|
|
apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, new_y);
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
rooms.push(new_room);
|
|
|
|
build_data.take_snapshot();
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:29:36 -05:00
|
|
|
build_data.rooms = Some(rooms);
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|