cellular automata and bsp interiors
This commit is contained in:
parent
d754aed52a
commit
d96d4881d5
7 changed files with 449 additions and 96 deletions
21
src/map.rs
21
src/map.rs
|
|
@ -42,7 +42,7 @@ impl Map {
|
|||
}
|
||||
|
||||
pub fn new(new_depth: i32) -> Map {
|
||||
Map {
|
||||
let mut map = Map {
|
||||
tiles: vec![TileType::Wall; MAPCOUNT],
|
||||
width: MAPWIDTH as i32,
|
||||
height: MAPHEIGHT as i32,
|
||||
|
|
@ -57,7 +57,25 @@ impl Map {
|
|||
depth: new_depth,
|
||||
bloodstains: HashSet::new(),
|
||||
tile_content: vec![Vec::new(); MAPCOUNT],
|
||||
};
|
||||
|
||||
const MAX_OFFSET: u8 = 32;
|
||||
let mut rng = rltk::RandomNumberGenerator::new();
|
||||
|
||||
for idx in 0..map.red_offset.len() {
|
||||
let roll = rng.roll_dice(1, MAX_OFFSET as i32);
|
||||
map.red_offset[idx] = roll as u8;
|
||||
}
|
||||
for idx in 0..map.green_offset.len() {
|
||||
let roll = rng.roll_dice(1, MAX_OFFSET as i32);
|
||||
map.green_offset[idx] = roll as u8;
|
||||
}
|
||||
for idx in 0..map.blue_offset.len() {
|
||||
let roll = rng.roll_dice(1, MAX_OFFSET as i32);
|
||||
map.blue_offset[idx] = roll as u8;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/// Takes an index, and calculates if it can be entered.
|
||||
|
|
@ -142,6 +160,7 @@ impl BaseMap for Map {
|
|||
pub fn draw_map(map: &Map, ctx: &mut Rltk) {
|
||||
let mut y = 0;
|
||||
let mut x = 0;
|
||||
|
||||
for (idx, tile) in map.tiles.iter().enumerate() {
|
||||
// Get our colour offsets. Credit to Brogue for the inspiration here.
|
||||
let offsets = RGB::from_u8(map.red_offset[idx], map.green_offset[idx], map.blue_offset[idx]);
|
||||
|
|
|
|||
160
src/map_builders/bsp_interior.rs
Normal file
160
src/map_builders/bsp_interior.rs
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
use super::{spawner, Map, MapBuilder, Position, Rect, TileType, SHOW_MAPGEN};
|
||||
use rltk::RandomNumberGenerator;
|
||||
use specs::prelude::*;
|
||||
|
||||
pub struct BspInteriorBuilder {
|
||||
map: Map,
|
||||
starting_position: Position,
|
||||
depth: i32,
|
||||
rooms: Vec<Rect>,
|
||||
history: Vec<Map>,
|
||||
rects: Vec<Rect>,
|
||||
}
|
||||
|
||||
impl MapBuilder for BspInteriorBuilder {
|
||||
fn build_map(&mut self, rng: &mut RandomNumberGenerator) {
|
||||
return self.build(rng);
|
||||
}
|
||||
fn spawn_entities(&mut self, ecs: &mut World) {
|
||||
for room in self.rooms.iter().skip(1) {
|
||||
spawner::spawn_room(ecs, room, self.depth);
|
||||
}
|
||||
}
|
||||
// Getters
|
||||
fn get_map(&mut self) -> Map {
|
||||
return self.map.clone();
|
||||
}
|
||||
fn get_starting_pos(&mut self) -> Position {
|
||||
return self.starting_position.clone();
|
||||
}
|
||||
// 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 BspInteriorBuilder {
|
||||
pub fn new(new_depth: i32) -> BspInteriorBuilder {
|
||||
BspInteriorBuilder {
|
||||
map: Map::new(new_depth),
|
||||
starting_position: Position { x: 0, y: 0 },
|
||||
depth: new_depth,
|
||||
rooms: Vec::new(),
|
||||
history: Vec::new(),
|
||||
rects: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build(&mut self, rng: &mut RandomNumberGenerator) {
|
||||
self.rects.clear();
|
||||
self.rects.push(Rect::new(1, 1, self.map.width - 2, self.map.height - 2)); // Start with a single map-sized rectangle
|
||||
let first_room = self.rects[0];
|
||||
self.add_subrects(first_room, rng); // Divide the first room
|
||||
|
||||
let rooms = self.rects.clone();
|
||||
for r in rooms.iter() {
|
||||
let room = *r;
|
||||
self.rooms.push(room);
|
||||
for y in room.y1..room.y2 {
|
||||
for x in room.x1..room.x2 {
|
||||
let idx = self.map.xy_idx(x, y);
|
||||
if idx > 0 && idx < ((self.map.width * self.map.height) - 1) as usize {
|
||||
self.map.tiles[idx] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.take_snapshot();
|
||||
}
|
||||
|
||||
let start = self.rooms[0].centre();
|
||||
self.starting_position = Position { x: start.0, y: start.1 };
|
||||
|
||||
// Now we want corridors
|
||||
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();
|
||||
}
|
||||
|
||||
// Don't forget the 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;
|
||||
}
|
||||
|
||||
fn add_subrects(&mut self, rect: Rect, rng: &mut RandomNumberGenerator) {
|
||||
const MIN_ROOM_SIZE: i32 = 6;
|
||||
// Remove last rect
|
||||
if !self.rects.is_empty() {
|
||||
self.rects.remove(self.rects.len() - 1);
|
||||
}
|
||||
|
||||
// Calc bounds
|
||||
let w = rect.x2 - rect.x1;
|
||||
let h = rect.y2 - rect.y1;
|
||||
let half_w = w / 2;
|
||||
let half_h = h / 2;
|
||||
|
||||
let split = rng.roll_dice(1, 4);
|
||||
|
||||
if split <= 2 {
|
||||
// Horizontal split
|
||||
let h1 = Rect::new(rect.x1, rect.y1, half_w - 1, h);
|
||||
self.rects.push(h1);
|
||||
if half_w > MIN_ROOM_SIZE {
|
||||
self.add_subrects(h1, rng);
|
||||
}
|
||||
let h2 = Rect::new(rect.x1 + half_w, rect.y1, half_w, h);
|
||||
self.rects.push(h2);
|
||||
if half_w > MIN_ROOM_SIZE {
|
||||
self.add_subrects(h2, rng);
|
||||
}
|
||||
} else {
|
||||
// Vertical split
|
||||
let v1 = Rect::new(rect.x1, rect.y1, w, half_h - 1);
|
||||
self.rects.push(v1);
|
||||
if half_h > MIN_ROOM_SIZE {
|
||||
self.add_subrects(v1, rng);
|
||||
}
|
||||
let v2 = Rect::new(rect.x1, rect.y1 + half_h, w, half_h);
|
||||
self.rects.push(v2);
|
||||
if half_h > MIN_ROOM_SIZE {
|
||||
self.add_subrects(v2, rng);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
171
src/map_builders/cellular_automata.rs
Normal file
171
src/map_builders/cellular_automata.rs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
use super::{spawner, Map, MapBuilder, Position, TileType, SHOW_MAPGEN};
|
||||
use rltk::RandomNumberGenerator;
|
||||
use specs::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const PASSES: i32 = 15;
|
||||
|
||||
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, rng: &mut RandomNumberGenerator) {
|
||||
return self.build(rng);
|
||||
}
|
||||
fn spawn_entities(&mut self, ecs: &mut World) {
|
||||
for area in self.noise_areas.iter() {
|
||||
spawner::spawn_region(ecs, area.1, self.depth);
|
||||
}
|
||||
}
|
||||
// Getters
|
||||
fn get_map(&mut self) -> Map {
|
||||
return self.map.clone();
|
||||
}
|
||||
fn get_starting_pos(&mut self) -> Position {
|
||||
return self.starting_position.clone();
|
||||
}
|
||||
// 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 CellularAutomataBuilder {
|
||||
pub fn new(new_depth: i32) -> CellularAutomataBuilder {
|
||||
CellularAutomataBuilder {
|
||||
map: Map::new(new_depth),
|
||||
starting_position: Position { x: 0, y: 0 },
|
||||
depth: new_depth,
|
||||
history: Vec::new(),
|
||||
noise_areas: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build(&mut self, rng: &mut RandomNumberGenerator) {
|
||||
// Set 55% of map to 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();
|
||||
|
||||
// Iteratively apply cellular automata rules
|
||||
for _i in 0..PASSES {
|
||||
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 neighbours = 0;
|
||||
if self.map.tiles[idx - 1] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
if self.map.tiles[idx + 1] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
if self.map.tiles[idx - self.map.width as usize] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
if self.map.tiles[idx + self.map.width as usize] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
if self.map.tiles[idx - (self.map.width as usize - 1)] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
if self.map.tiles[idx - (self.map.width as usize + 1)] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
if self.map.tiles[idx + (self.map.width as usize - 1)] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
if self.map.tiles[idx + (self.map.width as usize + 1)] == TileType::Wall {
|
||||
neighbours += 1;
|
||||
}
|
||||
|
||||
if neighbours > 4 || neighbours == 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 == std::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();
|
||||
|
||||
self.map.tiles[exit_tile.0] = TileType::DownStair;
|
||||
self.take_snapshot();
|
||||
|
||||
// Build noise map for spawning entities
|
||||
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;
|
||||
|
||||
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,9 +1,9 @@
|
|||
use super::{spawner, Map, Position, Rect, TileType, SHOW_MAPGEN};
|
||||
mod simple_map;
|
||||
use simple_map::SimpleMapBuilder;
|
||||
mod bsp_dungeon;
|
||||
use bsp_dungeon::BspDungeonBuilder;
|
||||
mod bsp_interior;
|
||||
mod cellular_automata;
|
||||
mod common;
|
||||
mod simple_map;
|
||||
use common::*;
|
||||
use rltk::RandomNumberGenerator;
|
||||
use specs::prelude::*;
|
||||
|
|
@ -19,9 +19,11 @@ pub trait MapBuilder {
|
|||
|
||||
pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
||||
let mut rng = rltk::RandomNumberGenerator::new();
|
||||
let builder = rng.roll_dice(1, 2);
|
||||
let builder = rng.roll_dice(1, 4);
|
||||
match builder {
|
||||
1 => Box::new(BspDungeonBuilder::new(new_depth)),
|
||||
_ => Box::new(SimpleMapBuilder::new(new_depth)),
|
||||
1 => Box::new(bsp_dungeon::BspDungeonBuilder::new(new_depth)),
|
||||
2 => Box::new(bsp_interior::BspInteriorBuilder::new(new_depth)),
|
||||
3 => Box::new(cellular_automata::CellularAutomataBuilder::new(new_depth)),
|
||||
_ => Box::new(simple_map::SimpleMapBuilder::new(new_depth)),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,20 +59,6 @@ impl SimpleMapBuilder {
|
|||
const MAX_ROOMS: i32 = 30;
|
||||
const MIN_SIZE: i32 = 6;
|
||||
const MAX_SIZE: i32 = 10;
|
||||
const MAX_OFFSET: u8 = 32;
|
||||
|
||||
for idx in 0..self.map.red_offset.len() {
|
||||
let roll = rng.roll_dice(1, MAX_OFFSET as i32);
|
||||
self.map.red_offset[idx] = roll as u8;
|
||||
}
|
||||
for idx in 0..self.map.green_offset.len() {
|
||||
let roll = rng.roll_dice(1, MAX_OFFSET as i32);
|
||||
self.map.green_offset[idx] = roll as u8;
|
||||
}
|
||||
for idx in 0..self.map.blue_offset.len() {
|
||||
let roll = rng.roll_dice(1, MAX_OFFSET as i32);
|
||||
self.map.blue_offset[idx] = roll as u8;
|
||||
}
|
||||
|
||||
for _i in 0..MAX_ROOMS {
|
||||
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> bool {
|
|||
}
|
||||
|
||||
if map.blocked[destination_idx] {
|
||||
gamelog::Logger::new().append("You can't move there.").log();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -90,7 +91,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> bool {
|
|||
return false;
|
||||
}
|
||||
|
||||
fn get_item(ecs: &mut World) {
|
||||
fn get_item(ecs: &mut World) -> bool {
|
||||
let player_pos = ecs.fetch::<Point>();
|
||||
let player_entity = ecs.fetch::<Entity>();
|
||||
let entities = ecs.entities();
|
||||
|
|
@ -105,12 +106,16 @@ fn get_item(ecs: &mut World) {
|
|||
}
|
||||
|
||||
match target_item {
|
||||
None => gamelog::Logger::new().append("There is nothing to pick up.").log(),
|
||||
None => {
|
||||
gamelog::Logger::new().append("There is nothing to pick up.").log();
|
||||
return false;
|
||||
}
|
||||
Some(item) => {
|
||||
let mut pickup = ecs.write_storage::<WantsToPickupItem>();
|
||||
pickup
|
||||
.insert(*player_entity, WantsToPickupItem { collected_by: *player_entity, item })
|
||||
.expect("Unable to insert want to pickup item.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -147,15 +152,17 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
|||
}
|
||||
return RunState::NextLevel; // > to descend
|
||||
} else {
|
||||
return skip_turn(&mut gs.ecs); // (Wait a turn)
|
||||
result = skip_turn(&mut gs.ecs); // (Wait a turn)
|
||||
}
|
||||
}
|
||||
VirtualKeyCode::NumpadDecimal => {
|
||||
return skip_turn(&mut gs.ecs);
|
||||
result = skip_turn(&mut gs.ecs);
|
||||
}
|
||||
|
||||
// Items
|
||||
VirtualKeyCode::G => get_item(&mut gs.ecs),
|
||||
VirtualKeyCode::G => {
|
||||
result = get_item(&mut gs.ecs);
|
||||
}
|
||||
VirtualKeyCode::I => return RunState::ShowInventory,
|
||||
VirtualKeyCode::D => return RunState::ShowDropItem,
|
||||
VirtualKeyCode::R => return RunState::ShowRemoveItem,
|
||||
|
|
@ -184,7 +191,7 @@ pub fn try_next_level(ecs: &mut World) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
fn skip_turn(ecs: &mut World) -> RunState {
|
||||
fn skip_turn(ecs: &mut World) -> bool {
|
||||
let player_entity = ecs.fetch::<Entity>();
|
||||
let viewshed_components = ecs.read_storage::<Viewshed>();
|
||||
let monsters = ecs.read_storage::<Monster>();
|
||||
|
|
@ -237,7 +244,7 @@ fn skip_turn(ecs: &mut World) -> RunState {
|
|||
} else {
|
||||
gamelog::Logger::new().append("You wait a turn.").log();
|
||||
}
|
||||
return RunState::PlayerTurn;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Playing around with autoexplore, without having read how to do it.
|
||||
|
|
|
|||
144
src/spawner.rs
144
src/spawner.rs
|
|
@ -1,8 +1,8 @@
|
|||
use super::{
|
||||
random_table::RandomTable, BlocksTile, CombatStats, Confusion, Consumable, Cursed, DefenceBonus, Destructible,
|
||||
EntryTrigger, EquipmentSlot, Equippable, Hidden, HungerClock, HungerState, InflictsDamage, Item, MagicMapper,
|
||||
EntryTrigger, EquipmentSlot, Equippable, Hidden, HungerClock, HungerState, InflictsDamage, Item, MagicMapper, Map,
|
||||
MeleePowerBonus, Mind, Monster, Name, Player, Position, ProvidesHealing, ProvidesNutrition, Ranged, Rect,
|
||||
Renderable, SerializeMe, SingleActivation, Viewshed, Wand, AOE, MAPWIDTH,
|
||||
Renderable, SerializeMe, SingleActivation, TileType, Viewshed, Wand, AOE, MAPWIDTH,
|
||||
};
|
||||
use rltk::{console, RandomNumberGenerator, RGB};
|
||||
use specs::prelude::*;
|
||||
|
|
@ -73,80 +73,88 @@ const MAX_ENTITIES: i32 = 4;
|
|||
|
||||
#[allow(clippy::map_entry)]
|
||||
pub fn spawn_room(ecs: &mut World, room: &Rect, map_depth: i32) {
|
||||
let mut spawn_points: HashMap<usize, String> = HashMap::new();
|
||||
|
||||
// Scope for borrow checker
|
||||
let mut possible_targets: Vec<usize> = Vec::new();
|
||||
{
|
||||
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||
let num_spawns = rng.roll_dice(1, MAX_ENTITIES + 2) - 2;
|
||||
console::log(format!("room of: {}", num_spawns));
|
||||
// [-1, 0, 1, 2, 3, 4] things in a room
|
||||
// 2/6 chance for nothing, 4/6 for something
|
||||
|
||||
for _i in 0..num_spawns {
|
||||
let mut added = false;
|
||||
let mut tries = 0;
|
||||
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 * MAPWIDTH) + x;
|
||||
if !spawn_points.contains_key(&idx) {
|
||||
let category = category_table().roll(&mut rng);
|
||||
let spawn_table;
|
||||
match category.as_ref() {
|
||||
"mob" => spawn_table = mob_table(map_depth),
|
||||
"item" => spawn_table = item_table(map_depth),
|
||||
"food" => spawn_table = food_table(map_depth),
|
||||
"trap" => spawn_table = trap_table(map_depth),
|
||||
_ => spawn_table = debug_table(),
|
||||
}
|
||||
spawn_points.insert(idx, spawn_table.roll(&mut rng));
|
||||
added = true;
|
||||
console::log(format!("added spawnpoint"));
|
||||
} else {
|
||||
tries += 1;
|
||||
console::log(format!("failed {} times", tries));
|
||||
let map = ecs.fetch::<Map>();
|
||||
for y in room.y1 + 1..room.y2 {
|
||||
for x in room.x1 + 1..room.x2 {
|
||||
let idx = map.xy_idx(x, y);
|
||||
if map.tiles[idx] == TileType::Floor {
|
||||
possible_targets.push(idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Spawn
|
||||
for spawn in spawn_points.iter() {
|
||||
let x = (*spawn.0 % MAPWIDTH) as i32;
|
||||
let y = (*spawn.0 / MAPWIDTH) as i32;
|
||||
spawn_region(ecs, &possible_targets, map_depth);
|
||||
}
|
||||
|
||||
match spawn.1.as_ref() {
|
||||
// Monsters
|
||||
"goblin" => goblin(ecs, x, y),
|
||||
"goblin chieftain" => goblin_chieftain(ecs, x, y),
|
||||
"orc" => orc(ecs, x, y),
|
||||
// Equipment
|
||||
"dagger" => dagger(ecs, x, y),
|
||||
"shortsword" => shortsword(ecs, x, y),
|
||||
"buckler" => buckler(ecs, x, y),
|
||||
"shield" => shield(ecs, x, y),
|
||||
// Potions
|
||||
"weak health potion" => weak_health_potion(ecs, x, y),
|
||||
"health potion" => health_potion(ecs, x, y),
|
||||
// Scrolls
|
||||
"fireball scroll" => fireball_scroll(ecs, x, y),
|
||||
"cursed fireball scroll" => cursed_fireball_scroll(ecs, x, y),
|
||||
"confusion scroll" => confusion_scroll(ecs, x, y),
|
||||
"magic missile scroll" => magic_missile_scroll(ecs, x, y),
|
||||
"magic map scroll" => magic_map_scroll(ecs, x, y),
|
||||
"cursed magic map scroll" => cursed_magic_map_scroll(ecs, x, y),
|
||||
// Wands
|
||||
"magic missile wand" => magic_missile_wand(ecs, x, y),
|
||||
"fireball wand" => fireball_wand(ecs, x, y),
|
||||
"confusion wand" => confusion_wand(ecs, x, y),
|
||||
// Food
|
||||
"rations" => rations(ecs, x, y),
|
||||
// Traps
|
||||
"bear trap" => bear_trap(ecs, x, y),
|
||||
"confusion trap" => confusion_trap(ecs, x, y),
|
||||
_ => console::log("Tried to spawn nothing. Bugfix needed!"),
|
||||
pub fn spawn_region(ecs: &mut World, area: &[usize], map_depth: i32) {
|
||||
let mut spawn_points: HashMap<usize, String> = HashMap::new();
|
||||
let mut areas: Vec<usize> = Vec::from(area);
|
||||
{
|
||||
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||
let category = category_table().roll(&mut rng);
|
||||
let spawn_table;
|
||||
match category.as_ref() {
|
||||
"mob" => spawn_table = mob_table(map_depth),
|
||||
"item" => spawn_table = item_table(map_depth),
|
||||
"food" => spawn_table = food_table(map_depth),
|
||||
"trap" => spawn_table = trap_table(map_depth),
|
||||
_ => spawn_table = debug_table(),
|
||||
}
|
||||
let num_spawns = i32::min(areas.len() as i32, rng.roll_dice(1, MAX_ENTITIES + 2) - 2);
|
||||
if num_spawns <= 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
for _i in 0..num_spawns {
|
||||
let array_idx = if areas.len() == 1 { 0usize } else { (rng.roll_dice(1, areas.len() as i32) - 1) as usize };
|
||||
let map_idx = areas[array_idx];
|
||||
spawn_points.insert(map_idx, spawn_table.roll(&mut rng));
|
||||
areas.remove(array_idx);
|
||||
}
|
||||
}
|
||||
|
||||
for spawn in spawn_points.iter() {
|
||||
spawn_entity(ecs, &spawn);
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) {
|
||||
let x = (*spawn.0 % MAPWIDTH) as i32;
|
||||
let y = (*spawn.0 / MAPWIDTH) as i32;
|
||||
|
||||
match spawn.1.as_ref() {
|
||||
// Monsters
|
||||
"goblin" => goblin(ecs, x, y),
|
||||
"goblin chieftain" => goblin_chieftain(ecs, x, y),
|
||||
"orc" => orc(ecs, x, y),
|
||||
// Equipment
|
||||
"dagger" => dagger(ecs, x, y),
|
||||
"shortsword" => shortsword(ecs, x, y),
|
||||
"buckler" => buckler(ecs, x, y),
|
||||
"shield" => shield(ecs, x, y),
|
||||
// Potions
|
||||
"weak health potion" => weak_health_potion(ecs, x, y),
|
||||
"health potion" => health_potion(ecs, x, y),
|
||||
// Scrolls
|
||||
"fireball scroll" => fireball_scroll(ecs, x, y),
|
||||
"cursed fireball scroll" => cursed_fireball_scroll(ecs, x, y),
|
||||
"confusion scroll" => confusion_scroll(ecs, x, y),
|
||||
"magic missile scroll" => magic_missile_scroll(ecs, x, y),
|
||||
"magic map scroll" => magic_map_scroll(ecs, x, y),
|
||||
"cursed magic map scroll" => cursed_magic_map_scroll(ecs, x, y),
|
||||
// Wands
|
||||
"magic missile wand" => magic_missile_wand(ecs, x, y),
|
||||
"fireball wand" => fireball_wand(ecs, x, y),
|
||||
"confusion wand" => confusion_wand(ecs, x, y),
|
||||
// Food
|
||||
"rations" => rations(ecs, x, y),
|
||||
// Traps
|
||||
"bear trap" => bear_trap(ecs, x, y),
|
||||
"confusion trap" => confusion_trap(ecs, x, y),
|
||||
_ => console::log("Tried to spawn nothing. Bugfix needed!"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue