lightyear 0.3.0

Server-client networking library for the Bevy game engine
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
use std::fmt::Debug;

use bevy::prelude::{
    Commands, Entity, EventReader, FixedUpdate, Query, Res, ResMut, Without, World,
};
use bevy::utils::{EntityHashSet, HashSet};
use tracing::{debug, error, info, trace, trace_span, warn};

use crate::client::components::{ComponentSyncMode, Confirmed, SyncComponent};
use crate::client::events::{ComponentInsertEvent, ComponentRemoveEvent, ComponentUpdateEvent};
use crate::client::prediction::predicted_history::ComponentState;
use crate::client::resource::Client;
use crate::protocol::Protocol;

use super::predicted_history::PredictionHistory;
use super::{Predicted, Rollback, RollbackState};

// TODO (unrelated): pattern for enabling replication-behaviour for a component/entity. (for example don't replicate this component)
//  Added a ReplicationBehaviour<C>.
//  And then maybe we can add an EntityCommands extension that adds a ReplicationBehaviour<C>

// NOTE: for rollback to work, all entities that are predicted need to be replicated on the same tick!

// We need:
// - Removed because we need to know if the component was removed on predicted, but we have to keep the history for the rest of rollback
// - To be able to handle missing component histories; (if a component suddenly gets added on confirmed for the first time, it won't exist on predicted, so existed wont have a history)
//   - BUT WE COULD JUST SPAWN A HISTORY FOR PREDICTED AS SOON AS WE RECEIVE THAT EVENT?
// - Add ComponentHistory if a component gets added on predicted; so we can start accumulating history for future rollbacks
// - Add ComponentHistory if a component gets added on confirmed; so we can initiate rollback (if predicted didn't have this component)
// - We don't really need to remove ComponentHistory. We could try to do it as an optimization later on. For now we just keep them around.

