Finish Prefab map builder
This commit is contained in:
parent
5454dd0755
commit
cce44f78f4
@ -67,8 +67,15 @@ pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
|||||||
//
|
//
|
||||||
// result
|
// result
|
||||||
|
|
||||||
Box::new(PrefabBuilder::new(
|
Box::new(PrefabBuilder::vaults(
|
||||||
new_depth,
|
new_depth,
|
||||||
Some(Box::new(SimpleMapBuilder::new(new_depth))),
|
Box::new(PrefabBuilder::sectional(
|
||||||
|
new_depth,
|
||||||
|
prefab_builder::prefab_sections::UNDERGROUND_FORT,
|
||||||
|
Box::new(WaveformCollapseBuilder::derived_map(
|
||||||
|
new_depth,
|
||||||
|
Box::new(CellularAutomataBuilder::new(new_depth)),
|
||||||
|
)),
|
||||||
|
)),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
mod prefab_levels;
|
pub mod prefab_levels;
|
||||||
mod prefab_rooms;
|
pub mod prefab_rooms;
|
||||||
mod prefab_sections;
|
pub mod prefab_sections;
|
||||||
|
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use rltk::RandomNumberGenerator;
|
use rltk::RandomNumberGenerator;
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
@ -8,7 +10,7 @@ use specs::prelude::*;
|
|||||||
use super::{remove_unreachable_areas_returning_most_distant, MapBuilder};
|
use super::{remove_unreachable_areas_returning_most_distant, MapBuilder};
|
||||||
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
|
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
|
||||||
|
|
||||||
#[derive(PartialEq, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub enum PrefabMode {
|
pub enum PrefabMode {
|
||||||
RexLevel {
|
RexLevel {
|
||||||
@ -23,6 +25,7 @@ pub enum PrefabMode {
|
|||||||
RoomVaults,
|
RoomVaults,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct PrefabBuilder {
|
pub struct PrefabBuilder {
|
||||||
map: Map,
|
map: Map,
|
||||||
starting_position: Position,
|
starting_position: Position,
|
||||||
@ -79,9 +82,65 @@ impl PrefabBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn rex_level(new_depth: i32, template: &'static str) -> PrefabBuilder {
|
||||||
|
PrefabBuilder {
|
||||||
|
map: Map::new(new_depth),
|
||||||
|
starting_position: Position { x: 0, y: 0 },
|
||||||
|
depth: new_depth,
|
||||||
|
history: Vec::new(),
|
||||||
|
mode: PrefabMode::RexLevel { template },
|
||||||
|
previous_builder: None,
|
||||||
|
spawn_list: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn constant(new_depth: i32, level: prefab_levels::PrefabLevel) -> PrefabBuilder {
|
||||||
|
PrefabBuilder {
|
||||||
|
map: Map::new(new_depth),
|
||||||
|
starting_position: Position { x: 0, y: 0 },
|
||||||
|
depth: new_depth,
|
||||||
|
history: Vec::new(),
|
||||||
|
mode: PrefabMode::Constant { level },
|
||||||
|
previous_builder: None,
|
||||||
|
spawn_list: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn sectional(
|
||||||
|
new_depth: i32,
|
||||||
|
section: prefab_sections::PrefabSection,
|
||||||
|
previous_builder: Box<dyn MapBuilder>,
|
||||||
|
) -> PrefabBuilder {
|
||||||
|
PrefabBuilder {
|
||||||
|
map: Map::new(new_depth),
|
||||||
|
starting_position: Position { x: 0, y: 0 },
|
||||||
|
depth: new_depth,
|
||||||
|
history: Vec::new(),
|
||||||
|
mode: PrefabMode::Sectional { section },
|
||||||
|
previous_builder: Some(previous_builder),
|
||||||
|
spawn_list: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn vaults(new_depth: i32, previous_builder: Box<dyn MapBuilder>) -> PrefabBuilder {
|
||||||
|
PrefabBuilder {
|
||||||
|
map: Map::new(new_depth),
|
||||||
|
starting_position: Position { x: 0, y: 0 },
|
||||||
|
depth: new_depth,
|
||||||
|
history: Vec::new(),
|
||||||
|
mode: PrefabMode::RoomVaults,
|
||||||
|
previous_builder: Some(previous_builder),
|
||||||
|
spawn_list: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn build(&mut self) {
|
fn build(&mut self) {
|
||||||
match self.mode {
|
match self.mode {
|
||||||
PrefabMode::RexLevel { template } => self.load_rex_map(template),
|
PrefabMode::RexLevel { template } => self.load_rex_map(&template),
|
||||||
PrefabMode::Constant { level } => self.load_ascii_map(&level),
|
PrefabMode::Constant { level } => self.load_ascii_map(&level),
|
||||||
PrefabMode::Sectional { section } => self.apply_sectional(§ion),
|
PrefabMode::Sectional { section } => self.apply_sectional(§ion),
|
||||||
PrefabMode::RoomVaults => self.apply_room_vaults(),
|
PrefabMode::RoomVaults => self.apply_room_vaults(),
|
||||||
@ -89,12 +148,13 @@ impl PrefabBuilder {
|
|||||||
self.take_snapshot();
|
self.take_snapshot();
|
||||||
|
|
||||||
// Find a starting point; start at the middle and walk left until we find an open tile
|
// Find a starting point; start at the middle and walk left until we find an open tile
|
||||||
|
let mut start_idx;
|
||||||
if self.starting_position.x == 0 {
|
if self.starting_position.x == 0 {
|
||||||
self.starting_position = Position {
|
self.starting_position = Position {
|
||||||
x: self.map.width / 2,
|
x: self.map.width / 2,
|
||||||
y: self.map.height / 2,
|
y: self.map.height / 2,
|
||||||
};
|
};
|
||||||
let mut start_idx = self
|
start_idx = self
|
||||||
.map
|
.map
|
||||||
.xy_idx(self.starting_position.x, self.starting_position.y);
|
.xy_idx(self.starting_position.x, self.starting_position.y);
|
||||||
while self.map.tiles[start_idx] != TileType::Floor {
|
while self.map.tiles[start_idx] != TileType::Floor {
|
||||||
@ -104,6 +164,18 @@ impl PrefabBuilder {
|
|||||||
.xy_idx(self.starting_position.x, self.starting_position.y);
|
.xy_idx(self.starting_position.x, self.starting_position.y);
|
||||||
}
|
}
|
||||||
self.take_snapshot();
|
self.take_snapshot();
|
||||||
|
}
|
||||||
|
let mut has_exit = false;
|
||||||
|
for t in self.map.tiles.iter() {
|
||||||
|
if *t == TileType::DownStairs {
|
||||||
|
has_exit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !has_exit {
|
||||||
|
start_idx = self
|
||||||
|
.map
|
||||||
|
.xy_idx(self.starting_position.x, self.starting_position.y);
|
||||||
|
|
||||||
// Find all tiles we can reach from the starting point
|
// Find all tiles we can reach from the starting point
|
||||||
let exit_tile =
|
let exit_tile =
|
||||||
@ -179,20 +251,17 @@ impl PrefabBuilder {
|
|||||||
.chars()
|
.chars()
|
||||||
.filter(|a| *a != '\r' && *a != '\n')
|
.filter(|a| *a != '\r' && *a != '\n')
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for c in string_vec.iter_mut() {
|
for c in string_vec.iter_mut() {
|
||||||
if *c as u8 == 160_u8 {
|
if *c as u8 == 160_u8 {
|
||||||
*c = ' ';
|
*c = ' ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string_vec
|
string_vec
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn load_ascii_map(&mut self, level: &prefab_levels::PrefabLevel) {
|
fn load_ascii_map(&mut self, level: &prefab_levels::PrefabLevel) {
|
||||||
// Start by converting to a vector, with newlines removed
|
let string_vec = PrefabBuilder::read_ascii_to_vec(level.template);
|
||||||
let string_vec = Self::read_ascii_to_vec(level.template);
|
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for ty in 0..level.height {
|
for ty in 0..level.height {
|
||||||
@ -201,7 +270,6 @@ impl PrefabBuilder {
|
|||||||
let idx = self.map.xy_idx(tx as i32, ty as i32);
|
let idx = self.map.xy_idx(tx as i32, ty as i32);
|
||||||
self.char_to_map(string_vec[i], idx);
|
self.char_to_map(string_vec[i], idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -215,13 +283,12 @@ impl PrefabBuilder {
|
|||||||
let prev_builder = self.previous_builder.as_mut().unwrap();
|
let prev_builder = self.previous_builder.as_mut().unwrap();
|
||||||
prev_builder.build_map();
|
prev_builder.build_map();
|
||||||
self.starting_position = prev_builder.get_starting_position();
|
self.starting_position = prev_builder.get_starting_position();
|
||||||
self.map = prev_builder.get_map();
|
self.map = prev_builder.get_map().clone();
|
||||||
self.history = prev_builder.get_snapshot_history();
|
self.history = prev_builder.get_snapshot_history();
|
||||||
for e in prev_builder.get_spawn_list().iter() {
|
for e in prev_builder.get_spawn_list().iter() {
|
||||||
let idx = e.0;
|
let idx = e.0;
|
||||||
let x = idx as i32 % self.map.width;
|
let x = idx as i32 % self.map.width;
|
||||||
let y = idx as i32 / self.map.width;
|
let y = idx as i32 / self.map.width;
|
||||||
|
|
||||||
if filter(x, y, e) {
|
if filter(x, y, e) {
|
||||||
self.spawn_list.push((idx, e.1.to_string()))
|
self.spawn_list.push((idx, e.1.to_string()))
|
||||||
}
|
}
|
||||||
@ -229,10 +296,11 @@ impl PrefabBuilder {
|
|||||||
self.take_snapshot();
|
self.take_snapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_sectional(&mut self, section: &prefab_sections::PrefabSection) {
|
#[allow(dead_code)]
|
||||||
|
fn apply_sectional(&mut self, section: &prefab_sections::PrefabSection) {
|
||||||
use prefab_sections::*;
|
use prefab_sections::*;
|
||||||
|
|
||||||
let string_vec = Self::read_ascii_to_vec(section.template);
|
let string_vec = PrefabBuilder::read_ascii_to_vec(section.template);
|
||||||
|
|
||||||
// Place the new section
|
// Place the new section
|
||||||
let chunk_x = match section.placement.0 {
|
let chunk_x = match section.placement.0 {
|
||||||
@ -248,7 +316,7 @@ impl PrefabBuilder {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Build the map
|
// Build the map
|
||||||
self.apply_previous_iteration(|x, y, e| {
|
self.apply_previous_iteration(|x, y, _e| {
|
||||||
x < chunk_x
|
x < chunk_x
|
||||||
|| x > (chunk_x + section.width as i32)
|
|| x > (chunk_x + section.width as i32)
|
||||||
|| y < chunk_y
|
|| y < chunk_y
|
||||||
@ -258,7 +326,11 @@ impl PrefabBuilder {
|
|||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for ty in 0..section.height {
|
for ty in 0..section.height {
|
||||||
for tx in 0..section.width {
|
for tx in 0..section.width {
|
||||||
if tx < self.map.width as usize && ty < self.map.height as usize {
|
if tx > 0
|
||||||
|
&& tx < self.map.width as usize - 1
|
||||||
|
&& ty < self.map.height as usize - 1
|
||||||
|
&& ty > 0
|
||||||
|
{
|
||||||
let idx = self.map.xy_idx(tx as i32 + chunk_x, ty as i32 + chunk_y);
|
let idx = self.map.xy_idx(tx as i32 + chunk_x, ty as i32 + chunk_y);
|
||||||
self.char_to_map(string_vec[i], idx);
|
self.char_to_map(string_vec[i], idx);
|
||||||
}
|
}
|
||||||
@ -275,11 +347,17 @@ impl PrefabBuilder {
|
|||||||
// Apply the previous builder, and keep all entities it spawns (for now)
|
// Apply the previous builder, and keep all entities it spawns (for now)
|
||||||
self.apply_previous_iteration(|_x, _y, _e| true);
|
self.apply_previous_iteration(|_x, _y, _e| true);
|
||||||
|
|
||||||
|
// Do we want a vault at all?
|
||||||
|
let vault_roll = rng.roll_dice(1, 6) + self.depth;
|
||||||
|
if vault_roll < 4 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Note that this is a place-holder and will be moved out of this function
|
// 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];
|
let master_vault_list = vec![TOTALLY_NOT_A_TRAP, CHECKERBOARD, SILLY_SMILE];
|
||||||
|
|
||||||
// Filter the possible vault list down to ones that are application to the current depth
|
// Filter the vault list down to ones that are applicable to the current depth
|
||||||
let possible_vaults: Vec<&PrefabRoom> = master_vault_list
|
let mut possible_vaults: Vec<&PrefabRoom> = master_vault_list
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|v| self.depth >= v.first_depth && self.depth <= v.last_depth)
|
.filter(|v| self.depth >= v.first_depth && self.depth <= v.last_depth)
|
||||||
.collect();
|
.collect();
|
||||||
@ -289,40 +367,48 @@ impl PrefabBuilder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let vault_index = if possible_vaults.len() == 1 {
|
let n_vaults = i32::min(rng.roll_dice(1, 3), possible_vaults.len() as i32);
|
||||||
0
|
let mut used_tiles: HashSet<usize> = HashSet::new();
|
||||||
} 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
|
for _i in 0..n_vaults {
|
||||||
let mut vault_positions: Vec<Position> = Vec::new();
|
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];
|
||||||
|
|
||||||
let mut idx = 0_usize;
|
// We'll make a list of places in which the vault could fit
|
||||||
loop {
|
let mut vault_positions: Vec<Position> = Vec::new();
|
||||||
let x = (idx % self.map.width as usize) as i32;
|
|
||||||
let y = (idx / self.map.width as usize) as i32;
|
|
||||||
|
|
||||||
// Check that we won't overflow the map
|
let mut idx = 0usize;
|
||||||
if x > 1
|
loop {
|
||||||
&& (x + vault.width as i32) < self.map.width - 2
|
let x = (idx % self.map.width as usize) as i32;
|
||||||
&& y > 1
|
let y = (idx / self.map.width as usize) as i32;
|
||||||
&& (y + vault.height as i32) < self.map.height - 2
|
|
||||||
{
|
// Check that we won't overflow the map
|
||||||
let mut possible = true;
|
if x > 1
|
||||||
for ty in 0..vault.height as i32 {
|
&& (x + vault.width as i32) < self.map.width - 2
|
||||||
for tx in 0..vault.width as i32 {
|
&& y > 1
|
||||||
let idx = self.map.xy_idx(tx + x, ty + y);
|
&& (y + vault.height as i32) < self.map.height - 2
|
||||||
if self.map.tiles[idx] != TileType::Floor {
|
{
|
||||||
possible = false;
|
let mut possible = true;
|
||||||
|
for ty in 0..vault.height as i32 {
|
||||||
|
for tx in 0..vault.width as i32 {
|
||||||
|
let idx = self.map.xy_idx(tx + x, ty + y);
|
||||||
|
if self.map.tiles[idx] != TileType::Floor {
|
||||||
|
possible = false;
|
||||||
|
}
|
||||||
|
if used_tiles.contains(&idx) {
|
||||||
|
possible = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if possible {
|
if possible {
|
||||||
vault_positions.push(Position { x, y });
|
vault_positions.push(Position { x, y });
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
idx += 1;
|
idx += 1;
|
||||||
@ -330,41 +416,44 @@ impl PrefabBuilder {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !vault_positions.is_empty() {
|
if !vault_positions.is_empty() {
|
||||||
let pos_idx = if vault_positions.len() == 1 {
|
let pos_idx = if vault_positions.len() == 1 {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
(rng.roll_dice(1, vault_positions.len() as i32) - 1) as usize
|
(rng.roll_dice(1, vault_positions.len() as i32) - 1) as usize
|
||||||
};
|
};
|
||||||
let pos = &vault_positions[pos_idx];
|
let pos = &vault_positions[pos_idx];
|
||||||
|
|
||||||
let chunk_x = pos.x;
|
let chunk_x = pos.x;
|
||||||
let chunk_y = pos.y;
|
let chunk_y = pos.y;
|
||||||
|
|
||||||
let width = self.map.width;
|
let width = self.map.width; // The borrow checker really doesn't like it
|
||||||
let height = self.map.height;
|
let height = self.map.height; // when we access `self` inside the `retain`
|
||||||
self.spawn_list.retain(|e| {
|
self.spawn_list.retain(|e| {
|
||||||
let idx = e.0 as i32;
|
let idx = e.0 as i32;
|
||||||
let x = idx % width;
|
let x = idx % width;
|
||||||
let y = idx / height;
|
let y = idx / height;
|
||||||
x < chunk_x
|
x < chunk_x
|
||||||
|| x > chunk_x + vault.width as i32
|
|| x > chunk_x + vault.width as i32
|
||||||
|| y < chunk_y
|
|| y < chunk_y
|
||||||
|| y > chunk_y + vault.height as i32
|
|| y > chunk_y + vault.height as i32
|
||||||
});
|
});
|
||||||
|
|
||||||
let string_vec = PrefabBuilder::read_ascii_to_vec(vault.template);
|
let string_vec = PrefabBuilder::read_ascii_to_vec(vault.template);
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for ty in 0..vault.height {
|
for ty in 0..vault.height {
|
||||||
for tx in 0..vault.width {
|
for tx in 0..vault.width {
|
||||||
let idx = self.map.xy_idx(tx as i32 + chunk_x, ty as i32 + chunk_y);
|
let idx = self.map.xy_idx(tx as i32 + chunk_x, ty as i32 + chunk_y);
|
||||||
self.char_to_map(string_vec[i], idx);
|
self.char_to_map(string_vec[i], idx);
|
||||||
i += 1;
|
used_tiles.insert(idx);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
self.take_snapshot();
|
||||||
|
|
||||||
|
possible_vaults.remove(vault_index);
|
||||||
}
|
}
|
||||||
self.take_snapshot();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user