subtr-actor 1.2.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
use super::*;

const DT: f32 = 0.25;

fn player_id(id: u64) -> PlayerId {
    boxcars::RemoteId::Steam(id)
}

fn rigid_body(position: glam::Vec3) -> boxcars::RigidBody {
    boxcars::RigidBody {
        sleeping: false,
        location: glam_to_vec(&position),
        rotation: boxcars::Quaternion {
            x: 0.0,
            y: 0.0,
            z: 0.0,
            w: 1.0,
        },
        linear_velocity: Some(glam_to_vec(&glam::Vec3::ZERO)),
        angular_velocity: Some(glam_to_vec(&glam::Vec3::ZERO)),
    }
}

fn frame(frame_number: usize, time: f32) -> FrameInfo {
    FrameInfo {
        frame_number,
        time,
        dt: DT,
        seconds_remaining: None,
    }
}

fn ball_at(y: f32, z: f32) -> BallFrameState {
    BallFrameState::Present(BallSample {
        rigid_body: rigid_body(glam::Vec3::new(0.0, y, z)),
    })
}

fn player_sample(id: u64, is_team_0: bool, position: glam::Vec3) -> PlayerSample {
    PlayerSample {
        player_id: player_id(id),
        is_team_0,
        hitbox: default_car_hitbox(),
        rigid_body: Some(rigid_body(position)),
        boost_amount: None,
        last_boost_amount: None,
        boost_active: false,
        dodge_active: false,
        dodge_torque: None,
        powerslide_active: false,
        match_goals: None,
        match_assists: None,
        match_saves: None,
        match_shots: None,
        match_score: None,
    }
}

fn players_at(position: glam::Vec3) -> PlayerFrameState {
    PlayerFrameState {
        players: vec![
            player_sample(1, true, position),
            player_sample(2, false, glam::Vec3::new(1000.0, -3000.0, 17.0)),
        ],
    }
}

fn players_with(team_zero: glam::Vec3, team_one: glam::Vec3) -> PlayerFrameState {
    PlayerFrameState {
        players: vec![
            player_sample(1, true, team_zero),
            player_sample(2, false, team_one),
        ],
    }
}

fn possession(player: Option<u64>, team_is_team_0: Option<bool>) -> PossessionState {
    PossessionState {
        active_team_before_sample: team_is_team_0,
        current_team_is_team_0: team_is_team_0,
        active_player_before_sample: player.map(player_id),
        current_player: player.map(player_id),
        ..Default::default()
    }
}

fn touch(frame: usize, time: f32, id: u64, position: glam::Vec3) -> TouchEvent {
    TouchEvent {
        touch_id: None,
        time,
        frame,
        team_is_team_0: true,
        player: Some(player_id(id)),
        player_position: Some(glam_to_vec(&position)),
        closest_approach_distance: Some(0.0),
        contact_local_ball_position: None,
        contact_local_hitbox_point: None,
        contact_world_hitbox_point: None,
        dodge_contact: false,
    }
}

fn touch_state(events: Vec<TouchEvent>) -> TouchState {
    TouchState {
        touch_events: events,
        last_touch: None,
        last_touch_player: None,
        last_touch_team_is_team_0: None,
    }
}

fn update(
    calculator: &mut PlayerPossessionCalculator,
    frame_number: usize,
    time: f32,
    ball: &BallFrameState,
    players: &PlayerFrameState,
    state: &PossessionState,
    touches: Vec<TouchEvent>,
) {
    calculator
        .update(
            &frame(frame_number, time),
            ball,
            players,
            state,
            &touch_state(touches),
            &LivePlayState::active_play(),
        )
        .expect("update succeeds");
}

#[test]
fn emits_single_enriched_span_for_continuous_possession() {
    let mut calculator = PlayerPossessionCalculator::new();
    let players = players_at(glam::Vec3::new(0.0, 0.0, 17.0));
    let state = possession(Some(1), Some(true));

    // Ball advances toward the team-zero goal (+y) by 100 uu per frame, well
    // off the player so no carry sample registers.
    for step in 0..8 {
        let time = step as f32 * DT;
        let ball = ball_at(step as f32 * 100.0, 1000.0);
        // Two distinct touches so the span qualifies as possession; they are
        // grounded and well clear of the carry gap, so enrichment is unchanged.
        let touches = if step == 0 || step == 4 {
            vec![touch(step, time, 1, glam::Vec3::new(0.0, 0.0, 17.0))]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            time,
            &ball,
            &players,
            &state,
            touches,
        );
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 1);
    let event = &events[0];
    assert_eq!(event.player_id, player_id(1));
    assert!(event.is_team_0);
    assert_eq!(event.touch_count, 2);
    assert_eq!(event.aerial_touch_count, 0);
    assert_eq!(event.wall_touch_count, 0);
    // Possessed duration is credited only up to the final touch (step 4);
    // the touchless tail through step 7 is not possession.
    assert!((event.duration - 5.0 * DT).abs() < 1e-4);
    // 4 inter-frame deltas of 100 uu each land before the final touch; the
    // drift after it is not credited.
    assert!((event.advance_distance - 400.0).abs() < 1.0);
    assert_eq!(event.retreat_distance, 0.0);
    assert_eq!(event.carry_time, 0.0);
}

