From cc4b8751f4ff7344d688c4ddf6bfbabbf904cc38 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 21 Mar 2022 09:14:44 -0500 Subject: [PATCH] Update maze-generator. --- Cargo.toml | 2 +- src/map.rs | 94 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 91bf858..0988120 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ derive_more = "0.99" futures-lite = "1" gilrs = "0.8" here_be_dragons = "0.1" -maze_generator = "1" +maze_generator = "2" once_cell = "1" pathfinding = "3" rand = "0.8" diff --git a/src/map.rs b/src/map.rs index 10b12b4..263ff8c 100644 --- a/src/map.rs +++ b/src/map.rs @@ -174,55 +174,59 @@ impl MapFilter for GridBuilder { fn modify_map(&self, _rng: &mut StdRng, map: &MapgenMap) -> MapgenMap { let mut map = map.clone(); let mut generator = RbGenerator::new(None); - let maze = generator.generate(self.width_in_rooms as i32, self.height_in_rooms as i32); - let total_height = (self.room_height + 1) * self.height_in_rooms + 1; - let half_width = self.room_width / 2; - let half_height = self.room_height / 2; - for y in 0..self.height_in_rooms { - for x in 0..self.width_in_rooms { - let x_offset = x * (self.room_width + 1); - let y_offset = total_height - (y * (self.room_height + 1)) - self.room_height - 2; - let room = MRect::new_i32( - x_offset as i32 + 1, - y_offset as i32 + 1, - self.room_width as i32, - self.room_height as i32, - ); - map.add_room(room); - let coords = maze_generator::prelude::Coordinates::new(x as i32, y as i32); - if let Some(field) = maze.get_field(&coords) { - use maze_generator::prelude::Direction::*; - if field.has_passage(&North) { - let x = x_offset + half_width; - let y = y_offset + self.room_height; - map.set_tile(x as usize, y as usize, Tile::floor()); - } - if field.has_passage(&South) { - let x = x_offset + half_width; - let y = y_offset; - map.set_tile(x as usize, y as usize, Tile::floor()); - } - if field.has_passage(&East) { - let x = x_offset + self.room_width; - let y = y_offset + half_height; - map.set_tile(x as usize, y as usize, Tile::floor()); - } - if field.has_passage(&West) { - let x = x_offset; - let y = y_offset + half_height; - map.set_tile(x as usize, y as usize, Tile::floor()); + if let Ok(maze) = + generator.generate(self.width_in_rooms as i32, self.height_in_rooms as i32) + { + let total_height = (self.room_height + 1) * self.height_in_rooms + 1; + let half_width = self.room_width / 2; + let half_height = self.room_height / 2; + for y in 0..self.height_in_rooms { + for x in 0..self.width_in_rooms { + let x_offset = x * (self.room_width + 1); + let y_offset = + total_height - (y * (self.room_height + 1)) - self.room_height - 2; + let room = MRect::new_i32( + x_offset as i32 + 1, + y_offset as i32 + 1, + self.room_width as i32, + self.room_height as i32, + ); + map.add_room(room); + let coords = maze_generator::prelude::Coordinates::new(x as i32, y as i32); + if let Some(field) = maze.get_field(&coords) { + use maze_generator::prelude::Direction::*; + if field.has_passage(&North) { + let x = x_offset + half_width; + let y = y_offset + self.room_height; + map.set_tile(x as usize, y as usize, Tile::floor()); + } + if field.has_passage(&South) { + let x = x_offset + half_width; + let y = y_offset; + map.set_tile(x as usize, y as usize, Tile::floor()); + } + if field.has_passage(&East) { + let x = x_offset + self.room_width; + let y = y_offset + half_height; + map.set_tile(x as usize, y as usize, Tile::floor()); + } + if field.has_passage(&West) { + let x = x_offset; + let y = y_offset + half_height; + map.set_tile(x as usize, y as usize, Tile::floor()); + } } } } + map.starting_point = Some(here_be_dragons::geometry::Point::new( + maze.start.x as usize + 1, + maze.start.y as usize + 1, + )); + map.exit_point = Some(here_be_dragons::geometry::Point::new( + (maze.goal.x as usize) * self.room_width + half_width, + (maze.goal.y as usize) * self.room_height + half_height, + )); } - map.starting_point = Some(here_be_dragons::geometry::Point::new( - maze.start.x as usize + 1, - maze.start.y as usize + 1, - )); - map.exit_point = Some(here_be_dragons::geometry::Point::new( - (maze.goal.x as usize) * self.room_width + half_width, - (maze.goal.y as usize) * self.room_height + half_height, - )); map } }