subtr-actor 1.0.0

Rocket League replay transformer
Documentation
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
use super::*;

const SOFT_TOUCH_BALL_SPEED_CHANGE_THRESHOLD: f32 = 320.0;
const HARD_TOUCH_BALL_SPEED_CHANGE_THRESHOLD: f32 = 900.0;
const AERIAL_TOUCH_MIN_PLAYER_Z: f32 = AIR_DRIBBLE_MIN_PLAYER_Z;

/// How long after a touch we keep watching the toucher's dodge component before
/// giving up on associating a flip with it. The `CarComponent_Dodge`
/// `ReplicatedActive` byte routinely replicates a frame or two *after* the
/// ball-hit it produced (the hit and the dodge-activation land on adjacent
/// frames), so a flip-into-ball contact can be sampled on the one frame where
/// the dodge flag has not yet flipped on. Re-checking for a brief window lets
/// such touches be recognized as dodge contacts after the fact.
const DODGE_LAG_TOLERANCE_SECONDS: f32 = 0.12;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TouchKind {
    Control,
    MediumHit,
    HardHit,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TouchSurface {
    Ground,
    Air,
    Wall,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TouchDodgeState {
    NoDodge,
    Dodge,
}

impl TouchKind {
    fn as_label_value(self) -> &'static str {
        match self {
            Self::Control => "control",
            Self::MediumHit => "medium_hit",
            Self::HardHit => "hard_hit",
        }
    }
}

impl TouchSurface {
    fn as_label_value(self) -> &'static str {
        match self {
            Self::Ground => "ground",
            Self::Air => "air",
            Self::Wall => "wall",
        }
    }
}

impl TouchDodgeState {
    fn from_dodge_active(dodge_active: bool) -> Self {
        if dodge_active {
            Self::Dodge
        } else {
            Self::NoDodge
        }
    }

    fn as_label_value(self) -> &'static str {
        match self {
            Self::NoDodge => "no_dodge",
            Self::Dodge => "dodge",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct TouchClassification {
    kind: TouchKind,
    height_band: PlayerVerticalBand,
    surface: TouchSurface,
    dodge_state: TouchDodgeState,
}

#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct TouchClassificationEvent {
    /// Identity of the source [`TouchEvent`](crate::TouchEvent) this
    /// classification was derived from. Join on this instead of player + frame.
    /// `None` only for data serialized before touch ids existed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[ts(type = "number")]
    pub touch_id: Option<u64>,
    pub time: f32,
    pub frame: usize,
    pub sample_time: f32,
    pub sample_frame: usize,
    #[ts(as = "crate::interop::ts_bindings::RemoteIdTs")]
    pub player: PlayerId,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub player_position: Option<[f32; 3]>,
    pub is_team_0: bool,
    pub kind: String,
    pub height_band: String,
    pub surface: String,
    pub dodge_state: String,
    pub intention: String,
    #[serde(default)]
    pub first_touch: bool,
    #[serde(default)]
    pub contested: bool,
    #[serde(default)]
    pub role: RoleState,
    #[serde(default)]
    pub play_depth: PlayDepthState,
    pub ball_speed_change: f32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ball_movement: Option<TouchBallMovement>,
}

#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct TouchBallMovement {
    pub start_time: f32,
    pub start_frame: usize,
    pub end_time: f32,
    pub end_frame: usize,
    pub duration: f32,
    pub travel_distance: f32,
    pub advance_distance: f32,
    pub retreat_distance: f32,
    pub finalized: bool,
}

impl TouchBallMovement {
    fn absorb_delta(&mut self, event: Self) {
        self.end_time = event.end_time;
        self.end_frame = event.end_frame;
        self.duration += event.duration;
        self.travel_distance += event.travel_distance;
        self.advance_distance += event.advance_distance;
        self.retreat_distance += event.retreat_distance;
    }

    fn finalized(mut self) -> Self {
        self.finalized = true;
        self
    }
}

/// A `no_dodge` touch still within [`DODGE_LAG_TOLERANCE_SECONDS`] of being
/// retroactively upgraded to a dodge contact, should the toucher's dodge
/// component go active in the frames immediately following the hit.
#[derive(Debug, Clone, PartialEq)]
struct PendingDodgeUpgrade {
    touch_index: usize,
    player_id: PlayerId,
    touch_time: f32,
}