#[test]
fn merges_spans_across_a_short_neutral_gap() {
    let mut calculator = PlayerPossessionCalculator::new();
    let players = players_at(glam::Vec3::new(0.0, 0.0, 17.0));
    let ball = ball_at(0.0, 1000.0);
    let owned = possession(Some(1), Some(true));
    let neutral = possession(None, None);

    // One touch before the gap and one after: the merged span has the two
    // distinct touches it needs to qualify as possession.
    for step in 0..4 {
        let touches = if step == 0 {
            vec![touch(
                step,
                step as f32 * DT,
                1,
                glam::Vec3::new(0.0, 0.0, 17.0),
            )]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &owned,
            touches,
        );
    }
    // Contested window shorter than the merge gap.
    for step in 4..8 {
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &neutral,
            vec![],
        );
    }
    for step in 8..12 {
        let touches = if step == 8 {
            vec![touch(
                step,
                step as f32 * DT,
                1,
                glam::Vec3::new(0.0, 0.0, 17.0),
            )]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &owned,
            touches,
        );
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 1);
    let event = &events[0];
    // Possessed duration credits touch-to-touch time only: the provisional
    // tail of the first stretch (after its lone touch at step 0) is rolled
    // back when the span suspends, the contested gap is excluded, and the
    // touchless tail after the final touch at step 8 is never credited. That
    // leaves just the two touch frames.
    assert!((event.duration - 2.0 * DT).abs() < 1e-4);
    assert_eq!(event.start_frame, 0);
    assert_eq!(event.end_frame, 11);
}

#[test]
fn turnover_to_another_player_splits_spans() {
    let mut calculator = PlayerPossessionCalculator::new();
    // Both players sit next to the ball so the loose-distance bound never
    // demotes whoever currently holds it.
    let players = players_with(
        glam::Vec3::new(0.0, 0.0, 17.0),
        glam::Vec3::new(0.0, 0.0, 17.0),
    );
    let ball = ball_at(0.0, 1000.0);

    for step in 0..4 {
        let touches = if step == 0 || step == 2 {
            vec![touch(
                step,
                step as f32 * DT,
                1,
                glam::Vec3::new(0.0, 0.0, 17.0),
            )]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &possession(Some(1), Some(true)),
            touches,
        );
    }
    for step in 4..8 {
        let touches = if step == 4 || step == 6 {
            vec![touch(
                step,
                step as f32 * DT,
                2,
                glam::Vec3::new(0.0, 0.0, 17.0),
            )]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &possession(Some(2), Some(false)),
            touches,
        );
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].player_id, player_id(1));
    assert!(events[0].is_team_0);
    assert_eq!(events[1].player_id, player_id(2));
    assert!(!events[1].is_team_0);
}

#[test]
fn expired_gap_finalizes_the_suspended_span() {
    let mut calculator = PlayerPossessionCalculator::new();
    let players = players_at(glam::Vec3::new(0.0, 0.0, 17.0));
    let ball = ball_at(0.0, 1000.0);
    let owned = possession(Some(1), Some(true));
    let neutral = possession(None, None);

    for step in 0..4 {
        let touches = if step == 0 || step == 2 {
            vec![touch(
                step,
                step as f32 * DT,
                1,
                glam::Vec3::new(0.0, 0.0, 17.0),
            )]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &owned,
            touches,
        );
    }
    // Neutral stretch much longer than the merge gap.
    for step in 4..20 {
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &neutral,
            vec![],
        );
    }
    for step in 20..24 {
        let touches = if step == 20 || step == 22 {
            vec![touch(
                step,
                step as f32 * DT,
                1,
                glam::Vec3::new(0.0, 0.0, 17.0),
            )]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &owned,
            touches,
        );
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].end_frame, 3);
    // Span starts are backdated one frame to cover the opening frame's dt.
    assert_eq!(events[1].start_frame, 19);
}

#[test]
fn accumulates_carry_time_when_ball_rides_the_player() {
    let mut calculator = PlayerPossessionCalculator::new();
    let player_position = glam::Vec3::new(0.0, 0.0, 17.0);
    let players = players_at(player_position);
    let state = possession(Some(1), Some(true));
    // Ball sitting just above the car: inside the grounded-carry gap bounds.
    let ball = ball_at(0.0, 120.0);

    for step in 0..8 {
        let touches = if step == 0 || step == 4 {
            vec![touch(
                step,
                step as f32 * DT,
                1,
                glam::Vec3::new(0.0, 0.0, 17.0),
            )]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            step as f32 * DT,
            &ball,
            &players,
            &state,
            touches,
        );
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 1);
    let event = &events[0];
    // Carry time is credited up to the final touch (step 4), like duration.
    assert!((event.carry_time - 5.0 * DT).abs() < 1e-4);
    assert_eq!(event.carry_count, 1);
    assert_eq!(event.air_dribble_time, 0.0);
}

