Compare commits

..

2 Commits

Author SHA1 Message Date
c1893aec74
Bsp: format 2022-01-29 22:38:38 +01:00
0511c00f09
Add base bsp 2022-01-29 22:38:26 +01:00
5 changed files with 239 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@ -24,7 +24,6 @@ use crate::particle_system::ParticleSpawnSystem;
use crate::state::{RunState, State};
use crate::trigger_system::TriggerSystem;
mod components;
mod damage_system;
mod gamelog;

View File

@ -4,8 +4,6 @@ use rltk::{Algorithm2D, BaseMap, FontCharType, Point, Rltk, RGB};
use serde::{Deserialize, Serialize};
use specs::prelude::*;
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum TileType {
Wall,

View File

@ -0,0 +1,230 @@
use rltk::{Point, RandomNumberGenerator};
use crate::map_builders::common::apply_room_to_map;
use crate::map_builders::MapBuilder;
use crate::rect::Rect;
use crate::{spawner, Map, Position, TileType, World, SHOW_MAPGEN_VISUALIZER};
pub struct BspDungeonBuilder {
map: Map,
starting_position: Position,
depth: i32,
rooms: Vec<Rect>,
history: Vec<Map>,
rects: Vec<Rect>,
}
impl BspDungeonBuilder {
pub fn new(new_depth: i32) -> Self {
Self {
starting_position: Position { x: 0, y: 0 },
depth: new_depth,
rects: Vec::new(),
map: Map::new(new_depth),
history: Vec::new(),
rooms: Vec::new(),
}
}
fn build(&mut self) {
let mut rng = RandomNumberGenerator::new();
self.rects.clear();
self.place_rooms(&mut rng);
self.place_corridors(&mut rng);
self.place_stairs();
self.place_start();
}
fn place_rooms(&mut self, rng: &mut RandomNumberGenerator) {
self.rects
.push(Rect::new(2, 2, self.map.width - 5, self.map.height - 5));
let first_room = self.rects[0];
self.add_subrects(first_room);
let mut n_rooms = 0;
while n_rooms < 240 {
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);
self.add_subrects(rect);
self.take_snapshot();
}
n_rooms += 1;
}
self.rooms.sort_by(|a, b| a.x1.cmp(&b.x1));
}
fn place_corridors(&mut self, rng: &mut RandomNumberGenerator) {
for i in 0..self.rooms.len() - 1 {
let room = self.rooms[i];
let next_room = self.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();
}
}
fn place_start(&mut self) {
let start = self.rooms[0].center();
self.starting_position = Position {
x: start.0,
y: start.1,
};
}
fn place_stairs(&mut self) {
let stairs = self.rooms[self.rooms.len() - 1].center();
let stairs_idx = self.map.xy_idx(stairs.0, stairs.1);
self.map.tiles[stairs_idx] = TileType::DownStairs;
}
fn add_subrects(&mut self, rect: Rect) {
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_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 {
if self.rects.len() == 1 {
return self.rects[0];
}
let idx = (rng.roll_dice(1, self.rects.len() as i32) - 1) as usize;
self.rects[idx]
}
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, 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;
result
}
fn is_possible(&self, rect: Rect) -> bool {
let mut expanded = rect;
expanded.x1 -= 2;
expanded.x2 += 2;
expanded.y1 -= 2;
expanded.y2 += 2;
let mut can_build = true;
for y in expanded.y1..=expanded.y2 {
for x in expanded.x1..=expanded.x2 {
if x > self.map.width - 2 {
can_build = false;
}
if y > self.map.height - 2 {
can_build = false;
}
if x < 1 {
can_build = false;
}
if y < 1 {
can_build = false;
}
if can_build {
let idx = self.map.xy_idx(x, y);
if self.map.tiles[idx] != TileType::Wall {
can_build = false;
}
}
}
}
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;
}
}
}
impl MapBuilder for BspDungeonBuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for room in self.rooms.iter().skip(1) {
spawner::spawn_room(ecs, room, self.depth);
}
}
fn get_map(&self) -> Map {
self.map.clone()
}
fn get_starting_position(&self) -> Position {
self.starting_position.clone()
}
fn get_snapshot_history(&self) -> Vec<Map> {
self.history.clone()
}
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);
}
}
}

View File

@ -1,8 +1,10 @@
use specs::World;
use crate::map_builders::bsp_dungeon::BspDungeonBuilder;
use crate::map_builders::simple_map::SimpleMapBuilder;
use crate::{Map, Position};
mod bsp_dungeon;
mod common;
mod simple_map;
@ -16,5 +18,10 @@ pub trait MapBuilder {
}
pub fn new_random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
Box::new(SimpleMapBuilder::new(new_depth))
let mut rng = rltk::RandomNumberGenerator::new();
let builder_choice = rng.roll_dice(1, 2);
match builder_choice {
1 => Box::new(SimpleMapBuilder::new(new_depth)),
_ => Box::new(BspDungeonBuilder::new(new_depth)),
}
}