Skip to main content

euv_core/game/algorithms/pathfinding/
struct.rs

1use super::*;
2
3/// Integer 2D cell coordinate `(x, y)` inside a grid or flow field.
4#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
5pub struct GridCoord {
6    /// Column index along the X axis.
7    #[get(type(copy))]
8    pub(crate) x: usize,
9    /// Row index along the Y axis.
10    #[get(type(copy))]
11    pub(crate) y: usize,
12}
13
14/// Shortest-path output produced by `GridPathfinder`.
15#[derive(Clone, Data, Debug, Default, PartialEq)]
16pub struct GridPathResult {
17    /// Ordered grid cells from start to goal inclusive.
18    pub(crate) path: Vec<GridCoord>,
19    /// Exact accumulated traversal cost along `path`.
20    #[get(type(copy))]
21    pub(crate) total_cost: f32,
22    /// Number of nodes expanded from the open priority queue.
23    #[get(type(copy))]
24    pub(crate) nodes_expanded: usize,
25}
26
27/// Reusable generation-stamped scratch buffers for zero-allocation grid pathfinding.
28#[derive(Clone, Debug, Default)]
29pub(crate) struct GridPathScratch {
30    /// Active search generation stamp avoiding O(W*H) array clears.
31    pub(crate) generation: u32,
32    /// Generation stamp marking cells discovered in the active search.
33    pub(crate) seen_gen: Vec<u32>,
34    /// Generation stamp marking cells closed in the active search.
35    pub(crate) closed_gen: Vec<u32>,
36    /// Best known g-score per flat cell index.
37    pub(crate) g_scores: Vec<f32>,
38    /// Parent flat cell index per visited cell.
39    pub(crate) came_from: Vec<usize>,
40    /// Binary min-heap of `(f_score, cell_index)` entries.
41    pub(crate) open_heap: Vec<(f32, usize)>,
42    /// Generation stamp marking active multi-goal target cells.
43    pub(crate) goal_flags: Vec<u32>,
44    /// Active multi-goal stamp counter.
45    pub(crate) goal_gen: u32,
46}
47
48/// Weighted 2D grid pathfinder supporting A*, multi-goal Dijkstra, and Jump Point Search.
49#[derive(Clone, Data, Debug)]
50pub struct GridPathfinder {
51    /// Number of grid columns.
52    #[get(type(copy))]
53    pub(crate) width: usize,
54    /// Number of grid rows.
55    #[get(type(copy))]
56    pub(crate) height: usize,
57    /// Minimum positive traversal cost across walkable cells for admissible heuristics.
58    #[get(type(copy))]
59    pub(crate) min_walkable_cost: f32,
60    /// Whether every walkable cell shares the same traversal cost (enabling JPS).
61    #[get(type(copy))]
62    pub(crate) uniform_walkable: bool,
63    /// Flat row-major traversal costs (`<= 0.0` or non-finite marks blocked cells).
64    #[get(pub(crate))]
65    #[get_mut(pub(crate))]
66    #[set(pub(crate))]
67    pub(crate) costs: Vec<f32>,
68    /// Reusable scratch storage keeping repeated queries allocation-free.
69    #[get(pub(crate))]
70    #[get_mut(pub(crate))]
71    #[set(pub(crate))]
72    pub(crate) scratch: GridPathScratch,
73}
74
75/// Convex 2D navigation polygon stored in counter-clockwise winding order.
76#[derive(Clone, Data, Debug, PartialEq)]
77pub struct NavPolygon2D {
78    /// Counter-clockwise ordered polygon perimeter vertices.
79    pub(crate) vertices: Vec<Vec2>,
80    /// Positive traversal cost multiplier for this polygon.
81    #[get(type(copy))]
82    pub(crate) cost_multiplier: f32,
83    /// Precomputed polygon centroid.
84    #[get(type(copy))]
85    pub(crate) center: Vec2,
86}
87
88/// Directed portal edge connecting one navigation polygon to an adjacent polygon.
89#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
90pub struct NavPortal2D {
91    /// Neighbor polygon index across this portal.
92    #[get(type(copy))]
93    pub(crate) target_polygon: usize,
94    /// Left portal endpoint when traversing outward into `target_polygon`.
95    #[get(type(copy))]
96    pub(crate) left: Vec2,
97    /// Right portal endpoint when traversing outward into `target_polygon`.
98    #[get(type(copy))]
99    pub(crate) right: Vec2,
100    /// Portal segment midpoint used for A* graph edge distances.
101    #[get(type(copy))]
102    pub(crate) midpoint: Vec2,
103}
104
105/// Reusable scratch buffers for `NavMesh2D` portal A* and funnel string-pulling.
106#[derive(Clone, Debug, Default)]
107pub(crate) struct NavMeshScratch {
108    /// Active search generation stamp.
109    pub(crate) generation: u32,
110    /// Discovery generation stamp per polygon.
111    pub(crate) seen_gen: Vec<u32>,
112    /// Closed generation stamp per polygon.
113    pub(crate) closed_gen: Vec<u32>,
114    /// Best accumulated g-score per polygon.
115    pub(crate) g_scores: Vec<f32>,
116    /// Parent polygon index per visited polygon.
117    pub(crate) came_from_poly: Vec<usize>,
118    /// Portal index within parent adjacency list used to enter each polygon.
119    pub(crate) came_from_portal: Vec<usize>,
120    /// Binary min-heap of `(f_score, polygon_index)` entries.
121    pub(crate) open_heap: Vec<(f32, usize)>,
122    /// Reusable portal corridor buffer `(left, right)` passed to the funnel solver.
123    pub(crate) portal_corridor: Vec<(Vec2, Vec2)>,
124}
125
126/// Convex polygon 2D navigation mesh with portal graph A* and Simple Stupid Funnel smoothing.
127#[derive(Clone, Data, Debug)]
128pub struct NavMesh2D {
129    /// Convex polygons normalized to counter-clockwise winding.
130    pub(crate) polygons: Vec<NavPolygon2D>,
131    /// Outgoing portals per polygon index.
132    pub(crate) adjacency: Vec<Vec<NavPortal2D>>,
133    /// Reusable scratch storage for path queries.
134    #[get(pub(crate))]
135    #[get_mut(pub(crate))]
136    #[set(pub(crate))]
137    pub(crate) scratch: NavMeshScratch,
138}
139
140/// Reusable scratch priority queue for `FlowFieldGrid` integration builds.
141#[derive(Clone, Debug, Default)]
142pub(crate) struct FlowFieldScratch {
143    /// Binary min-heap of `(integration_cost, cell_index)` entries.
144    pub(crate) open_heap: Vec<(u32, usize)>,
145}
146
147/// Integration and flow-vector grid enabling O(1) steering lookup for thousands of crowd units.
148#[derive(Clone, Data, Debug)]
149pub struct FlowFieldGrid {
150    /// Number of columns.
151    #[get(type(copy))]
152    pub(crate) width: usize,
153    /// Number of rows.
154    #[get(type(copy))]
155    pub(crate) height: usize,
156    /// World-space size of each square grid cell.
157    #[get(type(copy))]
158    pub(crate) cell_size: f32,
159    /// World-space origin `(min_x, min_y)` of cell `(0, 0)`.
160    #[get(type(copy))]
161    pub(crate) origin: Vec2,
162    /// Per-cell movement cost (`1..=254` walkable, `255` impassable).
163    pub(crate) costs: Vec<u8>,
164    /// Dijkstra integration distance to the nearest goal (`u32::MAX` unreachable).
165    pub(crate) integration: Vec<u32>,
166    /// Unit flow direction vector per cell pointing downhill toward the nearest goal.
167    pub(crate) flow_vectors: Vec<Vec2>,
168    /// Reusable priority queue scratch buffer.
169    #[get(pub(crate))]
170    #[get_mut(pub(crate))]
171    #[set(pub(crate))]
172    pub(crate) scratch: FlowFieldScratch,
173}
174
175/// Crowd agent state for 2D Reciprocal Velocity Obstacles (ORCA / RVO2).
176#[derive(Clone, Copy, Data, Debug, PartialEq)]
177pub struct OrcaAgent2D {
178    /// Current 2D world position.
179    #[get(type(copy))]
180    pub(crate) position: Vec2,
181    /// Current 2D velocity vector.
182    #[get(type(copy))]
183    pub(crate) velocity: Vec2,
184    /// Desired 2D preferred velocity toward the agent's waypoint or goal.
185    #[get(type(copy))]
186    pub(crate) preferred_velocity: Vec2,
187    /// Agent collision disk radius.
188    #[get(type(copy))]
189    pub(crate) radius: f32,
190    /// Maximum allowed speed.
191    #[get(type(copy))]
192    pub(crate) max_speed: f32,
193    /// Maximum radius within which other agents are treated as local neighbors.
194    #[get(type(copy))]
195    pub(crate) neighbor_dist: f32,
196    /// Collision anticipation lookahead horizon in seconds.
197    #[get(type(copy))]
198    pub(crate) time_horizon: f32,
199}
200
201/// Directed 2D half-plane constraint (`det(direction, v - point) >= 0` is feasible).
202#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
203pub struct OrcaLine2D {
204    /// Anchor point on the half-plane boundary.
205    #[get(type(copy))]
206    pub(crate) point: Vec2,
207    /// Unit direction along the half-plane boundary (feasible region lies to the left).
208    #[get(type(copy))]
209    pub(crate) direction: Vec2,
210}
211
212/// Configuration defaults for `OrcaCrowdSolver`.
213#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
214pub struct OrcaCrowdConfig {
215    /// Default neighbor detection radius for newly added agents.
216    #[get(type(copy))]
217    pub(crate) default_neighbor_dist: f32,
218    /// Default collision lookahead horizon in seconds.
219    #[get(type(copy))]
220    pub(crate) default_time_horizon: f32,
221    /// Maximum closest neighbors considered per agent during ORCA half-plane assembly.
222    #[get(type(copy))]
223    pub(crate) max_neighbors: usize,
224}
225
226/// Reusable linear-programming scratch buffers for zero-allocation ORCA crowd steps.
227#[derive(Clone, Debug, Default)]
228pub(crate) struct OrcaScratch {
229    /// Per-agent active ORCA half-plane constraints.
230    pub(crate) orca_lines: Vec<OrcaLine2D>,
231    /// Projected half-planes used during infeasible LP fallback (`linear_program3`).
232    pub(crate) proj_lines: Vec<OrcaLine2D>,
233    /// Newly solved collision-free velocities staged before simultaneous commit.
234    pub(crate) new_velocities: Vec<Vec2>,
235    /// Per-agent sorted `(dist_sq, neighbor_index)` candidate scratch buffer.
236    pub(crate) neighbor_indices: Vec<(f32, usize)>,
237}
238
239/// Multi-agent 2D ORCA / RVO2 collision-avoidance crowd solver with linear programming.
240#[derive(Clone, Data, Debug)]
241pub struct OrcaCrowdSolver {
242    /// Solver default parameters.
243    #[get(type(copy))]
244    pub(crate) config: OrcaCrowdConfig,
245    /// Registered crowd agents.
246    pub(crate) agents: Vec<OrcaAgent2D>,
247    /// Reusable LP and neighbor buffers.
248    #[get(pub(crate))]
249    #[get_mut(pub(crate))]
250    #[set(pub(crate))]
251    pub(crate) scratch: OrcaScratch,
252}