From d4f8c99c19801e395cc6fe1b32867b7337b35812 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 31 Jan 2023 17:24:11 -0600 Subject: [PATCH] chore: Resolve clippy warnings and add a bounds check. --- src/dijkstra.rs | 2 +- src/filter/bsp_rooms.rs | 6 +++--- src/filter/maze.rs | 2 +- src/filter/voronoi.rs | 2 +- src/map.rs | 14 +++++++++----- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/dijkstra.rs b/src/dijkstra.rs index 734f8cf..f4675bb 100644 --- a/src/dijkstra.rs +++ b/src/dijkstra.rs @@ -63,7 +63,7 @@ impl DijkstraMap { /// WARNING: Will give incorrect results when used with non-uniform exit costs. Much slower /// algorithm required to support that. fn build(&mut self, map: &Map) { - let mapsize: usize = (self.size_x * self.size_y) as usize; + let mapsize = self.size_x * self.size_y; let mut open_list: VecDeque<((usize, usize), f32)> = VecDeque::with_capacity(mapsize); if let Some(pos) = map.starting_point { diff --git a/src/filter/bsp_rooms.rs b/src/filter/bsp_rooms.rs index 9c3da49..ec64fe3 100644 --- a/src/filter/bsp_rooms.rs +++ b/src/filter/bsp_rooms.rs @@ -110,8 +110,8 @@ impl BspRooms { let rect_width = rect.width(); let rect_height = rect.height(); - let w = usize::max(3, rng.random_range(1, usize::min(rect_width as usize, 20))) + 1; - let h = usize::max(3, rng.random_range(1, usize::min(rect_height as usize, 20))) + 1; + let w = usize::max(3, rng.random_range(1, usize::min(rect_width, 20))) + 1; + let h = usize::max(3, rng.random_range(1, usize::min(rect_height, 20))) + 1; result.x1 += rng.random_range(0, 6); result.y1 += rng.random_range(0, 6); @@ -151,7 +151,7 @@ impl BspRooms { can_build = false; } if can_build { - if let Some(tile) = map.at(x as usize, y as usize) { + if let Some(tile) = map.at(x, y) { if tile.is_walkable() { can_build = false; } diff --git a/src/filter/maze.rs b/src/filter/maze.rs index a60ebc0..1105333 100644 --- a/src/filter/maze.rs +++ b/src/filter/maze.rs @@ -160,7 +160,7 @@ impl<'a, D: Clone + Default> Grid<'a, D> { if neighbors.len() == 1 { return Some(neighbors[0]); } else { - return Some(neighbors[(self.rng.roll_dice(1, neighbors.len()) - 1) as usize]); + return Some(neighbors[(self.rng.roll_dice(1, neighbors.len()) - 1)]); } } None diff --git a/src/filter/voronoi.rs b/src/filter/voronoi.rs index ced110a..0d17163 100644 --- a/src/filter/voronoi.rs +++ b/src/filter/voronoi.rs @@ -47,7 +47,7 @@ impl VoronoiHive { let seeds = self.generate_seeds(rng, map.width, map.height); let mut voronoi_distance = vec![(0, 0.0f32); self.n_seeds]; - let mut voronoi_membership: Vec = vec![0; map.width as usize * map.height as usize]; + let mut voronoi_membership: Vec = vec![0; map.width * map.height]; for (i, vid) in voronoi_membership.iter_mut().enumerate() { let x = i % map.width; let y = i / map.width; diff --git a/src/map.rs b/src/map.rs index 75b8f6d..c84c762 100644 --- a/src/map.rs +++ b/src/map.rs @@ -113,8 +113,12 @@ impl Map { /// Get TileType at the given location pub fn at(&self, x: usize, y: usize) -> Option<&Tile> { - let idx = (y as usize) * self.width + (x as usize); - self.tiles.get(idx) + if x < self.width && y < self.height { + let idx = y * self.width + x; + self.tiles.get(idx) + } else { + None + } } /// Get available exists from the given tile @@ -164,7 +168,7 @@ impl Map { /// Modify tile at the given location pub fn set_tile(&mut self, x: usize, y: usize, tile: Tile) { if x < self.width && y < self.height { - let idx = self.xy_idx(x as usize, y as usize); + let idx = self.xy_idx(x, y); self.tiles[idx] = tile; } } @@ -178,7 +182,7 @@ impl Map { pub fn add_room(&mut self, rect: Rect) { for x in rect.x1..rect.x2 { for y in rect.y1..rect.y2 { - self.set_tile(x as usize, y as usize, Tile::floor()); + self.set_tile(x, y, Tile::floor()); } } self.rooms.push(rect); @@ -279,7 +283,7 @@ impl fmt::Display for Map { .map(|x| if self.at(x, y).unwrap().is_blocked { '#' } else { ' ' } as u8) .collect(); let line = String::from_utf8(bytes).expect("Can't convert map to string"); - let _ = writeln!(f, "{}", line); + let _ = writeln!(f, "{line}"); } Ok(()) }