subtr-actor 1.1.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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
//! # Replay Data Collection Module
//!
//! This module provides comprehensive data structures and collection mechanisms
//! for extracting and organizing Rocket League replay data. It offers a complete
//! representation of ball, player, and game state information across all frames
//! of a replay.
//!
//! The module is built around the [`ReplayDataCollector`] which implements the
//! [`Collector`] trait, allowing it to process replay frames and extract
//! detailed information about player actions, ball movement, and game state.
//!
//! # Key Components
//!
//! - [`ReplayData`] - The complete replay data structure containing all extracted information
//! - [`FrameData`] - Frame-by-frame data including ball, player, and metadata information
//! - [`PlayerFrame`] - Detailed player state including position, controls, and actions
//! - [`BallFrame`] - Ball state including rigid body physics information
//! - [`MetadataFrame`] - Game state metadata including time and score information
//!
//! # Example Usage
//!
//! ```rust
//! use subtr_actor::collector::replay_data::ReplayDataCollector;
//! use boxcars::ParserBuilder;
//!
//! let data = std::fs::read("assets/replay-format-2025-06-10-v868-32-net10-replicated-boost.replay").unwrap();
//! let replay = ParserBuilder::new(&data).parse().unwrap();
//!
//! let collector = ReplayDataCollector::new();
//! let replay_data = collector.get_replay_data(&replay).unwrap();
//!
//! // Access frame-by-frame data
//! for metadata_frame in &replay_data.frame_data.metadata_frames {
//!     println!("Time: {:.2}s, Remaining: {}s",
//!              metadata_frame.time, metadata_frame.seconds_remaining);
//! }
//! ```

use boxcars;
use serde::Serialize;

use crate::*;

/// Represents the ball state for a single frame in a Rocket League replay.
///
/// The ball can either be in an empty state (when ball syncing is disabled or
/// the rigid body is unavailable) or contain full physics data including
/// position, rotation, and velocity information.
///
/// # Variants
///
/// - [`Empty`](BallFrame::Empty) - Indicates the ball is unavailable or ball syncing is disabled
/// - [`Data`](BallFrame::Data) - Contains the ball's rigid body physics information
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub enum BallFrame {
    /// Empty frame indicating the ball is unavailable or ball syncing is disabled
    Empty,
    /// Frame containing the ball's rigid body physics data
    Data {
        /// The ball's rigid body containing position, rotation, and velocity information
        #[ts(as = "crate::interop::ts_bindings::RigidBodyTs")]
        rigid_body: boxcars::RigidBody,
    },
}

impl BallFrame {
    /// Creates a new [`BallFrame`] from a [`ReplayProcessor`] at the specified time.
    ///
    /// This method extracts the ball's state from the replay processor, handling
    /// cases where ball syncing is disabled or the rigid body is unavailable.
    ///
    /// # Arguments
    ///
    /// * `processor` - The [`ReplayProcessor`] containing the replay data
    /// * `current_time` - The time in seconds at which to extract the ball state
    ///
    /// # Returns
    ///
    /// Returns a [`BallFrame`] which will be [`Empty`](BallFrame::Empty) if:
    /// - Ball syncing is disabled in the replay
    /// - The ball's rigid body cannot be retrieved
    ///
    /// Otherwise returns [`Data`](BallFrame::Data) containing the ball's rigid body.
    fn new_from_processor(processor: &dyn ProcessorView, current_time: f32) -> Self {
        if processor.get_ignore_ball_syncing().unwrap_or(false) {
            Self::Empty
        } else {
            match processor.get_interpolated_ball_rigid_body(current_time, 0.0) {
                Ok(rigid_body) => Self::new_from_rigid_body(rigid_body),
                _ => Self::Empty,
            }
        }
    }

    /// Creates a new [`BallFrame`] from a rigid body.
    ///
    /// # Arguments
    ///
    /// * `rigid_body` - The ball's rigid body containing physics information
    ///
    /// # Returns
    ///
    /// Returns [`Data`](BallFrame::Data) containing the rigid body even when the
    /// ball is sleeping, so stationary kickoff frames still retain the ball's
    /// position for downstream consumers such as the JS player.
    fn new_from_rigid_body(rigid_body: boxcars::RigidBody) -> Self {
        Self::Data { rigid_body }
    }
}

/// Replay-driven continuous camera look state for a player at a single frame.
///
/// Captured from the player's `TAGame.CameraSettingsActor_TA` actor. Rocket
/// League does not replicate the camera's world position, so this is the raw
/// material a renderer uses to *reconstruct* the player's point of view rather
/// than a literal camera transform. The discrete camera toggles (ball cam,
/// behind-view) flip rarely and are carried in the coalesced
/// [`PlayerCameraStateChange`] stream instead of on every frame.
///
/// Every field is optional: it is `None` when the replay does not replicate
/// that attribute for the player (e.g. very old replays, or a player whose
/// camera-settings actor has not appeared yet).
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct PlayerCameraFrame {
    /// Raw camera pitch byte (0-255) as replicated; convert at display time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pitch: Option<u8>,
    /// Raw camera yaw byte (0-255) as replicated; convert at display time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub yaw: Option<u8>,
}

/// Replay-driven vehicle input/state for a player at a single frame.
///
/// Captured from the car's `TAGame.Vehicle_TA` actor and dodge component.
/// These let a renderer drive accurate wheel steering/spin and flip direction
/// instead of estimating them from position deltas. The rarely-flipping driving
/// flag lives in the coalesced [`PlayerCameraStateChange`] stream instead.
///
/// Every field is optional: it is `None` when the replay does not replicate
/// that attribute for the frame.
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct PlayerInputFrame {
    /// Raw throttle byte (0-255, ~128 neutral); convert at display time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub throttle: Option<u8>,
    /// Raw steer byte (0-255, ~128 centered); convert at display time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub steer: Option<u8>,
    /// Impulse vector `(x, y, z)` in raw replay units of the most recent
    /// dodge. Meaningful while [`PlayerFrame::Data::dodge_active`] is set.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dodge_impulse: Option<(f32, f32, f32)>,
    /// Torque vector `(x, y, z)` in raw replay units of the most recent dodge.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dodge_torque: Option<(f32, f32, f32)>,
}