#[derive(Debug, Clone, Default, PartialEq)]
struct PendingFiftyFiftyMovement {
    start_frame: usize,
    travel_distance: f32,
    y_delta: f32,
}

#[derive(Debug, Clone, PartialEq)]
struct PendingTouchBallMovementCredit {
    touch_index: usize,
    movement: TouchBallMovement,
}

impl InFlightItem for PendingTouchBallMovementCredit {
    fn recognition(&self) -> Recognition {
        // A touch credit always corresponds to a real touch, so it is committed
        // from the moment it is armed.
        Recognition::committed(self.movement.start_time, self.movement.start_frame)
    }

    fn on_boundary(&mut self, boundary: Boundary) -> Disposition {
        // The credit window always closes at a boundary: the accumulated travel
        // up to the goal / stoppage / end of replay is exactly what we keep.
        Disposition::Finalize(FinalizeReason::Boundary(boundary))
    }
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct TouchCalculator {
    events: EventStream<TouchClassificationEvent>,
    ball_movement: InFlightLedger<PendingTouchBallMovementCredit>,
    active_touch_index_by_player: HashMap<PlayerId, usize>,
    previous_ball_velocity: Option<glam::Vec3>,
    previous_ball_position: Option<glam::Vec3>,
    pending_fifty_fifty_movement: Option<PendingFiftyFiftyMovement>,
    pending_dodge_upgrades: Vec<PendingDodgeUpgrade>,
    intention_classifier: TouchIntentionClassifier,
    control_follow: ControlFollowTracker,
}

impl TouchCalculator {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn events(&self) -> &[TouchClassificationEvent] {
        self.events.all()
    }

    pub fn new_events(&self) -> &[TouchClassificationEvent] {
        self.events.new_events()
    }

    pub fn flush_pending_ball_movement_credit(&mut self) {
        let finalized = self.ball_movement.finalize_all(FinalizeReason::Completed);
        self.write_finalized_ball_movement(finalized);
    }

    /// Finalize any pending ball-movement credit at end of stream. Routed
    /// through the ledger so the boundary is handled uniformly and can't be
    /// forgotten.
    pub fn finish(&mut self) {
        let finalized = self.ball_movement.finish();
        self.write_finalized_ball_movement(finalized);
        let resolution = self.control_follow.flush();
        self.apply_control_resolution(resolution);
    }

    fn finalize_ball_movement_at_boundary(&mut self, boundary: Boundary) {
        let finalized = self.ball_movement.apply_boundary(boundary);
        self.write_finalized_ball_movement(finalized);
    }

    fn write_finalized_ball_movement(
        &mut self,
        finalized: Vec<(PendingTouchBallMovementCredit, FinalizeReason)>,
    ) {
        for (pending, _reason) in finalized {
            if let Some(event) = self.events.get_mut(pending.touch_index) {
                event.ball_movement = Some(pending.movement.finalized());
            }
        }
    }

    fn ball_speed_change(
        frame: &FrameInfo,
        ball: &BallFrameState,
        previous_ball_velocity: Option<glam::Vec3>,
    ) -> f32 {
        const BALL_GRAVITY_Z: f32 = -650.0;

        let Some(ball) = ball.sample() else {
            return 0.0;
        };
        let Some(previous_ball_velocity) = previous_ball_velocity else {
            return 0.0;
        };

        let expected_linear_delta = glam::Vec3::new(0.0, 0.0, BALL_GRAVITY_Z * frame.dt.max(0.0));
        let residual_linear_impulse =
            ball.velocity() - previous_ball_velocity - expected_linear_delta;
        residual_linear_impulse.length()
    }

    fn classify_touch(
        height_band: PlayerVerticalBand,
        surface: TouchSurface,
        dodge_state: TouchDodgeState,
        ball_speed_change: f32,
        controlled_touch_kind: Option<BallCarryKind>,
    ) -> TouchClassification {
        let kind = if controlled_touch_kind.is_some()
            || ball_speed_change <= SOFT_TOUCH_BALL_SPEED_CHANGE_THRESHOLD
        {
            TouchKind::Control
        } else if ball_speed_change < HARD_TOUCH_BALL_SPEED_CHANGE_THRESHOLD {
            TouchKind::MediumHit
        } else {
            TouchKind::HardHit
        };

        TouchClassification {
            kind,
            height_band,
            surface,
            dodge_state,
        }
    }

