aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/route_traffic.rs
blob: 4aaad17da14d83396ed85b0a708b918e4527721e (plain) (blame)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! Temporary ground occupancy during one autonomous movement step.

use crate::{
    autodemo::MOBILE_UNIT, projectile::unit_sphere, route_obstacles::Footprint, LoadedMissionState,
    MissionAssets, ObjectHandle, TransformState, World,
};
use fparkan_world::{handle_by_original_id, transform_state};

struct Body {
    handle: ObjectHandle,
    radius: f32,
    swept: Footprint,
    moved: bool,
}

#[derive(Default)]
pub(super) struct FrameTraffic {
    bodies: Vec<Body>,
}

pub(super) fn body_radius(
    assets: &MissionAssets,
    index: usize,
    pose: TransformState,
) -> Option<f32> {
    let (center, radius) = unit_sphere(assets, index, pose)?;
    let [x, y, _] = pose.position.map(f32::from_bits);
    let radius = radius + (center[0] - x).hypot(center[1] - y);
    (radius.is_finite() && radius > 0.0).then_some(radius)
}

impl FrameTraffic {
    pub(super) fn read(world: &World, mission: &LoadedMissionState) -> Self {
        let bodies = mission
            .object_drafts
            .iter()
            .enumerate()
            .filter_map(|(index, draft)| {
                (draft.unit_dat_header_flags? & MOBILE_UNIT != 0).then_some(())?;
                let handle = handle_by_original_id(world, draft.original_id?)?;
                let pose = transform_state(world, handle).ok()?;
                let [x, y, _] = pose.position.map(f32::from_bits);
                Some(Body {
                    handle,
                    radius: body_radius(&mission.mission_assets, index, pose)?,
                    swept: Footprint::point([x, y]),
                    moved: false,
                })
            })
            .collect();
        Self { bodies }
    }

    pub(super) fn radius(&self, handle: ObjectHandle) -> f32 {
        self.bodies
            .iter()
            .find(|body| body.handle == handle)
            .map_or(0.0, |body| body.radius)
    }

    /// None means the unit's starting point is inside a trajectory that was
    /// already accepted this frame. It can try again with a fresh snapshot.
    pub(super) fn blockers(
        &self,
        handle: ObjectHandle,
        from: [f32; 2],
        radius: f32,
    ) -> Option<Vec<Footprint>> {
        self.bodies
            .iter()
            .filter(|body| body.handle != handle)
            .map(|body| {
                let bounds = body.swept.expanded(radius + body.radius);
                (!body.moved || !bounds.contains(from)).then_some(bounds)
            })
            .collect()
    }