/// Represents a player's state for a single frame in a Rocket League replay.
///
/// Contains comprehensive information about a player's position, movement,
/// and control inputs during a specific frame of the replay.
///
/// # Variants
///
/// - [`Empty`](PlayerFrame::Empty) - Indicates the player state is unavailable
/// - [`Data`](PlayerFrame::Data) - Contains the player's complete state information
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub enum PlayerFrame {
    /// Empty frame indicating the player state is unavailable
    Empty,
    /// Frame containing the player's complete state data
    Data {
        /// The player's rigid body containing position, rotation, and velocity information
        #[ts(as = "crate::interop::ts_bindings::RigidBodyTs")]
        rigid_body: boxcars::RigidBody,
        /// The player's current boost amount in raw replay units (0.0 to 255.0)
        boost_amount: f32,
        /// Whether the player is actively using boost
        boost_active: bool,
        /// Whether the player is actively powersliding / holding handbrake
        powerslide_active: bool,
        /// Whether the player is actively jumping
        jump_active: bool,
        /// Whether the player is performing a double jump
        double_jump_active: bool,
        /// Whether the player is performing a dodge maneuver
        dodge_active: bool,
        /// The player's name as it appears in the replay
        player_name: Option<String>,
        /// The team the player belongs to (0 or 1)
        team: Option<i32>,
        /// Whether the player is on team 0 (blue team typically)
        is_team_0: Option<bool>,
        /// Replay-driven camera state (ball cam, look direction) for the player
        camera: PlayerCameraFrame,
        /// Replay-driven vehicle inputs (throttle, steer, dodge vectors)
        input: PlayerInputFrame,
    },
}

impl PlayerFrame {
    /// Creates a new [`PlayerFrame`] from a [`ReplayProcessor`] for a specific player at the specified time.
    ///
    /// This method extracts comprehensive player state information from the replay processor,
    /// including position, control inputs, and team information.
    ///
    /// # Arguments
    ///
    /// * `processor` - The [`ReplayProcessor`] containing the replay data
    /// * `player_id` - The unique identifier for the player
    /// * `current_time` - The time in seconds at which to extract the player state
    ///
    /// # Returns
    ///
    /// Returns a [`SubtrActorResult`] containing a [`PlayerFrame::Data`] value
    /// with the player's complete state information.
    ///
    /// # Errors
    ///
    /// Returns a [`SubtrActorError`] if:
    /// - The player's rigid body cannot be retrieved
    fn new_from_processor(
        processor: &dyn ProcessorView,
        player_id: &PlayerId,
        current_time: f32,
    ) -> SubtrActorResult<Self> {
        let rigid_body =
            processor.get_interpolated_player_rigid_body(player_id, current_time, 0.0)?;

        let boost_amount = processor.get_player_boost_level(player_id).unwrap_or(0.0);
        let boost_active = processor.get_boost_active(player_id).unwrap_or(0) % 2 == 1;
        let powerslide_active = processor.get_powerslide_active(player_id).unwrap_or(false);
        let jump_active = processor.get_jump_active(player_id).unwrap_or(0) % 2 == 1;
        let double_jump_active = processor.get_double_jump_active(player_id).unwrap_or(0) % 2 == 1;
        let dodge_active = processor.get_dodge_active(player_id).unwrap_or(0) % 2 == 1;

        // Replay-driven continuous camera/vehicle state. Each read is optional:
        // older replays and frames without the attribute simply leave it `None`
        // so consumers can fall back to a synthesized value. Discrete toggles
        // (ball cam, behind-view, driving) are emitted as coalesced
        // `PlayerCameraStateChange`s rather than stored on every frame.
        let camera = PlayerCameraFrame {
            pitch: processor.get_camera_pitch(player_id).ok(),
            yaw: processor.get_camera_yaw(player_id).ok(),
        };
        let input = PlayerInputFrame {
            throttle: processor.get_throttle(player_id).ok(),
            steer: processor.get_steer(player_id).ok(),
            dodge_impulse: processor.get_dodge_impulse(player_id).ok(),
            dodge_torque: processor.get_dodge_torque(player_id).ok(),
        };

        // Extract player identity information
        let player_name = processor.get_player_name(player_id).ok();
        let team = processor
            .get_player_team_key(player_id)
            .ok()
            .and_then(|team_key| team_key.parse::<i32>().ok());
        let is_team_0 = processor.get_player_is_team_0(player_id).ok();

        Ok(Self::from_data(
            rigid_body,
            boost_amount,
            boost_active,
            powerslide_active,
            jump_active,
            double_jump_active,
            dodge_active,
            player_name,
            team,
            is_team_0,
            camera,
            input,
        ))
    }

    /// Creates a [`PlayerFrame`] from the provided data components.
    ///
    /// # Arguments
    ///
    /// * `rigid_body` - The player's rigid body physics information
    /// * `boost_amount` - The player's current boost level in raw replay units (0.0 to 255.0)
    /// * `boost_active` - Whether the player is actively using boost
    /// * `powerslide_active` - Whether the player is actively powersliding
    /// * `jump_active` - Whether the player is actively jumping
    /// * `double_jump_active` - Whether the player is performing a double jump
    /// * `dodge_active` - Whether the player is performing a dodge maneuver
    /// * `player_name` - The player's name, if available
    /// * `team` - The player's team number, if available
    /// * `is_team_0` - Whether the player is on team 0, if available
    /// * `camera` - Replay-driven camera state for the player
    /// * `input` - Replay-driven vehicle input/state for the player
    ///
    /// # Returns
    ///
    /// Returns [`Data`](PlayerFrame::Data) with all provided information, even
    /// when the rigid body is sleeping, so stationary kickoff and reset frames
    /// still retain the player's position for downstream consumers such as the
    /// JS player.
    #[allow(clippy::too_many_arguments)]
    fn from_data(
        rigid_body: boxcars::RigidBody,
        boost_amount: f32,
        boost_active: bool,
        powerslide_active: bool,
        jump_active: bool,
        double_jump_active: bool,
        dodge_active: bool,
        player_name: Option<String>,
        team: Option<i32>,
        is_team_0: Option<bool>,
        camera: PlayerCameraFrame,
        input: PlayerInputFrame,
    ) -> Self {
        Self::Data {
            rigid_body,
            boost_amount,
            boost_active,
            powerslide_active,
            jump_active,
            double_jump_active,
            dodge_active,
            player_name,
            team,
            is_team_0,
            camera,
            input,
        }
    }
}

/// Contains all frame data for a single player throughout the replay.
///
/// This structure holds a chronological sequence of [`PlayerFrame`] instances
/// representing the player's state at each processed frame of the replay.
///
/// # Fields
///
/// * `frames` - A vector of [`PlayerFrame`] instances in chronological order
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct PlayerData {
    /// Vector of player frames in chronological order
    frames: Vec<PlayerFrame>,
}

impl PlayerData {
    /// Creates a new empty [`PlayerData`] instance.
    ///
    /// # Returns
    ///
    /// Returns a new [`PlayerData`] with an empty frames vector.
    fn new() -> Self {
        Self { frames: Vec::new() }
    }

    /// Adds a player frame at the specified frame index.
    ///
    /// If the frame index is beyond the current length of the frames vector,
    /// empty frames will be inserted to fill the gap before adding the new frame.
    ///
    /// # Arguments
    ///
    /// * `frame_index` - The index at which to insert the frame
    /// * `frame` - The [`PlayerFrame`] to add
    fn add_frame(&mut self, frame_index: usize, frame: PlayerFrame) {
        let empty_frames_to_add = frame_index - self.frames.len();
        if empty_frames_to_add > 0 {
            for _ in 0..empty_frames_to_add {
                self.frames.push(PlayerFrame::Empty)
            }
        }
        self.frames.push(frame)
    }

