Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only broadcast entity actions to players who can see them #78

Closed
caelunshun opened this issue Aug 23, 2019 · 0 comments · Fixed by #105
Closed

Only broadcast entity actions to players who can see them #78

caelunshun opened this issue Aug 23, 2019 · 0 comments · Fixed by #105
Labels
difficulty: some enhancement New feature or request
Milestone

Comments

@caelunshun
Copy link
Member

Currently, entity movements, actions, and such are broadcasted to all online players, regardless of distance. Ideally, players would only be notified of entities within their view distance, and entities leaving that radius should be destroyed on the client.

Much of the functionality to do this is already implemented. In particular:

  • The chunks a player keeps loaded (and thus those within their view distance) are already stored in the ChunkHolders type. Given the position of the chunk an entity is in, we can easily find which players can see the chunk using this type. As a result, it is easy to determine which players to send entity actions to.
  • The entities for each chunk are stored in ChunkEntities, so when a player joins, we can loop over the chunks within the view distance and only send them the entities in those chunks.

This implementation consists of a few steps:

  • Implement a convenient way to broadcast packets to players who can see a given chunk. This would probably take the form of e.g. fn broadcast_packet<P: Packet>(chunk: ChunkPosition, chunk_holders: &ChunkHolders, entities: &Entities, packet: P, neq: Option<Entity>), similar to the structure of send_packet_to_all_players().
    • It might be slightly annoying to have to pass all those parameters to the function. For cleanliness, it's possible to investigate moving ChunkHolders into Util and having the function be a member function of Util.
  • Detect when a player leaves a chunk and destroy entities client-side in chunks which are no longer within the view distance.
  • Only send the inventories, positions, etc. of entities within the view distance when a player joins.
@caelunshun caelunshun added enhancement New feature or request difficulty: some labels Aug 23, 2019
@caelunshun caelunshun added this to the 0.5 milestone Aug 23, 2019
caelunshun added a commit that referenced this issue Sep 1, 2019
This function adds an easy way to broadcast a packet
to all players who are able to see a given chunk.
For example, movement packets for a player can
be sent to all players able to see that player's
chunk.

The function hasn't yet been used, but player action
broadcast systems will soon be modified to use
`Util::broadcast()`.

Also see #78.
caelunshun added a commit that referenced this issue Sep 1, 2019
This function efficiently finds all entities within the view
distance of a player. It can be used to, for example, send initial
entities to the player when they join. However, it should not
be used for broadcasting packets; `Util::broadcast` is more
efficient in this regard, since it does not have to iterate
over `view_distance^2` chunks.

See also #78.
caelunshun added a commit that referenced this issue Sep 5, 2019
* Add Util::broadcast() function

This function adds an easy way to broadcast a packet
to all players who are able to see a given chunk.
For example, movement packets for a player can
be sent to all players able to see that player's
chunk.

The function hasn't yet been used, but player action
broadcast systems will soon be modified to use
`Util::broadcast()`.

Also see #78.

* Add `ChunkEntites::entities_within_view_distance()`

This function efficiently finds all entities within the view
distance of a player. It can be used to, for example, send initial
entities to the player when they join. However, it should not
be used for broadcasting packets; `Util::broadcast` is more
efficient in this regard, since it does not have to iterate
over `view_distance^2` chunks.

See also #78.

* Implement view distance-based entity creation and destruction on client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: some enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant