From df80dfdd8aa2a42045864923397ba473fa450919 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Fri, 1 Nov 2024 13:08:02 +0100 Subject: [PATCH] introduce custom type for the graph --- src/level_generator.rs | 63 ++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/src/level_generator.rs b/src/level_generator.rs index 3e83bfa..f63240d 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -24,6 +24,7 @@ use crate::{ room::{Room, RoomType}, }; +type GeneratorGraph = Graph<(usize, usize), usize, petgraph::Undirected>; pub struct LevelGenerator { level: usize, rooms: [[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], @@ -135,9 +136,10 @@ impl LevelGenerator { } fn create_mst( + rng: &mut ThreadRng, rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], - ) -> Graph<(usize, usize), u16, petgraph::Undirected> { - let mut graph = UnGraph::<(usize, usize), u16>::default(); + ) -> GeneratorGraph { + let mut graph: GeneratorGraph = UnGraph::<(usize, usize), usize>::default(); for (col, tgt_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) { for (row, room) in tgt_col.iter().enumerate().take(ROOMS_VERTICAL) { if room.kind != RoomType::EmptyRoom { @@ -161,7 +163,12 @@ impl LevelGenerator { if let Some(tgt_index) = 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; } } @@ -171,7 +178,12 @@ impl LevelGenerator { if let Some(tgt_index) = 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; } } @@ -185,7 +197,7 @@ impl LevelGenerator { fn create_connections( rooms: &mut [[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], - mst: &Graph<(usize, usize), u16, petgraph::Undirected>, + mst: &GeneratorGraph, ) { for edge in mst.raw_edges() { // the tuples are (col, row) @@ -231,8 +243,7 @@ impl LevelGenerator { 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 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 { level, rooms, rng } } @@ -314,7 +325,7 @@ impl LevelGenerator { } if let Some(connection) = self.rooms[col][row].connection_right { // println!("right"); - connection.render(&mut self.rng,&mut structure); + connection.render(&mut self.rng, &mut structure); } } } @@ -333,7 +344,6 @@ impl LevelGenerator { #[test] fn test_level_gen() { - let mut rng = rand::thread_rng(); for _ in 0..1000 { LevelGenerator::generate(0, true, false).render(); } @@ -341,7 +351,6 @@ fn test_level_gen() { #[test] fn test_level_gen_respects_level() { - let mut rng = rand::thread_rng(); let level = LevelGenerator::generate(0, true, false).render(); assert_eq!(0, level.level); let level = LevelGenerator::generate(1, true, false).render(); @@ -481,32 +490,32 @@ fn test_place_rooms() { fn test_create_mst() { let mut rng = rand::thread_rng(); 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.edge_count(), 0); 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.edge_count(), 0); 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.edge_count(), 1); 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.edge_count(), 2); 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.edge_count(), 3); 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.edge_count(), 4); } @@ -526,7 +535,7 @@ fn test_create_connections() { rooms[3][0].height = ROOM_HEIGHT - 1; rooms[3][0].offset_y = 0; 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); 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].offset_y = 0; 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); 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].offset_y = 0; 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); 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].offset_y = 0; 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); 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].offset_y = 0; 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); 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].offset_y = 0; 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); 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].offset_y = 1; 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); 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].offset_y = 0; 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); 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].offset_y = 2; 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); 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].offset_y = 4; 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); 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].offset_y = 4; 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); assert!(rooms[1][1].connection_down.is_none());