2021-12-01 10:47:41 -05:00
|
|
|
use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder};
|
2021-12-01 14:45:27 -05:00
|
|
|
use crate::{spawner, SHOW_MAPGEN_VISUALIZER};
|
2021-12-01 11:02:39 -05:00
|
|
|
use crate::{Map, Position, Rect, TileType};
|
2021-12-01 10:47:41 -05:00
|
|
|
use rltk::RandomNumberGenerator;
|
2021-12-01 11:41:29 -05:00
|
|
|
use specs::prelude::*;
|
2021-12-01 10:47:41 -05:00
|
|
|
|
2021-12-01 12:03:49 -05:00
|
|
|
pub struct SimpleMapBuilder {
|
|
|
|
map: Map,
|
|
|
|
starting_position: Position,
|
2021-12-01 12:08:14 -05:00
|
|
|
depth: i32,
|
2021-12-01 14:45:27 -05:00
|
|
|
rooms: Vec<Rect>,
|
|
|
|
history: Vec<Map>,
|
2021-12-10 16:34:11 -05:00
|
|
|
spawn_list: Vec<(usize, String)>,
|
2021-12-01 12:03:49 -05:00
|
|
|
}
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
impl MapBuilder for SimpleMapBuilder {
|
2021-12-01 12:03:49 -05:00
|
|
|
fn get_map(&self) -> Map {
|
|
|
|
self.map.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_starting_position(&self) -> Position {
|
2021-12-02 15:03:55 -05:00
|
|
|
self.starting_position
|
2021-12-01 12:03:49 -05:00
|
|
|
}
|
2021-12-01 14:45:27 -05:00
|
|
|
|
|
|
|
fn get_snapshot_history(&self) -> Vec<Map> {
|
|
|
|
self.history.clone()
|
|
|
|
}
|
|
|
|
|
2021-12-10 11:30:58 -05:00
|
|
|
fn build_map(&mut self) {
|
|
|
|
self.rooms_and_corridors();
|
|
|
|
}
|
|
|
|
|
2021-12-01 14:45:27 -05:00
|
|
|
fn take_snapshot(&mut self) {
|
|
|
|
if SHOW_MAPGEN_VISUALIZER {
|
|
|
|
let mut snapshot = self.map.clone();
|
|
|
|
for v in snapshot.revealed_tiles.iter_mut() {
|
|
|
|
*v = true;
|
|
|
|
}
|
|
|
|
self.history.push(snapshot);
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 16:34:11 -05:00
|
|
|
|
|
|
|
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
|
|
|
|
&self.spawn_list
|
|
|
|
}
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SimpleMapBuilder {
|
2021-12-01 12:03:49 -05:00
|
|
|
pub fn new(new_depth: i32) -> SimpleMapBuilder {
|
2021-12-01 14:45:27 -05:00
|
|
|
SimpleMapBuilder {
|
2021-12-01 12:03:49 -05:00
|
|
|
map: Map::new(new_depth),
|
|
|
|
starting_position: Position { x: 0, y: 0 },
|
2021-12-01 12:08:14 -05:00
|
|
|
depth: new_depth,
|
|
|
|
rooms: Vec::new(),
|
2021-12-01 14:45:27 -05:00
|
|
|
history: Vec::new(),
|
2021-12-10 16:34:11 -05:00
|
|
|
spawn_list: Vec::new(),
|
2021-12-01 12:03:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rooms_and_corridors(&mut self) {
|
2021-12-01 10:47:41 -05:00
|
|
|
const MAX_ROOMS: i32 = 30;
|
|
|
|
const MIN_SIZE: i32 = 6;
|
|
|
|
const MAX_SIZE: i32 = 10;
|
|
|
|
|
|
|
|
let mut rng = RandomNumberGenerator::new();
|
|
|
|
|
|
|
|
for _ in 0..MAX_ROOMS {
|
|
|
|
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
|
|
|
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
2021-12-01 12:03:49 -05:00
|
|
|
let x = rng.roll_dice(1, self.map.width - w - 1) - 1;
|
|
|
|
let y = rng.roll_dice(1, self.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-01 12:08:14 -05:00
|
|
|
for other_room in self.rooms.iter() {
|
2021-12-01 10:47:41 -05:00
|
|
|
if new_room.intersect(other_room) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2021-12-01 12:03:49 -05:00
|
|
|
apply_room_to_map(&mut self.map, &new_room);
|
2021-12-01 10:47:41 -05:00
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
if !self.rooms.is_empty() {
|
2021-12-01 10:47:41 -05:00
|
|
|
let (new_x, new_y) = new_room.center();
|
2021-12-01 12:08:14 -05:00
|
|
|
let (prev_x, prev_y) = self.rooms[self.rooms.len() - 1].center();
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
if rng.range(0, 2) == 1 {
|
2021-12-01 12:03:49 -05:00
|
|
|
apply_horizontal_tunnel(&mut self.map, prev_x, new_x, prev_y);
|
|
|
|
apply_vertical_tunnel(&mut self.map, prev_y, new_y, new_x);
|
2021-12-01 10:47:41 -05:00
|
|
|
} else {
|
2021-12-01 12:03:49 -05:00
|
|
|
apply_vertical_tunnel(&mut self.map, prev_y, new_y, prev_x);
|
|
|
|
apply_horizontal_tunnel(&mut self.map, prev_x, new_x, new_y);
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
self.rooms.push(new_room);
|
2021-12-01 14:45:27 -05:00
|
|
|
self.take_snapshot();
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
let stairs_position = self.rooms[self.rooms.len() - 1].center();
|
2021-12-01 12:03:49 -05:00
|
|
|
let stairs_idx = self.map.xy_idx(stairs_position.0, stairs_position.1);
|
|
|
|
self.map.tiles[stairs_idx] = TileType::DownStairs;
|
2021-12-01 11:02:39 -05:00
|
|
|
|
2021-12-10 16:34:11 -05:00
|
|
|
self.starting_position = Position::from(self.rooms[0].center());
|
|
|
|
|
|
|
|
// Spawn some entities
|
|
|
|
for room in self.rooms.iter().skip(1) {
|
|
|
|
spawner::spawn_room(&self.map, &mut rng, room, self.depth, &mut self.spawn_list);
|
|
|
|
}
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|