    /// Returns a reference to the frames vector.
    ///
    /// # Returns
    ///
    /// Returns a reference to the vector of [`PlayerFrame`] instances.
    pub fn frames(&self) -> &Vec<PlayerFrame> {
        &self.frames
    }

    /// Returns the number of frames in this player's data.
    ///
    /// # Returns
    ///
    /// Returns the total number of frames stored for this player.
    pub fn frame_count(&self) -> usize {
        self.frames.len()
    }
}

/// Contains all frame data for the ball throughout the replay.
///
/// This structure holds a chronological sequence of [`BallFrame`] instances
/// representing the ball's state at each processed frame of the replay.
///
/// # Fields
///
/// * `frames` - A vector of [`BallFrame`] instances in chronological order
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct BallData {
    /// Vector of ball frames in chronological order
    frames: Vec<BallFrame>,
}

impl BallData {
    /// Creates a new empty [`BallData`] instance.
    ///
    /// # Returns
    ///
    /// Returns a new [`BallData`] with an empty frames vector.
    fn new() -> Self {
        Self { frames: Vec::new() }
    }

    /// Adds a ball frame at the specified frame index.
    ///
    /// If the frame index is beyond the current length of the frames vector,
    /// empty frames will be inserted to fill the gap before adding the new frame.
    ///
    /// # Arguments
    ///
    /// * `frame_index` - The index at which to insert the frame
    /// * `frame` - The [`BallFrame`] to add
    fn add_frame(&mut self, frame_index: usize, frame: BallFrame) {
        let empty_frames_to_add = frame_index - self.frames.len();
        if empty_frames_to_add > 0 {
            for _ in 0..empty_frames_to_add {
                self.frames.push(BallFrame::Empty)
            }
        }
        self.frames.push(frame)
    }

    /// Returns a reference to the frames vector.
    ///
    /// # Returns
    ///
    /// Returns a reference to the vector of [`BallFrame`] instances.
    pub fn frames(&self) -> &Vec<BallFrame> {
        &self.frames
    }

    /// Returns the number of frames in the ball data.
    ///
    /// # Returns
    ///
    /// Returns the total number of frames stored for the ball.
    pub fn frame_count(&self) -> usize {
        self.frames.len()
    }
}

/// Represents game metadata for a single frame in a Rocket League replay.
///
/// Contains timing information and game state data that applies to the entire
/// game at a specific point in time.
///
/// # Fields
///
/// * `time` - The current time in seconds since the start of the replay
/// * `seconds_remaining` - The number of seconds remaining in the current game period
/// * `replicated_game_state_name` - The game state enum value (indicates countdown, playing, goal, etc.)
/// * `replicated_game_state_time_remaining` - The kickoff countdown timer, usually 3 to 0
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct MetadataFrame {
    /// The current time in seconds since the start of the replay
    pub time: f32,
    /// The number of seconds remaining in the current game period
    pub seconds_remaining: i32,
    /// The game state enum value (indicates countdown, playing, goal scored, etc.)
    pub replicated_game_state_name: i32,
    /// The kickoff countdown timer exposed by the replay metadata actor.
    pub replicated_game_state_time_remaining: i32,
}

impl MetadataFrame {
    /// Creates a new [`MetadataFrame`] from a [`ReplayProcessor`] at the specified time.
    ///
    /// # Arguments
    ///
    /// * `processor` - The [`ReplayProcessor`] containing the replay data
    /// * `time` - The current time in seconds since the start of the replay
    ///
    /// # Returns
    ///
    /// Returns a [`SubtrActorResult`] containing a [`MetadataFrame`] with the
    /// current time and remaining seconds extracted from the processor.
    ///
    /// # Errors
    ///
    /// Missing replay metadata fields default to 0 so frame export can continue
    /// for replays whose metadata actor does not carry every optional property.
    fn new_from_processor(processor: &dyn ProcessorView, time: f32) -> SubtrActorResult<Self> {
        Ok(Self::new(
            time,
            metadata_i32_or_default(processor.get_seconds_remaining()),
            metadata_i32_or_default(processor.get_replicated_state_name()),
            metadata_i32_or_default(processor.get_replicated_game_state_time_remaining()),
        ))
    }

    /// Creates a new [`MetadataFrame`] with the specified time, seconds remaining, game state,
    /// and kickoff countdown value.
    ///
    /// # Arguments
    ///
    /// * `time` - The current time in seconds since the start of the replay
    /// * `seconds_remaining` - The number of seconds remaining in the current game period
    /// * `replicated_game_state_name` - The game state enum value
    /// * `replicated_game_state_time_remaining` - The kickoff countdown timer
    ///
    /// # Returns
    ///
    /// Returns a new [`MetadataFrame`] with the provided values.
    fn new(
        time: f32,
        seconds_remaining: i32,
        replicated_game_state_name: i32,
        replicated_game_state_time_remaining: i32,
    ) -> Self {
        MetadataFrame {
            time,
            seconds_remaining,
            replicated_game_state_name,
            replicated_game_state_time_remaining,
        }
    }
}

fn metadata_i32_or_default(value: SubtrActorResult<i32>) -> i32 {
    value.unwrap_or(0)
}

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

/// Contains all frame-by-frame data for a Rocket League replay.
///
/// This structure organizes ball data, player data, and metadata for each
/// frame of the replay, providing a complete picture of the game state
/// throughout the match.
///
/// # Fields
///
/// * `ball_data` - All ball state information across all frames
/// * `players` - Player data for each player, indexed by [`PlayerId`]
/// * `metadata_frames` - Game metadata for each frame including timing information
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct FrameData {
    /// All ball state information across all frames
    pub ball_data: BallData,
    /// Player data for each player, indexed by PlayerId
    #[ts(as = "Vec<(crate::interop::ts_bindings::RemoteIdTs, PlayerData)>")]
    pub players: Vec<(PlayerId, PlayerData)>,
    /// Game metadata for each frame including timing information
    pub metadata_frames: Vec<MetadataFrame>,
}