    fn height_band_for_touch(sample: Option<&PlayerVerticalSample>) -> PlayerVerticalBand {
        let Some(sample) = sample else {
            return PlayerVerticalBand::Ground;
        };

        if sample.height < AERIAL_TOUCH_MIN_PLAYER_Z {
            PlayerVerticalBand::Ground
        } else {
            sample.band
        }
    }

    fn surface_for_touch(
        player_position: Option<glam::Vec3>,
        height_band: PlayerVerticalBand,
    ) -> TouchSurface {
        if player_position.is_some_and(player_is_on_wall) {
            TouchSurface::Wall
        } else if height_band.is_grounded() {
            TouchSurface::Ground
        } else {
            TouchSurface::Air
        }
    }

    fn controlled_touch_kind(
        ball: &BallFrameState,
        players: &PlayerFrameState,
        player_id: &PlayerId,
    ) -> Option<BallCarryKind> {
        let ball = ball.sample()?;
        players
            .players
            .iter()
            .find(|player| &player.player_id == player_id)
            .and_then(|player| {
                BallCarryCalculator::carry_frame_sample(player, ball).map(|sample| sample.kind)
            })
    }

    fn player_position(players: &PlayerFrameState, player_id: &PlayerId) -> Option<glam::Vec3> {
        players
            .players
            .iter()
            .find(|player| &player.player_id == player_id)
            .and_then(PlayerSample::position)
    }

    fn player_dodge_active(players: &PlayerFrameState, player_id: &PlayerId) -> bool {
        players
            .players
            .iter()
            .find(|player| &player.player_id == player_id)
            .is_some_and(|player| player.dodge_active)
    }

    fn teammate_positions(
        players: &PlayerFrameState,
        player_id: &PlayerId,
        is_team_0: bool,
    ) -> Vec<glam::Vec3> {
        players
            .players
            .iter()
            .filter(|player| player.is_team_0 == is_team_0 && &player.player_id != player_id)
            .filter_map(PlayerSample::position)
            .collect()
    }

    #[allow(clippy::too_many_arguments)]
    fn apply_touch_events(
        &mut self,
        frame: &FrameInfo,
        ball: &BallFrameState,
        players: &PlayerFrameState,
        vertical_state: &PlayerVerticalState,
        rotation: &RotationCalculator,
        touch_events: &[TouchEvent],
        fifty_fifty_state: &FiftyFiftyState,
    ) {
        let ball_speed_change = Self::ball_speed_change(frame, ball, self.previous_ball_velocity);
        let contested_frame = touch_events.iter().any(|touch| touch.team_is_team_0)
            && touch_events.iter().any(|touch| !touch.team_is_team_0);

        for touch_event in touch_events {
            let Some(player_id) = touch_event.player.as_ref() else {
                continue;
            };
            let height_band = Self::height_band_for_touch(vertical_state.sample(player_id));
            let surface =
                Self::surface_for_touch(Self::player_position(players, player_id), height_band);
            let dodge_state = TouchDodgeState::from_dodge_active(
                touch_event.dodge_contact || Self::player_dodge_active(players, player_id),
            );
            let controlled_touch_kind = Self::controlled_touch_kind(ball, players, player_id);
            let (role, play_depth) = rotation.current_role_and_depth(player_id);
            let classification = Self::classify_touch(
                height_band,
                surface,
                dodge_state,
                ball_speed_change,
                controlled_touch_kind,
            );
            let contested = contested_frame
                || fifty_fifty_state
                    .active_event
                    .as_ref()
                    .is_some_and(|active| {
                        fifty_fifty_involves_player(active, player_id, touch_event.team_is_team_0)
                    });
            let teammate_positions =
                Self::teammate_positions(players, player_id, touch_event.team_is_team_0);
            let control_resolution = self
                .control_follow
                .observe_touch(player_id, touch_event.time);
            self.apply_control_resolution(control_resolution);
            let resolution = self.intention_classifier.classify(
                touch_event,
                player_id,
                &TouchIntentionFrameContext {
                    ball_position: ball.position(),
                    ball_velocity: ball.velocity(),
                    previous_ball_position: self.previous_ball_position,
                    previous_ball_velocity: self.previous_ball_velocity,
                    teammate_positions: &teammate_positions,
                    contested,
                },
            );
            let event = TouchClassificationEvent {
                touch_id: touch_event.touch_id,
                time: touch_event.time,
                frame: touch_event.frame,
                sample_time: frame.time,
                sample_frame: frame.frame_number,
                player: player_id.clone(),
                player_position: touch_event
                    .player_position
                    .map(|position| vec_to_glam(&position).to_array())
                    .or_else(|| {
                        Self::player_position(players, player_id)
                            .map(|position| position.to_array())
                    }),
                is_team_0: touch_event.team_is_team_0,
                kind: classification.kind.as_label_value().to_owned(),
                height_band: classification.height_band.as_label().value.to_owned(),
                surface: classification.surface.as_label_value().to_owned(),
                dodge_state: classification.dodge_state.as_label_value().to_owned(),
                intention: resolution.intention.as_label_value().to_owned(),
                first_touch: resolution.first_touch,
                contested: resolution.contested,
                role,
                play_depth,
                ball_speed_change,
                ball_movement: None,
            };
            let touch_index = self.events.len();
            self.events.push(event);
            self.active_touch_index_by_player
                .insert(player_id.clone(), touch_index);
            // The dodge byte often lags the hit by a frame or two; if this touch
            // looked dodge-less, keep watching the toucher's dodge component so a
            // flip-into-ball contact still gets recognized once it activates.
            if matches!(classification.dodge_state, TouchDodgeState::NoDodge) {
                self.pending_dodge_upgrades.push(PendingDodgeUpgrade {
                    touch_index,
                    player_id: player_id.clone(),
                    touch_time: touch_event.time,
                });
            }
            if matches!(
                resolution.intention,
                TouchIntention::Pass | TouchIntention::Neutral
            ) {
                self.control_follow
                    .open(touch_index, player_id, touch_event.time);
            }
        }
    }

