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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use crate::Scalar;
use petgraph::{
    algo::{astar, tarjan_scc},
    graph::NodeIndex,
    visit::EdgeRef,
    Directed, Graph,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[cfg(not(feature = "scalar64"))]
use std::f32::MAX as SCALAR_MAX;
#[cfg(feature = "scalar64")]
use std::f64::MAX as SCALAR_MAX;
use std::{
    collections::{HashMap, HashSet},
    hash::Hash,
};
use typid::ID;

#[cfg(feature = "parallel")]
macro_rules! iter {
    ($v:expr) => {
        $v.par_iter()
    };
}
#[cfg(not(feature = "parallel"))]
macro_rules! iter {
    ($v:expr) => {
        $v.iter()
    };
}

/// Nav islands identifier.
pub type NavIslandsID = ID<NavIslands<(), ()>>;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct NavIslandPortal<Island, Portal>
where
    Island: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
    Portal: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
{
    #[serde(bound(deserialize = "Island: Serialize + DeserializeOwned"))]
    pub island: Island,
    #[serde(bound(deserialize = "Portal: Serialize + DeserializeOwned"))]
    pub portal: Option<Portal>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavIslandsConnection<Island, Portal>
where
    Island: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
    Portal: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
{
    #[serde(bound(
        deserialize = "Island: Serialize + DeserializeOwned, Portal: Serialize + DeserializeOwned"
    ))]
    pub from: NavIslandPortal<Island, Portal>,
    #[serde(bound(
        deserialize = "Island: Serialize + DeserializeOwned, Portal: Serialize + DeserializeOwned"
    ))]
    pub to: NavIslandPortal<Island, Portal>,
    pub distance: Scalar,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct NavIslands<Island, Portal>
where
    Island: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
    Portal: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
{
    id: NavIslandsID,
    costs: Vec<Scalar>,
    #[serde(bound(
        deserialize = "Island: Serialize + DeserializeOwned, Portal: Serialize + DeserializeOwned"
    ))]
    portals: Vec<NavIslandPortal<Island, Portal>>,
    graph: Graph<(), Scalar, Directed>,
    nodes: Vec<NodeIndex>,
    nodes_map: HashMap<NodeIndex, usize>,
}

impl<Island, Portal> NavIslands<Island, Portal>
where
    Island: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
    Portal: std::fmt::Debug + Clone + Eq + Hash + Send + Sync,
{
    pub fn new(connections: Vec<NavIslandsConnection<Island, Portal>>, both_ways: bool) -> Self {
        let portals = connections
            .iter()
            .map(|c| c.from.clone())
            .chain(connections.iter().map(|c| c.to.clone()))
            .collect::<HashSet<_>>()
            .into_iter()
            .collect::<Vec<_>>();
        let costs = vec![1.0; portals.len()];
        let mut graph =
            Graph::<(), Scalar, Directed>::with_capacity(portals.len(), connections.len());
        let nodes = (0..portals.len())
            .map(|_| graph.add_node(()))
            .collect::<Vec<_>>();
        for connection in connections {
            let ia = portals.iter().position(|c| &connection.from == c);
            let ib = portals.iter().position(|c| &connection.to == c);
            if let (Some(ia), Some(ib)) = (ia, ib) {
                graph.add_edge(nodes[ia], nodes[ib], connection.distance);
                if both_ways {
                    graph.add_edge(nodes[ib], nodes[ia], connection.distance);
                }
            }
        }
        let nodes_map = iter!(nodes).enumerate().map(|(i, n)| (*n, i)).collect();
        Self {
            id: NavIslandsID::new(),
            costs,
            portals,
            graph,
            nodes,
            nodes_map,
        }
    }

    #[inline]
    pub fn id(&self) -> NavIslandsID {
        self.id
    }

    #[inline]
    pub fn portals(&self) -> &[NavIslandPortal<Island, Portal>] {
        &self.portals
    }

    #[inline]
    pub fn portals_costs(&self) -> &[Scalar] {
        &self.costs
    }

    #[inline]
    pub fn set_portal_cost(
        &mut self,
        portal: &NavIslandPortal<Island, Portal>,
        cost: Scalar,
    ) -> Option<Scalar> {
        let index = self.index(portal)?;
        let c = self.costs.get_mut(index)?;
        let old = *c;
        *c = cost.max(0.0);
        Some(old)
    }

    pub fn neighbors(
        &self,
        portal: &NavIslandPortal<Island, Portal>,
    ) -> Option<impl Iterator<Item = &NavIslandPortal<Island, Portal>> + '_> {
        let index = self.index(portal)?;
        let node = self.nodes[index];
        Some(self.graph.neighbors(node).filter_map(|node| {
            self.nodes_map
                .get(&node)
                .and_then(|index| self.portal(*index))
        }))
    }

    pub fn find_path(
        &self,
        from: &NavIslandPortal<Island, Portal>,
        to: &NavIslandPortal<Island, Portal>,
    ) -> Option<(Scalar, Vec<&NavIslandPortal<Island, Portal>>)> {
        self.find_path_custom(from, to, |_, _| true)
    }

    // filter params: first island-portal, second island-portal.
    pub fn find_path_custom<F>(
        &self,
        from: &NavIslandPortal<Island, Portal>,
        to: &NavIslandPortal<Island, Portal>,
        mut filter: F,
    ) -> Option<(Scalar, Vec<&NavIslandPortal<Island, Portal>>)>
    where
        F: FnMut(&NavIslandPortal<Island, Portal>, &NavIslandPortal<Island, Portal>) -> bool,
    {
        let start_index = self.index(from)?;
        let end_index = self.index(to)?;
        let start_node = *self.nodes.get(start_index)?;
        let end_node = *self.nodes.get(end_index)?;
        let (distance, nodes) = astar(
            &self.graph,
            start_node,
            |n| n == end_node,
            |e| {
                let a = self.nodes_map[&e.source()];
                let b = self.nodes_map[&e.target()];
                let w = *e.weight();
                if filter(self.portal(a).unwrap(), self.portal(b).unwrap()) {
                    let a = self.costs[a];
                    let b = self.costs[b];
                    w * a * b
                } else {
                    SCALAR_MAX
                }
            },
            |_| 0.0,
        )?;
        Some((
            distance,
            nodes
                .into_iter()
                .filter_map(|n| self.portal(self.nodes_map[&n]))
                .collect::<Vec<_>>(),
        ))
    }

    pub fn find_islands(&self) -> Vec<Vec<&NavIslandPortal<Island, Portal>>> {
        tarjan_scc(&self.graph)
            .into_iter()
            .map(|v| {
                v.into_iter()
                    .filter_map(|n| self.nodes_map.get(&n).and_then(|i| self.portal(*i)))
                    .collect::<Vec<_>>()
            })
            .filter(|v| !v.is_empty())
            .collect()
    }

    pub fn index(&self, portal: &NavIslandPortal<Island, Portal>) -> Option<usize> {
        self.portals.iter().position(|p| portal == p)
    }

    pub fn portal(&self, index: usize) -> Option<&NavIslandPortal<Island, Portal>> {
        self.portals.get(index)
    }
}