/// Complete replay data structure containing all extracted information from a Rocket League replay.
///
/// This is the top-level structure that contains all processed replay data including
/// frame-by-frame information, replay metadata, and special events like demolitions.
///
/// # Fields
///
/// * `frame_data` - All frame-by-frame data including ball, player, and metadata information
/// * `meta` - Replay metadata including player information, game settings, and statistics
/// * `demolish_infos` - Information about all demolition events that occurred during the replay
/// * `boost_pad_events` - Exact boost pad pickup/availability events detected while processing
/// * `boost_pads` - Resolved standard boost pad layout annotated with replay pad ids when known
/// * `touch_events` - Replay-authored team touch markers; player attribution is derived by stats
/// * `dodge_refreshed_events` - Exact counter-derived dodge refresh events from the replay
/// * `player_stat_events` - Exact shot/save/assist counter increment events
/// * `goal_events` - Exact goal explosion events with scorer and cumulative score when available
/// * `replay_tick_marks` - Replay-authored timeline tick marks/bookmarks
///
/// # Example
///
/// ```rust
/// use subtr_actor::collector::replay_data::ReplayDataCollector;
/// use boxcars::ParserBuilder;
///
/// let data = std::fs::read("assets/replay-format-2025-06-10-v868-32-net10-replicated-boost.replay").unwrap();
/// let replay = ParserBuilder::new(&data).parse().unwrap();
/// let collector = ReplayDataCollector::new();
/// let replay_data = collector.get_replay_data(&replay).unwrap();
///
/// // Access replay metadata
/// println!("Team 0 players: {}", replay_data.meta.team_zero.len());
///
/// // Access frame data
/// println!("Total frames: {}", replay_data.frame_data.metadata_frames.len());
///
/// // Access demolition events
/// println!("Total demolitions: {}", replay_data.demolish_infos.len());
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct ReplayData {
    /// All frame-by-frame data including ball, player, and metadata information
    pub frame_data: FrameData,
    /// Replay metadata including player information, game settings, and statistics
    pub meta: ReplayMeta,
    /// Information about all demolition events that occurred during the replay
    pub demolish_infos: Vec<DemolishInfo>,
    /// Exact boost pad pickup and availability events observed during the replay
    pub boost_pad_events: Vec<BoostPadEvent>,
    /// Resolved standard boost pad layout annotated with replay pad ids when known
    pub boost_pads: Vec<ResolvedBoostPad>,
    /// Replay-authored team touch markers observed during the replay
    pub touch_events: Vec<TouchEvent>,
    /// Exact dodge refresh events observed via the replay's refreshed-dodge counter
    pub dodge_refreshed_events: Vec<DodgeRefreshedEvent>,
    /// Coalesced camera/vehicle-toggle changes (ball cam, behind-view, driving)
    /// grouped by player — the player id is stored once and each entry holds
    /// that player's frame-ordered changes, rather than a value per frame.
    #[ts(as = "Vec<(crate::interop::ts_bindings::RemoteIdTs, Vec<PlayerCameraStateChange>)>")]
    pub player_camera_events: Vec<(PlayerId, Vec<PlayerCameraStateChange>)>,
    /// Exact player stat counter increments observed during the replay
    pub player_stat_events: Vec<PlayerStatEvent>,
    /// Exact goal events observed during the replay
    pub goal_events: Vec<GoalEvent>,
    /// Replay-authored tick marks/bookmarks from the replay body
    pub replay_tick_marks: Vec<ReplayTickMark>,
}

impl ReplayData {
    /// Serializes the replay data to a JSON string.
    ///
    /// # Returns
    ///
    /// Returns a [`Result`] containing either the JSON string representation
    /// of the replay data or a [`serde_json::Error`] if serialization fails.
    ///
    /// # Example
    ///
    /// ```rust
    /// use subtr_actor::collector::replay_data::ReplayDataCollector;
    /// use boxcars::ParserBuilder;
    ///
    /// let data = std::fs::read("assets/replay-format-2025-06-10-v868-32-net10-replicated-boost.replay").unwrap();
    /// let replay = ParserBuilder::new(&data).parse().unwrap();
    /// let collector = ReplayDataCollector::new();
    /// let replay_data = collector.get_replay_data(&replay).unwrap();
    ///
    /// let json_string = replay_data.as_json().unwrap();
    /// println!("Replay as JSON: {}", json_string);
    /// ```
    pub fn as_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Serializes the replay data to a pretty-printed JSON string.
    ///
    /// # Returns
    ///
    /// Returns a [`Result`] containing either the pretty-printed JSON string
    /// representation of the replay data or a [`serde_json::Error`] if serialization fails.
    pub fn as_pretty_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }
}

fn replay_tick_marks(
    replay: &boxcars::Replay,
    metadata_frames: &[MetadataFrame],
) -> Vec<ReplayTickMark> {
    replay
        .tick_marks
        .iter()
        .map(|tick_mark| ReplayTickMark {
            description: tick_mark.description.clone(),
            frame: tick_mark.frame,
            time: usize::try_from(tick_mark.frame)
                .ok()
                .and_then(|frame| metadata_frames.get(frame))
                .map(|frame| frame.time),
        })
        .collect()
}

/// Groups the processor's flat `(player, change)` camera stream by player,
/// preserving first-appearance player order and per-player frame order, so the
/// serialized form stores each player id once instead of per change.
pub(crate) fn group_player_camera_events(
    events: &[(PlayerId, PlayerCameraStateChange)],
) -> Vec<(PlayerId, Vec<PlayerCameraStateChange>)> {
    let mut grouped: Vec<(PlayerId, Vec<PlayerCameraStateChange>)> = Vec::new();
    for (player_id, change) in events {
        if let Some((_, changes)) = grouped.iter_mut().find(|(id, _)| id == player_id) {
            changes.push(change.clone());
        } else {
            grouped.push((player_id.clone(), vec![change.clone()]));
        }
    }
    grouped
}

#[cfg(test)]
pub(crate) fn player_stat_events_with_shot_saves(
    player_stat_events: &[PlayerStatEvent],
) -> Vec<PlayerStatEvent> {
    player_stat_events_with_shot_saves_and_frame_data(player_stat_events, None, None)
}