    pub(super) fn reserve(&mut self, handle: ObjectHandle, swept: Footprint) {
        if let Some(body) = self.bodies.iter_mut().find(|body| body.handle == handle) {
            if body.swept != swept {
                body.swept = swept;
                body.moved = true;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::route_motion::{follow_route, RouteClearance};
    use std::{collections::VecDeque, f32::consts::FRAC_PI_2};

    fn pose(xy: [f32; 2], yaw: f32) -> TransformState {
        TransformState {
            position: [xy[0], xy[1], 0.0].map(f32::to_bits),
            orientation: [0.0, 0.0, yaw].map(f32::to_bits),
            ..Default::default()
        }
    }

    fn xy(pose: TransformState) -> [f32; 2] {
        [
            f32::from_bits(pose.position[0]),
            f32::from_bits(pose.position[1]),
        ]
    }

    #[test]
    fn two_units_swap_positions_without_intersecting() {
        let handles = [1, 2].map(|slot| ObjectHandle {
            slot,
            generation: 1,
        });
        let destinations = [[7.0, 0.0], [-7.0, 0.0]];
        let mut poses = [
            pose(destinations[1], -FRAC_PI_2),
            pose(destinations[0], FRAC_PI_2),
        ];
        let mut routes = destinations.map(|target| VecDeque::from([target]));
        for _ in 0..250 {
            let mut traffic = FrameTraffic {
                bodies: handles
                    .into_iter()
                    .zip(poses)
                    .map(|(handle, pose)| Body {
                        handle,
                        radius: 0.5,
                        swept: Footprint::point(xy(pose)),
                        moved: false,
                    })
                    .collect(),
            };
            for i in 0..2 {
                let Some(blockers) = traffic.blockers(handles[i], xy(poses[i]), 0.5) else {
                    continue;
                };
                let (after, sweep) = follow_route(
                    poses[i],
                    &mut routes[i],
                    3.0,
                    0.1,
                    RouteClearance {
                        traffic: &blockers,
                        ..Default::default()
                    },
                    |_| Ok(Some(0.0)),
                )
                .unwrap();
                poses[i] = after;
                traffic.reserve(handles[i], sweep);
            }
            let [[a, b], [c, d]] = poses.map(xy);
            assert!(
                (a - c).hypot(b - d) > 1.0,
                "units overlapped at {a},{b} and {c},{d}"
            );
            if routes.iter().all(VecDeque::is_empty) {
                break;
            }
        }
        assert_eq!(poses.map(xy), destinations, "remaining routes: {routes:?}");
    }

    #[test]
    fn later_unit_avoids_the_first_units_crossing_during_a_long_frame() {
        let [first, second] = [1, 2].map(|slot| ObjectHandle {
            slot,
            generation: 1,
        });
        let start = [pose([-4.0, 0.0], -FRAC_PI_2), pose([0.0, -4.0], 0.0)];
        let mut traffic = FrameTraffic {
            bodies: [first, second]
                .into_iter()
                .zip(start)
                .map(|(handle, at)| Body {
                    handle,
                    radius: 0.5,
                    swept: Footprint::point(xy(at)),
                    moved: false,
                })
                .collect(),
        };
        let flat = |_| Ok(Some(0.0));
        let first_blockers = traffic.blockers(first, xy(start[0]), 0.5).unwrap();
        let (after, swept) = follow_route(
            start[0],
            &mut VecDeque::from([[4.0, 0.0]]),
            10.0,
            1.0,
            RouteClearance {
                traffic: &first_blockers,
                ..Default::default()
            },
            flat,
        )
        .unwrap();
        assert_eq!(xy(after), [4.0, 0.0]);
        traffic.reserve(first, swept);
        let second_blockers = traffic.blockers(second, xy(start[1]), 0.5).unwrap();
        let mut route = VecDeque::from([[0.0, 4.0]]);
        let (after, _) = follow_route(
            start[1],
            &mut route,
            10.0,
            1.0,
            RouteClearance {
                traffic: &second_blockers,
                ..Default::default()
            },
            flat,
        )
        .unwrap();
        assert!(xy(after)[1] < -1.0);
        assert_eq!(route.back(), Some(&[0.0, 4.0]));
    }

    #[test]
    fn stopped_vehicle_does_not_erase_an_occupied_destination() {
        let position = [0.0, 0.0];
        let target = [0.0, 5.0];
        let mut route = VecDeque::from([target]);
        let start = pose(position, 0.0);
        let bounds = Footprint::point(target).expanded(1.0);
        let flat = |_| Ok(Some(0.0));
        let (waiting, _) = follow_route(
            start,
            &mut route,
            10.0,
            2.0,
            RouteClearance {
                traffic: &[bounds],
                ..Default::default()
            },
            flat,
        )
        .unwrap();
        assert!(xy(waiting)[1] > 3.0);
        assert!(!bounds.contains(xy(waiting)));
        assert_eq!(route, [target]);
        let (arrived, _) = follow_route(
            waiting,
            &mut route,
            10.0,
            2.0,
            RouteClearance::default(),
            flat,
        )
        .unwrap();
        assert_eq!(xy(arrived), target);
        assert!(route.is_empty());
    }
}