From f5d257e82634dd3687c3d32f4292037f6072551f Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Thu, 31 Oct 2024 15:20:39 +0100 Subject: [PATCH] more work on generator and tests --- src/level_generator.rs | 217 ++++++++++++++++++++++++++++++----------- 1 file changed, 161 insertions(+), 56 deletions(-) diff --git a/src/level_generator.rs b/src/level_generator.rs index 6b28d12..dad9ea8 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -190,25 +190,25 @@ impl LevelGenerator { let tgt_room = rooms[tgt_node_col][tgt_node_row]; if src_node_col == tgt_node_col { - let start_col = + let start_col = src_node_col * ROOM_WIDTH + src_room.offset_x + (src_room.width + 1) / 2; let start_row = src_node_row * ROOM_HEIGHT + src_room.offset_y + src_room.height + 1; let end_col = tgt_node_col * ROOM_WIDTH + tgt_room.offset_x + (tgt_room.width + 1) / 2; - let end_row = tgt_node_row * ROOM_HEIGHT - 1 + tgt_room.offset_y; + let end_row = 1 + tgt_node_row * ROOM_HEIGHT - 1 + tgt_room.offset_y; rooms[src_node_col][src_node_row].connection_down = Some(Connection { start_pos: (start_col, start_row), end_pos: (end_col, end_row), }); } else { // println!("Right"); - let start_col = src_node_col * ROOM_WIDTH + src_room.offset_x + src_room.width; + let start_col = 1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width; let start_row = - src_node_row * ROOM_HEIGHT + src_room.offset_y + (src_room.height + 1) / 2; - let end_col = tgt_node_col * ROOM_WIDTH -1 + tgt_room.offset_x; + 1 + src_node_row * ROOM_HEIGHT + src_room.offset_y + (src_room.height-1) / 2; + let end_col = 1 + tgt_node_col * ROOM_WIDTH - 1 + tgt_room.offset_x; let end_row = - tgt_node_row * ROOM_HEIGHT + tgt_room.offset_y + (tgt_room.height + 1) / 2; + 1 + tgt_node_row * ROOM_HEIGHT + tgt_room.offset_y + (tgt_room.height-1) / 2; rooms[src_node_col][src_node_row].connection_right = Some(Connection { start_pos: (start_col, start_row), end_pos: (end_col, end_row), @@ -531,7 +531,112 @@ fn test_create_mst() { } #[test] -fn test_create_connections_down_1() { +fn test_create_connections() { + let mut rng = rand::thread_rng(); + let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; + rooms[1][0].width = ROOM_WIDTH - 1; + rooms[1][0].offset_x = 0; + rooms[1][0].height = ROOM_HEIGHT - 1; + rooms[1][0].offset_y = 0; + rooms[1][0].kind = RoomType::BasicRoom; + + rooms[3][0].width = ROOM_WIDTH - 1; + rooms[3][0].offset_x = 0; + 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); + println!("{:?}", mst); + LevelGenerator::create_connections(&mut rooms, &mst); + + assert!(rooms[1][0].connection_right.is_some()); + assert_eq!( + rooms[1][0].connection_right.unwrap().start_pos, + (18, 3), + "wrong start" + ); + assert_eq!( + rooms[1][0].connection_right.unwrap().end_pos, + (27, 3), + "wrong end" + ); + assert!(rooms[1][0].connection_down.is_none()); + assert!(rooms[3][0].connection_down.is_none()); + assert!(rooms[3][0].connection_right.is_none()); +} + +#[test] +fn test_create_connections_2() { + let mut rng = rand::thread_rng(); + let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; + rooms[1][0].width = ROOM_WIDTH - 1; + rooms[1][0].offset_x = 0; + rooms[1][0].height = ROOM_HEIGHT - 1; + rooms[1][0].offset_y = 0; + rooms[1][0].kind = RoomType::BasicRoom; + + rooms[3][0].width = ROOM_WIDTH - 2; + rooms[3][0].offset_x = 1; + 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); + LevelGenerator::create_connections(&mut rooms, &mst); + + assert!(rooms[1][0].connection_right.is_some()); + assert_eq!( + rooms[1][0].connection_right.unwrap().start_pos, + (18, 3), + "wrong start" + ); + assert_eq!( + rooms[1][0].connection_right.unwrap().end_pos, + (28, 3), + "wrong end" + ); + assert!(rooms[1][0].connection_down.is_none()); + assert!(rooms[3][0].connection_down.is_none()); + assert!(rooms[3][0].connection_right.is_none()); +} + +#[test] +fn test_create_connections_d_1() { + let mut rng = rand::thread_rng(); + let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; + rooms[0][1].width = ROOM_WIDTH - 1; + rooms[0][1].offset_x = 0; + rooms[0][1].height = ROOM_HEIGHT - 1; + rooms[0][1].offset_y = 0; + rooms[0][1].kind = RoomType::BasicRoom; + + rooms[0][3].width = ROOM_WIDTH - 1; + rooms[0][3].offset_x = 0; + 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); + println!("{:?}", mst); + LevelGenerator::create_connections(&mut rooms, &mst); + + assert!(rooms[0][1].connection_down.is_some()); + assert!(rooms[0][1].connection_right.is_none()); + assert!(rooms[0][3].connection_down.is_none()); + assert!(rooms[0][3].connection_right.is_none()); + + assert_eq!( + rooms[0][1].connection_down.unwrap().start_pos, + (4, 12), + "wrong start" + ); + assert_eq!( + rooms[0][1].connection_down.unwrap().end_pos, + (4, 18), + "wrong end" + ); +} + +#[test] +fn test_create_connections_d_2() { // test full sized rooms for downwards connection let mut rng = rand::thread_rng(); let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; @@ -552,12 +657,12 @@ fn test_create_connections_down_1() { assert!(rooms[1][1].connection_down.is_some()); assert_eq!( rooms[1][1].connection_down.unwrap().start_pos, - (ROOM_WIDTH + ROOM_WIDTH / 2, ROOM_HEIGHT * 2), + (13, 12), "wrong start" ); assert_eq!( rooms[1][1].connection_down.unwrap().end_pos, - (ROOM_WIDTH + ROOM_WIDTH / 2, ROOM_HEIGHT * 3 - 1), + (13, 18), "wrong end" ); assert!(rooms[1][1].connection_right.is_none()); @@ -566,7 +671,7 @@ fn test_create_connections_down_1() { } #[test] -fn test_create_connections_down_2() { +fn test_create_connections_d_3() { // test reduced width rooms (with overlap) for downwards connection let mut rng = rand::thread_rng(); let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; @@ -592,16 +697,16 @@ fn test_create_connections_down_2() { ); assert_eq!( rooms[1][1].connection_down.unwrap().end_pos, - (ROOM_WIDTH + 7, ROOM_HEIGHT * 3 - 1), + (ROOM_WIDTH + 7, ROOM_HEIGHT * 3), "wrong end" ); assert!(rooms[1][1].connection_right.is_none()); - assert!(rooms[1][2].connection_down.is_none()); - assert!(rooms[1][2].connection_right.is_none()); + assert!(rooms[1][3].connection_down.is_none()); + assert!(rooms[1][3].connection_right.is_none()); } #[test] -fn test_create_connections_down_3() { +fn test_create_connections_d_4() { // test reduced width rooms (with NO overlap) for downwards connection let mut rng = rand::thread_rng(); let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; @@ -627,16 +732,16 @@ fn test_create_connections_down_3() { ); assert_eq!( rooms[1][1].connection_down.unwrap().end_pos, - (ROOM_WIDTH + 7, ROOM_HEIGHT * 3 - 1), + (ROOM_WIDTH + 7, ROOM_HEIGHT * 3), "wrong end" ); assert!(rooms[1][1].connection_right.is_none()); - assert!(rooms[1][2].connection_down.is_none()); - assert!(rooms[1][2].connection_right.is_none()); + assert!(rooms[1][3].connection_down.is_none()); + assert!(rooms[1][3].connection_right.is_none()); } #[test] -fn test_create_connections_down_4() { +fn test_create_connections_d_5() { // test reduced and moved width rooms (with NO overlap) for downwards connection let mut rng = rand::thread_rng(); let mut rooms = [[Room::new(&mut rng); ROOMS_VERTICAL]; ROOMS_HORIZONTAL]; @@ -657,17 +762,17 @@ fn test_create_connections_down_4() { assert!(rooms[1][1].connection_down.is_some()); assert_eq!( rooms[1][1].connection_down.unwrap().start_pos, - (ROOM_WIDTH + 2, ROOM_HEIGHT + 5), + (11, 11), "wrong start" ); assert_eq!( rooms[1][1].connection_down.unwrap().end_pos, - (ROOM_WIDTH + 7, ROOM_HEIGHT * 3 + 0), + (16, 19), "wrong end" ); assert!(rooms[1][1].connection_right.is_none()); - assert!(rooms[1][2].connection_down.is_none()); - assert!(rooms[1][2].connection_right.is_none()); + assert!(rooms[1][3].connection_down.is_none()); + assert!(rooms[1][3].connection_right.is_none()); } #[test] @@ -692,16 +797,16 @@ fn test_create_connections_right_1() { assert!(rooms[1][1].connection_right.is_some()); assert_eq!( rooms[1][1].connection_right.unwrap().start_pos, - (ROOM_WIDTH * 2-1, ROOM_HEIGHT + ROOM_HEIGHT / 2), + (18, 9), "wrong start" ); assert_eq!( rooms[1][1].connection_right.unwrap().end_pos, - (ROOM_WIDTH * 3 - 1, ROOM_HEIGHT + ROOM_HEIGHT / 2), + (27, 9), "wrong end" ); - assert!(rooms[1][2].connection_down.is_none()); - assert!(rooms[1][2].connection_right.is_none()); + assert!(rooms[3][1].connection_down.is_none()); + assert!(rooms[3][1].connection_right.is_none()); } #[test] @@ -727,12 +832,12 @@ fn test_create_connections_right_2() { assert!(rooms[1][1].connection_right.is_some()); assert_eq!( rooms[1][1].connection_right.unwrap().start_pos, - (ROOM_WIDTH * 2-1, ROOM_HEIGHT + 2), + (18, 8), "wrong start" ); assert_eq!( rooms[1][1].connection_right.unwrap().end_pos, - (ROOM_WIDTH * 3 - 1, ROOM_HEIGHT + 4), + (27, 10), "wrong end" ); assert!(rooms[1][2].connection_down.is_none()); @@ -762,16 +867,16 @@ fn test_create_connections_right_3() { assert!(rooms[1][1].connection_right.is_some()); assert_eq!( rooms[1][1].connection_right.unwrap().start_pos, - (ROOM_WIDTH * 2 - 1, ROOM_HEIGHT + 1), + (18, 7), "wrong start" ); assert_eq!( rooms[1][1].connection_right.unwrap().end_pos, - (ROOM_WIDTH * 3 - 1, ROOM_HEIGHT + 5), + (27, 11), "wrong end" ); - assert!(rooms[1][2].connection_down.is_none()); - assert!(rooms[1][2].connection_right.is_none()); + assert!(rooms[1][3].connection_down.is_none()); + assert!(rooms[1][3].connection_right.is_none()); } #[test] @@ -797,35 +902,35 @@ fn test_create_connections_right_4() { assert!(rooms[1][1].connection_right.is_some()); assert_eq!( rooms[1][1].connection_right.unwrap().start_pos, - (ROOM_WIDTH +7, ROOM_HEIGHT + 1), + (17, 7), "wrong start" ); assert_eq!( rooms[1][1].connection_right.unwrap().end_pos, - (ROOM_WIDTH * 3 + 2, ROOM_HEIGHT + 5), + (30, 11), "wrong end" ); - assert!(rooms[1][2].connection_down.is_none()); - assert!(rooms[1][2].connection_right.is_none()); + assert!(rooms[1][3].connection_down.is_none()); + assert!(rooms[1][3].connection_right.is_none()); } -/* - println!(" 0 1 2 3 4 5 6 7"); - for r in 0..ROOMS_VERTICAL { - print!("{} ", r); - for c in 0..ROOMS_HORIZONTAL { - match res[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!(); - } - println!(); -*/ +// /* +// println!(" 0 1 2 3 4 5 6 7"); +// for r in 0..ROOMS_VERTICAL { +// print!("{} ", r); +// for c in 0..ROOMS_HORIZONTAL { +// match res[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!(); +// } +// println!(); +// */