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
use crate::navmesh::HammerUnit;
pub use crate::navmesh::{
ApproachArea, Connections, EncounterPath, LadderConnections, LadderDirection, LightIntensity,
NavDirection, NavHidingSpot, Vector3, VisibleArea,
};
use crate::parser::read_areas;
pub use crate::parser::{NavArea, ParseError};
use aabb_quadtree::{ItemId, QuadTree};
use euclid::{TypedPoint2D, TypedRect, TypedSize2D};
mod navmesh;
mod parser;
type Rect = TypedRect<f32, HammerUnit>;
pub struct NavTree(QuadTree<NavArea, HammerUnit, [(ItemId, Rect); 4]>);
pub fn get_area_tree(data: Vec<u8>) -> Result<NavTree, ParseError> {
let areas = read_areas(data)?;
let (min_x, min_y, max_x, max_y) = areas.iter().fold(
(f32::MAX, f32::MAX, f32::MIN, f32::MIN),
|(min_x, min_y, max_x, max_y), area| {
(
f32::min(min_x, area.north_west.0),
f32::min(min_y, area.north_west.1),
f32::max(max_x, area.south_east.0),
f32::max(max_y, area.south_east.1),
)
},
);
let mut tree = QuadTree::default(
Rect::new(
TypedPoint2D::new(min_x - 1.0, min_y - 1.0),
TypedSize2D::new(max_x - min_x + 2.0, max_y - min_y + 2.0),
),
areas.len(),
);
for area in areas {
tree.insert(area);
}
Ok(NavTree(tree))
}
impl NavTree {
pub fn query(&self, x: f32, y: f32) -> impl Iterator<Item = &NavArea> {
let query_box = Rect::new(TypedPoint2D::new(x, y), TypedSize2D::new(1.0, 1.0));
self.0.query(query_box).into_iter().map(|(area, ..)| area)
}
pub fn find_z_height<'a>(&'a self, x: f32, y: f32) -> impl Iterator<Item = f32> + 'a {
self.query(x, y).map(move |area| area.get_z_height(x, y))
}
pub fn find_best_height(&self, x: f32, y: f32, z_guess: f32) -> f32 {
let found_heights = self.find_z_height(x, y);
let best_z = f32::MIN;
found_heights.fold(best_z, |best_z, found_z| {
if (found_z - z_guess).abs() < (best_z - z_guess).abs() {
found_z
} else {
best_z
}
})
}
pub fn areas(&self) -> impl Iterator<Item = &NavArea> {
self.0.iter().map(|(_, (area, _))| area)
}
}
#[test]
fn test_tree() {
let file = std::fs::read("data/pl_badwater.nav").unwrap();
let tree = get_area_tree(file).unwrap();
let point1 = (1600.0, -1300.0);
assert_eq!(
vec![375.21506],
tree.find_z_height(point1.0, point1.1).collect::<Vec<f32>>()
);
let point2 = (360.0, -1200.0);
assert_eq!(
vec![290.2907, 108.144775],
tree.find_z_height(point2.0, point2.1).collect::<Vec<f32>>()
);
let point3 = (320.0, -1030.0);
assert_eq!(
vec![220.83125],
tree.find_z_height(point3.0, point3.1).collect::<Vec<f32>>()
);
let point4 = (205.0, -1030.0);
assert_eq!(
vec![147.23126],
tree.find_z_height(point4.0, point4.1).collect::<Vec<f32>>()
);
assert_eq!(
tree.query(point3.0, point3.1).next().map(|area| area.id),
tree.query(point4.0, point4.1).next().map(|area| area.id)
);
}
#[cfg(doctest)]
doc_comment::doctest!("../README.md");