// TODO: it seems pretty suboptimal to have one system per component, refactor to loop through all components
//  ESPECIALLY BECAUSE WE ROLLBACK EVERYTHING IF ONE COMPONENT IS MISPREDICTED!
/// Systems that try to see if we should perform rollback for the predicted entity.
/// For each component, we compare the confirmed component (server-state) with the history.
/// If we need to run rollback, we clear the predicted history and snap the history back to the server-state
// TODO: do not rollback if client is not time synced
#[allow(clippy::type_complexity)]
#[allow(clippy::too_many_arguments)]
pub(crate) fn client_rollback_check<C: SyncComponent, P: Protocol>(
    // TODO: have a way to only get the updates of entities that are predicted?
    mut commands: Commands,
    client: Res<Client<P>>,

    mut updates: EventReader<ComponentUpdateEvent<C>>,
    mut inserts: EventReader<ComponentInsertEvent<C>>,
    mut removals: EventReader<ComponentRemoveEvent<C>>,

    // We also snap the value of the component to the server state if we are in rollback
    // We use Option<> because the predicted component could have been removed while it still exists in Confirmed
    mut predicted_query: Query<
        (
            Entity,
            &Predicted,
            Option<&mut C>,
            &mut PredictionHistory<C>,
        ),
        Without<Confirmed>,
    >,
    confirmed_query: Query<(Option<&C>, &Confirmed)>,
    mut rollback: ResMut<Rollback>,
)
// where
// <P as Protocol>::Components: From<C>,
{
    // TODO: maybe change this into a run condition so that we don't even run the system (reduces parallelism)
    if C::mode() != ComponentSyncMode::Full {
        return;
    }
    if !client.is_synced() || !client.received_new_server_tick() {
        return;
    }
    // TODO: can just enable bevy spans?
    let _span = trace_span!("client rollback check");

    // 0. We want to do a rollback check every time the component for the confirmed entity got modified in any way (removed/added/updated)
    let confirmed_entity_with_updates = updates
        .read()
        .map(|event| *event.entity())
        .chain(inserts.read().map(|event| *event.entity()))
        .chain(removals.read().map(|event| *event.entity()))
        .collect::<EntityHashSet<Entity>>();

    for confirmed_entity in confirmed_entity_with_updates {
        let Ok((confirmed_component, confirmed)) = confirmed_query.get(confirmed_entity) else {
            // this could happen if the entity was despawned but we received updates for it.
            // maybe only send events for an entity if it still exists?
            debug!(
                "could not find the confirmed entity: {:?} that received an update",
                confirmed_entity
            );
            continue;
        };

        // TODO: get the tick of the update from context of ComponentUpdateEvent if we switch to that
        // let confirmed_entity = event.entity();
        // TODO: no need to get the Predicted component because we're not using it right now..
        //  we could use it in the future if we add more state in the Predicted Component
        // 1. Get the predicted entity, and it's history
        if let Some(p) = confirmed.predicted {
            let Ok((predicted_entity, predicted, predicted_component, mut predicted_history)) =
                predicted_query.get_mut(p)
            else {
                debug!("Predicted entity {:?} was not found", confirmed.predicted);
                continue;
            };

            // 2. We will compare the predicted history and the confirmed entity at the current confirmed entity tick
            // - Confirmed contains the server state at the tick
            // - History contains the history of what we predicted at the tick
            // get the tick that the confirmed entity is at
            let Some(channel) = client
                .replication_manager()
                .channel_by_local(confirmed_entity)
            else {
                error!(
                    "Could not find replication channel for entity {:?}",
                    confirmed_entity
                );
                continue;
            };
            let tick = channel.latest_tick;

            // Note: it may seem like an optimization to only compare the history/server-state if we are not sure
            // that we should rollback (RollbackState::Default)
            // That is not the case, because if we do rollback we will need to snap the client entity to the server state
            // So either way we will need to do an operation.
            match rollback.state {
                // 3.a We are still not sure if we should do rollback. Compare history against confirmed
                // We rollback if there's no history (newly added predicted entity, or if there is a mismatch)
                RollbackState::Default => {
                    // rollback table:
                    // - confirm exist. rollback if:
                    //    - predicted history exists and is different
                    //    - predicted history does not exist
                    //    To rollback:
                    //    - update the predicted component to the confirmed component if it exists
                    //    - insert the confirmed component to the predicted entity if it doesn't exist
                    // - confirm does not exist. rollback if:
                    //    - predicted history exists and doesn't contain Removed
                    //    -
                    //    To rollback:
                    //    - we remove the component from predicted.

                    let history_value = predicted_history.pop_until_tick(tick);
                    let should_rollback = match confirmed_component {
                        // TODO: history-value should not be empty here; should we panic if it is?
                        // confirm does not exist. rollback if history value is not Removed
                        None => history_value.map_or(false, |history_value| {
                            history_value != ComponentState::Removed
                        }),
                        // confirm exist. rollback if history value is different
                        Some(c) => {
                            history_value.map_or(true, |history_value| match history_value {
                                ComponentState::Updated(history_value) => history_value != *c,
                                ComponentState::Removed => true,
                            })
                        }
                    };
                    if should_rollback {
                        info!(
                                "Rollback check: mismatch for component between predicted and confirmed {:?}",
                                confirmed_entity
                            );

                        // we need to clear the history so we can write a new one
                        predicted_history.clear();
                        // SAFETY: we know the predicted entity exists
                        let mut entity_mut = commands.entity(predicted_entity);
                        match confirmed_component {
                            // confirm does not exist, remove on predicted
                            None => {
                                predicted_history
                                    .buffer
                                    .add_item(tick, ComponentState::Removed);
                                entity_mut.remove::<C>();
                            }
                            // confirm exist, update or insert on predicted
                            Some(c) => {
                                predicted_history
                                    .buffer
                                    .add_item(tick, ComponentState::Updated(c.clone()));
                                match predicted_component {
                                    None => {
                                        entity_mut.insert(c.clone());
                                    }
                                    Some(mut predicted_component) => {
                                        *predicted_component = c.clone();
                                    }
                                };
                            }
                        };
                        // TODO: try atomic enum update
                        rollback.state = RollbackState::ShouldRollback {
                            // we already rolled-back the state for the entity's latest_tick
                            // after this we will start right away with a physics update, so we need to start taking the inputs from the next tick
                            current_tick: tick + 1,
                        };
                    }
                }
                // 3.b We already know we should do rollback (because of another entity/component), start the rollback
                RollbackState::ShouldRollback { .. } => {
                    // we need to clear the history so we can write a new one
                    predicted_history.clear();

                    // SAFETY: we know the predicted entity exists
                    let mut entity_mut = commands.entity(predicted_entity);
                    match confirmed_component {
                        // confirm does not exist, remove on predicted
                        None => {
                            predicted_history
                                .buffer
                                .add_item(tick, ComponentState::Removed);
                            entity_mut.remove::<C>();
                        }
                        // confirm exist, update or insert on predicted
                        Some(c) => {
                            predicted_history
                                .buffer
                                .add_item(tick, ComponentState::Updated(c.clone()));
                            match predicted_component {
                                None => {
                                    entity_mut.insert(c.clone());
                                }
                                Some(mut predicted_component) => {
                                    *predicted_component = c.clone();
                                }
                            };
                        }
                    };
                }
            }
        }
    }
}