fn player_stat_events_with_shot_saves_and_frame_data(
    player_stat_events: &[PlayerStatEvent],
    frame_data: Option<&FrameData>,
    touch_events: Option<&[TouchEvent]>,
) -> Vec<PlayerStatEvent> {
    const MAX_SHOT_SAVE_LINK_SECONDS: f32 = 3.0;

    let mut annotated_events = player_stat_events.to_vec();
    let mut pending_shot_indices: Vec<usize> = Vec::new();

    for index in 0..annotated_events.len() {
        let current_time = annotated_events[index].time;
        pending_shot_indices.retain(|shot_index| {
            current_time - annotated_events[*shot_index].time <= MAX_SHOT_SAVE_LINK_SECONDS
        });

        match annotated_events[index].kind {
            PlayerStatEventKind::Shot => {
                if annotated_events[index].shot.is_some() {
                    pending_shot_indices.push(index);
                }
            }
            PlayerStatEventKind::Save => {
                let save = ShotSaveMetadata {
                    time: annotated_events[index].time,
                    frame: annotated_events[index].frame,
                    player: annotated_events[index].player.clone(),
                    player_position: annotated_events[index].player_position,
                    is_team_0: annotated_events[index].is_team_0,
                };
                let Some(pending_position) = pending_shot_indices.iter().rposition(|shot_index| {
                    let shot_event = &annotated_events[*shot_index];
                    if shot_event.is_team_0 == annotated_events[index].is_team_0 {
                        return false;
                    }
                    let save_time_after_shot = annotated_events[index].time - shot_event.time;
                    if save_time_after_shot <= 0.0
                        || save_time_after_shot > MAX_SHOT_SAVE_LINK_SECONDS
                    {
                        return false;
                    }
                    shot_event
                        .shot
                        .as_ref()
                        .and_then(|shot| shot.projected_goal_line_crossing.as_ref())
                        .is_none_or(|crossing| {
                            shot_goal_line_crossing_is_after_save_reference(
                                shot_event,
                                &save,
                                crossing,
                                touch_events,
                            )
                        })
                }) else {
                    continue;
                };
                let shot_index = pending_shot_indices.remove(pending_position);
                let should_estimate_crossing = annotated_events[shot_index]
                    .shot
                    .as_ref()
                    .is_some_and(|shot| {
                        shot.projected_goal_line_crossing
                            .as_ref()
                            .is_none_or(|crossing| !crossing.inside_goal_mouth)
                    });
                let estimated_crossing = should_estimate_crossing.then(|| {
                    frame_data.and_then(|frame_data| {
                        estimate_saved_shot_goal_line_crossing(
                            &annotated_events[shot_index],
                            &save,
                            frame_data,
                            touch_events,
                        )
                    })
                });
                let unavailable_reason = estimated_crossing
                    .as_ref()
                    .is_none_or(Option::is_none)
                    .then(|| {
                        frame_data.and_then(|frame_data| {
                            saved_shot_goal_line_crossing_unavailable_reason(
                                &annotated_events[shot_index],
                                &save,
                                frame_data,
                                touch_events,
                            )
                        })
                    });
                if let Some(shot) = annotated_events[shot_index].shot.as_mut() {
                    if let Some(Some(estimated_crossing)) = estimated_crossing {
                        shot.projected_goal_target_hit = Some(
                            ShotGoalTargetHit::from_goal_line_crossing(&estimated_crossing),
                        );
                        shot.projected_goal_line_crossing = Some(estimated_crossing);
                        shot.projected_goal_line_crossing_unavailable_reason = None;
                    } else if shot
                        .projected_goal_line_crossing
                        .as_ref()
                        .is_some_and(saved_shot_crossing_is_unphysical_free_flight)
                    {
                        shot.projected_goal_line_crossing = None;
                    }
                    if shot.projected_goal_line_crossing.is_none() {
                        if let Some(Some(unavailable_reason)) = unavailable_reason {
                            shot.projected_goal_line_crossing_unavailable_reason =
                                Some(unavailable_reason);
                        }
                    } else {
                        shot.projected_goal_line_crossing_unavailable_reason = None;
                    }
                    shot.resulting_save = Some(save);
                }
            }
            PlayerStatEventKind::Assist => {}
        }
    }

    annotated_events
}

fn estimate_saved_shot_goal_line_crossing(
    shot_event: &PlayerStatEvent,
    save: &ShotSaveMetadata,
    frame_data: &FrameData,
    touch_events: Option<&[TouchEvent]>,
) -> Option<ShotGoalLineCrossing> {
    const MAX_SAVE_TOUCH_STAT_LAG_SECONDS: f32 = 0.25;

    let prediction_window = saved_shot_prediction_window(shot_event, save, touch_events);
    estimate_saved_shot_goal_line_crossing_in_window(shot_event, frame_data, prediction_window)
        .or_else(|| {
            let lagged_prediction_window = saved_shot_prediction_window_with_save_touch_lag(
                shot_event,
                save,
                touch_events,
                MAX_SAVE_TOUCH_STAT_LAG_SECONDS,
            );
            (lagged_prediction_window.has_save_touch
                && !prediction_window.has_save_touch
                && lagged_prediction_window.estimation_time < prediction_window.shot_time)
                .then(|| {
                    estimate_saved_shot_goal_line_crossing_in_window(
                        shot_event,
                        frame_data,
                        lagged_prediction_window,
                    )
                })
                .flatten()
        })
}

fn estimate_saved_shot_goal_line_crossing_in_window(
    shot_event: &PlayerStatEvent,
    frame_data: &FrameData,
    prediction_window: SavedShotPredictionWindow,
) -> Option<ShotGoalLineCrossing> {
    const MAX_PRE_SAVE_LOOKBACK_SECONDS: f32 = 3.0;
    const MAX_NO_TOUCH_SHOT_STAT_LAG_SECONDS: f32 = 0.1;
    const FLOAT_EPSILON: f32 = 0.0001;

    shot_event.shot.as_ref()?;

    let target_direction = if shot_event.is_team_0 { 1.0 } else { -1.0 };
    let estimation_frame = prediction_window
        .estimation_frame
        .min(frame_data.ball_data.frames.len().saturating_sub(1));
    let mut fallback_crossing = None;
    for frame_index in (0..=estimation_frame).rev() {
        let Some(metadata) = frame_data.metadata_frames.get(frame_index) else {
            continue;
        };
        if metadata.time > prediction_window.estimation_time + FLOAT_EPSILON {
            continue;
        }
        if prediction_window.estimation_time - metadata.time > MAX_PRE_SAVE_LOOKBACK_SECONDS {
            break;
        }
        if prediction_window.has_inferred_shot_touch
            && metadata.time + FLOAT_EPSILON < prediction_window.shot_time
        {
            break;
        }

        let Some(BallFrame::Data { rigid_body }) = frame_data.ball_data.frames.get(frame_index)
        else {
            continue;
        };
        let Some(velocity) = rigid_body.linear_velocity else {
            continue;
        };
        if target_direction * velocity.y <= 0.0 {
            continue;
        }

        let Some(mut crossing) = ShotGoalLineCrossing::predict_saved_shot_from_rigid_body(
            shot_event.is_team_0,
            rigid_body,
        ) else {
            continue;
        };
        let crossing_time = metadata.time + crossing.time_after_shot;
        let mut prediction_start_time = prediction_window.shot_time;
        let mut prediction_start_frame = prediction_window.shot_frame;
        if crossing_time <= prediction_window.shot_time + FLOAT_EPSILON {
            if prediction_window.has_inferred_shot_touch
                || prediction_window.has_save_touch
                || prediction_window.shot_time - crossing_time > MAX_NO_TOUCH_SHOT_STAT_LAG_SECONDS
                || crossing_time <= metadata.time + FLOAT_EPSILON
            {
                continue;
            }
            prediction_start_time = metadata.time;
            prediction_start_frame = frame_index;
        }
        if prediction_window.has_save_touch
            && crossing_time <= prediction_window.estimation_time + FLOAT_EPSILON
        {
            continue;
        }
        crossing.time_after_shot = crossing_time - prediction_start_time;
        crossing.prediction_start_time = Some(prediction_start_time);
        crossing.prediction_start_frame = Some(prediction_start_frame);

        if crossing.inside_goal_mouth {
            return Some(crossing);
        }
        fallback_crossing.get_or_insert(crossing);
    }

    fallback_crossing
}

