introduce custom type for the graph

This commit is contained in:
Joachim Lusiardi 2024-11-01 13:08:02 +01:00
parent 8b23c565b8
commit df80dfdd8a
1 changed files with 36 additions and 27 deletions

View File

@ -24,6 +24,7 @@ use crate::{
room::{Room, RoomType}, room::{Room, RoomType},
}; };
type GeneratorGraph = Graph<(usize, usize), usize, petgraph::Undirected>;
pub struct LevelGenerator { pub struct LevelGenerator {
level: usize, level: usize,
rooms: [[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], rooms: [[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL],
@ -135,9 +136,10 @@ impl LevelGenerator {
} }
fn create_mst( fn create_mst(
rng: &mut ThreadRng,
rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL],
) -> Graph<(usize, usize), u16, petgraph::Undirected> { ) -> GeneratorGraph {
let mut graph = UnGraph::<(usize, usize), u16>::default(); let mut graph: GeneratorGraph = UnGraph::<(usize, usize), usize>::default();
for (col, tgt_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) { for (col, tgt_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
for (row, room) in tgt_col.iter().enumerate().take(ROOMS_VERTICAL) { for (row, room) in tgt_col.iter().enumerate().take(ROOMS_VERTICAL) {
if room.kind != RoomType::EmptyRoom { if room.kind != RoomType::EmptyRoom {
@ -161,7 +163,12 @@ impl LevelGenerator {
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))
{ {
graph.add_edge(src_index, tgt_index, 1); graph.add_edge(
src_index,
tgt_index,
(rng.gen_range(0..5) as f32 / 5.0) as usize
* col.abs_diff(col_1),
);
break; break;
} }
} }
@ -171,7 +178,12 @@ impl LevelGenerator {
if let Some(tgt_index) = if let Some(tgt_index) =
graph.node_indices().find(|i| graph[*i] == (col, row_1)) graph.node_indices().find(|i| graph[*i] == (col, row_1))
{ {
graph.add_edge(src_index, tgt_index, 1); graph.add_edge(
src_index,
tgt_index,
(rng.gen_range(0..5) as f32 / 5.0) as usize
* row.abs_diff(row_1),
);
break; break;
} }
} }
@ -185,7 +197,7 @@ impl LevelGenerator {
fn create_connections( fn create_connections(
rooms: &mut [[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], rooms: &mut [[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL],
mst: &Graph<(usize, usize), u16, petgraph::Undirected>, mst: &GeneratorGraph,
) { ) {
for edge in mst.raw_edges() { for edge in mst.raw_edges() {
// the tuples are (col, row) // the tuples are (col, row)
@ -231,8 +243,7 @@ impl LevelGenerator {
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);
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_mst(&rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
LevelGenerator { level, rooms, rng } LevelGenerator { level, rooms, rng }
} }
@ -333,7 +344,6 @@ impl LevelGenerator {
#[test] #[test]
fn test_level_gen() { fn test_level_gen() {
let mut rng = rand::thread_rng();
for _ in 0..1000 { for _ in 0..1000 {
LevelGenerator::generate(0, true, false).render(); LevelGenerator::generate(0, true, false).render();
} }
@ -341,7 +351,6 @@ fn test_level_gen() {
#[test] #[test]
fn test_level_gen_respects_level() { fn test_level_gen_respects_level() {
let mut rng = rand::thread_rng();
let level = LevelGenerator::generate(0, true, false).render(); let level = LevelGenerator::generate(0, true, false).render();
assert_eq!(0, level.level); assert_eq!(0, level.level);
let level = LevelGenerator::generate(1, true, false).render(); let level = LevelGenerator::generate(1, true, false).render();
@ -481,32 +490,32 @@ fn test_place_rooms() {
fn test_create_mst() { fn test_create_mst() {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL];
let res: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let res: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
assert_eq!(res.node_count(), 0); assert_eq!(res.node_count(), 0);
assert_eq!(res.edge_count(), 0); assert_eq!(res.edge_count(), 0);
rooms[1][1].kind = RoomType::BasicRoom; rooms[1][1].kind = RoomType::BasicRoom;
let res = LevelGenerator::create_mst(&rooms); let res = LevelGenerator::create_mst(&mut rng, &rooms);
assert_eq!(res.node_count(), 1); assert_eq!(res.node_count(), 1);
assert_eq!(res.edge_count(), 0); assert_eq!(res.edge_count(), 0);
rooms[1][3].kind = RoomType::BasicRoom; rooms[1][3].kind = RoomType::BasicRoom;
let res = LevelGenerator::create_mst(&rooms); let res = LevelGenerator::create_mst(&mut rng, &rooms);
assert_eq!(res.node_count(), 2); assert_eq!(res.node_count(), 2);
assert_eq!(res.edge_count(), 1); assert_eq!(res.edge_count(), 1);
rooms[3][1].kind = RoomType::BasicRoom; rooms[3][1].kind = RoomType::BasicRoom;
let res = LevelGenerator::create_mst(&rooms); let res = LevelGenerator::create_mst(&mut rng, &rooms);
assert_eq!(res.node_count(), 3); assert_eq!(res.node_count(), 3);
assert_eq!(res.edge_count(), 2); assert_eq!(res.edge_count(), 2);
rooms[3][3].kind = RoomType::BasicRoom; rooms[3][3].kind = RoomType::BasicRoom;
let res = LevelGenerator::create_mst(&rooms); let res = LevelGenerator::create_mst(&mut rng, &rooms);
assert_eq!(res.node_count(), 4); assert_eq!(res.node_count(), 4);
assert_eq!(res.edge_count(), 3); assert_eq!(res.edge_count(), 3);
rooms[3][5].kind = RoomType::BasicRoom; rooms[3][5].kind = RoomType::BasicRoom;
let res = LevelGenerator::create_mst(&rooms); let res = LevelGenerator::create_mst(&mut rng, &rooms);
assert_eq!(res.node_count(), 5); assert_eq!(res.node_count(), 5);
assert_eq!(res.edge_count(), 4); assert_eq!(res.edge_count(), 4);
} }
@ -526,7 +535,7 @@ fn test_create_connections() {
rooms[3][0].height = ROOM_HEIGHT - 1; rooms[3][0].height = ROOM_HEIGHT - 1;
rooms[3][0].offset_y = 0; rooms[3][0].offset_y = 0;
rooms[3][0].kind = RoomType::BasicRoom; rooms[3][0].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
println!("{:?}", mst); println!("{:?}", mst);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
@ -561,7 +570,7 @@ fn test_create_connections_2() {
rooms[3][0].height = ROOM_HEIGHT - 1; rooms[3][0].height = ROOM_HEIGHT - 1;
rooms[3][0].offset_y = 0; rooms[3][0].offset_y = 0;
rooms[3][0].kind = RoomType::BasicRoom; rooms[3][0].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][0].connection_right.is_some()); assert!(rooms[1][0].connection_right.is_some());
@ -595,7 +604,7 @@ fn test_create_connections_d_1() {
rooms[0][3].height = ROOM_HEIGHT - 1; rooms[0][3].height = ROOM_HEIGHT - 1;
rooms[0][3].offset_y = 0; rooms[0][3].offset_y = 0;
rooms[0][3].kind = RoomType::BasicRoom; rooms[0][3].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
println!("{:?}", mst); println!("{:?}", mst);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
@ -632,7 +641,7 @@ fn test_create_connections_d_2() {
rooms[1][3].height = ROOM_HEIGHT - 1; rooms[1][3].height = ROOM_HEIGHT - 1;
rooms[1][3].offset_y = 0; rooms[1][3].offset_y = 0;
rooms[1][3].kind = RoomType::BasicRoom; rooms[1][3].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_some()); assert!(rooms[1][1].connection_down.is_some());
@ -667,7 +676,7 @@ fn test_create_connections_d_3() {
rooms[1][3].height = ROOM_HEIGHT - 1; rooms[1][3].height = ROOM_HEIGHT - 1;
rooms[1][3].offset_y = 0; rooms[1][3].offset_y = 0;
rooms[1][3].kind = RoomType::BasicRoom; rooms[1][3].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_some()); assert!(rooms[1][1].connection_down.is_some());
@ -702,7 +711,7 @@ fn test_create_connections_d_4() {
rooms[1][3].height = ROOM_HEIGHT - 1; rooms[1][3].height = ROOM_HEIGHT - 1;
rooms[1][3].offset_y = 0; rooms[1][3].offset_y = 0;
rooms[1][3].kind = RoomType::BasicRoom; rooms[1][3].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_some()); assert!(rooms[1][1].connection_down.is_some());
@ -737,7 +746,7 @@ fn test_create_connections_d_5() {
rooms[1][3].height = 4; rooms[1][3].height = 4;
rooms[1][3].offset_y = 1; rooms[1][3].offset_y = 1;
rooms[1][3].kind = RoomType::BasicRoom; rooms[1][3].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_some()); assert!(rooms[1][1].connection_down.is_some());
@ -771,7 +780,7 @@ fn test_create_connections_right_1() {
rooms[3][1].height = ROOM_HEIGHT - 1; rooms[3][1].height = ROOM_HEIGHT - 1;
rooms[3][1].offset_y = 0; rooms[3][1].offset_y = 0;
rooms[3][1].kind = RoomType::BasicRoom; rooms[3][1].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_none()); assert!(rooms[1][1].connection_down.is_none());
@ -806,7 +815,7 @@ fn test_create_connections_right_2() {
rooms[3][1].height = 4; rooms[3][1].height = 4;
rooms[3][1].offset_y = 2; rooms[3][1].offset_y = 2;
rooms[3][1].kind = RoomType::BasicRoom; rooms[3][1].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_none()); assert!(rooms[1][1].connection_down.is_none());
@ -841,7 +850,7 @@ fn test_create_connections_right_3() {
rooms[3][1].height = 2; rooms[3][1].height = 2;
rooms[3][1].offset_y = 4; rooms[3][1].offset_y = 4;
rooms[3][1].kind = RoomType::BasicRoom; rooms[3][1].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_none()); assert!(rooms[1][1].connection_down.is_none());
@ -876,7 +885,7 @@ fn test_create_connections_right_4() {
rooms[3][1].height = 2; rooms[3][1].height = 2;
rooms[3][1].offset_y = 4; rooms[3][1].offset_y = 4;
rooms[3][1].kind = RoomType::BasicRoom; rooms[3][1].kind = RoomType::BasicRoom;
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
assert!(rooms[1][1].connection_down.is_none()); assert!(rooms[1][1].connection_down.is_none());