2021-12-14 11:19:03 -05:00
|
|
|
pub mod prefab_levels;
|
|
|
|
pub mod prefab_rooms;
|
|
|
|
pub mod prefab_sections;
|
|
|
|
|
|
|
|
use std::collections::HashSet;
|
2021-12-10 14:29:03 -05:00
|
|
|
|
|
|
|
use rltk::RandomNumberGenerator;
|
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
use crate::map_builders::{BuilderMap, InitialMapBuilder, MetaMapBuilder};
|
2021-12-15 12:08:23 -05:00
|
|
|
use crate::{Position, TileType};
|
2021-12-10 20:16:48 -05:00
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
2021-12-10 14:29:03 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum PrefabMode {
|
2021-12-10 16:35:09 -05:00
|
|
|
RexLevel {
|
|
|
|
template: &'static str,
|
|
|
|
},
|
|
|
|
Constant {
|
|
|
|
level: prefab_levels::PrefabLevel,
|
|
|
|
},
|
|
|
|
Sectional {
|
|
|
|
section: prefab_sections::PrefabSection,
|
|
|
|
},
|
2021-12-13 15:22:59 -05:00
|
|
|
RoomVaults,
|
2021-12-10 14:29:03 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
#[allow(dead_code)]
|
2021-12-10 14:29:03 -05:00
|
|
|
pub struct PrefabBuilder {
|
|
|
|
mode: PrefabMode,
|
|
|
|
}
|
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
impl MetaMapBuilder for PrefabBuilder {
|
|
|
|
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
|
|
|
self.build(rng, build_data);
|
2021-12-10 14:29:03 -05:00
|
|
|
}
|
2021-12-15 11:36:17 -05:00
|
|
|
}
|
2021-12-10 16:35:09 -05:00
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
impl InitialMapBuilder for PrefabBuilder {
|
|
|
|
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
|
|
|
self.build(rng, build_data);
|
2021-12-10 16:35:09 -05:00
|
|
|
}
|
2021-12-10 14:29:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PrefabBuilder {
|
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
pub fn new() -> Box<PrefabBuilder> {
|
|
|
|
Box::new(PrefabBuilder {
|
2021-12-13 15:22:59 -05:00
|
|
|
mode: PrefabMode::RoomVaults,
|
2021-12-15 11:36:17 -05:00
|
|
|
})
|
2021-12-10 14:29:03 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
pub fn rex_level(template: &'static str) -> Box<PrefabBuilder> {
|
|
|
|
Box::new(PrefabBuilder {
|
2021-12-14 11:19:03 -05:00
|
|
|
mode: PrefabMode::RexLevel { template },
|
2021-12-15 11:36:17 -05:00
|
|
|
})
|
2021-12-14 11:19:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
pub fn constant(level: prefab_levels::PrefabLevel) -> Box<PrefabBuilder> {
|
|
|
|
Box::new(PrefabBuilder {
|
2021-12-14 11:19:03 -05:00
|
|
|
mode: PrefabMode::Constant { level },
|
2021-12-15 11:36:17 -05:00
|
|
|
})
|
2021-12-14 11:19:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
pub fn sectional(section: prefab_sections::PrefabSection) -> Box<PrefabBuilder> {
|
|
|
|
Box::new(PrefabBuilder {
|
2021-12-14 11:19:03 -05:00
|
|
|
mode: PrefabMode::Sectional { section },
|
2021-12-15 11:36:17 -05:00
|
|
|
})
|
2021-12-14 11:19:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
pub fn vaults() -> Box<PrefabBuilder> {
|
|
|
|
Box::new(PrefabBuilder {
|
2021-12-14 11:19:03 -05:00
|
|
|
mode: PrefabMode::RoomVaults,
|
2021-12-15 11:36:17 -05:00
|
|
|
})
|
2021-12-14 11:19:03 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
2021-12-10 14:29:03 -05:00
|
|
|
match self.mode {
|
2021-12-15 11:36:17 -05:00
|
|
|
PrefabMode::RexLevel { template } => self.load_rex_map(template, build_data),
|
|
|
|
PrefabMode::Constant { level } => self.load_ascii_map(&level, build_data),
|
|
|
|
PrefabMode::Sectional { section } => self.apply_sectional(§ion, rng, build_data),
|
|
|
|
PrefabMode::RoomVaults => self.apply_room_vaults(rng, build_data),
|
2021-12-10 14:29:03 -05:00
|
|
|
}
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-10 14:29:03 -05:00
|
|
|
}
|
2021-12-10 15:17:51 -05:00
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
fn char_to_map(&mut self, ch: char, idx: usize, build_data: &mut BuilderMap) {
|
2021-12-10 15:17:51 -05:00
|
|
|
match ch {
|
2021-12-15 11:36:17 -05:00
|
|
|
' ' => build_data.map.tiles[idx] = TileType::Floor,
|
|
|
|
'#' => build_data.map.tiles[idx] = TileType::Wall,
|
2021-12-10 15:17:51 -05:00
|
|
|
'@' => {
|
2021-12-15 11:36:17 -05:00
|
|
|
let x = idx as i32 % build_data.map.width;
|
|
|
|
let y = idx as i32 / build_data.map.width;
|
|
|
|
build_data.map.tiles[idx] = TileType::Floor;
|
|
|
|
build_data.starting_position = Some(Position {
|
2021-12-10 15:17:51 -05:00
|
|
|
x: x as i32,
|
|
|
|
y: y as i32,
|
2021-12-15 11:36:17 -05:00
|
|
|
});
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
2021-12-15 11:36:17 -05:00
|
|
|
'>' => build_data.map.tiles[idx] = TileType::DownStairs,
|
2021-12-10 15:17:51 -05:00
|
|
|
'g' => {
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.map.tiles[idx] = TileType::Floor;
|
|
|
|
build_data.spawn_list.push((idx, "Goblin".to_string()));
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
|
|
|
'o' => {
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.map.tiles[idx] = TileType::Floor;
|
|
|
|
build_data.spawn_list.push((idx, "Orc".to_string()));
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
|
|
|
'^' => {
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.map.tiles[idx] = TileType::Floor;
|
|
|
|
build_data.spawn_list.push((idx, "Bear Trap".to_string()));
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
|
|
|
'%' => {
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.map.tiles[idx] = TileType::Floor;
|
|
|
|
build_data.spawn_list.push((idx, "Rations".to_string()));
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
|
|
|
'!' => {
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.map.tiles[idx] = TileType::Floor;
|
|
|
|
build_data
|
|
|
|
.spawn_list
|
|
|
|
.push((idx, "Health Potion".to_string()));
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
rltk::console::log(format!("Unknown glyph loading map: {}", (ch as u8) as char));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
fn load_rex_map(&mut self, path: &str, build_data: &mut BuilderMap) {
|
2021-12-10 15:17:51 -05:00
|
|
|
let xp_file = rltk::rex::XpFile::from_resource(path).unwrap();
|
|
|
|
|
|
|
|
for layer in &xp_file.layers {
|
|
|
|
for y in 0..layer.height {
|
|
|
|
for x in 0..layer.width {
|
|
|
|
let cell = layer.get(x, y).unwrap();
|
2021-12-15 11:36:17 -05:00
|
|
|
if x < build_data.map.width as usize && y < build_data.map.height as usize {
|
|
|
|
let idx = build_data.map.xy_idx(x as i32, y as i32);
|
2021-12-10 15:17:51 -05:00
|
|
|
// We're doing some nasty casting to make it easier to type things like '#' in the match
|
2021-12-15 11:36:17 -05:00
|
|
|
self.char_to_map(cell.ch as u8 as char, idx, build_data);
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_ascii_to_vec(template: &str) -> Vec<char> {
|
|
|
|
let mut string_vec: Vec<char> = template
|
|
|
|
.chars()
|
|
|
|
.filter(|a| *a != '\r' && *a != '\n')
|
|
|
|
.collect();
|
|
|
|
for c in string_vec.iter_mut() {
|
|
|
|
if *c as u8 == 160_u8 {
|
|
|
|
*c = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
string_vec
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
fn load_ascii_map(&mut self, level: &prefab_levels::PrefabLevel, build_data: &mut BuilderMap) {
|
|
|
|
let string_vec = Self::read_ascii_to_vec(level.template);
|
2021-12-10 15:17:51 -05:00
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
for ty in 0..level.height {
|
|
|
|
for tx in 0..level.width {
|
2021-12-15 11:36:17 -05:00
|
|
|
if tx < build_data.map.width as usize && ty < build_data.map.height as usize {
|
|
|
|
let idx = build_data.map.xy_idx(tx as i32, ty as i32);
|
|
|
|
self.char_to_map(string_vec[i], idx, build_data);
|
2021-12-10 15:17:51 -05:00
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 16:35:09 -05:00
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
fn apply_previous_iteration<F>(
|
|
|
|
&mut self,
|
|
|
|
mut filter: F,
|
|
|
|
_rng: &mut RandomNumberGenerator,
|
|
|
|
build_data: &mut BuilderMap,
|
|
|
|
) where
|
|
|
|
F: FnMut(i32, i32) -> bool,
|
2021-12-13 15:22:59 -05:00
|
|
|
{
|
2021-12-15 11:36:17 -05:00
|
|
|
let width = build_data.map.width;
|
|
|
|
build_data.spawn_list.retain(|(idx, _name)| {
|
|
|
|
let x = *idx as i32 % width;
|
|
|
|
let y = *idx as i32 / width;
|
|
|
|
|
|
|
|
filter(x, y)
|
|
|
|
});
|
|
|
|
build_data.take_snapshot();
|
2021-12-13 15:22:59 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
#[allow(dead_code)]
|
2021-12-15 11:36:17 -05:00
|
|
|
fn apply_sectional(
|
|
|
|
&mut self,
|
|
|
|
section: &prefab_sections::PrefabSection,
|
|
|
|
rng: &mut RandomNumberGenerator,
|
|
|
|
build_data: &mut BuilderMap,
|
|
|
|
) {
|
2021-12-10 16:35:09 -05:00
|
|
|
use prefab_sections::*;
|
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
let string_vec = Self::read_ascii_to_vec(section.template);
|
2021-12-10 16:35:09 -05:00
|
|
|
|
|
|
|
// Place the new section
|
|
|
|
let chunk_x = match section.placement.0 {
|
|
|
|
HorizontalPlacement::Left => 0,
|
2021-12-15 11:36:17 -05:00
|
|
|
HorizontalPlacement::Center => (build_data.map.width / 2) - (section.width as i32 / 2),
|
|
|
|
HorizontalPlacement::Right => (build_data.map.width - 1) - section.width as i32,
|
2021-12-10 16:35:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let chunk_y = match section.placement.1 {
|
|
|
|
VerticalPlacement::Top => 0,
|
2021-12-15 11:36:17 -05:00
|
|
|
VerticalPlacement::Center => (build_data.map.height / 2) - (section.height as i32 / 2),
|
|
|
|
VerticalPlacement::Bottom => (build_data.map.height - 1) - section.height as i32,
|
2021-12-10 16:35:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Build the map
|
2021-12-15 11:36:17 -05:00
|
|
|
self.apply_previous_iteration(
|
|
|
|
|x, y| {
|
|
|
|
x < chunk_x
|
|
|
|
|| x > (chunk_x + section.width as i32)
|
|
|
|
|| y < chunk_y
|
|
|
|
|| y > (chunk_y + section.height as i32)
|
|
|
|
},
|
|
|
|
rng,
|
|
|
|
build_data,
|
|
|
|
);
|
2021-12-10 16:35:09 -05:00
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
for ty in 0..section.height {
|
|
|
|
for tx in 0..section.width {
|
2021-12-14 11:19:03 -05:00
|
|
|
if tx > 0
|
2021-12-15 11:36:17 -05:00
|
|
|
&& tx < build_data.map.width as usize - 1
|
|
|
|
&& ty < build_data.map.height as usize - 1
|
2021-12-14 11:19:03 -05:00
|
|
|
&& ty > 0
|
|
|
|
{
|
2021-12-15 11:36:17 -05:00
|
|
|
let idx = build_data
|
|
|
|
.map
|
|
|
|
.xy_idx(tx as i32 + chunk_x, ty as i32 + chunk_y);
|
|
|
|
self.char_to_map(string_vec[i], idx, build_data);
|
2021-12-10 16:35:09 -05:00
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-10 16:35:09 -05:00
|
|
|
}
|
2021-12-13 15:22:59 -05:00
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
fn apply_room_vaults(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
2021-12-13 15:22:59 -05:00
|
|
|
use prefab_rooms::*;
|
|
|
|
|
|
|
|
// Apply the previous builder, and keep all entities it spawns (for now)
|
2021-12-15 11:36:17 -05:00
|
|
|
self.apply_previous_iteration(|_x, _y| true, rng, build_data);
|
2021-12-13 15:22:59 -05:00
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
// Do we want a vault at all?
|
2021-12-15 11:36:17 -05:00
|
|
|
let vault_roll = rng.roll_dice(1, 6) + build_data.map.depth;
|
2021-12-14 11:19:03 -05:00
|
|
|
if vault_roll < 4 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:22:59 -05:00
|
|
|
// Note that this is a place-holder and will be moved out of this function
|
|
|
|
let master_vault_list = vec![TOTALLY_NOT_A_TRAP, CHECKERBOARD, SILLY_SMILE];
|
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
// Filter the vault list down to ones that are applicable to the current depth
|
|
|
|
let mut possible_vaults: Vec<&PrefabRoom> = master_vault_list
|
2021-12-13 15:22:59 -05:00
|
|
|
.iter()
|
2021-12-15 11:36:17 -05:00
|
|
|
.filter(|v| {
|
|
|
|
build_data.map.depth >= v.first_depth && build_data.map.depth <= v.last_depth
|
|
|
|
})
|
2021-12-13 15:22:59 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Bail out if there's nothing to build
|
|
|
|
if possible_vaults.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
let n_vaults = i32::min(rng.roll_dice(1, 3), possible_vaults.len() as i32);
|
|
|
|
let mut used_tiles: HashSet<usize> = HashSet::new();
|
|
|
|
|
|
|
|
for _i in 0..n_vaults {
|
|
|
|
let vault_index = if possible_vaults.len() == 1 {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
(rng.roll_dice(1, possible_vaults.len() as i32) - 1) as usize
|
|
|
|
};
|
|
|
|
let vault = possible_vaults[vault_index];
|
|
|
|
|
|
|
|
// We'll make a list of places in which the vault could fit
|
|
|
|
let mut vault_positions: Vec<Position> = Vec::new();
|
|
|
|
|
|
|
|
let mut idx = 0usize;
|
|
|
|
loop {
|
2021-12-15 11:36:17 -05:00
|
|
|
let x = (idx % build_data.map.width as usize) as i32;
|
|
|
|
let y = (idx / build_data.map.width as usize) as i32;
|
2021-12-14 11:19:03 -05:00
|
|
|
|
|
|
|
// Check that we won't overflow the map
|
|
|
|
if x > 1
|
2021-12-15 11:36:17 -05:00
|
|
|
&& (x + vault.width as i32) < build_data.map.width - 2
|
2021-12-14 11:19:03 -05:00
|
|
|
&& y > 1
|
2021-12-15 11:36:17 -05:00
|
|
|
&& (y + vault.height as i32) < build_data.map.height - 2
|
2021-12-14 11:19:03 -05:00
|
|
|
{
|
|
|
|
let mut possible = true;
|
|
|
|
for ty in 0..vault.height as i32 {
|
|
|
|
for tx in 0..vault.width as i32 {
|
2021-12-15 11:36:17 -05:00
|
|
|
let idx = build_data.map.xy_idx(tx + x, ty + y);
|
|
|
|
if build_data.map.tiles[idx] != TileType::Floor {
|
2021-12-14 11:19:03 -05:00
|
|
|
possible = false;
|
|
|
|
}
|
|
|
|
if used_tiles.contains(&idx) {
|
|
|
|
possible = false;
|
|
|
|
}
|
2021-12-13 15:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
if possible {
|
|
|
|
vault_positions.push(Position { x, y });
|
|
|
|
break;
|
|
|
|
}
|
2021-12-13 15:22:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
idx += 1;
|
2021-12-15 11:36:17 -05:00
|
|
|
if idx >= build_data.map.tiles.len() - 1 {
|
2021-12-13 15:22:59 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 11:19:03 -05:00
|
|
|
if !vault_positions.is_empty() {
|
|
|
|
let pos_idx = if vault_positions.len() == 1 {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
(rng.roll_dice(1, vault_positions.len() as i32) - 1) as usize
|
|
|
|
};
|
|
|
|
let pos = &vault_positions[pos_idx];
|
|
|
|
|
|
|
|
let chunk_x = pos.x;
|
|
|
|
let chunk_y = pos.y;
|
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
let width = build_data.map.width; // The borrow checker really doesn't like it
|
|
|
|
let height = build_data.map.height; // when we access `self` inside the `retain`
|
|
|
|
build_data.spawn_list.retain(|e| {
|
2021-12-14 11:19:03 -05:00
|
|
|
let idx = e.0 as i32;
|
|
|
|
let x = idx % width;
|
|
|
|
let y = idx / height;
|
|
|
|
x < chunk_x
|
|
|
|
|| x > chunk_x + vault.width as i32
|
|
|
|
|| y < chunk_y
|
|
|
|
|| y > chunk_y + vault.height as i32
|
|
|
|
});
|
|
|
|
|
2021-12-15 11:36:17 -05:00
|
|
|
let string_vec = Self::read_ascii_to_vec(vault.template);
|
2021-12-14 11:19:03 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for ty in 0..vault.height {
|
|
|
|
for tx in 0..vault.width {
|
2021-12-15 11:36:17 -05:00
|
|
|
let idx = build_data
|
|
|
|
.map
|
|
|
|
.xy_idx(tx as i32 + chunk_x, ty as i32 + chunk_y);
|
|
|
|
self.char_to_map(string_vec[i], idx, build_data);
|
2021-12-14 11:19:03 -05:00
|
|
|
used_tiles.insert(idx);
|
|
|
|
i += 1;
|
|
|
|
}
|
2021-12-13 15:22:59 -05:00
|
|
|
}
|
2021-12-15 11:36:17 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-14 11:19:03 -05:00
|
|
|
|
|
|
|
possible_vaults.remove(vault_index);
|
2021-12-13 15:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 14:29:03 -05:00
|
|
|
}
|