    /// Apply a closed control-follow window: upgrade the touch's intention to
    /// control when the toucher stayed with the ball or earned the follow-up.
    fn apply_control_resolution(&mut self, resolution: Option<ControlResolution>) {
        let Some(resolution) = resolution else {
            return;
        };
        if !resolution.control {
            return;
        }
        if let Some(event) = self.events.get_mut(resolution.touch_index) {
            event.intention = TouchIntention::Control.as_label_value().to_owned();
        }
    }

    /// Re-examine recent `no_dodge` touches against this frame's dodge state.
    /// The dodge component's active byte frequently replicates a frame or two
    /// after the ball-hit it caused, so a flip-into-ball contact can be sampled
    /// on the one frame where the flag has not yet flipped on. Any pending touch
    /// whose toucher is now dodging within [`DODGE_LAG_TOLERANCE_SECONDS`] is
    /// upgraded to a dodge contact; entries that age out are dropped.
    fn advance_dodge_upgrades(&mut self, frame: &FrameInfo, players: &PlayerFrameState) {
        if self.pending_dodge_upgrades.is_empty() {
            return;
        }
        let mut resolved = Vec::new();
        self.pending_dodge_upgrades.retain(|pending| {
            if frame.time - pending.touch_time > DODGE_LAG_TOLERANCE_SECONDS {
                return false;
            }
            if Self::player_dodge_active(players, &pending.player_id) {
                resolved.push(pending.touch_index);
                return false;
            }
            true
        });
        for touch_index in resolved {
            if let Some(event) = self.events.get_mut(touch_index) {
                event.dodge_state = TouchDodgeState::Dodge.as_label_value().to_owned();
            }
        }
    }

    /// Feed this frame's ball and toucher samples to any open control-follow
    /// window and apply its resolution once it ages out.
    fn advance_control_follow(
        &mut self,
        frame: &FrameInfo,
        ball: &BallFrameState,
        players: &PlayerFrameState,
    ) {
        let Some(window_player) = self.control_follow.window_player().cloned() else {
            return;
        };
        let player_sample = players
            .players
            .iter()
            .find(|player| player.player_id == window_player);
        let resolution = self.control_follow.advance(
            frame,
            ball.position(),
            ball.velocity(),
            player_sample.and_then(PlayerSample::position),
            player_sample.and_then(PlayerSample::velocity),
        );
        self.apply_control_resolution(resolution);
    }