fn saved_shot_goal_line_crossing_unavailable_reason(
    shot_event: &PlayerStatEvent,
    save: &ShotSaveMetadata,
    frame_data: &FrameData,
    touch_events: Option<&[TouchEvent]>,
) -> Option<ShotGoalLineCrossingUnavailableReason> {
    let prediction_window = saved_shot_prediction_window(shot_event, save, touch_events);
    Some(saved_shot_goal_line_crossing_unavailable_reason_in_window(
        shot_event,
        save,
        frame_data,
        prediction_window,
    ))
}

fn saved_shot_goal_line_crossing_unavailable_reason_in_window(
    shot_event: &PlayerStatEvent,
    save: &ShotSaveMetadata,
    frame_data: &FrameData,
    prediction_window: SavedShotPredictionWindow,
) -> ShotGoalLineCrossingUnavailableReason {
    const MAX_PRE_SAVE_LOOKBACK_SECONDS: f32 = 3.0;
    const FLOAT_EPSILON: f32 = 0.0001;

    let target_direction = if shot_event.is_team_0 { 1.0 } else { -1.0 };
    let estimation_frame = prediction_window
        .estimation_frame
        .min(frame_data.ball_data.frames.len().saturating_sub(1));
    let mut saw_velocity = false;
    let mut inbound_frame_count = 0;
    let mut projected_inbound_frame_count = 0;
    let mut unphysical_free_flight_count = 0;
    let mut crossing_before_or_at_prediction_start_count = 0;
    let mut crossing_before_or_at_save_touch_count = 0;
    let mut crossing_before_or_at_save_count = 0;

    for frame_index in (0..=estimation_frame).rev() {
        let Some(metadata) = frame_data.metadata_frames.get(frame_index) else {
            continue;
        };
        if metadata.time > prediction_window.estimation_time + FLOAT_EPSILON {
            continue;
        }
        if prediction_window.estimation_time - metadata.time > MAX_PRE_SAVE_LOOKBACK_SECONDS {
            break;
        }
        if prediction_window.has_inferred_shot_touch
            && metadata.time + FLOAT_EPSILON < prediction_window.shot_time
        {
            break;
        }

        let Some(BallFrame::Data { rigid_body }) = frame_data.ball_data.frames.get(frame_index)
        else {
            continue;
        };
        let Some(velocity) = rigid_body.linear_velocity else {
            continue;
        };
        saw_velocity = true;
        if target_direction * velocity.y <= 0.0 {
            continue;
        }

        inbound_frame_count += 1;
        let Some((crossing_time, unphysical_free_flight)) =
            saved_shot_diagnostic_crossing_time(shot_event.is_team_0, rigid_body)
        else {
            continue;
        };
        projected_inbound_frame_count += 1;
        if unphysical_free_flight {
            unphysical_free_flight_count += 1;
            continue;
        }

        let absolute_crossing_time = metadata.time + crossing_time;
        if absolute_crossing_time <= prediction_window.shot_time + FLOAT_EPSILON {
            crossing_before_or_at_prediction_start_count += 1;
            continue;
        }
        if prediction_window.has_save_touch
            && absolute_crossing_time <= prediction_window.estimation_time + FLOAT_EPSILON
        {
            crossing_before_or_at_save_touch_count += 1;
            continue;
        }
        if absolute_crossing_time <= save.time + FLOAT_EPSILON {
            crossing_before_or_at_save_count += 1;
            continue;
        }

        return ShotGoalLineCrossingUnavailableReason::NoUsableProjection;
    }

    if !saw_velocity {
        return ShotGoalLineCrossingUnavailableReason::NoBallVelocity;
    }
    if inbound_frame_count == 0 {
        return ShotGoalLineCrossingUnavailableReason::NoGoalwardBallBeforeSaveReference;
    }
    if projected_inbound_frame_count == 0 {
        return ShotGoalLineCrossingUnavailableReason::NoGoalLineCrossingBeforeSaveReference;
    }
    if unphysical_free_flight_count == projected_inbound_frame_count {
        return ShotGoalLineCrossingUnavailableReason::OnlyUnphysicalFreeFlightCrossings;
    }
    if crossing_before_or_at_prediction_start_count == projected_inbound_frame_count {
        return ShotGoalLineCrossingUnavailableReason::CrossingsBeforePredictionStart;
    }
    if crossing_before_or_at_save_touch_count == projected_inbound_frame_count {
        return ShotGoalLineCrossingUnavailableReason::CrossingsBeforeSaveTouch;
    }
    if crossing_before_or_at_save_count == projected_inbound_frame_count {
        return ShotGoalLineCrossingUnavailableReason::CrossingsBeforeSaveStat;
    }

    ShotGoalLineCrossingUnavailableReason::NoUsableProjection
}

fn saved_shot_diagnostic_crossing_time(
    is_team_0: bool,
    rigid_body: &boxcars::RigidBody,
) -> Option<(f32, bool)> {
    let crossing_config = BallGoalLineCrossingConfig::attacking_goal(is_team_0);
    let surfaces = standard_soccar_goal_line_prediction_field_surfaces();
    predict_ball_with_surface_bounces_goal_line_crossing(
        rigid_body,
        crossing_config,
        BallTrajectoryConfig::STANDARD_SOCCAR,
        BallBounceConfig::STANDARD_SOCCAR,
        &surfaces,
    )
    .map(|crossing| (crossing.time, false))
    .or_else(|| {
        predict_free_flight_goal_line_crossing(
            rigid_body,
            crossing_config,
            BallTrajectoryConfig::STANDARD_SOCCAR,
        )
        .map(|crossing| {
            (
                crossing.time,
                crossing.position.z < STANDARD_BALL_RADIUS - STANDARD_GOAL_MOUTH_TRAJECTORY_MARGIN,
            )
        })
    })
}

fn saved_shot_crossing_is_unphysical_free_flight(crossing: &ShotGoalLineCrossing) -> bool {
    matches!(
        crossing.prediction_kind,
        ShotGoalLineCrossingPredictionKind::FreeFlight
            | ShotGoalLineCrossingPredictionKind::SavedShotPreSaveFreeFlight
    ) && crossing.position.z < STANDARD_BALL_RADIUS - STANDARD_GOAL_MOUTH_TRAJECTORY_MARGIN
}

fn shot_goal_line_crossing_is_after_save_reference(
    shot_event: &PlayerStatEvent,
    save: &ShotSaveMetadata,
    crossing: &ShotGoalLineCrossing,
    touch_events: Option<&[TouchEvent]>,
) -> bool {
    const FLOAT_EPSILON: f32 = 0.0001;

    let crossing_time =
        crossing.prediction_start_time.unwrap_or(shot_event.time) + crossing.time_after_shot;
    let save_reference_time =
        saved_shot_prediction_window(shot_event, save, touch_events).save_reference_time();
    crossing_time > save_reference_time + FLOAT_EPSILON
}

