From 1878959e6957595e468524d43214cb51d7c1aa6e Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Thu, 31 Oct 2024 22:36:23 +0100 Subject: [PATCH] Clippy --- src/level_generator.rs | 56 +++++++++++++----------------------------- src/room.rs | 12 ++++++--- 2 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/level_generator.rs b/src/level_generator.rs index 323c147..46959c7 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -84,8 +84,8 @@ impl LevelGenerator { match directions_to_try.pop().unwrap() { Direction::Horizontal => { let mut free_cols: Vec = vec![]; - for col in 0..ROOMS_HORIZONTAL { - if rooms[col][room_row].kind == RoomType::EmptyRoom { + for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) { + if room_col[room_row].kind == RoomType::EmptyRoom { free_cols.push(col); } } @@ -120,9 +120,9 @@ impl LevelGenerator { // all fields in the row/column was full so we can place it at any empty position if !placed { let mut free_pos: Vec<(usize, usize)> = vec![]; - for col in 0..ROOMS_HORIZONTAL { - for row in 0..ROOMS_VERTICAL { - if rooms[col][row].kind == RoomType::EmptyRoom { + for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) { + for (row, room) in room_col.iter().enumerate().take(ROOMS_VERTICAL) { + if room.kind == RoomType::EmptyRoom { free_pos.push((col, row)); } } @@ -138,9 +138,9 @@ impl LevelGenerator { rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], ) -> Graph<(usize, usize), u16, petgraph::Undirected> { let mut graph = UnGraph::<(usize, usize), u16>::default(); - for col in 0..ROOMS_HORIZONTAL { - for row in 0..ROOMS_VERTICAL { - if rooms[col][row].kind != RoomType::EmptyRoom { + 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 { graph.add_node((col, row)); } } @@ -151,8 +151,13 @@ impl LevelGenerator { continue; } if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) { - for col_1 in col + 1..ROOMS_HORIZONTAL { - if rooms[col_1][row].kind != RoomType::EmptyRoom { + for (col_1, tgt_col) in rooms + .iter() + .enumerate() + .take(ROOMS_HORIZONTAL) + .skip(col + 1) + { + if tgt_col[row].kind != RoomType::EmptyRoom { if let Some(tgt_index) = graph.node_indices().find(|i| graph[*i] == (col_1, row)) { @@ -190,6 +195,7 @@ impl LevelGenerator { let tgt_room = rooms[tgt_node_col][tgt_node_row]; if src_node_col == tgt_node_col { + // Right let start_col = src_node_col * ROOM_WIDTH + src_room.offset_x + (src_room.width + 1) / 2; let start_row = @@ -202,7 +208,7 @@ impl LevelGenerator { end_pos: (end_col, end_row), }); } else { - // println!("Right"); + // Down let start_col = 1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width; let start_row = @@ -221,40 +227,12 @@ impl LevelGenerator { pub fn generate(level: usize, first: bool, last: bool) -> Self { let mut rng = rand::thread_rng(); - /* - Fill grid with unconnected rooms - */ - let mut rooms_to_place: Vec = 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); - // debug print a text view of the dungeon - println!(" 0 1 2 3 4 5 6 7"); - for r in 0..ROOMS_VERTICAL { - print!("{} ", r); - for c in 0..ROOMS_HORIZONTAL { - match rooms[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!(); - } - - /* - Construct a graph from the unconnected rooms and make a minum spanning tree of it - */ - let mst: Graph<(usize, usize), u16, petgraph::Undirected> = LevelGenerator::create_mst(&rooms); - println!("{:?}", mst); LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator { level, rooms, rng } } diff --git a/src/room.rs b/src/room.rs index 2eb1725..03901a1 100644 --- a/src/room.rs +++ b/src/room.rs @@ -75,8 +75,8 @@ impl Connection { } else { // more left/right let d_col = self.end_pos.0 - self.start_pos.0; - for c in self.start_pos.0..=(self.start_pos.0 + d_col / 2) { - tgt[c][self.start_pos.1] = StructureElement::Floor; + for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) { + tgt_col[self.start_pos.1] = StructureElement::Floor; } let range = if self.end_pos.1 > self.start_pos.1 { self.start_pos.1..=self.end_pos.1 @@ -86,8 +86,12 @@ impl Connection { for r in range { tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor; } - for c in (self.start_pos.0 + d_col / 2)..=self.end_pos.0 { - tgt[c][self.end_pos.1] = StructureElement::Floor; + for tgt_col in tgt + .iter_mut() + .take(self.end_pos.0 + 1) + .skip(self.start_pos.0 + d_col / 2) + { + tgt_col[self.end_pos.1] = StructureElement::Floor; } } }