Skip to main content

subtr_actor/stats/accumulators/
movement.rs

1use super::*;
2
3const MOVEMENT_SPEED_BAND_LABEL_VALUES: [&str; 3] = ["slow", "boost", "supersonic"];
4
5fn movement_speed_band_label(value: &str) -> StatLabel {
6    match value {
7        "boost" => StatLabel::new("speed_band", "boost"),
8        "supersonic" => StatLabel::new("speed_band", "supersonic"),
9        _ => StatLabel::new("speed_band", "slow"),
10    }
11}
12
13fn movement_height_band_label(value: &str) -> StatLabel {
14    match value {
15        "low_air" => StatLabel::new("height_band", "low_air"),
16        "high_air" => StatLabel::new("height_band", "high_air"),
17        _ => StatLabel::new("height_band", "ground"),
18    }
19}
20
21/// Accumulated movement stats: tracked time, distance, speed integral, and time in speed/air bands.
22#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, ts_rs::TS)]
23#[ts(export)]
24pub struct MovementStats {
25    pub tracked_time: f32,
26    pub total_distance: f32,
27    pub speed_integral: f32,
28    pub time_slow_speed: f32,
29    pub time_boost_speed: f32,
30    pub time_supersonic_speed: f32,
31    pub time_on_ground: f32,
32    pub time_low_air: f32,
33    pub time_high_air: f32,
34    #[serde(default, skip_serializing_if = "LabeledFloatSums::is_empty")]
35    pub labeled_tracked_time: LabeledFloatSums,
36}
37
38impl MovementStats {
39    pub fn average_speed(&self) -> f32 {
40        if self.tracked_time == 0.0 {
41            0.0
42        } else {
43            self.speed_integral / self.tracked_time
44        }
45    }
46
47    pub fn average_speed_pct(&self) -> f32 {
48        self.average_speed() * 100.0 / CAR_MAX_SPEED
49    }
50
51    pub fn slow_speed_pct(&self) -> f32 {
52        if self.tracked_time == 0.0 {
53            0.0
54        } else {
55            self.time_slow_speed * 100.0 / self.tracked_time
56        }
57    }
58
59    pub fn boost_speed_pct(&self) -> f32 {
60        if self.tracked_time == 0.0 {
61            0.0
62        } else {
63            self.time_boost_speed * 100.0 / self.tracked_time
64        }
65    }
66
67    pub fn supersonic_speed_pct(&self) -> f32 {
68        if self.tracked_time == 0.0 {
69            0.0
70        } else {
71            self.time_supersonic_speed * 100.0 / self.tracked_time
72        }
73    }
74
75    pub fn on_ground_pct(&self) -> f32 {
76        if self.tracked_time == 0.0 {
77            0.0
78        } else {
79            self.time_on_ground * 100.0 / self.tracked_time
80        }
81    }
82
83    pub fn low_air_pct(&self) -> f32 {
84        if self.tracked_time == 0.0 {
85            0.0
86        } else {
87            self.time_low_air * 100.0 / self.tracked_time
88        }
89    }
90
91    pub fn high_air_pct(&self) -> f32 {
92        if self.tracked_time == 0.0 {
93            0.0
94        } else {
95            self.time_high_air * 100.0 / self.tracked_time
96        }
97    }
98
99    pub fn tracked_time_with_labels(&self, labels: &[StatLabel]) -> f32 {
100        self.labeled_tracked_time.sum_matching(labels)
101    }
102
103    pub fn complete_labeled_tracked_time(&self) -> LabeledFloatSums {
104        let mut entries: Vec<_> = ALL_PLAYER_VERTICAL_BANDS
105            .into_iter()
106            .flat_map(|height_band| {
107                MOVEMENT_SPEED_BAND_LABEL_VALUES
108                    .into_iter()
109                    .map(move |speed_band| {
110                        let mut labels = vec![
111                            StatLabel::new("speed_band", speed_band),
112                            height_band.as_label(),
113                        ];
114                        labels.sort();
115                        LabeledFloatSumEntry {
116                            value: self.labeled_tracked_time.sum_exact(&labels),
117                            labels,
118                        }
119                    })
120            })
121            .collect();
122
123        entries.sort_by(|left, right| left.labels.cmp(&right.labels));
124
125        LabeledFloatSums { entries }
126    }
127
128    pub fn with_complete_labeled_tracked_time(mut self) -> Self {
129        self.labeled_tracked_time = self.complete_labeled_tracked_time();
130        self
131    }
132}
133
134/// Accumulates per-player movement stats over the replay.
135#[derive(Debug, Clone, Default, PartialEq)]
136pub struct MovementStatsAccumulator {
137    player_stats: HashMap<PlayerId, MovementStats>,
138    team_zero_stats: MovementStats,
139    team_one_stats: MovementStats,
140}
141
142impl MovementStatsAccumulator {
143    pub fn new() -> Self {
144        Self::default()
145    }
146
147    pub fn player_stats(&self) -> &HashMap<PlayerId, MovementStats> {
148        &self.player_stats
149    }
150
151    pub fn team_zero_stats(&self) -> &MovementStats {
152        &self.team_zero_stats
153    }
154
155    pub fn team_one_stats(&self) -> &MovementStats {
156        &self.team_one_stats
157    }
158
159    pub fn apply_event(&mut self, event: &MovementEvent) {
160        let stats = self.player_stats.entry(event.player.clone()).or_default();
161        Self::apply_to_stats(stats, event);
162        let team_stats = if event.is_team_0 {
163            &mut self.team_zero_stats
164        } else {
165            &mut self.team_one_stats
166        };
167        Self::apply_to_stats(team_stats, event);
168    }
169
170    fn apply_to_stats(stats: &mut MovementStats, event: &MovementEvent) {
171        stats.tracked_time += event.dt;
172        stats.speed_integral += event.speed * event.dt;
173        stats.total_distance += event.distance;
174
175        match event.speed_band.as_str() {
176            "boost" => stats.time_boost_speed += event.dt,
177            "supersonic" => stats.time_supersonic_speed += event.dt,
178            _ => stats.time_slow_speed += event.dt,
179        }
180
181        match event.height_band.as_str() {
182            "low_air" => stats.time_low_air += event.dt,
183            "high_air" => stats.time_high_air += event.dt,
184            _ => stats.time_on_ground += event.dt,
185        }
186
187        stats.labeled_tracked_time.add(
188            [
189                movement_speed_band_label(&event.speed_band),
190                movement_height_band_label(&event.height_band),
191            ],
192            event.dt,
193        );
194    }
195}