diff --git a/src/constants.rs b/src/constants.rs index f108f93..3b6c4e6 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -19,7 +19,7 @@ pub const ROOM_MIN_WIDTH: usize = 4; pub const ROOM_MIN_HEIGHT: usize = 4; /// How many levels does the dungeon have? -pub const LEVELS: usize = 2; +pub const LEVELS: usize = 4; /// length of a game frame in ms pub const FRAME_LENGTH: u64 = 100; @@ -46,6 +46,12 @@ pub fn get_monsters_per_level() -> Vec Vec Vec GeneratorGraph { + // calculate the weight of an edge + fn calc_edge_weight(delta: usize) -> usize { + delta * delta + } + 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) { @@ -166,8 +172,7 @@ impl LevelGenerator { graph.add_edge( src_index, tgt_index, - (rng.gen_range(0..5) as f32 / 5.0) as usize - * col.abs_diff(col_1), + calc_edge_weight(col.abs_diff(col_1)), ); break; } @@ -181,8 +186,7 @@ impl LevelGenerator { graph.add_edge( src_index, tgt_index, - (rng.gen_range(0..5) as f32 / 5.0) as usize - * row.abs_diff(row_1), + calc_edge_weight(row.abs_diff(row_1)), ); break; } @@ -191,8 +195,35 @@ impl LevelGenerator { } } } - Graph::from_elements(min_spanning_tree(&graph)) - // graph + let mut mst: GeneratorGraph = Graph::from_elements(min_spanning_tree(&graph)); + + // readd some of the short edges that were remove (all between a weight of 1 to 10) + let initial_edges = graph.raw_edges(); + let minimal_edges = mst.raw_edges(); + let mut extra_edges: Vec> = vec![]; + for init_edge in initial_edges { + let start_node = init_edge.source(); + let end_node = init_edge.target(); + let mut found = false; + for min_edge in minimal_edges { + if min_edge.source() == start_node && min_edge.target() == end_node { + found = true; + break; + } + } + if !found { + extra_edges.push(init_edge.clone()); + } + } + extra_edges.sort_by(|a, b| a.weight.cmp(&b.weight)); + for e in extra_edges + .iter() + .filter(|x| x.weight > 1) + .filter(|x| x.weight < 7) + { + mst.add_edge(e.source(), e.target(), e.weight); + } + mst } fn create_connections(