Skip to main content

subtr_actor/stats/calculators/
touch_state.rs

1use super::*;
2
3#[derive(Debug, Clone, Default)]
4pub struct TouchState {
5    pub touch_events: Vec<TouchEvent>,
6    pub last_touch: Option<TouchEvent>,
7    pub last_touch_player: Option<PlayerId>,
8    pub last_touch_team_is_team_0: Option<bool>,
9}
10
11#[derive(Clone, Default)]
12pub struct TouchStateCalculator {
13    previous_ball_linear_velocity: Option<glam::Vec3>,
14    previous_ball_angular_velocity: Option<glam::Vec3>,
15    current_last_touch: Option<TouchEvent>,
16    recent_touch_candidates: HashMap<PlayerId, TouchEvent>,
17}
18
19impl TouchStateCalculator {
20    pub fn new() -> Self {
21        Self::default()
22    }
23
24    fn prune_recent_touch_candidates(&mut self, current_frame: usize) {
25        const TOUCH_CANDIDATE_WINDOW_FRAMES: usize = 4;
26
27        self.recent_touch_candidates.retain(|_, candidate| {
28            current_frame.saturating_sub(candidate.frame) <= TOUCH_CANDIDATE_WINDOW_FRAMES
29        });
30    }
31
32    fn current_ball_angular_velocity(ball: &BallFrameState) -> Option<glam::Vec3> {
33        ball.sample()
34            .map(|ball| {
35                ball.rigid_body
36                    .angular_velocity
37                    .unwrap_or(boxcars::Vector3f {
38                        x: 0.0,
39                        y: 0.0,
40                        z: 0.0,
41                    })
42            })
43            .map(|velocity| vec_to_glam(&velocity))
44    }
45
46    fn current_ball_linear_velocity(ball: &BallFrameState) -> Option<glam::Vec3> {
47        ball.velocity()
48    }
49
50    fn is_touch_candidate(&self, frame: &FrameInfo, ball: &BallFrameState) -> bool {
51        const BALL_GRAVITY_Z: f32 = -650.0;
52        const TOUCH_LINEAR_IMPULSE_THRESHOLD: f32 = 120.0;
53        const TOUCH_ANGULAR_VELOCITY_DELTA_THRESHOLD: f32 = 0.5;
54
55        let Some(current_linear_velocity) = Self::current_ball_linear_velocity(ball) else {
56            return false;
57        };
58        let Some(previous_linear_velocity) = self.previous_ball_linear_velocity else {
59            return false;
60        };
61        let Some(current_angular_velocity) = Self::current_ball_angular_velocity(ball) else {
62            return false;
63        };
64        let Some(previous_angular_velocity) = self.previous_ball_angular_velocity else {
65            return false;
66        };
67
68        let expected_linear_delta = glam::Vec3::new(0.0, 0.0, BALL_GRAVITY_Z * frame.dt.max(0.0));
69        let residual_linear_impulse =
70            current_linear_velocity - previous_linear_velocity - expected_linear_delta;
71        let angular_velocity_delta = current_angular_velocity - previous_angular_velocity;
72
73        residual_linear_impulse.length() > TOUCH_LINEAR_IMPULSE_THRESHOLD
74            || angular_velocity_delta.length() > TOUCH_ANGULAR_VELOCITY_DELTA_THRESHOLD
75    }
76
77    fn proximity_touch_candidates(
78        &self,
79        frame: &FrameInfo,
80        ball: &BallFrameState,
81        players: &PlayerFrameState,
82        max_collision_distance: f32,
83    ) -> Vec<TouchEvent> {
84        const OCTANE_HITBOX_LENGTH: f32 = 118.01;
85        const OCTANE_HITBOX_WIDTH: f32 = 84.2;
86        const OCTANE_HITBOX_HEIGHT: f32 = 36.16;
87        const OCTANE_HITBOX_OFFSET: f32 = 13.88;
88        const OCTANE_HITBOX_ELEVATION: f32 = 17.05;
89
90        let Some(ball) = ball.sample() else {
91            return Vec::new();
92        };
93        let ball_position = vec_to_glam(&ball.rigid_body.location);
94
95        let mut candidates = players
96            .players
97            .iter()
98            .filter_map(|player| {
99                let rigid_body = player.rigid_body.as_ref()?;
100                let player_position = vec_to_glam(&rigid_body.location);
101                let local_ball_position = quat_to_glam(&rigid_body.rotation).inverse()
102                    * (ball_position - player_position);
103
104                let x_distance = if local_ball_position.x
105                    < -OCTANE_HITBOX_LENGTH / 2.0 + OCTANE_HITBOX_OFFSET
106                {
107                    (-OCTANE_HITBOX_LENGTH / 2.0 + OCTANE_HITBOX_OFFSET) - local_ball_position.x
108                } else if local_ball_position.x > OCTANE_HITBOX_LENGTH / 2.0 + OCTANE_HITBOX_OFFSET
109                {
110                    local_ball_position.x - (OCTANE_HITBOX_LENGTH / 2.0 + OCTANE_HITBOX_OFFSET)
111                } else {
112                    0.0
113                };
114                let y_distance = if local_ball_position.y < -OCTANE_HITBOX_WIDTH / 2.0 {
115                    (-OCTANE_HITBOX_WIDTH / 2.0) - local_ball_position.y
116                } else if local_ball_position.y > OCTANE_HITBOX_WIDTH / 2.0 {
117                    local_ball_position.y - OCTANE_HITBOX_WIDTH / 2.0
118                } else {
119                    0.0
120                };
121                let z_distance = if local_ball_position.z
122                    < -OCTANE_HITBOX_HEIGHT / 2.0 + OCTANE_HITBOX_ELEVATION
123                {
124                    (-OCTANE_HITBOX_HEIGHT / 2.0 + OCTANE_HITBOX_ELEVATION) - local_ball_position.z
125                } else if local_ball_position.z
126                    > OCTANE_HITBOX_HEIGHT / 2.0 + OCTANE_HITBOX_ELEVATION
127                {
128                    local_ball_position.z - (OCTANE_HITBOX_HEIGHT / 2.0 + OCTANE_HITBOX_ELEVATION)
129                } else {
130                    0.0
131                };
132
133                let collision_distance =
134                    glam::Vec3::new(x_distance, y_distance, z_distance).length();
135                if collision_distance > max_collision_distance {
136                    return None;
137                }
138
139                Some(TouchEvent {
140                    time: frame.time,
141                    frame: frame.frame_number,
142                    team_is_team_0: player.is_team_0,
143                    player: Some(player.player_id.clone()),
144                    closest_approach_distance: Some(collision_distance),
145                })
146            })
147            .collect::<Vec<_>>();
148
149        candidates.sort_by(|left, right| {
150            let left_distance = left.closest_approach_distance.unwrap_or(f32::INFINITY);
151            let right_distance = right.closest_approach_distance.unwrap_or(f32::INFINITY);
152            left_distance.total_cmp(&right_distance)
153        });
154        candidates
155    }
156
157    fn candidate_touch_event(
158        &self,
159        frame: &FrameInfo,
160        ball: &BallFrameState,
161        players: &PlayerFrameState,
162    ) -> Option<TouchEvent> {
163        const TOUCH_COLLISION_DISTANCE_THRESHOLD: f32 = 300.0;
164
165        self.proximity_touch_candidates(frame, ball, players, TOUCH_COLLISION_DISTANCE_THRESHOLD)
166            .into_iter()
167            .next()
168    }
169
170    fn update_recent_touch_candidates(
171        &mut self,
172        frame: &FrameInfo,
173        ball: &BallFrameState,
174        players: &PlayerFrameState,
175    ) {
176        const PROXIMITY_CANDIDATE_DISTANCE_THRESHOLD: f32 = 220.0;
177
178        for candidate in self.proximity_touch_candidates(
179            frame,
180            ball,
181            players,
182            PROXIMITY_CANDIDATE_DISTANCE_THRESHOLD,
183        ) {
184            let Some(player_id) = candidate.player.clone() else {
185                continue;
186            };
187
188            self.recent_touch_candidates.insert(player_id, candidate);
189        }
190    }
191
192    fn candidate_for_player(&self, player_id: &PlayerId) -> Option<TouchEvent> {
193        self.recent_touch_candidates.get(player_id).cloned()
194    }
195
196    fn best_candidate_for_team(&self, team_is_team_0: bool) -> Option<TouchEvent> {
197        self.recent_touch_candidates
198            .values()
199            .filter(|candidate| candidate.team_is_team_0 == team_is_team_0)
200            .min_by(|left, right| {
201                let left_distance = left.closest_approach_distance.unwrap_or(f32::INFINITY);
202                let right_distance = right.closest_approach_distance.unwrap_or(f32::INFINITY);
203                left_distance.total_cmp(&right_distance)
204            })
205            .cloned()
206    }
207
208    fn enrich_explicit_touch_event(&self, event: &TouchEvent) -> TouchEvent {
209        let candidate = if let Some(player_id) = event.player.as_ref() {
210            self.candidate_for_player(player_id)
211        } else {
212            self.best_candidate_for_team(event.team_is_team_0)
213        };
214        let Some(candidate) = candidate else {
215            return event.clone();
216        };
217
218        TouchEvent {
219            player: event.player.clone().or(candidate.player),
220            closest_approach_distance: event
221                .closest_approach_distance
222                .or(candidate.closest_approach_distance),
223            ..event.clone()
224        }
225    }
226
227    fn contested_touch_candidates(&self, primary: &TouchEvent) -> Vec<TouchEvent> {
228        const CONTESTED_TOUCH_DISTANCE_MARGIN: f32 = 80.0;
229
230        let primary_distance = primary.closest_approach_distance.unwrap_or(f32::INFINITY);
231
232        let best_opposing_candidate = self
233            .recent_touch_candidates
234            .values()
235            .filter(|candidate| candidate.team_is_team_0 != primary.team_is_team_0)
236            .filter(|candidate| {
237                candidate.closest_approach_distance.unwrap_or(f32::INFINITY)
238                    <= primary_distance + CONTESTED_TOUCH_DISTANCE_MARGIN
239            })
240            .min_by(|left, right| {
241                let left_distance = left.closest_approach_distance.unwrap_or(f32::INFINITY);
242                let right_distance = right.closest_approach_distance.unwrap_or(f32::INFINITY);
243                left_distance.total_cmp(&right_distance)
244            })
245            .cloned();
246
247        best_opposing_candidate.into_iter().collect()
248    }
249
250    fn confirmed_touch_events(
251        &self,
252        frame: &FrameInfo,
253        ball: &BallFrameState,
254        players: &PlayerFrameState,
255        events: &FrameEventsState,
256    ) -> Vec<TouchEvent> {
257        let mut touch_events = Vec::new();
258        let mut confirmed_players = HashSet::new();
259
260        for event in &events.touch_events {
261            let event = self.enrich_explicit_touch_event(event);
262            if let Some(player_id) = event.player.clone() {
263                confirmed_players.insert(player_id);
264            }
265            touch_events.push(event);
266        }
267
268        if touch_events.is_empty() && self.is_touch_candidate(frame, ball) {
269            if let Some(candidate) = self.candidate_touch_event(frame, ball, players) {
270                for contested_candidate in self.contested_touch_candidates(&candidate) {
271                    if let Some(player_id) = contested_candidate.player.clone() {
272                        confirmed_players.insert(player_id);
273                    }
274                    touch_events.push(contested_candidate);
275                }
276                if let Some(player_id) = candidate.player.clone() {
277                    confirmed_players.insert(player_id);
278                }
279                touch_events.push(candidate);
280            }
281        }
282
283        for dodge_refresh in &events.dodge_refreshed_events {
284            if !confirmed_players.insert(dodge_refresh.player.clone()) {
285                continue;
286            }
287            let Some(candidate) = self.candidate_for_player(&dodge_refresh.player) else {
288                continue;
289            };
290            touch_events.push(candidate);
291        }
292
293        touch_events
294    }
295
296    pub fn update(
297        &mut self,
298        frame: &FrameInfo,
299        ball: &BallFrameState,
300        players: &PlayerFrameState,
301        events: &FrameEventsState,
302        live_play_state: &LivePlayState,
303    ) -> TouchState {
304        let touch_events = if live_play_state.is_live_play {
305            self.prune_recent_touch_candidates(frame.frame_number);
306            self.update_recent_touch_candidates(frame, ball, players);
307            self.confirmed_touch_events(frame, ball, players, events)
308        } else {
309            self.current_last_touch = None;
310            self.recent_touch_candidates.clear();
311            Vec::new()
312        };
313
314        if let Some(last_touch) = touch_events.last() {
315            self.current_last_touch = Some(last_touch.clone());
316        }
317        self.previous_ball_linear_velocity = Self::current_ball_linear_velocity(ball);
318        self.previous_ball_angular_velocity = Self::current_ball_angular_velocity(ball);
319
320        TouchState {
321            touch_events,
322            last_touch: self.current_last_touch.clone(),
323            last_touch_player: self
324                .current_last_touch
325                .as_ref()
326                .and_then(|touch| touch.player.clone()),
327            last_touch_team_is_team_0: self
328                .current_last_touch
329                .as_ref()
330                .map(|touch| touch.team_is_team_0),
331        }
332    }
333}
334
335#[cfg(test)]
336#[path = "touch_state_tests.rs"]
337mod tests;