1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
pub use grid_2d::{Coord, Grid, Size};
use rand::Rng;

#[derive(Debug, Clone, Copy)]
enum WallDirection {
    East,
    South,
}

#[derive(Debug, Clone, Copy)]
struct Wall {
    coord: Coord,
    direction: WallDirection,
}

impl Wall {
    fn walls_around(coord: Coord) -> [Wall; 4] {
        [
            Wall {
                coord,
                direction: WallDirection::East,
            },
            Wall {
                coord,
                direction: WallDirection::South,
            },
            Wall {
                coord: coord - Coord::new(1, 0),
                direction: WallDirection::East,
            },
            Wall {
                coord: coord - Coord::new(0, 1),
                direction: WallDirection::South,
            },
        ]
    }
    fn coords(&self) -> [Coord; 2] {
        [
            self.coord,
            match self.direction {
                WallDirection::East => self.coord + Coord::new(1, 0),
                WallDirection::South => self.coord + Coord::new(0, 1),
            },
        ]
    }
}

struct Walls {
    east: bool,
    south: bool,
}

impl Walls {
    fn get_mut(&mut self, direction: WallDirection) -> &mut bool {
        match direction {
            WallDirection::East => &mut self.east,
            WallDirection::South => &mut self.south,
        }
    }
}

struct GenerationCell {
    in_maze: bool,
    seen_walls: Walls,
    passages: Walls,
}

impl Default for GenerationCell {
    fn default() -> Self {
        Self {
            in_maze: false,
            seen_walls: Walls {
                east: false,
                south: false,
            },
            passages: Walls {
                east: false,
                south: false,
            },
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub enum MazeCell {
    Wall,
    Passage,
}

pub struct MazeGenerator {
    grid: Grid<GenerationCell>,
    walls_to_visit: Vec<Wall>,
}

impl MazeGenerator {
    pub fn new(size: Size) -> Self {
        Self {
            grid: Grid::new_default(size),
            walls_to_visit: Vec::new(),
        }
    }

    fn add_wall(&mut self, wall: Wall) {
        if let Some(cell) = self.grid.get_mut(wall.coord) {
            let seen = cell.seen_walls.get_mut(wall.direction);
            if !*seen {
                *seen = true;
                self.walls_to_visit.push(wall);
            }
        }
    }

    fn add_cell_at(&mut self, coord: Coord) {
        if let Some(cell) = self.grid.get_mut(coord) {
            if !cell.in_maze {
                cell.in_maze = true;
                for &wall in Wall::walls_around(coord).iter() {
                    self.add_wall(wall);
                }
            }
        }
    }

    fn add_passage(&mut self, wall: Wall) {
        if let Some(cell) = self.grid.get_mut(wall.coord) {
            let passage = cell.passages.get_mut(wall.direction);
            *passage = true;
        }
    }

    fn process_wall(&mut self, wall: Wall) -> Option<()> {
        let [coord_a, coord_b] = wall.coords();
        let in_maze_a = self.grid.get(coord_a)?.in_maze;
        let in_maze_b = self.grid.get(coord_b)?.in_maze;
        if in_maze_a {
            if in_maze_b {
                return None;
            } else {
                self.add_cell_at(coord_b);
            }
        } else {
            if in_maze_b {
                self.add_cell_at(coord_a);
            } else {
                return None;
            }
        }
        self.add_passage(wall);
        Some(())
    }

    fn remove_random_wall<R: Rng>(&mut self, rng: &mut R) -> Option<Wall> {
        if self.walls_to_visit.is_empty() {
            None
        } else {
            let index = rng.gen_range(0..self.walls_to_visit.len());
            Some(self.walls_to_visit.swap_remove(index))
        }
    }

    fn build_maze(&self) -> Grid<MazeCell> {
        let size = self.grid.size() * 2 - Size::new(1, 1);
        let mut maze = Grid::new_clone(size, MazeCell::Wall);
        for (coord, cell) in self.grid.enumerate() {
            let maze_cell_coord = coord * 2;
            if cell.in_maze {
                if let Some(maze_cell) = maze.get_mut(maze_cell_coord) {
                    *maze_cell = MazeCell::Passage;
                }
            }
            if cell.passages.east {
                let coord = maze_cell_coord + Coord::new(1, 0);
                if let Some(maze_cell) = maze.get_mut(coord) {
                    *maze_cell = MazeCell::Passage;
                }
            }
            if cell.passages.south {
                let coord = maze_cell_coord + Coord::new(0, 1);
                if let Some(maze_cell) = maze.get_mut(coord) {
                    *maze_cell = MazeCell::Passage;
                }
            }
        }
        maze
    }

    pub fn generate<R: Rng>(&mut self, start: Coord, rng: &mut R) -> Grid<MazeCell> {
        for cell in self.grid.iter_mut() {
            *cell = GenerationCell::default();
        }
        self.add_cell_at(start);
        while let Some(wall) = self.remove_random_wall(rng) {
            self.process_wall(wall);
        }
        self.build_maze()
    }
}