Generate map with cellular automata, completes section 4.5
This commit is contained in:
parent
e8c03b9536
commit
545deb24d8
@ -5,7 +5,7 @@ use specs::prelude::*;
|
|||||||
use specs::saveload::{ConvertSaveload, Marker};
|
use specs::saveload::{ConvertSaveload, Marker};
|
||||||
use specs_derive::*;
|
use specs_derive::*;
|
||||||
|
|
||||||
#[derive(Component, ConvertSaveload, Copy, Clone)]
|
#[derive(Component, ConvertSaveload, Default, Copy, Clone)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
|
196
src/map_builders/cellular_automata.rs
Normal file
196
src/map_builders/cellular_automata.rs
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
use super::MapBuilder;
|
||||||
|
use crate::{components::Position, spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER};
|
||||||
|
use rltk::RandomNumberGenerator;
|
||||||
|
use specs::prelude::*;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub struct CellularAutomataBuilder {
|
||||||
|
map: Map,
|
||||||
|
starting_position: Position,
|
||||||
|
depth: i32,
|
||||||
|
history: Vec<Map>,
|
||||||
|
noise_areas: HashMap<i32, Vec<usize>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MapBuilder for CellularAutomataBuilder {
|
||||||
|
fn build_map(&mut self) {
|
||||||
|
self.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_entities(&mut self, ecs: &mut World) {
|
||||||
|
for area in self.noise_areas.iter() {
|
||||||
|
spawner::spawn_region(ecs, area.1, self.depth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_map(&self) -> Map {
|
||||||
|
self.map.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_starting_position(&self) -> Position {
|
||||||
|
self.starting_position
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CellularAutomataBuilder {
|
||||||
|
pub fn new(new_depth: i32) -> CellularAutomataBuilder {
|
||||||
|
CellularAutomataBuilder {
|
||||||
|
map: Map::new(new_depth),
|
||||||
|
starting_position: Position::default(),
|
||||||
|
depth: new_depth,
|
||||||
|
history: Vec::new(),
|
||||||
|
noise_areas: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build(&mut self) {
|
||||||
|
let mut rng = RandomNumberGenerator::new();
|
||||||
|
|
||||||
|
// First we completely randomize the map, setting 55% of it to be floor.
|
||||||
|
for y in 1..self.map.height - 1 {
|
||||||
|
for x in 1..self.map.width - 1 {
|
||||||
|
let roll = rng.roll_dice(1, 100);
|
||||||
|
let idx = self.map.xy_idx(x, y);
|
||||||
|
|
||||||
|
if roll > 55 {
|
||||||
|
self.map.tiles[idx] = TileType::Floor
|
||||||
|
} else {
|
||||||
|
self.map.tiles[idx] = TileType::Wall
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.take_snapshot();
|
||||||
|
|
||||||
|
// Now we iteratively apply cellular automata rules
|
||||||
|
for _i in 0..15 {
|
||||||
|
let mut newtiles = self.map.tiles.clone();
|
||||||
|
|
||||||
|
for y in 1..self.map.height - 1 {
|
||||||
|
for x in 1..self.map.width - 1 {
|
||||||
|
let idx = self.map.xy_idx(x, y);
|
||||||
|
let mut neighbors = 0;
|
||||||
|
|
||||||
|
if self.map.tiles[idx - 1] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
if self.map.tiles[idx + 1] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
if self.map.tiles[idx - self.map.width as usize] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
if self.map.tiles[idx + self.map.width as usize] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
if self.map.tiles[idx - (self.map.width as usize - 1)] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
if self.map.tiles[idx - (self.map.width as usize + 1)] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
if self.map.tiles[idx + (self.map.width as usize - 1)] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
if self.map.tiles[idx + (self.map.width as usize + 1)] == TileType::Wall {
|
||||||
|
neighbors += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if neighbors > 4 || neighbors == 0 {
|
||||||
|
newtiles[idx] = TileType::Wall;
|
||||||
|
} else {
|
||||||
|
newtiles[idx] = TileType::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.map.tiles = newtiles.clone();
|
||||||
|
self.take_snapshot();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find a starting point; start at the middle and walk left until we find an open tile
|
||||||
|
self.starting_position = Position {
|
||||||
|
x: self.map.width / 2,
|
||||||
|
y: self.map.height / 2,
|
||||||
|
};
|
||||||
|
let mut start_idx = self
|
||||||
|
.map
|
||||||
|
.xy_idx(self.starting_position.x, self.starting_position.y);
|
||||||
|
while self.map.tiles[start_idx] != TileType::Floor {
|
||||||
|
self.starting_position.x -= 1;
|
||||||
|
start_idx = self
|
||||||
|
.map
|
||||||
|
.xy_idx(self.starting_position.x, self.starting_position.y)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find all tiles we can reach from the starting point
|
||||||
|
let map_starts: Vec<usize> = vec![start_idx];
|
||||||
|
let dijkstra_map = rltk::DijkstraMap::new(
|
||||||
|
self.map.width,
|
||||||
|
self.map.height,
|
||||||
|
&map_starts,
|
||||||
|
&self.map,
|
||||||
|
200.0,
|
||||||
|
);
|
||||||
|
let mut exit_tile = (0, 0.0f32);
|
||||||
|
for (i, tile) in self.map.tiles.iter_mut().enumerate() {
|
||||||
|
if *tile == TileType::Floor {
|
||||||
|
let distance_to_start = dijkstra_map.map[i];
|
||||||
|
|
||||||
|
// We can't get to this tile - so we'll make it a wall
|
||||||
|
if distance_to_start == f32::MAX {
|
||||||
|
*tile = TileType::Wall;
|
||||||
|
} else {
|
||||||
|
// If it is further away than our current exit candidate, move the exit
|
||||||
|
if distance_to_start > exit_tile.1 {
|
||||||
|
exit_tile.0 = i;
|
||||||
|
exit_tile.1 = distance_to_start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.take_snapshot();
|
||||||
|
|
||||||
|
// Place the stairs
|
||||||
|
self.map.tiles[exit_tile.0] = TileType::DownStairs;
|
||||||
|
self.take_snapshot();
|
||||||
|
|
||||||
|
// Now we build a noise map for use in spawning entities later
|
||||||
|
let mut noise = rltk::FastNoise::seeded(rng.roll_dice(1, 65536) as u64);
|
||||||
|
noise.set_noise_type(rltk::NoiseType::Cellular);
|
||||||
|
noise.set_frequency(0.08);
|
||||||
|
noise.set_cellular_distance_function(rltk::CellularDistanceFunction::Manhattan);
|
||||||
|
|
||||||
|
for y in 1..self.map.height - 1 {
|
||||||
|
for x in 1..self.map.width - 1 {
|
||||||
|
let idx = self.map.xy_idx(x, y);
|
||||||
|
if self.map.tiles[idx] == TileType::Floor {
|
||||||
|
let cell_value_f = noise.get_noise(x as f32, y as f32) * 10240.0;
|
||||||
|
let cell_value = cell_value_f as i32;
|
||||||
|
|
||||||
|
#[allow(clippy::map_entry)]
|
||||||
|
if self.noise_areas.contains_key(&cell_value) {
|
||||||
|
self.noise_areas.get_mut(&cell_value).unwrap().push(idx);
|
||||||
|
} else {
|
||||||
|
self.noise_areas.insert(cell_value, vec![idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,13 @@
|
|||||||
mod bsp_dungeon;
|
mod bsp_dungeon;
|
||||||
mod bsp_interior;
|
mod bsp_interior;
|
||||||
|
mod cellular_automata;
|
||||||
mod common;
|
mod common;
|
||||||
mod simple_map;
|
mod simple_map;
|
||||||
|
|
||||||
use crate::{Map, Position};
|
use crate::{Map, Position};
|
||||||
use bsp_dungeon::BspDungeonBuilder;
|
use bsp_dungeon::BspDungeonBuilder;
|
||||||
use bsp_interior::BspInteriorBuilder;
|
use bsp_interior::BspInteriorBuilder;
|
||||||
|
use cellular_automata::CellularAutomataBuilder;
|
||||||
use common::*;
|
use common::*;
|
||||||
use simple_map::SimpleMapBuilder;
|
use simple_map::SimpleMapBuilder;
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
@ -21,9 +23,10 @@ pub trait MapBuilder {
|
|||||||
|
|
||||||
pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
||||||
let mut rng = rltk::RandomNumberGenerator::new();
|
let mut rng = rltk::RandomNumberGenerator::new();
|
||||||
match rng.roll_dice(1, 3) {
|
match rng.roll_dice(1, 4) {
|
||||||
1 => Box::new(BspDungeonBuilder::new(new_depth)),
|
1 => Box::new(BspDungeonBuilder::new(new_depth)),
|
||||||
2 => Box::new(BspInteriorBuilder::new(new_depth)),
|
2 => Box::new(BspInteriorBuilder::new(new_depth)),
|
||||||
|
3 => Box::new(CellularAutomataBuilder::new(new_depth)),
|
||||||
_ => Box::new(SimpleMapBuilder::new(new_depth)),
|
_ => Box::new(SimpleMapBuilder::new(new_depth)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
105
src/spawner.rs
105
src/spawner.rs
@ -1,5 +1,5 @@
|
|||||||
use crate::components::*;
|
use crate::components::*;
|
||||||
use crate::{map::MAP_WIDTH, random_table::RandomTable, Rect};
|
use crate::{map::MAP_WIDTH, random_table::RandomTable, Map, Rect, TileType};
|
||||||
use rltk::{RandomNumberGenerator, RGB};
|
use rltk::{RandomNumberGenerator, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use specs::saveload::{MarkedBuilder, SimpleMarker};
|
use specs::saveload::{MarkedBuilder, SimpleMarker};
|
||||||
@ -57,54 +57,79 @@ fn room_table(map_depth: i32) -> RandomTable {
|
|||||||
/// fills a room with stuff!
|
/// fills a room with stuff!
|
||||||
#[allow(clippy::map_entry)]
|
#[allow(clippy::map_entry)]
|
||||||
pub fn spawn_room(ecs: &mut World, room: &Rect, map_depth: i32) {
|
pub fn spawn_room(ecs: &mut World, room: &Rect, map_depth: i32) {
|
||||||
let spawn_table = room_table(map_depth);
|
let mut possible_targets: Vec<usize> = Vec::new();
|
||||||
let mut spawn_points: HashMap<usize, String> = HashMap::new();
|
|
||||||
|
|
||||||
// Scope to keep the borrow checker happy
|
// Borrow scope - to keep access to the map separated
|
||||||
{
|
{
|
||||||
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
let map = ecs.fetch::<Map>();
|
||||||
let num_spawns = rng.roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3;
|
for y in room.y1 + 1..room.y2 {
|
||||||
|
for x in room.x1 + 1..room.x2 {
|
||||||
for _i in 0..num_spawns {
|
let idx = map.xy_idx(x, y);
|
||||||
let mut added = false;
|
if map.tiles[idx] == TileType::Floor {
|
||||||
let mut tries = 0;
|
possible_targets.push(idx);
|
||||||
|
|
||||||
while !added && tries < 20 {
|
|
||||||
let x = (room.x1 + rng.roll_dice(1, i32::abs(room.x2 - room.x1))) as usize;
|
|
||||||
let y = (room.y1 + rng.roll_dice(1, i32::abs(room.y2 - room.y1))) as usize;
|
|
||||||
let idx = (y * MAP_WIDTH) + x;
|
|
||||||
|
|
||||||
if !spawn_points.contains_key(&idx) {
|
|
||||||
spawn_points.insert(idx, spawn_table.roll(&mut rng));
|
|
||||||
added = true;
|
|
||||||
} else {
|
|
||||||
tries += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actually spawn stuff
|
spawn_region(ecs, &possible_targets, map_depth);
|
||||||
for spawn in spawn_points.iter() {
|
}
|
||||||
let x = (*spawn.0 % MAP_WIDTH) as i32;
|
|
||||||
let y = (*spawn.0 / MAP_WIDTH) as i32;
|
|
||||||
|
|
||||||
match spawn.1.as_ref() {
|
pub fn spawn_region(ecs: &mut World, area: &[usize], map_depth: i32) {
|
||||||
"Goblin" => goblin(ecs, x, y),
|
let spawn_table = room_table(map_depth);
|
||||||
"Orc" => orc(ecs, x, y),
|
let mut spawn_points: HashMap<usize, String> = HashMap::new();
|
||||||
"Health Potion" => health_potion(ecs, x, y),
|
let mut areas: Vec<usize> = Vec::from(area);
|
||||||
"Fireball Scroll" => fireball_scroll(ecs, x, y),
|
|
||||||
"Confusion Scroll" => confusion_scroll(ecs, x, y),
|
// Scope to keep the borrow checker happy
|
||||||
"Magic Missile Scroll" => magic_missile_scroll(ecs, x, y),
|
{
|
||||||
"Dagger" => dagger(ecs, x, y),
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||||
"Shield" => shield(ecs, x, y),
|
let num_spawns = i32::min(
|
||||||
"Longsword" => longsword(ecs, x, y),
|
areas.len() as i32,
|
||||||
"Tower Shield" => tower_shield(ecs, x, y),
|
rng.roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3,
|
||||||
"Rations" => rations(ecs, x, y),
|
);
|
||||||
"Magic Mapping Scroll" => magic_mapping_scroll(ecs, x, y),
|
|
||||||
"Bear Trap" => bear_trap(ecs, x, y),
|
if num_spawns == 0 {
|
||||||
_ => {}
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _i in 0..num_spawns {
|
||||||
|
let array_index = if areas.len() == 1 {
|
||||||
|
0usize
|
||||||
|
} else {
|
||||||
|
(rng.roll_dice(1, areas.len() as i32) - 1) as usize
|
||||||
|
};
|
||||||
|
let map_idx = areas[array_index];
|
||||||
|
spawn_points.insert(map_idx, spawn_table.roll(&mut rng));
|
||||||
|
areas.remove(array_index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actually spawn the monsters
|
||||||
|
for spawn in spawn_points.iter() {
|
||||||
|
spawn_entity(ecs, &spawn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawns a named entity (name in tuple.1) at the location in (tuple.0)
|
||||||
|
fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) {
|
||||||
|
let x = (*spawn.0 % MAP_WIDTH) as i32;
|
||||||
|
let y = (*spawn.0 / MAP_WIDTH) as i32;
|
||||||
|
|
||||||
|
match spawn.1.as_ref() {
|
||||||
|
"Goblin" => goblin(ecs, x, y),
|
||||||
|
"Orc" => orc(ecs, x, y),
|
||||||
|
"Health Potion" => health_potion(ecs, x, y),
|
||||||
|
"Fireball Scroll" => fireball_scroll(ecs, x, y),
|
||||||
|
"Confusion Scroll" => confusion_scroll(ecs, x, y),
|
||||||
|
"Magic Missile Scroll" => magic_missile_scroll(ecs, x, y),
|
||||||
|
"Dagger" => dagger(ecs, x, y),
|
||||||
|
"Shield" => shield(ecs, x, y),
|
||||||
|
"Longsword" => longsword(ecs, x, y),
|
||||||
|
"Tower Shield" => tower_shield(ecs, x, y),
|
||||||
|
"Rations" => rations(ecs, x, y),
|
||||||
|
"Magic Mapping Scroll" => magic_mapping_scroll(ecs, x, y),
|
||||||
|
"Bear Trap" => bear_trap(ecs, x, y),
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user