chore: Resolve clippy warnings and add a bounds check.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2023-01-31 17:24:11 -06:00
parent 92047cd102
commit d4f8c99c19
5 changed files with 15 additions and 11 deletions

View File

@ -63,7 +63,7 @@ impl<D: Clone + Default> DijkstraMap<D> {
/// WARNING: Will give incorrect results when used with non-uniform exit costs. Much slower /// WARNING: Will give incorrect results when used with non-uniform exit costs. Much slower
/// algorithm required to support that. /// algorithm required to support that.
fn build(&mut self, map: &Map<D>) { fn build(&mut self, map: &Map<D>) {
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); let mut open_list: VecDeque<((usize, usize), f32)> = VecDeque::with_capacity(mapsize);
if let Some(pos) = map.starting_point { if let Some(pos) = map.starting_point {

View File

@ -110,8 +110,8 @@ impl<D: Clone + Default> BspRooms<D> {
let rect_width = rect.width(); let rect_width = rect.width();
let rect_height = rect.height(); let rect_height = rect.height();
let w = usize::max(3, rng.random_range(1, usize::min(rect_width 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 as usize, 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.x1 += rng.random_range(0, 6);
result.y1 += rng.random_range(0, 6); result.y1 += rng.random_range(0, 6);
@ -151,7 +151,7 @@ impl<D: Clone + Default> BspRooms<D> {
can_build = false; can_build = false;
} }
if can_build { 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() { if tile.is_walkable() {
can_build = false; can_build = false;
} }

View File

@ -160,7 +160,7 @@ impl<'a, D: Clone + Default> Grid<'a, D> {
if neighbors.len() == 1 { if neighbors.len() == 1 {
return Some(neighbors[0]); return Some(neighbors[0]);
} else { } 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 None

View File

@ -47,7 +47,7 @@ impl<D: Clone + Default> VoronoiHive<D> {
let seeds = self.generate_seeds(rng, map.width, map.height); let seeds = self.generate_seeds(rng, map.width, map.height);
let mut voronoi_distance = vec![(0, 0.0f32); self.n_seeds]; let mut voronoi_distance = vec![(0, 0.0f32); self.n_seeds];
let mut voronoi_membership: Vec<i32> = vec![0; map.width as usize * map.height as usize]; let mut voronoi_membership: Vec<i32> = vec![0; map.width * map.height];
for (i, vid) in voronoi_membership.iter_mut().enumerate() { for (i, vid) in voronoi_membership.iter_mut().enumerate() {
let x = i % map.width; let x = i % map.width;
let y = i / map.width; let y = i / map.width;

View File

@ -113,8 +113,12 @@ impl<D: Clone + Default> Map<D> {
/// Get TileType at the given location /// Get TileType at the given location
pub fn at(&self, x: usize, y: usize) -> Option<&Tile> { pub fn at(&self, x: usize, y: usize) -> Option<&Tile> {
let idx = (y as usize) * self.width + (x as usize); if x < self.width && y < self.height {
self.tiles.get(idx) let idx = y * self.width + x;
self.tiles.get(idx)
} else {
None
}
} }
/// Get available exists from the given tile /// Get available exists from the given tile
@ -164,7 +168,7 @@ impl<D: Clone + Default> Map<D> {
/// Modify tile at the given location /// Modify tile at the given location
pub fn set_tile(&mut self, x: usize, y: usize, tile: Tile) { pub fn set_tile(&mut self, x: usize, y: usize, tile: Tile) {
if x < self.width && y < self.height { 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; self.tiles[idx] = tile;
} }
} }
@ -178,7 +182,7 @@ impl<D: Clone + Default> Map<D> {
pub fn add_room(&mut self, rect: Rect) { pub fn add_room(&mut self, rect: Rect) {
for x in rect.x1..rect.x2 { for x in rect.x1..rect.x2 {
for y in rect.y1..rect.y2 { 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); self.rooms.push(rect);
@ -279,7 +283,7 @@ impl<D: Clone + Default> fmt::Display for Map<D> {
.map(|x| if self.at(x, y).unwrap().is_blocked { '#' } else { ' ' } as u8) .map(|x| if self.at(x, y).unwrap().is_blocked { '#' } else { ' ' } as u8)
.collect(); .collect();
let line = String::from_utf8(bytes).expect("Can't convert map to string"); let line = String::from_utf8(bytes).expect("Can't convert map to string");
let _ = writeln!(f, "{}", line); let _ = writeln!(f, "{line}");
} }
Ok(()) Ok(())
} }