#[test]
fn labels_sustained_control_using_controlled_play_criteria() {
    let mut calculator = PlayerPossessionCalculator::new();
    // Ball hovers 300uu over the player: inside the 700uu proximity radius,
    // so close time accrues every frame.
    let players = players_at(glam::Vec3::new(0.0, 0.0, 17.0));
    let ball = ball_at(0.0, 300.0);
    let state = possession(Some(1), Some(true));

    // Two distinct touches 1.5s apart over a 2s possession with constant
    // proximity: meets every controlled-play criterion.
    for step in 0..8 {
        let time = step as f32 * DT;
        let touches = if step == 0 || step == 6 {
            vec![touch(step, time, 1, glam::Vec3::new(0.0, 0.0, 17.0))]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            time,
            &ball,
            &players,
            &state,
            touches,
        );
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 1);
    let event = &events[0];
    assert!(event.close_time > 0.0, "proximity time should accrue");
    assert!(
        event.sustained_control,
        "two spaced touches with sustained proximity qualify as controlled play"
    );
}

#[test]
fn single_touch_span_is_dropped() {
    // A lone touch the player never follows up on — the canonical kickoff poke
    // that stayed "possession" only because nobody else touched until the
    // opponent did — is not possession and must not be emitted at all.
    let mut calculator = PlayerPossessionCalculator::new();
    let players = players_at(glam::Vec3::new(0.0, 0.0, 17.0));
    let ball = ball_at(0.0, 1000.0);
    let state = possession(Some(1), Some(true));

    for step in 0..8 {
        let time = step as f32 * DT;
        let touches = if step == 0 {
            vec![touch(step, time, 1, glam::Vec3::new(0.0, 0.0, 17.0))]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            time,
            &ball,
            &players,
            &state,
            touches,
        );
    }
    calculator.finish();

    assert!(
        calculator.events().is_empty(),
        "a single-touch span never qualifies as possession"
    );
}

#[test]
fn ball_drifting_far_ends_the_span_without_crediting_the_drift() {
    // The holder makes two real touches, then the ball rockets away and they
    // never play it again. The far frames must not be credited, and once the
    // drift outlasts the merge gap the span finalizes on its own. `current_player`
    // stays sticky on the holder throughout — only the loose-distance bound ends
    // the span.
    let mut calculator = PlayerPossessionCalculator::new();
    let players = players_at(glam::Vec3::new(0.0, 0.0, 17.0));
    let state = possession(Some(1), Some(true));

    for step in 0..4 {
        let time = step as f32 * DT;
        let ball = ball_at(0.0, 200.0);
        let touches = if step == 0 || step == 2 {
            vec![touch(step, time, 1, glam::Vec3::new(0.0, 0.0, 17.0))]
        } else {
            vec![]
        };
        update(
            &mut calculator,
            step,
            time,
            &ball,
            &players,
            &state,
            touches,
        );
    }
    // Ball well beyond the loose-distance bound; the player stays put.
    for step in 4..20 {
        let time = step as f32 * DT;
        let ball = ball_at(6000.0, 1000.0);
        update(&mut calculator, step, time, &ball, &players, &state, vec![]);
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 1);
    let event = &events[0];
    assert_eq!(event.touch_count, 2);
    // The span's window ends where the drift starts, and possessed duration
    // ends at the final touch (step 2).
    assert_eq!(event.end_frame, 3);
    assert!((event.duration - 3.0 * DT).abs() < 1e-4);
}

#[test]
fn classifies_aerial_and_wall_touches() {
    let mut calculator = PlayerPossessionCalculator::new();
    let players = players_at(glam::Vec3::new(0.0, 0.0, 17.0));
    let ball = ball_at(0.0, 1000.0);
    let state = possession(Some(1), Some(true));

    let touches = [
        touch(0, 0.0, 1, glam::Vec3::new(0.0, 0.0, 17.0)),
        touch(1, 1.0, 1, glam::Vec3::new(0.0, 0.0, 800.0)),
        touch(2, 2.0, 1, glam::Vec3::new(4000.0, 0.0, 800.0)),
    ];
    for (step, touch) in touches.into_iter().enumerate() {
        update(
            &mut calculator,
            step,
            step as f32,
            &ball,
            &players,
            &state,
            vec![touch],
        );
    }
    calculator.finish();

    let events = calculator.events();
    assert_eq!(events.len(), 1);
    let event = &events[0];
    assert_eq!(event.touch_count, 3);
    assert_eq!(event.aerial_touch_count, 1);
    assert_eq!(event.wall_touch_count, 1);
}