@@ -72,7 +72,21 @@ void NS::SyncGroup::remove_listening_peer(int p_peer) {
7272}
7373
7474uint32_t NS::SyncGroup::add_new_sync_object (ObjectData *p_object_data, bool p_is_simulated) {
75- if (p_object_data->get_controlled_by_peer () != -1 ) {
75+ if (p_is_simulated) {
76+ // Make sure the node is not contained into the trickled sync.
77+ const int tso_index = trickled_sync_objects.find (p_object_data);
78+ if (tso_index >= 0 ) {
79+ remove_sync_object (tso_index, false );
80+ }
81+ } else {
82+ // Make sure the node is not contained into the realtime sync.
83+ const int rsn_index = simulated_sync_objects.find (p_object_data);
84+ if (rsn_index >= 0 ) {
85+ remove_sync_object (rsn_index, true );
86+ }
87+ }
88+
89+ if (p_object_data->get_controlled_by_peer () > 0 ) {
7690 // This is a controller with an associated peer, update the networked_peer list.
7791 // Regardless if it's simulated or not.
7892 const int peer = p_object_data->get_controlled_by_peer ();
@@ -82,12 +96,6 @@ uint32_t NS::SyncGroup::add_new_sync_object(ObjectData *p_object_data, bool p_is
8296 }
8397
8498 if (p_is_simulated) {
85- // Make sure the node is not contained into the trickled sync.
86- const int tso_index = trickled_sync_objects.find (p_object_data);
87- if (tso_index >= 0 ) {
88- remove_sync_object (tso_index, false );
89- }
90-
9199 // Add it into the realtime sync nodes
92100 int index = simulated_sync_objects.find (p_object_data);
93101
@@ -112,12 +120,6 @@ uint32_t NS::SyncGroup::add_new_sync_object(ObjectData *p_object_data, bool p_is
112120
113121 return index;
114122 } else {
115- // Make sure the node is not contained into the realtime sync.
116- const int rsn_index = simulated_sync_objects.find (p_object_data);
117- if (rsn_index >= 0 ) {
118- remove_sync_object (rsn_index, true );
119- }
120-
121123 // Add it into the trickled sync nodes
122124 int index = trickled_sync_objects.find (p_object_data);
123125
@@ -153,45 +155,7 @@ void NS::SyncGroup::remove_sync_object(std::size_t p_index, bool p_is_simulated)
153155 trickled_sync_objects_list_changed = true ;
154156 }
155157
156- if (associted_peer > 0 ) {
157- // If no other simulated objects controlled by `associated_peer` remove it from
158- bool is_simulating = false ;
159- bool is_networking = false ;
160-
161- for (auto &soi : simulated_sync_objects) {
162- if (soi.od ->get_controlled_by_peer () == associted_peer) {
163- is_networking = true ;
164- is_simulating = true ;
165- break ;
166- }
167- }
168-
169- if (!is_networking) {
170- if (NS::VecFunc::has (listening_peers, associted_peer)) {
171- is_networking = true ;
172- }
173- }
174-
175- if (!is_networking) {
176- for (auto &toi : trickled_sync_objects) {
177- if (toi.od ->get_controlled_by_peer () == associted_peer) {
178- is_networking = true ;
179- break ;
180- }
181- }
182- }
183-
184- if (!is_simulating) {
185- // No other objects associated to this peer are simulated, remove from simulating peers.
186- VecFunc::remove_unordered (simulating_peers, associted_peer);
187- update_listeners_to_simulating_peer (associted_peer, false );
188- }
189-
190- if (!is_networking) {
191- NS::VecFunc::remove_unordered (networked_peers, associted_peer);
192- NS::VecFunc::remove_unordered (peers_with_newly_calculated_latency, associted_peer);
193- }
194- }
158+ validate_peer_association (associted_peer);
195159}
196160
197161void NS::SyncGroup::remove_sync_object (const ObjectData &p_object_data) {
@@ -324,6 +288,38 @@ void NS::SyncGroup::notify_peer_has_newly_calculated_latency(int p_peer) {
324288 }
325289}
326290
291+ void NS::SyncGroup::notify_controller_changed (NS::ObjectData *p_object_data, int p_previous_controlling_peer) {
292+ if (p_object_data->get_controlled_by_peer () == p_previous_controlling_peer) {
293+ return ;
294+ }
295+
296+ bool is_in_this_sync_group = false ;
297+ bool is_simulated = false ;
298+ if (simulated_sync_objects.find (p_object_data) != -1 ) {
299+ is_in_this_sync_group = true ;
300+ is_simulated = true ;
301+ } else if (trickled_sync_objects.find (p_object_data) != -1 ) {
302+ is_in_this_sync_group = true ;
303+ }
304+
305+ if (is_in_this_sync_group) {
306+ validate_peer_association (p_previous_controlling_peer);
307+
308+ if (p_object_data->get_controlled_by_peer () > 0 ) {
309+ const int peer = p_object_data->get_controlled_by_peer ();
310+
311+ if (is_simulated) {
312+ VecFunc::insert_unique (simulating_peers, peer);
313+ update_listeners_to_simulating_peer (peer, true );
314+ }
315+
316+ if (NS::VecFunc::insert_unique (networked_peers, peer)) {
317+ NS::VecFunc::insert_unique (peers_with_newly_calculated_latency, peer);
318+ }
319+ }
320+ }
321+ }
322+
327323void NS::SyncGroup::notify_simulating_peers_about_listener_status (int p_peer_listener, bool p_simulating) {
328324 for (int peer : simulating_peers) {
329325 NetworkedControllerBase *controller = scene_sync->get_controller_for_peer (peer);
@@ -338,6 +334,44 @@ void NS::SyncGroup::update_listeners_to_simulating_peer(int p_simulating_peer, b
338334 }
339335}
340336
337+ void NS::SyncGroup::validate_peer_association (int p_peer) {
338+ if (p_peer <= 0 ) {
339+ return ;
340+ }
341+
342+ // If no other simulated objects controlled by `associated_peer` remove it from
343+ bool is_simulating = false ;
344+ bool is_networking = false ;
345+
346+ for (auto &soi : simulated_sync_objects) {
347+ if (soi.od ->get_controlled_by_peer () == p_peer) {
348+ is_networking = true ;
349+ is_simulating = true ;
350+ break ;
351+ }
352+ }
353+
354+ if (!is_networking) {
355+ for (auto &toi : trickled_sync_objects) {
356+ if (toi.od ->get_controlled_by_peer () == p_peer) {
357+ is_networking = true ;
358+ break ;
359+ }
360+ }
361+ }
362+
363+ if (!is_simulating) {
364+ // No other objects associated to this peer are simulated, remove from simulating peers.
365+ VecFunc::remove_unordered (simulating_peers, p_peer);
366+ update_listeners_to_simulating_peer (p_peer, false );
367+ }
368+
369+ if (!is_networking) {
370+ NS::VecFunc::remove_unordered (networked_peers, p_peer);
371+ NS::VecFunc::remove_unordered (peers_with_newly_calculated_latency, p_peer);
372+ }
373+ }
374+
341375std::size_t NS::SyncGroup::find_simulated (const struct ObjectData &p_object_data) const {
342376 std::size_t i = 0 ;
343377 for (auto sso : simulated_sync_objects) {
0 commit comments