diff options
Diffstat (limited to 'src/level_gen')
-rw-r--r-- | src/level_gen/cave_automata.rs | 23 | ||||
-rw-r--r-- | src/level_gen/mod.rs | 2 | ||||
-rw-r--r-- | src/level_gen/util.rs | 10 |
3 files changed, 16 insertions, 19 deletions
diff --git a/src/level_gen/cave_automata.rs b/src/level_gen/cave_automata.rs index de584f4111ab..e5e2807ab251 100644 --- a/src/level_gen/cave_automata.rs +++ b/src/level_gen/cave_automata.rs @@ -53,12 +53,12 @@ impl Default for Params { } pub fn generate<R: Rng + ?Sized>( - dimensions: &Dimensions, + dimensions: Dimensions, params: &Params, rand: &mut R, ) -> Vec<Vec<bool>> { let mut cells = - rand_initialize(&dimensions, rand, params.chance_to_start_alive); + rand_initialize(dimensions, rand, params.chance_to_start_alive); for _ in 0..params.steps { step_automata(&mut cells, dimensions, params); } @@ -70,7 +70,7 @@ pub fn generate<R: Rng + ?Sized>( fn step_automata( cells: &mut Vec<Vec<bool>>, - dimensions: &Dimensions, + dimensions: Dimensions, params: &Params, ) { let orig_cells = (*cells).clone(); @@ -83,12 +83,10 @@ fn step_automata( } else { cells[x][y] = true; } + } else if nbs > params.birth_limit { + cells[x][y] = true; } else { - if nbs > params.birth_limit { - cells[x][y] = true; - } else { - cells[x][y] = false; - } + cells[x][y] = false; } } } @@ -96,7 +94,7 @@ fn step_automata( const COUNT_EDGES_AS_NEIGHBORS: bool = true; -fn num_alive_neighbors(cells: &Vec<Vec<bool>>, x: i32, y: i32) -> i32 { +fn num_alive_neighbors(cells: &[Vec<bool>], x: i32, y: i32) -> i32 { let mut count = 0; for i in -1..2 { for j in -1..2 { @@ -107,15 +105,14 @@ fn num_alive_neighbors(cells: &Vec<Vec<bool>>, x: i32, y: i32) -> i32 { let neighbor_x = x + i; let neighbor_y = y + j; - if COUNT_EDGES_AS_NEIGHBORS + if (COUNT_EDGES_AS_NEIGHBORS && (neighbor_x < 0 || neighbor_y < 0 || neighbor_x >= (cells.len() as i32) - || neighbor_y >= (cells[0].len()) as i32) + || neighbor_y >= (cells[0].len()) as i32)) + || cells[neighbor_x as usize][neighbor_y as usize] { count += 1; - } else if cells[neighbor_x as usize][neighbor_y as usize] { - count += 1; } } } diff --git a/src/level_gen/mod.rs b/src/level_gen/mod.rs index df742bb3a1b0..d796a103b11e 100644 --- a/src/level_gen/mod.rs +++ b/src/level_gen/mod.rs @@ -68,7 +68,7 @@ pub fn draw_level<W: io::Write>( level: Vec<Vec<bool>>, out: &mut W, ) -> io::Result<()> { - if level.len() == 0 { + if level.is_empty() { return Ok(()); } diff --git a/src/level_gen/util.rs b/src/level_gen/util.rs index c9cd87309257..4f56fe6c9557 100644 --- a/src/level_gen/util.rs +++ b/src/level_gen/util.rs @@ -1,7 +1,7 @@ use crate::types::Dimensions; use rand::{distributions, Rng}; -pub fn falses(dims: &Dimensions) -> Vec<Vec<bool>> { +pub fn falses(dims: Dimensions) -> Vec<Vec<bool>> { let mut ret = Vec::with_capacity(dims.h as usize); for _ in 0..dims.h { let mut row = Vec::with_capacity(dims.w as usize); @@ -16,7 +16,7 @@ pub fn falses(dims: &Dimensions) -> Vec<Vec<bool>> { /// Randomly initialize a 2-dimensional boolean vector of the given /// `Dimensions`, using the given random number generator and alive chance pub fn rand_initialize<R: Rng + ?Sized>( - dims: &Dimensions, + dims: Dimensions, rng: &mut R, alive_chance: f64, ) -> Vec<Vec<bool>> { @@ -40,9 +40,9 @@ pub fn fill_outer_edges(level: &mut Vec<Vec<bool>>) { } let ymax = level[0].len(); - for x in 0..xmax { - level[x][0] = true; - level[x][ymax - 1] = true; + for row in level.iter_mut() { + row[0] = true; + row[ymax - 1] = true; } for y in 0..level[0].len() { |