pub struct PlayerAreaMap {
chunks: HashMap<ChunkPos, FxHashSet<i32>>,
player_chunks: HashMap<i32, FxHashSet<ChunkPos>>,
}Expand description
Spatial index for player proximity queries.
Uses packed ChunkPos chunk coordinates as keys for efficient hashing.
Thread-safe via scc::HashMap for concurrent access.
The map maintains a dual index:
chunks: Maps chunk coords to players whose tracking area includes that chunkplayer_chunks: Maps player entity IDs to the set of chunks they’re registered in
This enables O(1) lookup of nearby players and O(tracking area) removal.
Entity IDs are used instead of UUIDs because:
- They are globally unique within a server session (vanilla uses a static atomic counter)
- They are smaller (4 bytes vs 16 bytes) and faster to hash
PlayerAreaMaponly tracks players during a session, not persisted
Fields§
§chunks: HashMap<ChunkPos, FxHashSet<i32>>Maps packed chunk coords (ChunkPos) to set of player entity IDs
player_chunks: HashMap<i32, FxHashSet<ChunkPos>>Maps player entity ID to its current set of tracked chunks (for efficient removal)
Implementations§
Source§impl PlayerAreaMap
impl PlayerAreaMap
Sourcepub fn on_player_join(&self, player: &Player, view: &PlayerChunkView)
pub fn on_player_join(&self, player: &Player, view: &PlayerChunkView)
Registers a player at their current position using their chunk view.
Sourcepub fn on_player_leave(&self, player: &Arc<Player>)
pub fn on_player_leave(&self, player: &Arc<Player>)
Removes a player from all tracked chunks.
Sourcepub fn remove_by_entity_id(&self, entity_id: i32)
pub fn remove_by_entity_id(&self, entity_id: i32)
Removes a player from all tracked chunks by entity ID.
This is useful when you don’t have an Arc<Player> reference,
such as during respawn cleanup.
Sourcepub fn on_player_view_change(
&self,
entity_id: i32,
added_chunks: &[ChunkPos],
removed_chunks: &[ChunkPos],
)
pub fn on_player_view_change( &self, entity_id: i32, added_chunks: &[ChunkPos], removed_chunks: &[ChunkPos], )
Updates a player’s tracked chunks using pre-computed view differences.
Call this after computing the difference via PlayerChunkView::difference().
Sourcepub fn get_tracking_players(&self, chunk: ChunkPos) -> Vec<i32>
pub fn get_tracking_players(&self, chunk: ChunkPos) -> Vec<i32>
Gets all players tracking the given chunk.
fn add_to_chunk(&self, chunk: ChunkPos, entity_id: i32)
fn remove_from_chunk(&self, chunk: ChunkPos, entity_id: i32)
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PlayerAreaMap
impl RefUnwindSafe for PlayerAreaMap
impl Send for PlayerAreaMap
impl Sync for PlayerAreaMap
impl Unpin for PlayerAreaMap
impl UnsafeUnpin for PlayerAreaMap
impl UnwindSafe for PlayerAreaMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreArc<SyncMutex<>>