#[derive(Debug, Clone, Copy)]
struct SavedShotPredictionWindow {
    shot_frame: usize,
    shot_time: f32,
    has_inferred_shot_touch: bool,
    has_save_touch: bool,
    estimation_frame: usize,
    estimation_time: f32,
}

impl SavedShotPredictionWindow {
    fn save_reference_time(self) -> f32 {
        if self.has_save_touch {
            self.estimation_time
        } else {
            self.estimation_time.max(self.shot_time)
        }
    }
}

fn saved_shot_prediction_window(
    shot_event: &PlayerStatEvent,
    save: &ShotSaveMetadata,
    touch_events: Option<&[TouchEvent]>,
) -> SavedShotPredictionWindow {
    saved_shot_prediction_window_with_save_touch_lag(shot_event, save, touch_events, 0.0)
}

fn saved_shot_prediction_window_with_save_touch_lag(
    shot_event: &PlayerStatEvent,
    save: &ShotSaveMetadata,
    touch_events: Option<&[TouchEvent]>,
    max_save_touch_stat_lag_seconds: f32,
) -> SavedShotPredictionWindow {
    const FLOAT_EPSILON: f32 = 0.0001;
    const MAX_SHOT_TOUCH_LOOKBACK_SECONDS: f32 = 3.0;

    let save_touch = touch_events.and_then(|touch_events| {
        let player_touch = touch_events.iter().rev().find(|touch| {
            touch.team_is_team_0 == save.is_team_0
                && touch.player.as_ref() == Some(&save.player)
                && touch.time >= shot_event.time - max_save_touch_stat_lag_seconds - FLOAT_EPSILON
                && touch.time <= save.time + FLOAT_EPSILON
        });
        let team_touch = || {
            touch_events.iter().rev().find(|touch| {
                touch.team_is_team_0 == save.is_team_0
                    && touch.time
                        >= shot_event.time - max_save_touch_stat_lag_seconds - FLOAT_EPSILON
                    && touch.time <= save.time + FLOAT_EPSILON
            })
        };
        player_touch.or_else(team_touch)
    });
    let shot_touch = touch_events.and_then(|touch_events| {
        let player_touch = touch_events.iter().rev().find(|touch| {
            touch.team_is_team_0 == shot_event.is_team_0
                && touch.player.as_ref() == Some(&shot_event.player)
                && touch.time >= shot_event.time - MAX_SHOT_TOUCH_LOOKBACK_SECONDS - FLOAT_EPSILON
                && touch.time <= shot_event.time + FLOAT_EPSILON
        });
        let team_touch = || {
            touch_events.iter().rev().find(|touch| {
                touch.team_is_team_0 == shot_event.is_team_0
                    && touch.time
                        >= shot_event.time - MAX_SHOT_TOUCH_LOOKBACK_SECONDS - FLOAT_EPSILON
                    && touch.time <= shot_event.time + FLOAT_EPSILON
            })
        };
        player_touch.or_else(team_touch)
    });

    let (estimation_frame, estimation_time) = save_touch
        .map(|touch| {
            let frame = if touch.frame > 0 {
                touch.frame - 1
            } else {
                touch.frame
            };
            (frame, touch.time)
        })
        .unwrap_or((save.frame, save.time));
    let has_save_touch = save_touch.is_some();
    let inferred_shot_touch =
        shot_touch.filter(|touch| touch.time <= estimation_time + FLOAT_EPSILON);
    let has_inferred_shot_touch = inferred_shot_touch.is_some();
    let (shot_frame, shot_time) = inferred_shot_touch
        .map(|touch| (touch.frame, touch.time))
        .unwrap_or((shot_event.frame, shot_event.time));

    if shot_frame <= estimation_frame {
        SavedShotPredictionWindow {
            shot_frame,
            shot_time,
            has_inferred_shot_touch,
            has_save_touch,
            estimation_frame,
            estimation_time,
        }
    } else {
        SavedShotPredictionWindow {
            shot_frame: shot_event.frame,
            shot_time: shot_event.time,
            has_inferred_shot_touch: false,
            has_save_touch,
            estimation_frame,
            estimation_time,
        }
    }
}

impl FrameData {
    /// Creates a new empty [`FrameData`] instance.
    ///
    /// # Returns
    ///
    /// Returns a new [`FrameData`] with empty ball data, player data, and metadata frames.
    fn new() -> Self {
        FrameData {
            ball_data: BallData::new(),
            players: Vec::new(),
            metadata_frames: Vec::new(),
        }
    }

    /// Returns the total number of frames in this frame data.
    ///
    /// # Returns
    ///
    /// Returns the number of metadata frames, which represents the total frame count.
    pub fn frame_count(&self) -> usize {
        self.metadata_frames.len()
    }

    /// Returns the duration of the replay in seconds.
    ///
    /// # Returns
    ///
    /// Returns the time of the last frame, or 0.0 if no frames exist.
    pub fn duration(&self) -> f32 {
        self.metadata_frames.last().map(|f| f.time).unwrap_or(0.0)
    }

    /// Adds a complete frame of data to the frame data structure.
    ///
    /// This method adds metadata, ball data, and player data for a single frame
    /// to their respective collections, maintaining frame synchronization across
    /// all data types.
    ///
    /// # Arguments
    ///
    /// * `frame_metadata` - The metadata for this frame (time, game state, etc.)
    /// * `ball_frame` - The ball state for this frame
    /// * `player_frames` - Player state data for all players in this frame
    ///
    /// # Returns
    ///
    /// Returns a [`SubtrActorResult`] indicating success or failure of the operation.
    ///
    /// # Errors
    ///
    /// May return a [`SubtrActorError`] if frame data cannot be processed correctly.
    fn add_frame(
        &mut self,
        frame_metadata: MetadataFrame,
        ball_frame: BallFrame,
        player_frames: Vec<(PlayerId, PlayerFrame)>,
    ) -> SubtrActorResult<()> {
        let frame_index = self.metadata_frames.len();
        self.metadata_frames.push(frame_metadata);
        self.ball_data.add_frame(frame_index, ball_frame);
        for (player_id, frame) in player_frames {
            self.players
                .get_entry(player_id)
                .or_insert_with(PlayerData::new)
                .add_frame(frame_index, frame)
        }
        Ok(())
    }
}