pub(crate) fn run_rollback<P: Protocol>(world: &mut World) {
    let client = world.get_resource::<Client<P>>().unwrap();
    let rollback = world.get_resource::<Rollback>().unwrap();

    // NOTE: all predicted entities should be on the same tick!
    // TODO: might not need to check the state, because we only run this system if we are in rollback
    if let RollbackState::ShouldRollback { current_tick } = rollback.state {
        let num_rollback_ticks = client.tick() - current_tick;
        debug!(
            "Rollback between {:?} and {:?}",
            current_tick,
            client.tick()
        );

        // run the physics fixed update schedule (which should contain ALL predicted/rollback components)
        for i in 0..num_rollback_ticks {
            // TODO: if we are in rollback, there are some FixedUpdate systems that we don't want to re-run ??
            //  for example we only want to run the physics on non-confirmed entities
            world.run_schedule(FixedUpdate)
        }
    }

    // revert the state of Rollback for the next frame
    let mut rollback = world.get_resource_mut::<Rollback>().unwrap();
    rollback.state = RollbackState::Default;
}

pub(crate) fn increment_rollback_tick(mut rollback: ResMut<Rollback>) {
    trace!("increment rollback tick");
    // update the rollback tick
    // (we already set the history for client.last_received_server_tick() in the rollback check,
    // we will start at the next tick. This is valid because this system runs after the physics systems)
    if let RollbackState::ShouldRollback {
        ref mut current_tick,
    } = rollback.state
    {
        *current_tick += 1;
    }
}
//
// #[cfg(test)]
// mod tests {
//     use std::time::Duration;
//
//     use bevy::prelude::*;
//     use tracing::{debug, info};
//
//     use crate::_reexport::*;
//     use crate::prelude::client::*;
//     use crate::prelude::*;
//     use crate::tests::protocol::*;
//     use crate::tests::stepper::{BevyStepper, Step};
//
//     fn increment_component(
//         mut commands: Commands,
//         mut query: Query<(Entity, &mut Component1), With<Predicted>>,
//     ) {
//         for (entity, mut component) in query.iter_mut() {
//             component.0 += 1.0;
//             if component.0 == 5.0 {
//                 commands.entity(entity).remove::<Component1>();
//             }
//         }
//     }
//
//     fn setup() -> BevyStepper {
//         let frame_duration = Duration::from_millis(10);
//         let tick_duration = Duration::from_millis(10);
//         let shared_config = SharedConfig {
//             enable_replication: false,
//             tick: TickConfig::new(tick_duration),
//             log: LogConfig {
//                 level: tracing::Level::DEBUG,
//                 ..Default::default()
//             },
//             ..Default::default()
//         };
//         let link_conditioner = LinkConditionerConfig {
//             incoming_latency: Duration::from_millis(40),
//             incoming_jitter: Duration::from_millis(5),
//             incoming_loss: 0.05,
//         };
//         let sync_config = SyncConfig::default().speedup_factor(1.0);
//         let prediction_config = PredictionConfig::default().disable(false);
//         let interpolation_delay = Duration::from_millis(100);
//         let interpolation_config = InterpolationConfig::default().with_delay(InterpolationDelay {
//             min_delay: interpolation_delay,
//             send_interval_ratio: 0.0,
//         });
//         let mut stepper = BevyStepper::new(
//             shared_config,
//             sync_config,
//             prediction_config,
//             interpolation_config,
//             link_conditioner,
//             frame_duration,
//         );
//         stepper.client_mut().set_synced();
//         stepper.client_app.add_systems(
//             FixedUpdate,
//             increment_component.in_set(FixedUpdateSet::Main),
//         );
//         stepper
//     }
//
//     // Test that if a component gets removed from the predicted entity erroneously
//     // We are still able to rollback properly (the rollback adds the component to the predicted entity)
//     #[test]
//     fn test_removed_predicted_component_rollback() -> anyhow::Result<()> {
//         let mut stepper = setup();
//
//         // Create a confirmed entity
//         let confirmed = stepper
//             .client_app
//             .world
//             .spawn((Component1(0.0), ShouldBePredicted))
//             .id();
//
//         // Tick once
//         stepper.frame_step();
//         assert_eq!(stepper.client().tick(), Tick(1));
//         let predicted = stepper
//             .client_app
//             .world
//             .get::<Confirmed>(confirmed)
//             .unwrap()
//             .predicted
//             .unwrap();
//
//         // check that the predicted entity got spawned
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<Predicted>(predicted)
//                 .unwrap()
//                 .confirmed_entity,
//             confirmed
//         );
//
//         // check that the component history got created
//         let mut history = PredictionHistory::<Component1>::default();
//         // this is added during the first rollback call after we create the history
//         history
//             .buffer
//             .add_item(Tick(0), ComponentState::Updated(Component1(0.0)));
//         history
//             .buffer
//             .add_item(Tick(1), ComponentState::Updated(Component1(1.0)));
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<PredictionHistory<Component1>>(predicted)
//                 .unwrap(),
//             &history,
//         );
//         // check that the confirmed component got replicated
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<Component1>(predicted)
//                 .unwrap(),
//             &Component1(1.0)
//         );
//
//         // advance five more frames, so that the component gets removed on predicted
//         for i in 0..5 {
//             stepper.frame_step();
//         }
//         assert_eq!(stepper.client().tick(), Tick(6));
//
//         // check that the component got removed on predicted
//         assert!(stepper
//             .client_app
//             .world
//             .get::<Component1>(predicted)
//             .is_none());
//         // check that the component history is still there and that the value of the component history is correct
//         let mut history = PredictionHistory::<Component1>::default();
//         for i in 0..5 {
//             history
//                 .buffer
//                 .add_item(Tick(i), ComponentState::Updated(Component1(i as f32)));
//         }
//         history.buffer.add_item(Tick(5), ComponentState::Removed);
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<PredictionHistory<Component1>>(predicted)
//                 .unwrap(),
//             &history,
//         );
//
//         // TODO: need to revisit this, a rollback situation is created from receiving a replication update now
//         // create a rollback situation
//         stepper.client_mut().set_synced();
//         stepper
//             .client_mut()
//             .set_latest_received_server_tick(Tick(3));
//         stepper
//             .client_app
//             .world
//             .get_mut::<Component1>(confirmed)
//             .unwrap()
//             .0 = 1.0;
//         // update without incrementing time, because we want to force a rollback check
//         stepper.client_app.update();
//
//         // check that rollback happened
//         // predicted got the component re-added
//         stepper
//             .client_app
//             .world
//             .get_mut::<Component1>(predicted)
//             .unwrap()
//             .0 = 4.0;
//         // check that the history is how we expect after rollback
//         let mut history = PredictionHistory::<Component1>::default();
//         for i in 3..7 {
//             history
//                 .buffer
//                 .add_item(Tick(i), ComponentState::Updated(Component1(i as f32 - 2.0)));
//         }
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<PredictionHistory<Component1>>(predicted)
//                 .unwrap(),
//             &history
//         );
//
//         Ok(())
//     }
//
//     // Test that if a component gets added to the predicted entity erroneously but didn't exist on the confirmed entity)
//     // We are still able to rollback properly (the rollback removes the component from the predicted entity)
//     #[test]
//     fn test_added_predicted_component_rollback() -> anyhow::Result<()> {
//         let mut stepper = setup();
//
//         // Create a confirmed entity
//         let confirmed = stepper.client_app.world.spawn(ShouldBePredicted).id();
//
//         // Tick once
//         stepper.frame_step();
//         assert_eq!(stepper.client().tick(), Tick(1));
//         let predicted = stepper
//             .client_app
//             .world
//             .get::<Confirmed>(confirmed)
//             .unwrap()
//             .predicted
//             .unwrap();
//
//         // check that the predicted entity got spawned
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<Predicted>(predicted)
//                 .unwrap()
//                 .confirmed_entity,
//             confirmed
//         );
//
//         // add a new component to Predicted
//         stepper
//             .client_app
//             .world
//             .entity_mut(predicted)
//             .insert(Component1(1.0));
//
//         // create a rollback situation (confirmed doesn't have a component that predicted has)
//         stepper.client_mut().set_synced();
//         stepper
//             .client_mut()
//             .set_latest_received_server_tick(Tick(1));
//         // update without incrementing time, because we want to force a rollback check
//         stepper.client_app.update();
//
//         // check that rollback happened: the component got removed from predicted
//         assert!(stepper
//             .client_app
//             .world
//             .get::<Component1>(predicted)
//             .is_none());
//
//         // check that history contains the removal
//         let mut history = PredictionHistory::<Component1>::default();
//         history.buffer.add_item(Tick(1), ComponentState::Removed);
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<PredictionHistory<Component1>>(predicted)
//                 .unwrap(),
//             &history,
//         );
//         Ok(())
//     }
//
//     // Test that if a component gets removed from the confirmed entity
//     // We are still able to rollback properly (the rollback removes the component from the predicted entity)
//     #[test]
//     fn test_removed_confirmed_component_rollback() -> anyhow::Result<()> {
//         let mut stepper = setup();
//
//         // Create a confirmed entity
//         let confirmed = stepper
//             .client_app
//             .world
//             .spawn((Component1(0.0), ShouldBePredicted))
//             .id();
//
//         // Tick once
//         stepper.frame_step();
//         assert_eq!(stepper.client().tick(), Tick(1));
//         let predicted = stepper
//             .client_app
//             .world
//             .get::<Confirmed>(confirmed)
//             .unwrap()
//             .predicted
//             .unwrap();
//
//         // check that the predicted entity got spawned
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<Predicted>(predicted)
//                 .unwrap()
//                 .confirmed_entity,
//             confirmed
//         );
//
//         // check that the component history got created
//         let mut history = PredictionHistory::<Component1>::default();
//         history
//             .buffer
//             .add_item(Tick(0), ComponentState::Updated(Component1(0.0)));
//         history
//             .buffer
//             .add_item(Tick(1), ComponentState::Updated(Component1(1.0)));
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<PredictionHistory<Component1>>(predicted)
//                 .unwrap(),
//             &history,
//         );
//
//         // create a rollback situation by removing the component on confirmed
//         stepper.client_mut().set_synced();
//         stepper
//             .client_mut()
//             .set_latest_received_server_tick(Tick(1));
//         stepper
//             .client_app
//             .world
//             .entity_mut(confirmed)
//             .remove::<Component1>();
//         // update without incrementing time, because we want to force a rollback check
//         // (need duration_since_latest_received_server_tick = 0)
//         stepper.client_app.update();
//
//         // check that rollback happened
//         // predicted got the component removed
//         assert!(stepper
//             .client_app
//             .world
//             .get_mut::<Component1>(predicted)
//             .is_none());
//
//         // check that the history is how we expect after rollback
//         let mut history = PredictionHistory::<Component1>::default();
//         history.buffer.add_item(Tick(1), ComponentState::Removed);
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<PredictionHistory<Component1>>(predicted)
//                 .unwrap(),
//             &history
//         );
//
//         Ok(())
//     }
//
//     // Test that if a component gets added to the confirmed entity (but didn't exist on the predicted entity)
//     // We are still able to rollback properly (the rollback adds the component to the predicted entity)
//     #[test]
//     fn test_added_confirmed_component_rollback() -> anyhow::Result<()> {
//         let mut stepper = setup();
//
//         // Create a confirmed entity
//         let confirmed = stepper.client_app.world.spawn(ShouldBePredicted).id();
//
//         // Tick once
//         stepper.frame_step();
//         assert_eq!(stepper.client().tick(), Tick(1));
//         let predicted = stepper
//             .client_app
//             .world
//             .get::<Confirmed>(confirmed)
//             .unwrap()
//             .predicted
//             .unwrap();
//
//         // check that the predicted entity got spawned
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<Predicted>(predicted)
//                 .unwrap()
//                 .confirmed_entity,
//             confirmed
//         );
//
//         // check that the component history did not get created
//         assert!(stepper
//             .client_app
//             .world
//             .get::<PredictionHistory<Component1>>(predicted)
//             .is_none());
//
//         // advance five more frames, so that the component gets removed on predicted
//         for i in 0..5 {
//             stepper.frame_step();
//         }
//         assert_eq!(stepper.client().tick(), Tick(6));
//
//         // create a rollback situation by adding the component on confirmed
//         stepper.client_mut().set_synced();
//         stepper
//             .client_mut()
//             .set_latest_received_server_tick(Tick(3));
//         stepper
//             .client_app
//             .world
//             .entity_mut(confirmed)
//             .insert(Component1(1.0));
//         // update without incrementing time, because we want to force a rollback check
//         stepper.client_app.update();
//
//         // check that rollback happened
//         // predicted got the component re-added
//         stepper
//             .client_app
//             .world
//             .get_mut::<Component1>(predicted)
//             .unwrap()
//             .0 = 4.0;
//         // check that the history is how we expect after rollback
//         let mut history = PredictionHistory::<Component1>::default();
//         for i in 3..7 {
//             history
//                 .buffer
//                 .add_item(Tick(i), ComponentState::Updated(Component1(i as f32 - 2.0)));
//         }
//         assert_eq!(
//             stepper
//                 .client_app
//                 .world
//                 .get::<PredictionHistory<Component1>>(predicted)
//                 .unwrap(),
//             &history
//         );
//
//         Ok(())
//     }
// }