From e13d0ffe7a8bdcac943dbf5d5ae988135408b2c4 Mon Sep 17 00:00:00 2001 From: Daniel Huth <4455258+Agreon@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:12:30 +1300 Subject: [PATCH] Skip spawn of non visible player characters (#100) * Skip spawn of non visible player characters * Don't spawn player if player list is `None` Co-authored-by: Ryan --- src/client.rs | 29 ++++++++++++++++++++--------- src/player_list.rs | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/client.rs b/src/client.rs index cbd4308..c64b17e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -188,8 +188,8 @@ pub struct Client { username: String, textures: Option, world: WorldId, + player_list: Option, old_player_list: Option, - new_player_list: Option, position: Vec3, old_position: Vec3, /// Measured in m/s. @@ -271,7 +271,7 @@ impl Client { textures: ncd.textures, world: WorldId::default(), old_player_list: None, - new_player_list: None, + player_list: None, position: Vec3::default(), old_position: Vec3::default(), velocity: Vec3::default(), @@ -334,14 +334,14 @@ impl Client { /// Gets the player list this client sees. pub fn player_list(&self) -> Option<&PlayerListId> { - self.new_player_list.as_ref() + self.player_list.as_ref() } /// Sets the player list this client sees. /// /// The previous player list ID is returned. pub fn set_player_list(&mut self, id: impl Into>) -> Option { - mem::replace(&mut self.new_player_list, id.into()) + mem::replace(&mut self.player_list, id.into()) } /// Sets if this client sees the world as superflat. Superflat worlds have @@ -1035,7 +1035,7 @@ impl Client { if self.created_this_tick() { self.bits.set_spawn(false); - if let Some(id) = &self.new_player_list { + if let Some(id) = &self.player_list { player_lists .get(id) .initial_packets(|p| send_packet(&mut self.send, p)); @@ -1118,7 +1118,7 @@ impl Client { } // If the player list was changed... - if self.old_player_list != self.new_player_list { + if self.old_player_list != self.player_list { // Delete existing entries from old player list. if let Some(id) = &self.old_player_list { player_lists @@ -1127,14 +1127,14 @@ impl Client { } // Get initial packets for new player list. - if let Some(id) = &self.new_player_list { + if let Some(id) = &self.player_list { player_lists .get(id) .initial_packets(|p| send_packet(&mut self.send, p)); } - self.old_player_list = self.new_player_list.clone(); - } else if let Some(id) = &self.new_player_list { + self.old_player_list = self.player_list.clone(); + } else if let Some(id) = &self.player_list { // Update current player list. player_lists .get(id) @@ -1452,6 +1452,17 @@ impl Client { let entity = entities .get(id) .expect("entity IDs in spatial index should be valid at this point"); + + // Skip spawning players not in the player list because they would be invisible + // otherwise. + if entity.kind() == EntityKind::Player { + if let Some(list_id) = &self.player_list { + player_lists.get(list_id).entry(entity.uuid())?; + } else { + return None; + } + } + if entity.kind() != EntityKind::Marker && entity.uuid() != self.uuid && self.loaded_entities.insert(id) diff --git a/src/player_list.rs b/src/player_list.rs index f116004..8415006 100644 --- a/src/player_list.rs +++ b/src/player_list.rs @@ -222,6 +222,20 @@ impl PlayerList { } } + /// Returns a reference to the entry with the given UUID. + /// + /// If the entry does not exist, `None` is returned. + pub fn entry(&self, uuid: Uuid) -> Option<&PlayerListEntry> { + self.entries.get(&uuid) + } + + /// Returns a mutable reference to the entry with the given UUID. + /// + /// If the entry does not exist, `None` is returned. + pub fn entry_mut(&mut self, uuid: Uuid) -> Option<&mut PlayerListEntry> { + self.entries.get_mut(&uuid) + } + /// Returns an iterator over all entries in an unspecified order. pub fn entries(&self) -> impl Iterator + '_ { self.entries.iter().map(|(k, v)| (*k, v))