• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

naomijub / bevy_knossos / 14673949004

25 Apr 2025 09:38PM UTC coverage: 89.164% (-0.06%) from 89.22%
14673949004

push

github

web-flow
Update bevy 016 and Edition 2024 (#9)

Chore: Update to bevy 0.16 and to Edition 2024.

- adds pedantic linting

54 of 68 new or added lines in 18 files covered. (79.41%)

1 existing line in 1 file now uncovered.

864 of 969 relevant lines covered (89.16%)

1.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/src/maze/algorithms/binary_tree.rs
1
use super::Algorithm;
2
use crate::maze::grid::cell::Cell;
3
use crate::maze::grid::Grid;
4
use crate::utils::types::Coords;
5
use clap::ValueEnum;
6
use rand::prelude::*;
7

8
/// An enumeration over supported biases for the "Binary Tree" algorithm
9
///
10
/// Each bias represents the two of four sides of the maze that will be spanned
11
/// by a single corridor.
12
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
13
pub enum Bias {
14
    /// Produces two long corridors on the Northern and Western sides of the maze
15
    NorthWest,
16

17
    /// Produces two long corridors on the Northern and Eastern sides of the maze
18
    NorthEast,
19

20
    /// Produces two long corridors on the Southern and Western sides of the maze
21
    SouthWest,
22

23
    /// Produces two long corridors on the Southern and Eastern sides of the maze
24
    SouthEast,
25
}
26

27
/// The "Binary Tree" algorithm for generating mazes
28
///
29
/// This is an almost-trivially simple one, but you pay for that simplicity with a few side effects:
30
/// a notable bias (routes tend to run diagonally) and long corridors spanning two sides. Still,
31
/// this is quite a performant algorithm since it operates without any state at all looking at the
32
/// current cell only, without regard for the rest of the cells and rows in the maze.
33
pub struct BinaryTree {
34
    bias: Bias,
35
}
36

37
impl BinaryTree {
38
    /// Create a new instance of the algorithm with a given bias
39
    ///
40
    /// # Example
41
    /// ```
42
    /// use bevy_knossos::maze::{BinaryTree, Bias};
43
    ///
44
    /// let algorithm = BinaryTree::new(Bias::NorthWest);
45
    /// ```
46
    #[must_use]
47
    pub const fn new(bias: Bias) -> BinaryTree {
1✔
48
        BinaryTree { bias }
49
    }
50

51
    fn populate_dirs(&self, coords: Coords, grid: &Grid) -> Vec<Cell> {
1✔
52
        let mut dirs = vec![];
1✔
53
        let (x, y) = coords;
1✔
54

55
        match self.bias {
1✔
56
            Bias::NorthWest => {
57
                if y > 0 {
1✔
58
                    dirs.push(Cell::NORTH);
2✔
59
                }
60
                if x > 0 {
1✔
61
                    dirs.push(Cell::WEST);
1✔
62
                }
63
            }
64
            Bias::NorthEast => {
65
                if y > 0 {
1✔
66
                    dirs.push(Cell::NORTH);
1✔
67
                }
68
                if x + 1 < grid.width() {
2✔
69
                    dirs.push(Cell::EAST);
1✔
70
                }
71
            }
72
            Bias::SouthWest => {
73
                if y + 1 < grid.height() {
2✔
74
                    dirs.push(Cell::SOUTH);
1✔
75
                }
76
                if x > 0 {
1✔
77
                    dirs.push(Cell::WEST);
1✔
78
                }
79
            }
80
            Bias::SouthEast => {
81
                if y + 1 < grid.height() {
2✔
82
                    dirs.push(Cell::SOUTH);
1✔
83
                }
84
                if x + 1 < grid.width() {
2✔
85
                    dirs.push(Cell::EAST);
1✔
86
                }
87
            }
88
        }
89

90
        dirs
1✔
91
    }
92
}
93

94
/// An implementation of the "Binary Tree" algorithm for generating mazes.
95
/// Does not support start coords.
96
///
97
/// The algorithm is pretty simple: for every cell in the grid, randomly carve a passage either
98
/// north, or west.
99
///  
100
/// # Warn
101
///
102
/// The `generate` function will warn in case a [`start_coords`] is passed.
103
impl Algorithm for BinaryTree {
104
    fn generate(&mut self, grid: &mut Grid, c: Option<Coords>, rng: &mut StdRng) {
1✔
105
        if c.is_some() {
1✔
NEW
106
            eprintln!("Algorithm `{}` doesn't suppoer `start_coords`", self.name());
×
107
        }
108
        for y in 0..grid.height() {
2✔
109
            for x in 0..grid.width() {
2✔
110
                let dirs = self.populate_dirs((x, y), grid);
1✔
111
                if let Some(dir) = dirs.choose(rng) {
2✔
112
                    grid.carve_passage((x, y), *dir).ok();
2✔
113
                }
114
            }
115
        }
116
    }
117

118
    fn has_start_coords(&self) -> bool {
×
119
        false
120
    }
121

122
    fn name(&self) -> &'static str {
×
123
        "BinaryTree"
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc