more work on generator and tests

This commit is contained in:
Joachim Lusiardi 2024-10-31 15:20:39 +01:00
parent a82a847ecd
commit f5d257e826
1 changed files with 161 additions and 56 deletions

View File

@ -190,25 +190,25 @@ 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 {
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 =
src_node_row * ROOM_HEIGHT + src_room.offset_y + src_room.height + 1; src_node_row * ROOM_HEIGHT + src_room.offset_y + src_room.height + 1;
let end_col = let end_col =
tgt_node_col * ROOM_WIDTH + tgt_room.offset_x + (tgt_room.width + 1) / 2; 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 { rooms[src_node_col][src_node_row].connection_down = Some(Connection {
start_pos: (start_col, start_row), start_pos: (start_col, start_row),
end_pos: (end_col, end_row), end_pos: (end_col, end_row),
}); });
} else { } else {
// println!("Right"); // 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 = let start_row =
src_node_row * ROOM_HEIGHT + src_room.offset_y + (src_room.height + 1) / 2; 1 + 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; let end_col = 1 + tgt_node_col * ROOM_WIDTH - 1 + tgt_room.offset_x;
let end_row = 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 { rooms[src_node_col][src_node_row].connection_right = Some(Connection {
start_pos: (start_col, start_row), start_pos: (start_col, start_row),
end_pos: (end_col, end_row), end_pos: (end_col, end_row),
@ -531,7 +531,112 @@ fn test_create_mst() {
} }
#[test] #[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 // test full sized rooms for downwards connection
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];
@ -552,12 +657,12 @@ fn test_create_connections_down_1() {
assert!(rooms[1][1].connection_down.is_some()); assert!(rooms[1][1].connection_down.is_some());
assert_eq!( assert_eq!(
rooms[1][1].connection_down.unwrap().start_pos, rooms[1][1].connection_down.unwrap().start_pos,
(ROOM_WIDTH + ROOM_WIDTH / 2, ROOM_HEIGHT * 2), (13, 12),
"wrong start" "wrong start"
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_down.unwrap().end_pos, rooms[1][1].connection_down.unwrap().end_pos,
(ROOM_WIDTH + ROOM_WIDTH / 2, ROOM_HEIGHT * 3 - 1), (13, 18),
"wrong end" "wrong end"
); );
assert!(rooms[1][1].connection_right.is_none()); assert!(rooms[1][1].connection_right.is_none());
@ -566,7 +671,7 @@ fn test_create_connections_down_1() {
} }
#[test] #[test]
fn test_create_connections_down_2() { fn test_create_connections_d_3() {
// test reduced width rooms (with overlap) for downwards connection // test reduced width rooms (with overlap) for downwards connection
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];
@ -592,16 +697,16 @@ fn test_create_connections_down_2() {
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_down.unwrap().end_pos, rooms[1][1].connection_down.unwrap().end_pos,
(ROOM_WIDTH + 7, ROOM_HEIGHT * 3 - 1), (ROOM_WIDTH + 7, ROOM_HEIGHT * 3),
"wrong end" "wrong end"
); );
assert!(rooms[1][1].connection_right.is_none()); assert!(rooms[1][1].connection_right.is_none());
assert!(rooms[1][2].connection_down.is_none()); assert!(rooms[1][3].connection_down.is_none());
assert!(rooms[1][2].connection_right.is_none()); assert!(rooms[1][3].connection_right.is_none());
} }
#[test] #[test]
fn test_create_connections_down_3() { fn test_create_connections_d_4() {
// test reduced width rooms (with NO overlap) for downwards connection // test reduced width rooms (with NO overlap) for downwards connection
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];
@ -627,16 +732,16 @@ fn test_create_connections_down_3() {
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_down.unwrap().end_pos, rooms[1][1].connection_down.unwrap().end_pos,
(ROOM_WIDTH + 7, ROOM_HEIGHT * 3 - 1), (ROOM_WIDTH + 7, ROOM_HEIGHT * 3),
"wrong end" "wrong end"
); );
assert!(rooms[1][1].connection_right.is_none()); assert!(rooms[1][1].connection_right.is_none());
assert!(rooms[1][2].connection_down.is_none()); assert!(rooms[1][3].connection_down.is_none());
assert!(rooms[1][2].connection_right.is_none()); assert!(rooms[1][3].connection_right.is_none());
} }
#[test] #[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 // test reduced and moved width rooms (with NO overlap) for downwards connection
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];
@ -657,17 +762,17 @@ fn test_create_connections_down_4() {
assert!(rooms[1][1].connection_down.is_some()); assert!(rooms[1][1].connection_down.is_some());
assert_eq!( assert_eq!(
rooms[1][1].connection_down.unwrap().start_pos, rooms[1][1].connection_down.unwrap().start_pos,
(ROOM_WIDTH + 2, ROOM_HEIGHT + 5), (11, 11),
"wrong start" "wrong start"
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_down.unwrap().end_pos, rooms[1][1].connection_down.unwrap().end_pos,
(ROOM_WIDTH + 7, ROOM_HEIGHT * 3 + 0), (16, 19),
"wrong end" "wrong end"
); );
assert!(rooms[1][1].connection_right.is_none()); assert!(rooms[1][1].connection_right.is_none());
assert!(rooms[1][2].connection_down.is_none()); assert!(rooms[1][3].connection_down.is_none());
assert!(rooms[1][2].connection_right.is_none()); assert!(rooms[1][3].connection_right.is_none());
} }
#[test] #[test]
@ -692,16 +797,16 @@ fn test_create_connections_right_1() {
assert!(rooms[1][1].connection_right.is_some()); assert!(rooms[1][1].connection_right.is_some());
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().start_pos, rooms[1][1].connection_right.unwrap().start_pos,
(ROOM_WIDTH * 2-1, ROOM_HEIGHT + ROOM_HEIGHT / 2), (18, 9),
"wrong start" "wrong start"
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().end_pos, rooms[1][1].connection_right.unwrap().end_pos,
(ROOM_WIDTH * 3 - 1, ROOM_HEIGHT + ROOM_HEIGHT / 2), (27, 9),
"wrong end" "wrong end"
); );
assert!(rooms[1][2].connection_down.is_none()); assert!(rooms[3][1].connection_down.is_none());
assert!(rooms[1][2].connection_right.is_none()); assert!(rooms[3][1].connection_right.is_none());
} }
#[test] #[test]
@ -727,12 +832,12 @@ fn test_create_connections_right_2() {
assert!(rooms[1][1].connection_right.is_some()); assert!(rooms[1][1].connection_right.is_some());
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().start_pos, rooms[1][1].connection_right.unwrap().start_pos,
(ROOM_WIDTH * 2-1, ROOM_HEIGHT + 2), (18, 8),
"wrong start" "wrong start"
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().end_pos, rooms[1][1].connection_right.unwrap().end_pos,
(ROOM_WIDTH * 3 - 1, ROOM_HEIGHT + 4), (27, 10),
"wrong end" "wrong end"
); );
assert!(rooms[1][2].connection_down.is_none()); 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!(rooms[1][1].connection_right.is_some());
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().start_pos, rooms[1][1].connection_right.unwrap().start_pos,
(ROOM_WIDTH * 2 - 1, ROOM_HEIGHT + 1), (18, 7),
"wrong start" "wrong start"
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().end_pos, rooms[1][1].connection_right.unwrap().end_pos,
(ROOM_WIDTH * 3 - 1, ROOM_HEIGHT + 5), (27, 11),
"wrong end" "wrong end"
); );
assert!(rooms[1][2].connection_down.is_none()); assert!(rooms[1][3].connection_down.is_none());
assert!(rooms[1][2].connection_right.is_none()); assert!(rooms[1][3].connection_right.is_none());
} }
#[test] #[test]
@ -797,35 +902,35 @@ fn test_create_connections_right_4() {
assert!(rooms[1][1].connection_right.is_some()); assert!(rooms[1][1].connection_right.is_some());
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().start_pos, rooms[1][1].connection_right.unwrap().start_pos,
(ROOM_WIDTH +7, ROOM_HEIGHT + 1), (17, 7),
"wrong start" "wrong start"
); );
assert_eq!( assert_eq!(
rooms[1][1].connection_right.unwrap().end_pos, rooms[1][1].connection_right.unwrap().end_pos,
(ROOM_WIDTH * 3 + 2, ROOM_HEIGHT + 5), (30, 11),
"wrong end" "wrong end"
); );
assert!(rooms[1][2].connection_down.is_none()); assert!(rooms[1][3].connection_down.is_none());
assert!(rooms[1][2].connection_right.is_none()); assert!(rooms[1][3].connection_right.is_none());
} }
/* // /*
println!(" 0 1 2 3 4 5 6 7"); // println!(" 0 1 2 3 4 5 6 7");
for r in 0..ROOMS_VERTICAL { // for r in 0..ROOMS_VERTICAL {
print!("{} ", r); // print!("{} ", r);
for c in 0..ROOMS_HORIZONTAL { // for c in 0..ROOMS_HORIZONTAL {
match res[c][r].kind { // match res[c][r].kind {
RoomType::Start => print!("S "), // RoomType::Start => print!("S "),
RoomType::End => print!("E "), // RoomType::End => print!("E "),
RoomType::StairUp => print!("< "), // RoomType::StairUp => print!("< "),
RoomType::StairDown => print!("> "), // RoomType::StairDown => print!("> "),
RoomType::BasicRoom => print!("_ "), // RoomType::BasicRoom => print!("_ "),
RoomType::ArtifactRoom => print!("A "), // RoomType::ArtifactRoom => print!("A "),
RoomType::MonsterRoom => print!("M "), // RoomType::MonsterRoom => print!("M "),
RoomType::EmptyRoom => print!(" "), // RoomType::EmptyRoom => print!(" "),
}; // };
} // }
println!(); // println!();
} // }
println!(); // println!();
*/ // */