core_pb/grid/
standard_grid.rs

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
#[cfg(feature = "std")]
use crate::grid::computed_grid::ComputedGrid;
use crate::grid::Grid;
#[cfg(feature = "std")]
use core::f32::consts::PI;
use nalgebra::Point2;
#[cfg(feature = "std")]
use nalgebra::{Isometry2, Vector2};
#[cfg(feature = "std")]
use pacbot_rs::variables::PACMAN_SPAWN_LOC;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Default, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)]
pub enum StandardGrid {
    #[default]
    Pacman,
    Playground,
    Blank,
    Outer,
    Open,
}

#[allow(dead_code)]
impl StandardGrid {
    /// Get a list of all available grids
    pub fn get_all() -> [Self; 5] {
        [
            Self::Pacman,
            Self::Playground,
            Self::Outer,
            Self::Blank,
            Self::Open,
        ]
    }

    /// Get the [`Grid`] associated with this enum
    ///
    /// From generated file
    pub fn get_grid(&self) -> Grid {
        match self {
            Self::Pacman => GRID_PACMAN,
            Self::Playground => GRID_PLAYGROUND,
            Self::Outer => GRID_OUTER,
            Self::Blank => GRID_BLANK,
            Self::Open => GRID_OPEN,
        }
    }

    /// Get the [`ComputedGrid`] associated with this enum
    #[cfg(feature = "std")]
    pub fn compute_grid(self) -> ComputedGrid {
        self.into()
    }

    /// Get the default Pacbot [`Isometry2`] associated with this enum
    #[cfg(feature = "std")]
    pub fn get_default_pacbot_isometry(&self) -> Isometry2<f32> {
        match self {
            StandardGrid::Pacman => Isometry2::new(
                Vector2::new(PACMAN_SPAWN_LOC.row as f32, PACMAN_SPAWN_LOC.col as f32),
                PI / 2.0,
            ),
            StandardGrid::Playground => Isometry2::new(Vector2::new(1.0, 1.0), 0.0),
            StandardGrid::Outer => Isometry2::new(Vector2::new(1.0, 1.0), 0.0),
            StandardGrid::Blank => Isometry2::new(Vector2::new(1.0, 1.0), 0.0),
            StandardGrid::Open => Isometry2::new(Vector2::new(16.0, 16.0), 0.0),
        }
    }

    /// Get the part of the [`Grid`] that should actually show on the gui
    pub fn get_soft_boundaries(&self) -> (Point2<f32>, Point2<f32>) {
        match self {
            Self::Pacman => (Point2::new(-1.0, -1.0), Point2::new(31.0, 28.0)),
            Self::Playground => (Point2::new(25.0, 22.0), Point2::new(32.0, 32.0)),
            _ => (Point2::new(-1.0, -1.0), Point2::new(32.0, 32.0)),
        }
    }

    /// Get the rectangles (in grid coordinates) that should be repainted with the background color
    #[cfg(feature = "std")]
    pub fn get_outside_soft_boundaries(&self) -> Vec<(Point2<f32>, Point2<f32>)> {
        match self {
            Self::Pacman => vec![
                (Point2::new(-1.0, 28.0), Point2::new(32.1, 32.1)),
                (Point2::new(31.0, -1.0), Point2::new(32.1, 32.1)),
            ],
            Self::Playground => vec![
                (Point2::new(-1.0, -1.0), Point2::new(25.0, 32.1)),
                (Point2::new(-1.0, -1.0), Point2::new(32.1, 22.0)),
            ],
            _ => vec![],
        }
    }
}

include!(concat!(env!("OUT_DIR"), "/generated_grids.rs"));

/// The official Pacbot [`Grid`]
///
/// Out-of-bounds areas are replaced with walls to adhere to ComputedGrid rules
///
/// ```
/// use core_pb::grid::standard_grid::GRID_PACMAN;
/// use core_pb::grid::Grid;
/// use core_pb::grid::computed_grid::ComputedGrid;
///
/// let grid: Grid = GRID_PACMAN;
/// let computed_grid: ComputedGrid = grid.try_into().unwrap();
/// ```
#[rustfmt::skip]
pub const GRID_PACMAN: Grid = GENERATED_GRID_PACMAN;

/// A (mostly) blank [`Grid`] - (1, 1) is walkable
///
/// ```
/// use core_pb::grid::standard_grid::GRID_BLANK;
/// use core_pb::grid::Grid;
/// use core_pb::grid::computed_grid::ComputedGrid;
///
/// let grid: Grid = GRID_BLANK;
/// let computed_grid: ComputedGrid = grid.try_into().unwrap();
/// ```
#[rustfmt::skip]
pub const GRID_BLANK: Grid = GENERATED_GRID_BLANK;

/// A special [`Grid`] with no internal walls
///
/// ```
/// use core_pb::grid::standard_grid::GRID_OPEN;
/// use core_pb::grid::Grid;
/// use core_pb::grid::computed_grid::ComputedGrid;
///
/// let grid: Grid = GRID_OPEN;
/// let computed_grid: ComputedGrid = grid.try_into().unwrap();
/// ```
#[rustfmt::skip]
pub const GRID_OPEN: Grid = GENERATED_GRID_OPEN;

/// A [`Grid`] where the outermost path is empty
///
/// ```
/// use core_pb::grid::standard_grid::GRID_OUTER;
/// use core_pb::grid::Grid;
/// use core_pb::grid::computed_grid::ComputedGrid;
///
/// let grid: Grid = GRID_OUTER;
/// let computed_grid: ComputedGrid = grid.try_into().unwrap();
/// ```
#[rustfmt::skip]
pub const GRID_OUTER: Grid = GENERATED_GRID_OUTER;

/// A [`Grid`] with many smaller paths to practice maneuvering
///
/// ```
/// use core_pb::grid::standard_grid::GRID_PLAYGROUND;
/// use core_pb::grid::Grid;
/// use core_pb::grid::computed_grid::ComputedGrid;
///
/// let grid: Grid = GRID_PLAYGROUND;
/// let computed_grid: ComputedGrid = grid.try_into().unwrap();
/// ```
#[rustfmt::skip]
pub const GRID_PLAYGROUND: Grid = GENERATED_GRID_PLAYGROUND;