Skip to content

Commit d9a25eb

Browse files
committed
Fixed a bunch of small bugs but the hiccup test still fails.
1 parent 4e6dfc1 commit d9a25eb

3 files changed

Lines changed: 77 additions & 99 deletions

File tree

core/peer_networked_controller.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,7 @@ void DollController::process(double p_delta) {
15201520

15211521
void DollController::on_state_validated(FrameIndex p_frame_index, bool p_detected_desync) {
15221522
notify_frame_checked(last_doll_compared_input);
1523+
notify_frame_processing(current_input_buffer_id);
15231524
}
15241525

15251526
void DollController::notify_frame_checked(FrameIndex p_doll_frame_index) {
@@ -1528,7 +1529,7 @@ void DollController::notify_frame_checked(FrameIndex p_doll_frame_index) {
15281529
return;
15291530
}
15301531

1531-
if (p_doll_frame_index != FrameIndex::NONE) {
1532+
if make_likely (p_doll_frame_index != FrameIndex::NONE) {
15321533
// Removes all the inputs older than the known one (included).
15331534
while (!frames_input.empty() && frames_input.front().id <= p_doll_frame_index) {
15341535
if (frames_input.front().id == p_doll_frame_index) {
@@ -1555,6 +1556,21 @@ void DollController::notify_frame_checked(FrameIndex p_doll_frame_index) {
15551556
last_doll_validated_input = p_doll_frame_index;
15561557
}
15571558

1559+
void DollController::notify_frame_processing(FrameIndex p_input_id) {
1560+
if make_likely (p_input_id != FrameIndex::NONE) {
1561+
// Removed all the client snapshots which input is more than the specified one
1562+
// to ensure the function `__pcr__fetch_recovery_info` works properly.
1563+
for (int i = int(client_snapshots.size()) - 1; i >= 0; i--) {
1564+
if (client_snapshots[i].doll_executed_input > p_input_id) {
1565+
VecFunc::remove_at(client_snapshots, i);
1566+
i--;
1567+
} else {
1568+
break;
1569+
}
1570+
}
1571+
}
1572+
}
1573+
15581574
void DollController::on_received_server_snapshot(const Snapshot &p_snapshot) {
15591575
NS_PROFILE
15601576
const FrameIndex doll_executed_input = MapFunc::at(p_snapshot.peers_frames_index, peer_controller->get_authority_peer(), FrameIndex::NONE);
@@ -1634,6 +1650,8 @@ void DollController::copy_controlled_objects_snapshot(
16341650
snap->data.object_vars.resize(biggest_id.id + 1);
16351651
}
16361652

1653+
snap->data.simulated_objects.clear();
1654+
16371655
// Now store the vars info.
16381656
for (ObjectData *object_data : *controlled_objects) {
16391657
if (!VecFunc::has(p_snapshot.simulated_objects, object_data->get_net_id())) {
@@ -1662,6 +1680,7 @@ void DollController::copy_controlled_objects_snapshot(
16621680
FrameIndex DollController::fetch_checkable_snapshot(DollSnapshot *&r_client_snapshot, DollSnapshot *&r_server_snapshot) {
16631681
for (auto client_snap_it = client_snapshots.rbegin(); client_snap_it != client_snapshots.rend(); client_snap_it++) {
16641682
if (client_snap_it->doll_executed_input != FrameIndex::NONE) {
1683+
ASSERT_COND_MSG(client_snap_it->doll_executed_input <= current_input_buffer_id, "All the client snapshots are properly cleared when the `current_input_id` is manipulated. So this function is impossible to trigger. If it does, there is a bug on the `notify_frame_processing`.");
16651684
auto server_snap_it = VecFunc::find(server_snapshots, client_snap_it->doll_executed_input);
16661685
if (server_snap_it != server_snapshots.end()) {
16671686
r_client_snapshot = &(*client_snap_it);
@@ -1887,6 +1906,7 @@ void DollController::apply_snapshot_rewinding_input_reconciliation(const Snapsho
18871906

18881907
// 5. Fetch frame index to start processing.
18891908
queued_frame_index_to_process = last_doll_compared_input + 1;
1909+
current_input_buffer_id = last_doll_compared_input;
18901910

18911911
if make_unlikely (server_snapshot) {
18921912
// 6. Apply the server snapshot.

core/peer_networked_controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ struct DollController final : public RemotelyControlledController {
407407
virtual void process(double p_delta) override;
408408
void on_state_validated(FrameIndex p_frame_index, bool p_detected_desync);
409409
void notify_frame_checked(FrameIndex p_input_id);
410+
void notify_frame_processing(FrameIndex p_input_id);
410411

411412
void on_received_server_snapshot(const Snapshot &p_snapshot);
412413
void on_snapshot_update_finished(const Snapshot &p_snapshot);

tests/test_doll_simulation.cpp

Lines changed: 55 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,59 @@ void test_simulation_reconciliation(float p_frame_confirmation_timespan) {
426426
// Run another 30 frames.
427427
test.do_test(30);
428428

429-
// Make sure there was 1 desyc
430-
ASSERT_COND(test.peer1_desync_detected.size() == 1);
431-
ASSERT_COND(test.peer2_desync_detected.size() == 1);
429+
// Make sure the reconciliation was successful.
430+
// NOTE: 45 is a margin established basing on the `p_frame_confirmation_timespan`.
431+
const NS::FrameIndex ensure_no_desync_after{ 45 };
432+
test.assert_no_desync(ensure_no_desync_after, ensure_no_desync_after);
432433

433434
// and despite that the simulations are correct.
434-
test.assert_positions(test.peer1_desync_detected.front(), test.peer2_desync_detected.front());
435+
test.assert_positions(ensure_no_desync_after, ensure_no_desync_after);
436+
}
437+
438+
void test_simulation_with_hiccups(TestDollSimulationStorePositions &test) {
439+
// Partially process.
440+
test.network_properties.rtt_seconds = 0.0;
441+
442+
{
443+
NS::FrameIndex controller_1_doll_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
444+
NS::FrameIndex controller_2_doll_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
445+
446+
for (int i = 0; i < 10; i++) {
447+
if (i % 2 == 0) {
448+
test.do_test(10, false, true, false, true);
449+
} else {
450+
test.do_test(10, false, true, true, false);
451+
}
452+
453+
const NS::FrameIndex controller_1_doll_new_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
454+
const NS::FrameIndex controller_2_doll_new_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
455+
456+
// Ensure the doll keep going forward.
457+
ASSERT_COND(controller_1_doll_frame_index == NS::FrameIndex::NONE || controller_1_doll_frame_index <= controller_1_doll_new_frame_index);
458+
ASSERT_COND(controller_2_doll_frame_index == NS::FrameIndex::NONE || controller_2_doll_frame_index <= controller_2_doll_new_frame_index);
459+
460+
controller_1_doll_frame_index = controller_1_doll_new_frame_index;
461+
controller_2_doll_frame_index = controller_2_doll_new_frame_index;
462+
}
463+
}
464+
465+
test.do_test(30);
466+
467+
const NS::FrameIndex controller_1_last_player_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
468+
const NS::FrameIndex controller_2_last_player_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
469+
470+
const NS::FrameIndex controller_1_last_doll_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
471+
const NS::FrameIndex controller_2_last_doll_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
472+
473+
const int latency_factor = 15;
474+
475+
ASSERT_COND(controller_1_last_player_frame_index - latency_factor <= controller_1_last_doll_frame_index);
476+
ASSERT_COND(controller_2_last_player_frame_index - latency_factor <= controller_2_last_doll_frame_index);
477+
478+
// Make sure the last frames are identical.
479+
test.assert_positions(
480+
test.peer1_desync_detected.back(),
481+
test.peer2_desync_detected.back());
435482
}
436483

437484
void test_simulation_with_latency() {
@@ -527,106 +574,16 @@ void test_simulation_with_latency() {
527574
controller_2_last_player_frame_index - latency_factor);
528575
}
529576

530-
// TODO test this again.
531-
// TODO consider to remove this or leave it.
532577
// Partially process.
533-
{
534-
test.network_properties.rtt_seconds = 0.0;
535-
536-
{
537-
NS::FrameIndex controller_1_doll_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
538-
NS::FrameIndex controller_2_doll_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
539-
540-
for (int i = 0; i < 10; i++) {
541-
if (i % 2 == 0) {
542-
test.do_test(10, false, true, false, true);
543-
} else {
544-
test.do_test(10, false, true, true, false);
545-
}
546-
547-
const NS::FrameIndex controller_1_doll_new_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
548-
const NS::FrameIndex controller_2_doll_new_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
549-
550-
// Ensure the doll keep going forward.
551-
ASSERT_COND(controller_1_doll_frame_index <= controller_1_doll_new_frame_index);
552-
ASSERT_COND(controller_2_doll_frame_index <= controller_2_doll_new_frame_index);
553-
554-
controller_1_doll_frame_index = controller_1_doll_new_frame_index;
555-
controller_2_doll_frame_index = controller_2_doll_new_frame_index;
556-
}
557-
}
558-
559-
test.do_test(10);
560-
561-
const NS::FrameIndex controller_1_last_player_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
562-
const NS::FrameIndex controller_2_last_player_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
563-
564-
const NS::FrameIndex controller_1_last_doll_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
565-
const NS::FrameIndex controller_2_last_doll_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
566-
567-
const int latency_factor = 15;
568-
569-
ASSERT_COND(controller_1_last_player_frame_index - latency_factor <= controller_1_last_doll_frame_index);
570-
ASSERT_COND(controller_2_last_player_frame_index - latency_factor <= controller_2_last_doll_frame_index);
571-
572-
test.assert_positions(
573-
controller_1_last_player_frame_index - latency_factor,
574-
controller_2_last_player_frame_index - latency_factor);
575-
}
576-
577-
int a = 0;
578+
test_simulation_with_hiccups(test);
578579
}
579580

580581
void test_simulation_with_hiccups() {
581582
TestDollSimulationStorePositions test;
582583
test.frame_confirmation_timespan = 1.0 / 10.0;
583584
test.init_test();
584585

585-
// Partially process.
586-
test.network_properties.rtt_seconds = 0.0;
587-
588-
{
589-
NS::FrameIndex controller_1_doll_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
590-
NS::FrameIndex controller_2_doll_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
591-
592-
for (int i = 0; i < 10; i++) {
593-
if (i % 2 == 0) {
594-
test.do_test(10, false, true, false, true);
595-
} else {
596-
test.do_test(10, false, true, true, false);
597-
}
598-
599-
const NS::FrameIndex controller_1_doll_new_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
600-
const NS::FrameIndex controller_2_doll_new_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
601-
602-
// Ensure the doll keep going forward.
603-
ASSERT_COND(controller_1_doll_frame_index == NS::FrameIndex::NONE || controller_1_doll_frame_index <= controller_1_doll_new_frame_index);
604-
ASSERT_COND(controller_2_doll_frame_index == NS::FrameIndex::NONE || controller_2_doll_frame_index <= controller_2_doll_new_frame_index);
605-
606-
controller_1_doll_frame_index = controller_1_doll_new_frame_index;
607-
controller_2_doll_frame_index = controller_2_doll_new_frame_index;
608-
}
609-
}
610-
611-
test.do_test(30);
612-
613-
const NS::FrameIndex controller_1_last_player_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
614-
const NS::FrameIndex controller_2_last_player_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
615-
616-
const NS::FrameIndex controller_1_last_doll_frame_index = test.peer_2_scene.scene_sync->get_controller_for_peer(test.peer_1_scene.get_peer())->get_current_frame_index();
617-
const NS::FrameIndex controller_2_last_doll_frame_index = test.peer_1_scene.scene_sync->get_controller_for_peer(test.peer_2_scene.get_peer())->get_current_frame_index();
618-
619-
const int latency_factor = 15;
620-
621-
ASSERT_COND(controller_1_last_player_frame_index - latency_factor <= controller_1_last_doll_frame_index);
622-
ASSERT_COND(controller_2_last_player_frame_index - latency_factor <= controller_2_last_doll_frame_index);
623-
624-
// Make sure the last frames are identical.
625-
test.assert_positions(
626-
test.peer1_desync_detected.back(),
627-
test.peer2_desync_detected.back());
628-
629-
int a = 0;
586+
test_simulation_with_hiccups(test);
630587
}
631588

632589
void test_latency() {
@@ -675,9 +632,9 @@ void test_doll_simulation() {
675632
//test_simulation_without_reconciliation(0.0);
676633
//test_simulation_without_reconciliation(1. / 30.);
677634
//test_simulation_reconciliation(0.0);
678-
test_simulation_reconciliation(1.0 / 10.0);
635+
//test_simulation_reconciliation(1.0 / 10.0);
679636
//test_simulation_with_latency();
680-
//test_simulation_with_hiccups();
637+
test_simulation_with_hiccups();
681638
// TODO test with great latency and lag compensation.
682639
//test_latency();
683640

0 commit comments

Comments
 (0)