This commit is contained in:
Joachim Lusiardi 2024-10-31 22:36:23 +01:00
parent 8bae1c7668
commit 1878959e69
2 changed files with 25 additions and 43 deletions

View File

@ -84,8 +84,8 @@ impl LevelGenerator {
match directions_to_try.pop().unwrap() { match directions_to_try.pop().unwrap() {
Direction::Horizontal => { Direction::Horizontal => {
let mut free_cols: Vec<usize> = vec![]; let mut free_cols: Vec<usize> = vec![];
for col in 0..ROOMS_HORIZONTAL { for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
if rooms[col][room_row].kind == RoomType::EmptyRoom { if room_col[room_row].kind == RoomType::EmptyRoom {
free_cols.push(col); free_cols.push(col);
} }
} }
@ -120,9 +120,9 @@ impl LevelGenerator {
// all fields in the row/column was full so we can place it at any empty position // all fields in the row/column was full so we can place it at any empty position
if !placed { if !placed {
let mut free_pos: Vec<(usize, usize)> = vec![]; let mut free_pos: Vec<(usize, usize)> = vec![];
for col in 0..ROOMS_HORIZONTAL { for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
for row in 0..ROOMS_VERTICAL { for (row, room) in room_col.iter().enumerate().take(ROOMS_VERTICAL) {
if rooms[col][row].kind == RoomType::EmptyRoom { if room.kind == RoomType::EmptyRoom {
free_pos.push((col, row)); free_pos.push((col, row));
} }
} }
@ -138,9 +138,9 @@ impl LevelGenerator {
rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL],
) -> Graph<(usize, usize), u16, petgraph::Undirected> { ) -> Graph<(usize, usize), u16, petgraph::Undirected> {
let mut graph = UnGraph::<(usize, usize), u16>::default(); let mut graph = UnGraph::<(usize, usize), u16>::default();
for col in 0..ROOMS_HORIZONTAL { for (col, tgt_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
for row in 0..ROOMS_VERTICAL { for (row, room) in tgt_col.iter().enumerate().take(ROOMS_VERTICAL) {
if rooms[col][row].kind != RoomType::EmptyRoom { if room.kind != RoomType::EmptyRoom {
graph.add_node((col, row)); graph.add_node((col, row));
} }
} }
@ -151,8 +151,13 @@ impl LevelGenerator {
continue; continue;
} }
if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) { if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) {
for col_1 in col + 1..ROOMS_HORIZONTAL { for (col_1, tgt_col) in rooms
if rooms[col_1][row].kind != RoomType::EmptyRoom { .iter()
.enumerate()
.take(ROOMS_HORIZONTAL)
.skip(col + 1)
{
if tgt_col[row].kind != RoomType::EmptyRoom {
if let Some(tgt_index) = if let Some(tgt_index) =
graph.node_indices().find(|i| graph[*i] == (col_1, row)) graph.node_indices().find(|i| graph[*i] == (col_1, row))
{ {
@ -190,6 +195,7 @@ impl LevelGenerator {
let tgt_room = rooms[tgt_node_col][tgt_node_row]; let tgt_room = rooms[tgt_node_col][tgt_node_row];
if src_node_col == tgt_node_col { if src_node_col == tgt_node_col {
// Right
let start_col = let start_col =
src_node_col * ROOM_WIDTH + src_room.offset_x + (src_room.width + 1) / 2; src_node_col * ROOM_WIDTH + src_room.offset_x + (src_room.width + 1) / 2;
let start_row = let start_row =
@ -202,7 +208,7 @@ impl LevelGenerator {
end_pos: (end_col, end_row), end_pos: (end_col, end_row),
}); });
} else { } else {
// println!("Right"); // Down
let start_col = let start_col =
1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width; 1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
let start_row = let start_row =
@ -221,40 +227,12 @@ 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();
/*
Fill grid with unconnected rooms
*/
let mut rooms_to_place: Vec<Room> = let mut rooms_to_place: Vec<Room> =
LevelGenerator::generate_rooms_to_place(&mut rng, level, first, last); LevelGenerator::generate_rooms_to_place(&mut rng, level, first, last);
let mut rooms: [[Room; 7]; 8] = LevelGenerator::place_rooms(&mut rng, &mut rooms_to_place); let mut rooms: [[Room; 7]; 8] = LevelGenerator::place_rooms(&mut rng, &mut rooms_to_place);
// debug print a text view of the dungeon
println!(" 0 1 2 3 4 5 6 7");
for r in 0..ROOMS_VERTICAL {
print!("{} ", r);
for c in 0..ROOMS_HORIZONTAL {
match rooms[c][r].kind {
RoomType::Start => print!("S "),
RoomType::End => print!("E "),
RoomType::StairUp => print!("< "),
RoomType::StairDown => print!("> "),
RoomType::BasicRoom => print!("_ "),
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 mst: Graph<(usize, usize), u16, petgraph::Undirected> = let mst: Graph<(usize, usize), u16, petgraph::Undirected> =
LevelGenerator::create_mst(&rooms); LevelGenerator::create_mst(&rooms);
println!("{:?}", mst);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
LevelGenerator { level, rooms, rng } LevelGenerator { level, rooms, rng }
} }

View File

@ -75,8 +75,8 @@ impl Connection {
} else { } else {
// more left/right // more left/right
let d_col = self.end_pos.0 - self.start_pos.0; let d_col = self.end_pos.0 - self.start_pos.0;
for c in self.start_pos.0..=(self.start_pos.0 + d_col / 2) { for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) {
tgt[c][self.start_pos.1] = StructureElement::Floor; tgt_col[self.start_pos.1] = StructureElement::Floor;
} }
let range = if self.end_pos.1 > self.start_pos.1 { let range = if self.end_pos.1 > self.start_pos.1 {
self.start_pos.1..=self.end_pos.1 self.start_pos.1..=self.end_pos.1
@ -86,8 +86,12 @@ impl Connection {
for r in range { for r in range {
tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor; tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor;
} }
for c in (self.start_pos.0 + d_col / 2)..=self.end_pos.0 { for tgt_col in tgt
tgt[c][self.end_pos.1] = StructureElement::Floor; .iter_mut()
.take(self.end_pos.0 + 1)
.skip(self.start_pos.0 + d_col / 2)
{
tgt_col[self.end_pos.1] = StructureElement::Floor;
} }
} }
} }