    #[allow(clippy::too_many_arguments)]
    fn apply_ball_movement_credit(
        &mut self,
        frame: usize,
        time: f32,
        duration: f32,
        player_id: &PlayerId,
        team_is_team_0: bool,
        player_position: Option<[f32; 3]>,
        delta: glam::Vec3,
        travel_distance: f32,
    ) {
        let team_forward_sign = if team_is_team_0 { 1.0 } else { -1.0 };
        let advance_distance = delta.y * team_forward_sign;
        let (advance_distance, retreat_distance) = if advance_distance >= 0.0 {
            (advance_distance, 0.0)
        } else {
            (0.0, -advance_distance)
        };
        let movement = TouchBallMovement {
            start_time: time,
            start_frame: frame,
            end_time: time,
            end_frame: frame,
            duration,
            travel_distance,
            advance_distance,
            retreat_distance,
            finalized: false,
        };
        self.record_ball_movement_credit(player_id, player_position, movement);
    }

    fn record_ball_movement_credit(
        &mut self,
        player_id: &PlayerId,
        player_position: Option<[f32; 3]>,
        movement: TouchBallMovement,
    ) {
        let Some(&touch_index) = self.active_touch_index_by_player.get(player_id) else {
            return;
        };
        let pending_index = self
            .ball_movement
            .in_flight()
            .first()
            .map(|pending| pending.touch_index);

        if pending_index == Some(touch_index) {
            // Same touch still in flight: fold this frame's travel into it.
            let merged = {
                let pending = self
                    .ball_movement
                    .in_flight_mut()
                    .first_mut()
                    .expect("pending credit present");
                pending.movement.absorb_delta(movement);
                pending.movement.clone()
            };
            if let Some(event) = self.events.get_mut(touch_index) {
                event.player_position = player_position.or(event.player_position);
                event.ball_movement = Some(merged);
            }
            return;
        }

        // A different touch (or none) is in flight: supersede the old credit and
        // arm a fresh one for this touch.
        if pending_index.is_some() {
            let finalized = self.ball_movement.finalize_all(FinalizeReason::Superseded);
            self.write_finalized_ball_movement(finalized);
        }
        if let Some(event) = self.events.get_mut(touch_index) {
            event.player_position = player_position.or(event.player_position);
            event.ball_movement = Some(movement.clone());
        }
        self.ball_movement.arm(PendingTouchBallMovementCredit {
            touch_index,
            movement,
        });
    }

    fn resolved_fifty_fifty_winner(event: &FiftyFiftyEvent) -> Option<(&PlayerId, bool)> {
        let winning_team_is_team_0 = event.winning_team_is_team_0?;
        let player = if winning_team_is_team_0 {
            event.team_zero_player.as_ref()
        } else {
            event.team_one_player.as_ref()
        }?;
        Some((player, winning_team_is_team_0))
    }

    fn buffer_fifty_fifty_movement(
        &mut self,
        start_frame: usize,
        delta: glam::Vec3,
        travel_distance: f32,
    ) {
        let pending = self
            .pending_fifty_fifty_movement
            .get_or_insert(PendingFiftyFiftyMovement {
                start_frame,
                travel_distance: 0.0,
                y_delta: 0.0,
            });
        if pending.start_frame != start_frame {
            *pending = PendingFiftyFiftyMovement {
                start_frame,
                travel_distance: 0.0,
                y_delta: 0.0,
            };
        }
        pending.travel_distance += travel_distance;
        pending.y_delta += delta.y;
    }

    fn flush_fifty_fifty_movement(&mut self, event: &FiftyFiftyEvent) {
        let Some(pending) = self.pending_fifty_fifty_movement.take() else {
            return;
        };
        if pending.start_frame != event.start_frame {
            return;
        }
        let Some((player_id, team_is_team_0)) = Self::resolved_fifty_fifty_winner(event) else {
            return;
        };

        let team_forward_sign = if team_is_team_0 { 1.0 } else { -1.0 };
        let advance_distance = pending.y_delta * team_forward_sign;
        let (advance_distance, retreat_distance) = if advance_distance >= 0.0 {
            (advance_distance, 0.0)
        } else {
            (0.0, -advance_distance)
        };
        let movement = TouchBallMovement {
            start_time: event.resolve_time,
            start_frame: event.resolve_frame,
            end_time: event.resolve_time,
            end_frame: event.resolve_frame,
            duration: 0.0,
            travel_distance: pending.travel_distance,
            advance_distance,
            retreat_distance,
            finalized: false,
        };
        self.flush_pending_ball_movement_credit();
        self.record_ball_movement_credit(
            player_id,
            if team_is_team_0 {
                Some(event.team_zero_position)
            } else {
                Some(event.team_one_position)
            },
            movement,
        );
        self.flush_pending_ball_movement_credit();
    }

