Skip to main content

unbanished_path/sectors/
search.rs

1//! Searches over the sector graph: the open list, node paths and distances, and the distance cache.
2
3use super::*;
4
5impl SectorGraph {
6    pub(super) fn push(&mut self, node: i32, parent: i32, g: i32, f: i32) {
7        let s = self.search[node as usize];
8        if s.open == self.search_id {
9            self.open.remove(&((s.key.0, Reverse(s.key.1)), node));
10        }
11        let counter = self.counter;
12        self.counter += 1;
13        let s = &mut self.search[node as usize];
14        s.closed = s.closed.wrapping_sub(1);
15        s.parent = parent;
16        s.open = self.search_id;
17        s.g = g;
18        s.key = (f, counter);
19        self.open.insert(((f, Reverse(counter)), node));
20    }
21
22    /// `FUN_14001e560` / `FUN_14001e630`: node path from `a` to `b`.
23    pub(super) fn node_path(&mut self, a: i32, b: i32) -> Option<Vec<i32>> {
24        self.search_id = self.search_id.wrapping_add(1);
25        if self.search.len() < self.nodes.len() {
26            self.search.resize(self.nodes.len(), Search::default());
27        }
28        self.open.clear();
29        self.counter = 0;
30        let h = length(self.nodes[b as usize].pos, self.nodes[a as usize].pos);
31        self.push(a, -1, 0, h);
32        let mut found = false;
33        while let Some(&first) = self.open.first() {
34            let cur = first.1;
35            if cur == b {
36                found = true;
37                break;
38            }
39            self.open.remove(&first);
40            let id = self.search_id;
41            let s = &mut self.search[cur as usize];
42            s.open = s.open.wrapping_sub(1);
43            s.closed = id;
44            let g_cur = s.g;
45            let pos = self.nodes[cur as usize].pos;
46            for k in 0..self.nodes[cur as usize].links.len() {
47                let nb = self.nodes[cur as usize].links[k];
48                if nb < 0 {
49                    continue;
50                }
51                let npos = self.nodes[nb as usize].pos;
52                let g = length(npos, pos) + g_cur;
53                let s = &mut self.search[nb as usize];
54                if s.closed == id {
55                    if s.g <= g {
56                        continue;
57                    }
58                    s.closed = s.closed.wrapping_sub(1);
59                }
60                if s.open != id || g < s.g {
61                    let f = length(self.nodes[b as usize].pos, npos) + g;
62                    self.push(nb, cur, g, f);
63                }
64            }
65        }
66        if !found {
67            return None;
68        }
69        let mut path = Vec::new();
70        let mut c = b;
71        while c != -1 {
72            path.push(c);
73            c = self.search[c as usize].parent;
74        }
75        path.reverse();
76        path.truncate(MAX_NODES);
77        Some(path)
78    }
79
80    /// `FUN_1400ae070`: summed hop lengths of the node path (0 for fewer than 2 nodes).
81    pub(super) fn node_distance(&mut self, a: i32, b: i32, types: &[CellType]) -> i32 {
82        self.rebuild(types);
83        let Some(path) = self.node_path(a, b) else {
84            return 0;
85        };
86        path.windows(2)
87            .map(|w| length(self.nodes[w[1] as usize].pos, self.nodes[w[0] as usize].pos))
88            .sum()
89    }
90
91    /// The path-rank distance from cell `a` to cell `b`, or -1 when `b` cannot be reached.
92    /// `reachable` is the caller's region test (both cells in the same walkable region); `store`
93    /// keeps a computed distance in the cache.
94    ///
95    /// Original: `FUN_1400a4ce0`.
96    pub fn distance(
97        &mut self,
98        a: i32,
99        b: i32,
100        reachable: bool,
101        store: bool,
102        types: &[CellType],
103    ) -> i32 {
104        if !reachable {
105            return -1;
106        }
107        let na = self.node_near(a);
108        let nb = self.node_near(b);
109        if na < 0 || nb < 0 {
110            return -1;
111        }
112        if na == nb {
113            return 1;
114        }
115        let key = (na.max(nb), na.min(nb));
116        if let Some(&slot) = self.cache.index.get(&key) {
117            if let Some((_, v, _)) = self.cache.ring[slot] {
118                return v;
119            }
120        }
121        let d = self.node_distance(na, nb, types);
122        if store {
123            self.cache_store(key, d);
124        }
125        d
126    }
127
128    /// `FUN_1400ab000`.
129    pub(super) fn cache_store(&mut self, key: (i32, i32), value: i32) {
130        if let Some(&slot) = self.cache.index.get(&key) {
131            if let Some(e) = &mut self.cache.ring[slot] {
132                e.1 = value;
133                return;
134            }
135        }
136        let slot = self.cache.head;
137        self.cache.head = (slot + 1) % CACHE_SIZE;
138        if self.cache.head == self.cache.tail {
139            self.evict_oldest();
140        }
141        self.cache.ring[slot] = Some((key, value, self.cache.time));
142        self.cache.index.insert(key, slot);
143    }
144
145    pub(super) fn evict_oldest(&mut self) {
146        let t = self.cache.tail;
147        if let Some((k, _, _)) = self.cache.ring[t].take() {
148            if self.cache.index.get(&k) == Some(&t) {
149                self.cache.index.remove(&k);
150            }
151        }
152        self.cache.tail = (t + 1) % CACHE_SIZE;
153    }
154
155    /// Age the cache by `dt` ticks, and drop the entries older than 120 seconds. The game calls it
156    /// once per simulation iteration.
157    ///
158    /// Original: `FUN_1400ab1e0`.
159    pub fn age_cache(&mut self, dt: i64) {
160        self.cache.time += dt;
161        loop {
162            let t = self.cache.tail;
163            let entry_time = self.cache.ring[t].map_or(0, |e| e.2);
164            if !(CACHE_LIFETIME < self.cache.time - entry_time) {
165                return;
166            }
167            if t == self.cache.head {
168                return;
169            }
170            self.evict_oldest();
171        }
172    }
173}