@@ -1466,7 +1466,7 @@ bool DollController::fetch_next_input(double p_delta) {
14661466 return false ;
14671467 }
14681468
1469- if (closest_frame_index > 0 ) {
1469+ if (closest_frame_index >= 0 ) {
14701470 // It was impossible to find the input, so just pick the closest one and
14711471 // assume it's the one we are executing.
14721472 FrameInput guessed_fi = frames_input[closest_frame_index];
@@ -1518,33 +1518,33 @@ void DollController::on_state_validated(FrameIndex p_frame_index, bool p_detecte
15181518}
15191519
15201520void DollController::notify_frame_checked (FrameIndex p_doll_frame_index) {
1521- if (p_doll_frame_index == FrameIndex::NONE) {
1522- // Nothing to do.
1523- return ;
1524- }
1525-
15261521 if (last_doll_validated_input != FrameIndex::NONE && last_doll_validated_input >= p_doll_frame_index) {
15271522 // Already checked.
15281523 return ;
15291524 }
15301525
1531- // Removes all the inputs older than the known one (included).
1532- while (!frames_input.empty () && frames_input.front ().id <= p_doll_frame_index) {
1533- if (frames_input.front ().id == p_doll_frame_index) {
1534- // Pause the streaming if the last frame is empty.
1535- streaming_paused = (frames_input.front ().buffer_size_bit - METADATA_SIZE) <= 0 ;
1526+ if (p_doll_frame_index != FrameIndex::NONE) {
1527+ // Removes all the inputs older than the known one (included).
1528+ while (!frames_input.empty () && frames_input.front ().id <= p_doll_frame_index) {
1529+ if (frames_input.front ().id == p_doll_frame_index) {
1530+ // Pause the streaming if the last frame is empty.
1531+ streaming_paused = (frames_input.front ().buffer_size_bit - METADATA_SIZE) <= 0 ;
1532+ }
1533+ frames_input.pop_front ();
15361534 }
1537- frames_input.pop_front ();
1538- }
15391535
1540- // Remove all the server snapshots which doll frame was already executed.
1541- while (!server_snapshots.empty () && server_snapshots.front ().doll_executed_input <= p_doll_frame_index) {
1542- VecFunc::remove_at (server_snapshots, 0 );
1543- }
1536+ // Remove all the server snapshots which doll frame was already executed.
1537+ while (!server_snapshots.empty () && server_snapshots.front ().doll_executed_input <= p_doll_frame_index) {
1538+ VecFunc::remove_at (server_snapshots, 0 );
1539+ }
15441540
1545- // Removed all the checked doll frame snapshots.
1546- while (!client_snapshots.empty () && client_snapshots.front ().doll_executed_input <= p_doll_frame_index) {
1547- VecFunc::remove_at (client_snapshots, 0 );
1541+ // Removed all the checked doll frame snapshots.
1542+ while (!client_snapshots.empty () && client_snapshots.front ().doll_executed_input <= p_doll_frame_index) {
1543+ VecFunc::remove_at (client_snapshots, 0 );
1544+ }
1545+ } else {
1546+ VecFunc::remove (server_snapshots, FrameIndex::NONE);
1547+ VecFunc::remove (client_snapshots, FrameIndex::NONE);
15481548 }
15491549
15501550 last_doll_validated_input = p_doll_frame_index;
@@ -1650,7 +1650,7 @@ void DollController::copy_controlled_objects_snapshot(
16501650 is_doll_snap_A_older);
16511651}
16521652
1653- FrameIndex DollController::fetch_best_recoverable_snapshot (DollSnapshot *&r_client_snapshot, DollSnapshot *&r_server_snapshot) {
1653+ FrameIndex DollController::fetch_last_processed_recoverable_snapshot (DollSnapshot *&r_client_snapshot, DollSnapshot *&r_server_snapshot) {
16541654 for (auto client_snap_it = client_snapshots.rbegin (); client_snap_it != client_snapshots.rend (); client_snap_it++) {
16551655 if (client_snap_it->doll_executed_input != FrameIndex::NONE) {
16561656 auto server_snap_it = VecFunc::find (server_snapshots, client_snap_it->doll_executed_input );
@@ -1696,7 +1696,7 @@ bool DollController::__pcr__fetch_recovery_info(
16961696
16971697 // This is valid until we reset it again.
16981698 is_last_doll_compared_input_valid = true ;
1699- last_doll_compared_input = fetch_best_recoverable_snapshot (client_snapshot, server_snapshot);
1699+ last_doll_compared_input = fetch_last_processed_recoverable_snapshot (client_snapshot, server_snapshot);
17001700
17011701 if (last_doll_compared_input == FrameIndex::NONE) {
17021702 // Nothing to check.
@@ -1729,18 +1729,15 @@ void DollController::on_snapshot_applied(
17291729
17301730 queued_frame_index_to_process = FrameIndex{ 0 };
17311731
1732- if (!is_last_doll_compared_input_valid) {
1733- DollSnapshot *client_snapshot;
1734- DollSnapshot *server_snapshot;
1735- last_doll_compared_input = fetch_best_recoverable_snapshot (client_snapshot, server_snapshot);
1736- }
1732+ const int input_count = frames_input.size ();
17371733
1738- if make_unlikely (last_doll_compared_input == FrameIndex::NONE || p_frame_count_to_rewind <= 0 ) {
1734+ // 1. Ensure the input reconciliation algorithm can process, otherwise:
1735+ if make_unlikely (input_count <= 0 || p_frame_count_to_rewind <= 0 ) {
17391736 // - On the server, the doll was not processed so just apply the server snapshot.
17401737 // - In case of no rewinding, it applies the most up to date server snapshot right away.
1741- auto server_snapshot_it = VecFunc::find ( server_snapshots, last_doll_compared_input );
1742- ENSURE_MSG (server_snapshot_it != server_snapshots.end (), " This should be impossible. The doll was unable to set the server snapshot since the snapshot was not found: " + last_doll_compared_input );
1743- static_cast <ClientSynchronizer *>(peer_controller-> scene_synchronizer -> get_synchronizer_internal ())-> apply_snapshot (server_snapshot_it-> data , 0 , 0 , nullptr , true , true , true , true , true ) ;
1738+ ENSURE_MSG (! server_snapshots. empty (), " This should be impossible. The doll was unable to set the server snapshot since there are no server snapshots. " );
1739+ static_cast <ClientSynchronizer *>(peer_controller-> scene_synchronizer -> get_synchronizer_internal ())-> apply_snapshot ( server_snapshots.back (). data , 0 , 0 , nullptr , true , true , true , true , true );
1740+ last_doll_compared_input = server_snapshots. back (). doll_executed_input ;
17441741 // Reset the current_input_buffer_id, to make sure the next Frame processed starts from here.
17451742 current_input_buffer_id = last_doll_compared_input;
17461743 return ;
@@ -1751,14 +1748,10 @@ void DollController::on_snapshot_applied(
17511748 // best moment to manipulate the processing (to consume or build the input
17521749 // queue) and avoid to make it noticeable.
17531750
1754- // 1 . Fetch the optimal queued inputs (how many inputs should be queued based
1751+ // 2 . Fetch the optimal queued inputs (how many inputs should be queued based
17551752 // on the current connection).
17561753 const int optimal_queued_inputs = fetch_optimal_queued_inputs ();
17571754
1758- // 2. Get the input count.
1759- const int input_count = frames_input.size ();
1760- ENSURE_MSG (input_count > 0 , " This function is not supposed to work with 0 inputs. Report this bug." )
1761-
17621755 // 3. Fetch the best input to start processing.
17631756 const int optimal_input_count = p_frame_count_to_rewind + optimal_queued_inputs;
17641757
0 commit comments