/// A collector that extracts comprehensive frame-by-frame data from Rocket League replays.
///
/// [`ReplayDataCollector`] implements the [`Collector`] trait to process replay frames
/// and extract detailed information about ball movement, player actions, and game state.
/// It builds a complete [`ReplayData`] structure containing all available information
/// from the replay.
///
/// # Usage
///
/// The collector is designed to be used with the [`ReplayProcessor`] to extract
/// comprehensive replay data:
///
/// ```rust
/// use subtr_actor::collector::replay_data::ReplayDataCollector;
/// use boxcars::ParserBuilder;
///
/// let data = std::fs::read("assets/replay-format-2025-06-10-v868-32-net10-replicated-boost.replay").unwrap();
/// let replay = ParserBuilder::new(&data).parse().unwrap();
///
/// let collector = ReplayDataCollector::new();
/// let replay_data = collector.get_replay_data(&replay).unwrap();
///
/// // Process the extracted data
/// for (frame_idx, metadata) in replay_data.frame_data.metadata_frames.iter().enumerate() {
///     println!("Frame {}: Time={:.2}s, Remaining={}s",
///              frame_idx, metadata.time, metadata.seconds_remaining);
/// }
/// ```
///
/// # Fields
///
/// * `frame_data` - Internal storage for frame-by-frame data during collection
pub struct ReplayDataCollector {
    /// Internal storage for frame-by-frame data during collection
    frame_data: FrameData,
}

impl Default for ReplayDataCollector {
    /// Creates a default [`ReplayDataCollector`] instance.
    ///
    /// This is equivalent to calling [`ReplayDataCollector::new()`].
    fn default() -> Self {
        Self::new()
    }
}

impl ReplayDataCollector {
    /// Creates a new [`ReplayDataCollector`] instance.
    ///
    /// # Returns
    ///
    /// Returns a new collector ready to process replay frames.
    pub fn new() -> Self {
        ReplayDataCollector {
            frame_data: FrameData::new(),
        }
    }

    /// Consumes the collector and returns the collected frame data.
    ///
    /// # Returns
    ///
    /// Returns the [`FrameData`] containing all processed frame information.
    pub fn get_frame_data(self) -> FrameData {
        self.frame_data
    }

    pub fn into_replay_data(self, processor: ReplayProcessor<'_>) -> SubtrActorResult<ReplayData> {
        let meta = processor.get_replay_meta()?;
        let frame_data = self.get_frame_data();
        Ok(ReplayData {
            meta,
            demolish_infos: processor.demolishes().to_vec(),
            boost_pad_events: processor.boost_pad_events().to_vec(),
            boost_pads: processor.resolved_boost_pads(),
            touch_events: processor.touch_events().to_vec(),
            dodge_refreshed_events: processor.dodge_refreshed_events().to_vec(),
            player_camera_events: group_player_camera_events(processor.player_camera_events()),
            player_stat_events: player_stat_events_with_shot_saves_and_frame_data(
                processor.player_stat_events(),
                Some(&frame_data),
                Some(processor.touch_events()),
            ),
            goal_events: processor.goal_events().to_vec(),
            replay_tick_marks: replay_tick_marks(processor.replay, &frame_data.metadata_frames),
            frame_data,
        })
    }

    /// Processes a replay and returns complete replay data.
    ///
    /// This method processes the entire replay using a [`ReplayProcessor`] and
    /// extracts all available information including frame-by-frame data, metadata,
    /// and special events like demolitions.
    ///
    /// # Arguments
    ///
    /// * `replay` - The parsed replay data from the [`boxcars`] library
    ///
    /// # Returns
    ///
    /// Returns a [`SubtrActorResult`] containing the complete [`ReplayData`] structure
    /// with all extracted information.
    ///
    /// # Errors
    ///
    /// Returns a [`SubtrActorError`] if:
    /// - The replay processor cannot be created
    /// - Frame processing fails
    /// - Replay metadata cannot be extracted
    ///
    /// # Example
    ///
    /// ```rust
    /// use subtr_actor::collector::replay_data::ReplayDataCollector;
    /// use boxcars::ParserBuilder;
    ///
    /// let data = std::fs::read("assets/replay-format-2025-06-10-v868-32-net10-replicated-boost.replay").unwrap();
    /// let replay = ParserBuilder::new(&data).parse().unwrap();
    ///
    /// let collector = ReplayDataCollector::new();
    /// let replay_data = collector.get_replay_data(&replay).unwrap();
    ///
    /// println!("Processed {} frames", replay_data.frame_data.frame_count());
    /// ```
    pub fn get_replay_data(mut self, replay: &boxcars::Replay) -> SubtrActorResult<ReplayData> {
        let mut processor = ReplayProcessor::new(replay)?;
        processor.process_all(&mut [&mut self])?;
        self.into_replay_data(processor)
    }

    /// Extracts player frame data for all players at the specified time.
    ///
    /// This method iterates through all players in the replay and extracts their
    /// state information at the given time, returning a vector of player frames
    /// indexed by player ID.
    ///
    /// # Arguments
    ///
    /// * `processor` - The [`ReplayProcessor`] containing the replay data
    /// * `current_time` - The time in seconds at which to extract player states
    ///
    /// # Returns
    ///
    /// Returns a [`SubtrActorResult`] containing a vector of tuples with player IDs
    /// and their corresponding [`PlayerFrame`] data.
    ///
    /// # Errors
    ///
    /// Returns a [`SubtrActorError`] if player frame data cannot be extracted.
    fn get_player_frames(
        &self,
        processor: &dyn ProcessorView,
        current_time: f32,
    ) -> SubtrActorResult<Vec<(PlayerId, PlayerFrame)>> {
        Ok(processor
            .iter_player_ids_in_order()
            .map(|player_id| {
                (
                    player_id.clone(),
                    PlayerFrame::new_from_processor(processor, player_id, current_time)
                        .unwrap_or(PlayerFrame::Empty),
                )
            })
            .collect())
    }
}

impl Collector for ReplayDataCollector {
    /// Processes a single frame of the replay and extracts all relevant data.
    ///
    /// This method is called by the [`ReplayProcessor`] for each frame in the replay.
    /// It extracts metadata, ball state, and player state information and adds them
    /// to the internal frame data structure.
    ///
    /// # Arguments
    ///
    /// * `processor` - The [`ReplayProcessor`] containing the replay data and context
    /// * `_frame` - The current frame data (unused in this implementation)
    /// * `_frame_number` - The current frame number (unused in this implementation)
    /// * `current_time` - The current time in seconds since the start of the replay
    ///
    /// # Returns
    ///
    /// Returns a [`SubtrActorResult`] containing [`TimeAdvance::NextFrame`] to
    /// indicate that processing should continue to the next frame.
    ///
    /// # Errors
    ///
    /// Returns a [`SubtrActorError`] if:
    /// - Metadata frame cannot be created
    /// - Player frame data cannot be extracted
    /// - Frame data cannot be added to the collection
    fn process_frame(
        &mut self,
        processor: &dyn ProcessorView,
        _frame: &boxcars::Frame,
        _frame_number: usize,
        current_time: f32,
    ) -> SubtrActorResult<TimeAdvance> {
        let metadata_frame = MetadataFrame::new_from_processor(processor, current_time)?;
        let ball_frame = BallFrame::new_from_processor(processor, current_time);
        let player_frames = self.get_player_frames(processor, current_time)?;
        self.frame_data
            .add_frame(metadata_frame, ball_frame, player_frames)?;
        Ok(TimeAdvance::NextFrame)
    }
}