Skip to content

Commit

Permalink
Fix hang on repeated dungeon generation calls
Browse files Browse the repository at this point in the history
  • Loading branch information
seilis committed Nov 16, 2024
1 parent 20e985b commit 59d12e3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/rpgtools/map/gridmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ impl GridMap {
}

pub fn generate_dungeon(&mut self, num_rooms: usize, room_size: usize) {
self.clear();

for _ in 0..num_rooms {
self.place_random_room(room_size, false);
}
Expand Down Expand Up @@ -582,4 +584,30 @@ impl GridMap {
// Room processing is done. Return.
out
}

/// Delete everything in this map and reset to nothing
fn clear(&mut self) {
for x in 0..self.xmax {
for y in 0..self.ymax {
self.cells[x][y].area = AreaType::Nothing;
}
}
}
}

#[cfg(test)]
mod tests {

use super::*;

/// Ensure that regenerating halls multiple times doesn't hang
#[test]
fn regenerate_dungeon() {
let mut map = GridMap::new(25, 25);

// This used to fail due to an infinite loop in the halls algorithm.
for _ in 0..10 {
map.generate_dungeon(10, 10);
}
}
}

0 comments on commit 59d12e3

Please sign in to comment.