This commit is contained in:
Joachim Lusiardi 2024-11-06 09:22:22 +01:00
parent 72f7be2ed8
commit 3249529f94
2 changed files with 22 additions and 23 deletions

View File

@ -136,10 +136,7 @@ impl LevelGenerator {
rooms rooms
} }
fn create_mst( fn create_mst(rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL]) -> GeneratorGraph {
rng: &mut ThreadRng,
rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL],
) -> GeneratorGraph {
// calculate the weight of an edge // calculate the weight of an edge
fn calc_edge_weight(delta: usize) -> usize { fn calc_edge_weight(delta: usize) -> usize {
delta * delta delta * delta
@ -274,7 +271,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
LevelGenerator { level, rooms, rng } LevelGenerator { level, rooms, rng }
} }
@ -521,32 +518,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let res: GeneratorGraph = LevelGenerator::create_mst(&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(&mut rng, &rooms); let res = LevelGenerator::create_mst(&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(&mut rng, &rooms); let res = LevelGenerator::create_mst(&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(&mut rng, &rooms); let res = LevelGenerator::create_mst(&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(&mut rng, &rooms); let res = LevelGenerator::create_mst(&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(&mut rng, &rooms); let res = LevelGenerator::create_mst(&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);
} }
@ -566,7 +563,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&rooms);
println!("{:?}", mst); println!("{:?}", mst);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
@ -601,7 +598,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -635,7 +632,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&rooms);
println!("{:?}", mst); println!("{:?}", mst);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
@ -672,7 +669,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -707,7 +704,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -742,7 +739,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -777,7 +774,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -811,7 +808,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -846,7 +843,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -881,7 +878,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());
@ -916,7 +913,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: GeneratorGraph = LevelGenerator::create_mst(&mut rng, &rooms); let mst: GeneratorGraph = LevelGenerator::create_mst(&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());

View File

@ -4,7 +4,9 @@ use std::{
}; };
use crate::{ use crate::{
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_MIN_HEIGHT, ROOM_MIN_WIDTH, ROOM_WIDTH}, constants::{
LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_MIN_HEIGHT, ROOM_MIN_WIDTH, ROOM_WIDTH,
},
level::StructureElement, level::StructureElement,
}; };
use rand::rngs::ThreadRng; use rand::rngs::ThreadRng;