pathfinding/map/
point.rs

1#[cfg(feature = "point_poll")]
2use crate::map::PointPool;
3
4pub type IndexType = i32;
5
6#[derive(Debug, Clone, Default)]
7pub struct Point {
8    pub x: IndexType,
9    pub y: IndexType,
10
11    pub f: IndexType,
12    pub g: IndexType,
13    pub h: IndexType,
14}
15
16impl Ord for Point {
17    #[inline]
18    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
19        other.f.cmp(&self.f)
20    }
21}
22
23impl PartialOrd for Point {
24    #[inline]
25    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
26        Some(self.cmp(other))
27    }
28}
29
30impl PartialEq for Point {
31    #[inline]
32    fn eq(&self, other: &Self) -> bool {
33        self.x == other.x && self.y == other.y
34    }
35
36    #[inline]
37    fn ne(&self, other: &Self) -> bool {
38        self.x != other.x || self.y != other.y
39    }
40}
41
42impl Eq for Point {}
43
44impl Point {
45    #[cfg(feature = "point_poll")]
46    #[inline]
47    pub fn new_other(
48        x: IndexType,
49        y: IndexType,
50        f: IndexType,
51        g: IndexType,
52        h: IndexType,
53    ) -> Box<Point> {
54        PointPool::get_instance()
55            .write()
56            .unwrap()
57            .allocate(x, y, f, g, h)
58    }
59
60    #[cfg(not(feature = "point_poll"))]
61    #[inline]
62    pub fn new_other(
63        x: IndexType,
64        y: IndexType,
65        f: IndexType,
66        g: IndexType,
67        h: IndexType,
68    ) -> Point {
69        Point::new(x, y, f, g, h)
70    }
71
72    pub fn new(x: IndexType, y: IndexType, f: IndexType, g: IndexType, h: IndexType) -> Point {
73        Point { x, y, f, g, h }
74    }
75
76    #[inline]
77    pub fn f(&self) -> IndexType {
78        self.g + self.h
79    }
80
81    #[inline]
82    pub fn neighbors(&self) -> Vec<(IndexType, IndexType)> {
83        vec![
84            (-1, 0),
85            (-1, -1),
86            (0, -1),
87            (1, -1),
88            (1, 0),
89            (1, 1),
90            (0, 1),
91            (-1, 1),
92        ]
93    }
94}
95
96#[cfg(feature = "point_poll")]
97impl Drop for Point {
98    fn drop(&mut self) {
99        let stolen_point = std::mem::replace(&mut *self, Point::default());
100        PointPool::get_instance()
101            .write()
102            .unwrap()
103            .deallocate(stolen_point)
104    }
105}