    fn credit_ball_movement(
        &mut self,
        frame: &FrameInfo,
        ball: &BallFrameState,
        players: &PlayerFrameState,
        possession_state: &PossessionState,
        fifty_fifty_state: &FiftyFiftyState,
        live_play_state: &LivePlayState,
    ) {
        let current_ball_position = ball.position();
        if !live_play_state.is_live_play {
            self.finalize_ball_movement_at_boundary(Boundary::LivePlayEnded);
            self.previous_ball_position = current_ball_position;
            self.pending_fifty_fifty_movement = None;
            return;
        }

        let Some(current_ball_position) = current_ball_position else {
            self.flush_pending_ball_movement_credit();
            self.previous_ball_position = None;
            self.pending_fifty_fifty_movement = None;
            return;
        };
        let Some(previous_ball_position) = self.previous_ball_position else {
            self.previous_ball_position = Some(current_ball_position);
            return;
        };
        self.previous_ball_position = Some(current_ball_position);

        let delta = current_ball_position - previous_ball_position;
        let travel_distance = delta.length();
        if travel_distance <= f32::EPSILON {
            return;
        }

        if let Some(active_event) = fifty_fifty_state.active_event.as_ref() {
            self.flush_pending_ball_movement_credit();
            self.buffer_fifty_fifty_movement(active_event.start_frame, delta, travel_distance);
            return;
        }

        if let Some(event) = fifty_fifty_state.resolved_events.last() {
            self.buffer_fifty_fifty_movement(event.start_frame, delta, travel_distance);
            self.flush_fifty_fifty_movement(event);
            return;
        }

        self.pending_fifty_fifty_movement = None;

        let (Some(player_id), Some(team_is_team_0)) = (
            possession_state.active_player_before_sample.as_ref(),
            possession_state.active_team_before_sample,
        ) else {
            self.flush_pending_ball_movement_credit();
            return;
        };

        self.apply_ball_movement_credit(
            frame.frame_number,
            frame.time,
            frame.dt,
            player_id,
            team_is_team_0,
            players.player_position(player_id),
            delta,
            travel_distance,
        );
    }

    #[allow(clippy::too_many_arguments)]
    pub fn update(
        &mut self,
        frame: &FrameInfo,
        ball: &BallFrameState,
        players: &PlayerFrameState,
        vertical_state: &PlayerVerticalState,
        rotation: &RotationCalculator,
        touch_state: &TouchState,
        possession_state: &PossessionState,
        fifty_fifty_state: &FiftyFiftyState,
        events_state: &FrameEventsState,
        live_play_state: &LivePlayState,
    ) -> SubtrActorResult<()> {
        self.events.begin_update();
        if !live_play_state.is_live_play {
            self.finalize_ball_movement_at_boundary(Boundary::LivePlayEnded);
            self.previous_ball_velocity = ball.velocity();
            self.previous_ball_position = ball.position();
            self.pending_fifty_fifty_movement = None;
            self.pending_dodge_upgrades.clear();
            self.intention_classifier.reset();
            let resolution = self.control_follow.flush();
            self.apply_control_resolution(resolution);
            return Ok(());
        }
        self.intention_classifier
            .begin_frame(frame, &events_state.player_stat_events);
        self.apply_touch_events(
            frame,
            ball,
            players,
            vertical_state,
            rotation,
            &touch_state.touch_events,
            fifty_fifty_state,
        );
        self.advance_dodge_upgrades(frame, players);
        self.advance_control_follow(frame, ball, players);
        self.credit_ball_movement(
            frame,
            ball,
            players,
            possession_state,
            fifty_fifty_state,
            live_play_state,
        );
        self.previous_ball_velocity = ball.velocity();

        Ok(())
    }
}

#[cfg(test)]
#[path = "touch_tests.rs"]
mod tests;