first version of the level new generator

monsters and artefact are missing
roomsize is fixed
This commit is contained in:
Joachim Lusiardi 2024-10-29 06:48:45 +01:00
parent 74831857cf
commit 7f288dbcd1
4 changed files with 504 additions and 350 deletions

View File

@ -1,11 +1,12 @@
use std::{collections::HashMap, ops::RangeInclusive}; use std::{collections::HashMap, ops::RangeInclusive};
use crate::monster::MonsterTypes; use crate::{monster::MonsterTypes, room::RoomType};
/// the number of rooms in vertical direction /// the number of rooms in vertical direction
pub const ROOMS_VERTICAL: usize = 7; pub const ROOMS_HORIZONAL: usize = 8;
/// the number of rooms in horizontal direction /// the number of rooms in horizontal direction
pub const ROOMS_HORIZONTAL: usize = 7; pub const ROOMS_VERTICAL: usize = 7;
/// the width of a room in the grid of rooms (number of characters) /// the width of a room in the grid of rooms (number of characters)
pub const ROOM_WIDTH: usize = 9; pub const ROOM_WIDTH: usize = 9;
@ -13,7 +14,7 @@ pub const ROOM_WIDTH: usize = 9;
pub const ROOM_HEIGHT: usize = 6; pub const ROOM_HEIGHT: usize = 6;
/// How many levels does the dungeon have? /// How many levels does the dungeon have?
pub const LEVELS: usize = 3; pub const LEVELS: usize = 2;
/// length of a game frame in ms /// length of a game frame in ms
pub const FRAME_LENGTH: u64 = 100; pub const FRAME_LENGTH: u64 = 100;
@ -24,33 +25,89 @@ pub const MIN_WIDTH: u16 = 120;
pub const MIN_HEIGHT: u16 = LEVEL_HEIGHT as u16; pub const MIN_HEIGHT: u16 = LEVEL_HEIGHT as u16;
/// the calculated width of a level /// the calculated width of a level
pub const LEVEL_WIDTH: usize = 1 + ROOMS_VERTICAL * ROOM_WIDTH; pub const LEVEL_WIDTH: usize = 1 + ROOMS_HORIZONAL * ROOM_WIDTH;
/// the calculated height of a level /// the calculated height of a level
pub const LEVEL_HEIGHT: usize = 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT; pub const LEVEL_HEIGHT: usize = 1 + ROOMS_VERTICAL * ROOM_HEIGHT;
pub fn get_monsters_per_level() -> Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> { pub fn get_monsters_per_level() -> Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> {
let tmp =[ let tmp = [
// level 1 // level 1
vec![(MonsterTypes::Rat, 50), (MonsterTypes::Spider, 50)], vec![(MonsterTypes::Rat, 50), (MonsterTypes::Spider, 50)],
// level 2 // level 2
vec![(MonsterTypes::Rat, 50), (MonsterTypes::Snake, 50)], vec![(MonsterTypes::Rat, 50), (MonsterTypes::Snake, 50)],
// level 3 // level 3
vec![(MonsterTypes::Orc, 34), (MonsterTypes::Skeleton, 33), (MonsterTypes::Snake, 33)], vec![
(MonsterTypes::Orc, 34),
(MonsterTypes::Skeleton, 33),
(MonsterTypes::Snake, 33),
],
]; ];
if tmp.len() != LEVELS { if tmp.len() < LEVELS {
panic!("Only {} monster sets defined for {} levels!", tmp.len(), LEVELS); panic!(
"Only {} monster sets defined for {} levels!",
tmp.len(),
LEVELS
);
} }
let mut result: Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> = vec![]; let mut result: Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> = vec![];
for (idx, level) in tmp.iter().enumerate() { for (idx, level) in tmp.iter().enumerate() {
let mut sum = 0; let mut sum = 0;
let mut map: HashMap<MonsterTypes, RangeInclusive<u8>> = HashMap::new(); let mut map: HashMap<MonsterTypes, RangeInclusive<u8>> = HashMap::new();
for monster in level { for monster in level {
map.insert(monster.0, RangeInclusive::new(sum+1, sum+monster.1)); map.insert(monster.0, RangeInclusive::new(sum + 1, sum + monster.1));
sum += monster.1; sum += monster.1;
} }
if sum != 100 { if sum != 100 {
panic!("all percentages must add to 100 (was {}) per level, error in level {}!", sum, idx+1); panic!(
"all percentages must add to 100 (was {}) per level, error in level {}!",
sum,
idx + 1
);
}
result.push(map);
}
result
}
pub fn get_room_type_per_level() -> Vec<HashMap<RoomType, std::ops::RangeInclusive<u8>>> {
let tmp = [
// level 1
vec![
(RoomType::EmptyRoom, 75),
(RoomType::MonsterRoom, 5),
(RoomType::BasicRoom, 20),
],
// level 2
vec![
(RoomType::EmptyRoom, 50),
(RoomType::BasicRoom, 25),
(RoomType::MonsterRoom, 12),
(RoomType::ArtifactRoom, 13),
],
// level 3
vec![(RoomType::EmptyRoom, 50), (RoomType::MonsterRoom, 50)],
];
if tmp.len() < LEVELS {
panic!(
"Only {} room sets defined for {} levels!",
tmp.len(),
LEVELS
);
}
let mut result: Vec<HashMap<RoomType, std::ops::RangeInclusive<u8>>> = vec![];
for (idx, level) in tmp.iter().enumerate() {
let mut sum = 0;
let mut map: HashMap<RoomType, RangeInclusive<u8>> = HashMap::new();
for room in level {
map.insert(room.0, RangeInclusive::new(sum + 1, sum + room.1));
sum += room.1;
}
if sum != 100 {
panic!(
"all percentages must add to 100 (was {}) per level, error in level {}!",
sum,
idx + 1
);
} }
result.push(map); result.push(map);
} }

View File

@ -1,393 +1,256 @@
use std::cmp::{max, min};
use std::ops::Range;
use petgraph::algo::min_spanning_tree; use petgraph::algo::min_spanning_tree;
use petgraph::data::*; use petgraph::data::*;
use petgraph::graph::Graph; use petgraph::graph::Graph;
use petgraph::graph::UnGraph; use petgraph::graph::UnGraph;
use rand::prelude::SliceRandom; use std::cmp::max;
use rand::rngs::ThreadRng; use std::cmp::min;
use std::ops::Range;
use rand::Rng; use rand::Rng;
use rand::{rngs::ThreadRng, seq::SliceRandom};
use crate::artifacts::{Artifact, Chest, Potion}; use crate::constants::ROOM_HEIGHT;
use crate::constants::{ use crate::constants::ROOM_WIDTH;
get_monsters_per_level, ROOMS_HORIZONTAL, ROOMS_VERTICAL, ROOM_HEIGHT, ROOM_WIDTH, use crate::room::Connection;
use crate::{
constants::{
get_room_type_per_level, LEVEL_HEIGHT, LEVEL_WIDTH, ROOMS_HORIZONAL, ROOMS_VERTICAL,
},
level::{Level, StructureElement},
room::{Room, RoomType},
}; };
use crate::level::{Level, StructureElement};
use crate::monster::{create_monster_by_type, Monster, Orc, Rat, Snake};
use crate::position::Position;
#[derive(PartialEq, Copy, Clone)]
enum RoomType {
Start,
End,
StairUp,
StairDown,
BasicRoom,
ArtifactRoom,
MonsterRoom,
EmptyRoom,
}
#[derive(Copy, Clone)]
struct ConnectionInfo {
offset: usize,
distance: usize,
}
#[derive(Copy, Clone)]
struct Room {
pub kind: RoomType,
pub offset_x: usize,
pub offset_y: usize,
pub width: usize,
pub height: usize,
pub connection_down: Option<ConnectionInfo>,
pub connection_right: Option<ConnectionInfo>,
}
impl Room {
fn new() -> Self {
Self {
kind: RoomType::EmptyRoom,
offset_x: 0,
offset_y: 0,
width: 0,
height: 0,
connection_down: None,
connection_right: None,
}
}
/// change the size and position of a room randomly within its bounds
fn random(&mut self, rng: &mut ThreadRng) {
let width = rng.gen_range(3..6);
let height = rng.gen_range(3..5);
self.width = width;
self.height = height;
self.offset_x = rng.gen_range(0..(ROOM_WIDTH - width));
self.offset_y = rng.gen_range(0..(ROOM_HEIGHT - height));
}
fn get_x_range(&self) -> Range<usize> {
self.offset_x..self.offset_x + self.width
}
fn get_y_range(&self) -> Range<usize> {
self.offset_y..self.offset_y + self.height
}
}
pub struct LevelGenerator { pub struct LevelGenerator {
rooms: [[Room; ROOMS_HORIZONTAL]; ROOMS_VERTICAL],
level: usize, level: usize,
rooms: [[Room; ROOMS_VERTICAL]; ROOMS_HORIZONAL],
}
enum Direction {
Horizontal,
Vertical,
} }
impl LevelGenerator { impl LevelGenerator {
pub fn generate(level: usize, first: bool, last: bool) -> Self { pub fn generate(level: usize, first: bool, last: bool) -> Self {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut rooms = [[Room::new(); ROOMS_HORIZONTAL]; ROOMS_VERTICAL]; let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONAL];
let mut graph = UnGraph::<(usize, usize), u16>::default();
// trick the room_connectable function into failing on the first iteration /*
rooms[0][0].kind = RoomType::BasicRoom; Fill grid with unconnected rooms
*/
while !LevelGenerator::rooms_connectable(&rooms) { let mut rooms_to_place: Vec<Room> = Vec::with_capacity(ROOMS_VERTICAL * ROOMS_HORIZONAL);
let mut room_types: Vec<RoomType> =
Vec::with_capacity(ROOMS_HORIZONTAL * ROOMS_VERTICAL); let mut start_room = Room::new(&mut rng);
// level 0 contains a start room, all others contain a stair up
if first { if first {
room_types.push(RoomType::Start); start_room.kind = RoomType::Start;
} else { } else {
room_types.push(RoomType::StairUp); start_room.kind = RoomType::StairUp;
} }
// level 24 (the last) contains an end room, all others a stair down rooms_to_place.push(start_room);
let mut end_room = Room::new(&mut rng);
if last { if last {
room_types.push(RoomType::End); end_room.kind = RoomType::End;
} else { } else {
room_types.push(RoomType::StairDown); end_room.kind = RoomType::StairDown;
} }
room_types.push(RoomType::MonsterRoom); rooms_to_place.push(end_room);
room_types.push(RoomType::ArtifactRoom); for _ in 2..ROOMS_HORIZONAL * ROOMS_VERTICAL {
// generate a random set of rooms and shuffle them let mut room = Room::new(&mut rng);
for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL { room.kind = LevelGenerator::select_room_type(level, &mut rng);
match rng.gen_range(1..=100) {
// TODO tune room type distribution
1..=33 => room_types.push(RoomType::EmptyRoom),
34..=66 => room_types.push(RoomType::ArtifactRoom),
67..=90 => room_types.push(RoomType::MonsterRoom),
_ => room_types.push(RoomType::BasicRoom),
}
}
room_types.shuffle(&mut rng);
graph.clear();
// place the rooms in the array an add nodes to the graph for every non empty room
for (c, rs) in rooms.iter_mut().enumerate().take(ROOMS_VERTICAL) {
for (r, room) in rs.iter_mut().enumerate().take(ROOMS_HORIZONTAL) {
room.kind = room_types.pop().unwrap();
if room.kind != RoomType::EmptyRoom { if room.kind != RoomType::EmptyRoom {
room.random(&mut rng); rooms_to_place.push(room);
graph.add_node((c, r));
} }
} }
let mut room_row = rng.gen_range(1..ROOMS_VERTICAL);
let mut room_col = rng.gen_range(1..ROOMS_HORIZONAL);
rooms[room_col][room_row] = rooms_to_place.pop().unwrap();
while let Some(room) = rooms_to_place.pop() {
let mut directions_to_try = vec![Direction::Horizontal, Direction::Vertical];
directions_to_try.shuffle(&mut rng);
while !directions_to_try.is_empty() {
match directions_to_try.pop().unwrap() {
Direction::Horizontal => {
let mut free_cols: Vec<usize> = vec![];
for col in 0..ROOMS_HORIZONAL {
if rooms[col][room_row].kind == RoomType::EmptyRoom {
free_cols.push(col);
} }
} }
if free_cols.is_empty() {
// add edges to the graph connecting each room to all of its neighbours (max 4 of them)
for c in 0..ROOMS_VERTICAL {
for r in 0..ROOMS_HORIZONTAL {
if rooms[c][r].kind == RoomType::EmptyRoom {
continue; continue;
} }
let src_index = graph.node_indices().find(|i| graph[*i] == (c, r)).unwrap(); free_cols.shuffle(&mut rng);
for r_1 in r + 1..ROOMS_HORIZONTAL { room_col = *free_cols.first().unwrap();
if rooms[c][r_1].kind != RoomType::EmptyRoom { rooms[room_col][room_row] = room;
let tgt_index = graph
.node_indices()
.find(|i| graph[*i] == (c, r_1))
.unwrap();
// todo use random weight for edge
graph.add_edge(src_index, tgt_index, 1);
break; break;
} }
Direction::Vertical => {
let mut free_rows: Vec<usize> = vec![];
for row in 0..ROOMS_VERTICAL {
if rooms[room_col][row].kind == RoomType::EmptyRoom {
free_rows.push(row);
} }
for (c_1, rs) in rooms.iter().enumerate().take(ROOMS_VERTICAL).skip(c + 1) { }
if rs[r].kind != RoomType::EmptyRoom { if free_rows.is_empty() {
let tgt_index = graph continue;
.node_indices() }
.find(|i| graph[*i] == (c_1, r)) free_rows.shuffle(&mut rng);
.unwrap(); room_row = *free_rows.first().unwrap();
// todo use random weight for edge rooms[room_col][room_row] = room;
graph.add_edge(src_index, tgt_index, 1);
break; break;
} }
} }
} }
} }
// calculate a minimum spanning tree
println!(" 0 1 2 3 4 5 6 7");
for r in 0..ROOMS_VERTICAL {
print!("{} ", r);
for c in 0..ROOMS_HORIZONAL {
match rooms[c][r].kind {
RoomType::Start => print!("S "),
RoomType::End => print!("E "),
RoomType::StairUp => print!("< "),
RoomType::StairDown => print!("> "),
RoomType::BasicRoom => print!("B "),
RoomType::ArtifactRoom => print!("A "),
RoomType::MonsterRoom => print!("M "),
RoomType::EmptyRoom => print!(" "),
};
}
println!();
}
/*
Construct a graph from the unconnected rooms and make a minum spanning tree of it
*/
let mut graph = UnGraph::<(usize, usize), u16>::default();
// collect nodes
for col in 0..ROOMS_HORIZONAL {
for row in 0..ROOMS_VERTICAL {
if rooms[col][row].kind != RoomType::EmptyRoom {
graph.add_node((col, row));
}
}
}
// collect edges between nodes, each right and down till we find the next room
for col in 0..ROOMS_HORIZONAL {
for row in 0..ROOMS_VERTICAL {
if rooms[col][row].kind == RoomType::EmptyRoom {
continue;
}
if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) {
for col_1 in col + 1..ROOMS_HORIZONAL {
if rooms[col_1][row].kind != RoomType::EmptyRoom {
if let Some(tgt_index) =
graph.node_indices().find(|i| graph[*i] == (col_1, row))
{
graph.add_edge(src_index, tgt_index, 1);
break;
}
}
}
for row_1 in row + 1..ROOMS_VERTICAL {
if rooms[col][row_1].kind != RoomType::EmptyRoom {
if let Some(tgt_index) =
graph.node_indices().find(|i| graph[*i] == (col, row_1))
{
graph.add_edge(src_index, tgt_index, 1);
break;
}
}
}
}
}
}
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = let mst: Graph<(usize, usize), u16, petgraph::Undirected> =
Graph::from_elements(min_spanning_tree(&graph)); Graph::from_elements(min_spanning_tree(&graph));
for edge in mst.raw_edges() { for edge in mst.raw_edges() {
let src = mst[edge.source()]; // the tuples are (col, row)
let tgt = mst[edge.target()]; let (src_node_col, src_node_row) = mst[edge.source()];
let (tgt_node_col, tgt_node_row) = mst[edge.target()];
// println!("MST Edge from {:?} to {:?}", (src_node_col, src_node_row), (tgt_node_col, tgt_node_row));
let src_room = rooms[src.0][src.1]; if src_node_col == tgt_node_col {
let mut tgt_room = rooms[tgt.0][tgt.1]; // println!("Down");
let start_col = src_node_col * ROOM_WIDTH+ROOM_WIDTH/2;
// cols are the same, either up or down let start_row = src_node_row * ROOM_HEIGHT+ROOM_HEIGHT;
if src.0 == tgt.0 { let end_col = tgt_node_col * ROOM_WIDTH+ROOM_WIDTH/2;
let range = let end_row = tgt_node_row * ROOM_HEIGHT;
LevelGenerator::range_overlap(src_room.get_x_range(), tgt_room.get_x_range()); rooms[src_node_col][src_node_row].connection_down = Some(Connection {
let position: usize = if range.is_empty() { start_pos: (start_col, start_row),
range.start end_pos: (end_col, end_row),
} else {
rng.gen_range(range)
};
if src.1 < tgt.1 {
// src to tgt
rooms[src.0][src.1].connection_down = Some(ConnectionInfo {
offset: position,
distance: tgt.1 - src.1,
}); });
} else { } else {
// tgt to src // println!("Right");
tgt_room.connection_down = Some(ConnectionInfo { let start_col = src_node_col * ROOM_WIDTH+ROOM_WIDTH;
offset: position, let start_row = src_node_row * ROOM_HEIGHT+ROOM_HEIGHT/2;
distance: src.1 - tgt.1, let end_col = tgt_node_col * ROOM_WIDTH;
}); let end_row = tgt_node_row * ROOM_HEIGHT+ROOM_HEIGHT/2;
} rooms[src_node_col][src_node_row].connection_right = Some(Connection {
} start_pos: (start_col, start_row),
// rows are the same, either left or right end_pos: (end_col, end_row),
if src.1 == tgt.1 {
let range =
LevelGenerator::range_overlap(src_room.get_y_range(), tgt_room.get_y_range());
let mut position: usize;
if range.is_empty() {
position = range.start;
} else {
position = rng.gen_range(range);
}
if src.1 == 0 && position == 0 {
position = 1;
}
if src.0 < tgt.0 {
// src to tgt
rooms[src.0][src.1].connection_right = Some(ConnectionInfo {
offset: position,
distance: tgt.0 - src.0,
});
} else {
// tgt to src
tgt_room.connection_right = Some(ConnectionInfo {
offset: position,
distance: src.1 - tgt.1,
}); });
} }
} }
LevelGenerator { level, rooms }
} }
LevelGenerator { rooms, level } fn select_room_type(level: usize, rng: &mut ThreadRng) -> RoomType {
}
fn range_overlap(r1: Range<usize>, r2: Range<usize>) -> Range<usize> {
max(r1.start, r2.start)..min(r1.end, r2.end)
}
/// Verifies that for a given matrix of rooms each room has at least one other room in the
/// same row or column.
fn rooms_connectable(rooms: &[[Room; ROOMS_HORIZONTAL]; ROOMS_VERTICAL]) -> bool {
for c in 0..ROOMS_VERTICAL {
for r in 0..ROOMS_HORIZONTAL {
if rooms[c][r].kind != RoomType::EmptyRoom {
let mut connected = 0;
for room in rooms.iter().take(ROOMS_VERTICAL) {
if room[r].kind != RoomType::EmptyRoom {
connected += 1;
}
}
for r1 in 0..ROOMS_HORIZONTAL {
if rooms[c][r1].kind != RoomType::EmptyRoom {
connected += 1;
}
}
if connected <= 2 {
return false;
}
}
}
}
true
}
fn select_monster(position: Position, rng: &mut ThreadRng) -> Box<dyn Monster> {
let level = position.get_level();
let value = rng.gen_range(1..=100); let value = rng.gen_range(1..=100);
let t = get_monsters_per_level(); let t = get_room_type_per_level();
if level < t.len() { if level < t.len() {
for (mtype, range) in &t[level] { for (mtype, range) in &t[level] {
if range.contains(&value) { if range.contains(&value) {
return create_monster_by_type(mtype, position); return *mtype;
} }
} }
} }
match rng.gen_range(1..=100) { panic!("no room selectable!");
1..=30 => Box::new(Orc::new_with_position(position)),
31..=60 => Box::new(Snake::new_with_position(position)),
_ => Box::new(Rat::new_with_position(position)),
}
} }
pub fn render(&self) -> Level { pub fn render(&mut self) -> Level {
let mut rng = rand::thread_rng(); let mut structure = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
let mut structure = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; let mut start_pos = (0, 0);
1 + ROOMS_VERTICAL * ROOM_WIDTH]; let mut end_pos = (0, 0);
let mut artifacts: Vec<Box<dyn Artifact>> = Vec::with_capacity(10); for col in 0..ROOMS_HORIZONAL {
let mut enemies: Vec<Box<dyn Monster>> = Vec::with_capacity(10); for row in 0..ROOMS_VERTICAL {
let mut start_x: usize = 0; let position = self.rooms[col][row].render(&mut structure, col, row);
let mut start_y: usize = 0; if self.rooms[col][row].kind == RoomType::Start
let mut end_x: usize = 0; || self.rooms[col][row].kind == RoomType::StairUp
let mut end_y: usize = 0; {
for c in 0..ROOMS_VERTICAL { start_pos = position;
for r in 0..ROOMS_HORIZONTAL {
let top = 1 + r * ROOM_HEIGHT;
let left = 1 + c * ROOM_WIDTH;
let room = self.rooms[c][r];
for x in 0..room.width {
for y in 0..room.height {
structure[left + room.offset_x + x][top + room.offset_y + y] =
StructureElement::Floor;
} }
} if self.rooms[col][row].kind == RoomType::End
if room.kind == RoomType::ArtifactRoom { || self.rooms[col][row].kind == RoomType::StairDown
let t_x = left + room.offset_x + rng.gen_range(0..room.width); {
let t_y = top + room.offset_y + rng.gen_range(0..room.height); end_pos = position;
// TODO randomize artifacts
match rng.gen_range(1..=100) {
1..=50 => {
artifacts
.push(Box::new(Chest::new(Position::new(self.level, t_x, t_y))));
}
_ => {
artifacts
.push(Box::new(Potion::new(Position::new(self.level, t_x, t_y))));
}
};
}
if room.kind == RoomType::MonsterRoom {
let t_x = left + room.offset_x + rng.gen_range(0..room.width);
let t_y = top + room.offset_y + rng.gen_range(0..room.height);
// TODO randomize enemies here
enemies.push(LevelGenerator::select_monster(
Position::new(self.level, t_x, t_y),
&mut rng,
));
}
if room.kind == RoomType::End || room.kind == RoomType::StairDown {
end_x = left + room.offset_x + rng.gen_range(0..room.width);
end_y = top + room.offset_y + rng.gen_range(0..room.height);
}
if room.kind == RoomType::StairDown {
structure[end_x][end_y] = StructureElement::StairDown;
}
if room.kind == RoomType::End {
structure[end_x][end_y] = StructureElement::End;
}
if room.kind == RoomType::Start || room.kind == RoomType::StairUp {
start_x = left + room.offset_x + rng.gen_range(0..room.width);
start_y = top + room.offset_y + rng.gen_range(0..room.height);
}
if room.kind == RoomType::StairUp {
structure[start_x][start_y] = StructureElement::StairUp;
}
if room.kind == RoomType::Start {
structure[start_x][start_y] = StructureElement::Start;
} }
} }
} }
// for col in 0..ROOMS_HORIZONAL {
for c in 0..ROOMS_VERTICAL { for row in 0..ROOMS_VERTICAL {
for r in 0..ROOMS_HORIZONTAL { if let Some(connection) = self.rooms[col][row].connection_down {
let src_room = self.rooms[c][r]; // println!("down");
if let Some(x_conn) = src_room.connection_down { connection.render(&mut structure);
let tgt_room = self.rooms[c][r + x_conn.distance];
let top = 1 + r * ROOM_HEIGHT + src_room.offset_y;
let left = 1 + c * ROOM_WIDTH + x_conn.offset;
let bottom = 1
+ (r + x_conn.distance) * ROOM_HEIGHT
+ tgt_room.offset_y
+ tgt_room.height;
for i in top..bottom {
if structure[left][i] == StructureElement::Wall {
structure[left][i] = StructureElement::Floor;
}
}
}
if let Some(y_conn) = src_room.connection_right {
let tgt_room = self.rooms[c + y_conn.distance][r];
let top = 1 + r * ROOM_HEIGHT + src_room.offset_y + y_conn.offset - 1;
let left = 1 + c * ROOM_WIDTH + src_room.offset_x;
let right =
1 + (c + y_conn.distance) * ROOM_WIDTH + tgt_room.offset_x + tgt_room.width;
for room in structure.iter_mut().take(right).skip(left) {
if room[top] == StructureElement::Wall {
room[top] = StructureElement::Floor;
}
} }
if let Some(connection) = self.rooms[col][row].connection_right {
// println!("right");
connection.render(&mut structure);
} }
} }
} }
Level { Level {
level: self.level, level: self.level,
structure, structure,
discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
1 + ROOMS_VERTICAL * ROOM_WIDTH], monsters: vec![],
monsters: enemies, artifacts: vec![],
artifacts, start: start_pos,
start: (start_x, start_y), end: end_pos,
end: (end_x, end_y),
rng: rand::thread_rng(), rng: rand::thread_rng(),
} }
} }
@ -395,11 +258,15 @@ impl LevelGenerator {
#[test] #[test]
fn test_level_gen() { fn test_level_gen() {
let level = LevelGenerator::generate(23, false, false).render(); for _ in 0..1000 {
assert_eq!(level.level, 23); LevelGenerator::generate(0, true, false).render();
assert_ne!(level.start, (0, 0)); }
assert_ne!(level.end, (0, 0)); }
assert_ne!(level.start, level.end);
assert_ne!(level.monsters.len(), 0); #[test]
assert_ne!(level.artifacts.len(), 0); fn test_level_gen_respects_level() {
let level = LevelGenerator::generate(0, true, false).render();
assert_eq!(0, level.level);
let level = LevelGenerator::generate(1, true, false).render();
assert_eq!(1, level.level);
} }

View File

@ -36,6 +36,7 @@ mod level_widget;
mod monster; mod monster;
mod player; mod player;
mod position; mod position;
mod room;
// //
fn main() -> Result<()> { fn main() -> Result<()> {

229
src/room.rs Normal file
View File

@ -0,0 +1,229 @@
use std::{
cmp::{max, min},
ops::Range,
};
use crate::{
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH},
level::StructureElement,
};
use rand::rngs::ThreadRng;
use rand::Rng;
#[derive(PartialEq, Copy, Clone, Eq, Hash, Debug)]
pub enum RoomType {
Start,
End,
StairUp,
StairDown,
BasicRoom,
ArtifactRoom,
MonsterRoom,
EmptyRoom,
}
#[derive(Copy, Clone, Debug)]
pub struct Connection {
pub start_pos: (usize, usize),
pub end_pos: (usize, usize),
}
impl Connection {
pub fn render(&self, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) {
// the tuples are (col, row)
println!("{:?} -> {:?}",self.start_pos, self.end_pos);
let d_col = max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
let d_row = max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
if d_col == 0 {
for row in self.start_pos.1..=self.end_pos.1 {
tgt[self.end_pos.0][row] = StructureElement::Floor;
}
}
if d_row == 0 {
for col in self.start_pos.0..=self.end_pos.0 {
tgt[col][self.end_pos.1] = StructureElement::Floor;
}
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct Room {
pub kind: RoomType,
pub offset_x: usize,
pub offset_y: usize,
pub width: usize,
pub height: usize,
pub special: (usize, usize),
pub connection_down: Option<Connection>,
pub connection_right: Option<Connection>,
}
impl Room {
pub fn new(rng: &mut ThreadRng) -> Self {
let width = ROOM_WIDTH-1; //rng.gen_range(3..ROOM_WIDTH);
let height = ROOM_HEIGHT-1; //rng.gen_range(3..ROOM_HEIGHT);
let offset_x = 0; //rng.gen_range(0..(ROOM_WIDTH - width));
let offset_y = 0; //rng.gen_range(0..(ROOM_HEIGHT - height));
let sx = offset_x + rng.gen_range(1..width-1);
let sy = offset_y + rng.gen_range(1..height-1);
Self {
kind: RoomType::EmptyRoom,
offset_x,
offset_y,
width,
height,
special: (sx, sy),
connection_down: None,
connection_right: None,
}
}
pub fn get_x_range(&self) -> Range<usize> {
self.offset_x..self.offset_x + self.width
}
pub fn get_y_range(&self) -> Range<usize> {
self.offset_y..self.offset_y + self.height
}
pub fn render(
&self,
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
col: usize,
row: usize,
) -> (usize, usize) {
let top = 1 + row * ROOM_HEIGHT;
let left = 1 + col * ROOM_WIDTH;
for col in tgt.iter_mut().skip(left).take(ROOM_WIDTH) {
//left..left + ROOM_WIDTH {
for row_element in col.iter_mut().skip(top).take(ROOM_HEIGHT) {
// top..top + ROOM_HEIGHT {
*row_element = StructureElement::Wall;
}
}
if self.kind == RoomType::EmptyRoom {
return (0, 0);
}
// render floor elements
for x in 0..self.width {
for y in 0..self.height {
tgt[left + self.offset_x + x][top + self.offset_y + y] = StructureElement::Floor;
}
}
match self.kind {
RoomType::Start => {
tgt[left + self.special.0][top + self.special.1] = StructureElement::Start;
(left + self.special.0, top + self.special.1)
}
RoomType::End => {
tgt[left + self.special.0][top + self.special.1] = StructureElement::End;
(left + self.special.0, top + self.special.1)
}
RoomType::StairUp => {
tgt[left + self.special.0][top + self.special.1] = StructureElement::StairUp;
(left + self.special.0, top + self.special.1)
}
RoomType::StairDown => {
tgt[left + self.special.0][top + self.special.1] = StructureElement::StairDown;
(left + self.special.0, top + self.special.1)
}
RoomType::BasicRoom => (0, 0),
RoomType::ArtifactRoom => (0, 0),
RoomType::MonsterRoom => (0, 0),
RoomType::EmptyRoom => (0, 0),
}
}
}
#[test]
fn test_room_creation() {
let mut rng = rand::thread_rng();
let room = Room::new(&mut rng);
assert_eq!(room.kind, RoomType::EmptyRoom);
for _ in 0..1000 {
let room = Room::new(&mut rng);
assert!((3..=ROOM_WIDTH).contains(&room.width));
}
}
#[cfg(test)]
fn all_wall(
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
col: usize,
row: usize,
) -> bool {
let top = 1 + row * ROOM_HEIGHT;
let left = 1 + col * ROOM_WIDTH;
for x in left..left + ROOM_WIDTH {
for y in top..top + ROOM_HEIGHT {
if tgt[x][y] != StructureElement::Wall {
return false;
}
}
}
true
}
#[cfg(test)]
fn has_structure_element(
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
col: usize,
row: usize,
element: StructureElement,
) -> bool {
let top = 1 + row * ROOM_HEIGHT;
let left = 1 + col * ROOM_WIDTH;
for x in left..left + ROOM_WIDTH {
for y in top..top + ROOM_HEIGHT {
if tgt[x][y] == element {
return true;
}
}
}
false
}
#[test]
fn test_room_render_empty() {
let mut rng = rand::thread_rng();
let room = Room::new(&mut rng);
assert_eq!(room.kind, RoomType::EmptyRoom);
let mut structure = [[StructureElement::Floor; LEVEL_HEIGHT]; LEVEL_WIDTH];
room.render(&mut structure, 0, 0);
assert!(all_wall(&mut structure, 0, 0));
}
#[test]
fn test_room_render_basic() {
let mut rng = rand::thread_rng();
let mut room = Room::new(&mut rng);
room.kind = RoomType::BasicRoom;
let mut structure = [[StructureElement::Floor; LEVEL_HEIGHT]; LEVEL_WIDTH];
room.render(&mut structure, 1, 3);
assert!(!all_wall(&mut structure, 1, 3));
}
#[test]
fn test_room_render_start() {
let mut rng = rand::thread_rng();
let mut room = Room::new(&mut rng);
room.kind = RoomType::Start;
let mut structure = [[StructureElement::Floor; LEVEL_HEIGHT]; LEVEL_WIDTH];
room.render(&mut structure, 1, 3);
assert!(!all_wall(&mut structure, 1, 3));
assert!(has_structure_element(
&mut structure,
1,
3,
StructureElement::Start
));
}