refactors mapgen into chained builders

This commit is contained in:
Llywelwyn 2023-07-21 18:34:08 +01:00
parent 8a5600267c
commit dd367dc39b
22 changed files with 1381 additions and 1480 deletions

View file

@ -1,119 +1,74 @@
use super::{apply_room_to_map, spawner, Map, MapBuilder, Position, Rect, TileType, SHOW_MAPGEN};
use super::{apply_room_to_map, draw_corridor, BuilderMap, InitialMapBuilder, Map, Rect, TileType};
use rltk::RandomNumberGenerator;
pub struct BspDungeonBuilder {
map: Map,
starting_position: Position,
depth: i32,
rooms: Vec<Rect>,
history: Vec<Map>,
rects: Vec<Rect>,
spawn_list: Vec<(usize, String)>,
}
impl MapBuilder for BspDungeonBuilder {
fn build_map(&mut self, rng: &mut RandomNumberGenerator) {
return self.build(rng);
}
// Getters
fn get_map(&mut self) -> Map {
return self.map.clone();
}
fn get_starting_pos(&mut self) -> Position {
return self.starting_position.clone();
}
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
return &self.spawn_list;
}
// Mapgen visualisation stuff
fn get_snapshot_history(&self) -> Vec<Map> {
return self.history.clone();
}
fn take_snapshot(&mut self) {
if SHOW_MAPGEN {
let mut snapshot = self.map.clone();
for v in snapshot.revealed_tiles.iter_mut() {
*v = true;
}
self.history.push(snapshot);
}
impl InitialMapBuilder for BspDungeonBuilder {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut rltk::RandomNumberGenerator, build_data: &mut BuilderMap) {
self.build(rng, build_data);
}
}
impl BspDungeonBuilder {
#[allow(dead_code)]
pub fn new(new_depth: i32) -> BspDungeonBuilder {
BspDungeonBuilder {
map: Map::new(new_depth),
starting_position: Position { x: 0, y: 0 },
depth: new_depth,
rooms: Vec::new(),
history: Vec::new(),
rects: Vec::new(),
spawn_list: Vec::new(),
}
pub fn new() -> Box<BspDungeonBuilder> {
Box::new(BspDungeonBuilder { rects: Vec::new() })
}
fn build(&mut self, mut rng: &mut RandomNumberGenerator) {
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
let mut rooms: Vec<Rect> = Vec::new();
self.rects.clear();
self.rects.push(Rect::new(2, 2, self.map.width - 5, self.map.height - 5));
self.rects.push(Rect::new(2, 2, build_data.map.width - 5, build_data.map.height - 5)); // Start with a single map-sized rectangle
let first_room = self.rects[0];
self.add_subrects(first_room); // Divide first room
self.add_subrects(first_room); // Divide the first room
// Up to 240 times, get a random rect and divide it. If it's possible
// to place a room in there, place it and add it to the rooms list.
// Up to 240 times, we get a random rectangle and divide it. If its possible to squeeze a
// room in there, we place it and add it to the rooms list.
let mut n_rooms = 0;
while n_rooms < 240 {
let rect = self.get_random_rect(&mut rng);
let candidate = self.get_random_subrect(rect, &mut rng);
let rect = self.get_random_rect(rng);
let candidate = self.get_random_sub_rect(rect, rng);
if self.is_possible(candidate) {
apply_room_to_map(&mut self.map, &candidate);
self.rooms.push(candidate);
if self.is_possible(candidate, &build_data.map) {
apply_room_to_map(&mut build_data.map, &candidate);
rooms.push(candidate);
self.add_subrects(rect);
self.take_snapshot();
build_data.take_snapshot();
}
n_rooms += 1;
}
let start = self.rooms[0].centre();
self.starting_position = Position { x: start.0, y: start.1 };
// Sort rooms by left co-ordinate. Optional, but helps to make connected rooms line up.
self.rooms.sort_by(|a, b| a.x1.cmp(&b.x1));
// Now we sort the rooms
rooms.sort_by(|a, b| a.x1.cmp(&b.x1));
// Corridors
for i in 0..self.rooms.len() - 1 {
let room = self.rooms[i];
let next_room = self.rooms[i + 1];
// Now we want corridors
for i in 0..rooms.len() - 1 {
let room = rooms[i];
let next_room = rooms[i + 1];
let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2)) - 1);
let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2)) - 1);
let end_x = next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1);
let end_y = next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1);
self.draw_corridor(start_x, start_y, end_x, end_y);
self.take_snapshot();
}
// Stairs
let stairs = self.rooms[self.rooms.len() - 1].centre();
let stairs_idx = self.map.xy_idx(stairs.0, stairs.1);
self.map.tiles[stairs_idx] = TileType::DownStair;
// Spawn entities
for room in self.rooms.iter().skip(1) {
spawner::spawn_room(&self.map, rng, room, self.depth, &mut self.spawn_list);
draw_corridor(&mut build_data.map, start_x, start_y, end_x, end_y);
build_data.take_snapshot();
}
build_data.rooms = Some(rooms);
}
fn add_subrects(&mut self, rect: Rect) {
let w = i32::abs(rect.x1 - rect.x2);
let h = i32::abs(rect.y1 - rect.y2);
let half_w = i32::max(w / 2, 1);
let half_h = i32::max(h / 2, 1);
let width = i32::abs(rect.x1 - rect.x2);
let height = i32::abs(rect.y1 - rect.y2);
let half_width = i32::max(width / 2, 1);
let half_height = i32::max(height / 2, 1);
self.rects.push(Rect::new(rect.x1, rect.y1, half_w, half_h));
self.rects.push(Rect::new(rect.x1, rect.y1 + half_h, half_w, half_h));
self.rects.push(Rect::new(rect.x1 + half_w, rect.y1, half_w, half_h));
self.rects.push(Rect::new(rect.x1 + half_w, rect.y1 + half_h, half_w, half_h));
self.rects.push(Rect::new(rect.x1, rect.y1, half_width, half_height));
self.rects.push(Rect::new(rect.x1, rect.y1 + half_height, half_width, half_height));
self.rects.push(Rect::new(rect.x1 + half_width, rect.y1, half_width, half_height));
self.rects.push(Rect::new(rect.x1 + half_width, rect.y1 + half_height, half_width, half_height));
}
fn get_random_rect(&mut self, rng: &mut RandomNumberGenerator) -> Rect {
@ -121,26 +76,26 @@ impl BspDungeonBuilder {
return self.rects[0];
}
let idx = (rng.roll_dice(1, self.rects.len() as i32) - 1) as usize;
return self.rects[idx];
self.rects[idx]
}
fn get_random_subrect(&self, rect: Rect, rng: &mut RandomNumberGenerator) -> Rect {
fn get_random_sub_rect(&self, rect: Rect, rng: &mut RandomNumberGenerator) -> Rect {
let mut result = rect;
let rect_width = i32::abs(rect.x1 - rect.x2);
let rect_height = i32::abs(rect.y1 - rect.y2);
let w = i32::max(3, rng.roll_dice(1, i32::min(rect_width, 14)) - 1) + 1;
let h = i32::max(3, rng.roll_dice(1, i32::min(rect_height, 14)) - 1) + 1;
let w = i32::max(3, rng.roll_dice(1, i32::min(rect_width, 10)) - 1) + 1;
let h = i32::max(3, rng.roll_dice(1, i32::min(rect_height, 10)) - 1) + 1;
result.x1 += rng.roll_dice(1, 6) - 1;
result.y1 += rng.roll_dice(1, 6) - 1;
result.x2 = result.x1 + w;
result.y2 = result.y1 + h;
return result;
result
}
fn is_possible(&self, rect: Rect) -> bool {
fn is_possible(&self, rect: Rect, map: &Map) -> bool {
let mut expanded = rect;
expanded.x1 -= 2;
expanded.x2 += 2;
@ -151,10 +106,10 @@ impl BspDungeonBuilder {
for y in expanded.y1..=expanded.y2 {
for x in expanded.x1..=expanded.x2 {
if x > self.map.width - 2 {
if x > map.width - 2 {
can_build = false;
}
if y > self.map.height - 2 {
if y > map.height - 2 {
can_build = false;
}
if x < 1 {
@ -164,34 +119,14 @@ impl BspDungeonBuilder {
can_build = false;
}
if can_build {
let idx = self.map.xy_idx(x, y);
if self.map.tiles[idx] != TileType::Wall {
let idx = map.xy_idx(x, y);
if map.tiles[idx] != TileType::Wall {
can_build = false;
}
}
}
}
return can_build;
}
fn draw_corridor(&mut self, x1: i32, y1: i32, x2: i32, y2: i32) {
let mut x = x1;
let mut y = y1;
while x != x2 || y != y2 {
if x < x2 {
x += 1;
} else if x > x2 {
x -= 1;
} else if y < y2 {
y += 1;
} else if y > y2 {
y -= 1;
}
let idx = self.map.xy_idx(x, y);
self.map.tiles[idx] = TileType::Floor;
}
can_build
}
}