Use the spatial index in client updates

This commit is contained in:
Ryan 2022-06-19 01:01:48 -07:00
parent d709eb5ec8
commit 004f84370d
2 changed files with 35 additions and 18 deletions

View file

@ -29,7 +29,8 @@ use crate::slotmap::{Key, SlotMap};
use crate::util::{chunks_in_view_distance, is_chunk_in_view_distance}; use crate::util::{chunks_in_view_distance, is_chunk_in_view_distance};
use crate::var_int::VarInt; use crate::var_int::VarInt;
use crate::{ use crate::{
ident, ChunkPos, Chunks, Entities, EntityId, Server, Text, Ticks, WorldMeta, LIBRARY_NAMESPACE, ident, ChunkPos, Chunks, Entities, EntityId, Server, SpatialIndex, Text, Ticks, WorldMeta,
LIBRARY_NAMESPACE,
}; };
pub struct Clients { pub struct Clients {
@ -332,6 +333,7 @@ impl<'a> ClientMut<'a> {
&mut self, &mut self,
server: &Server, server: &Server,
entities: &Entities, entities: &Entities,
spatial_index: &SpatialIndex,
chunks: &Chunks, chunks: &Chunks,
meta: &WorldMeta, meta: &WorldMeta,
) { ) {
@ -608,24 +610,33 @@ impl<'a> ClientMut<'a> {
}); });
} }
// Spawn new entities within the view distance. // Spawn new entities within the view distance.
// TODO: use BVH let pos = self.position();
for (id, entity) in entities.iter() { spatial_index.query::<_, _, ()>(
if self.position().distance(entity.position()) <= view_dist as f64 * 16.0 |bb| {
&& entity.typ() != EntityType::Marker bb.projected_point(pos)
&& self.0.loaded_entities.insert(id) .distance(pos)
{ <= view_dist as f64 * 16.0
self.send_packet( },
entity |id, _| {
.spawn_packet(id) if self.0.loaded_entities.insert(id) {
.expect("should not be a marker entity"), let entity = entities.get(id).unwrap();
); if entity.typ() != EntityType::Marker {
self.send_packet(
if let Some(meta) = entity.initial_metadata_packet(id) { entity
self.send_packet(meta); .spawn_packet(id)
.expect("should not be a marker entity"),
);
if let Some(meta) = entity.initial_metadata_packet(id) {
self.send_packet(meta);
}
}
} }
} None
} },
);
self.0.old_position = self.0.new_position; self.0.old_position = self.0.new_position;
} }

View file

@ -375,7 +375,13 @@ fn do_update_loop(server: Server, mut worlds: WorldsMut) -> ShutdownResult {
world.spatial_index.update(world.entities.reborrow()); world.spatial_index.update(world.entities.reborrow());
world.clients.par_iter_mut().for_each(|(_, mut client)| { world.clients.par_iter_mut().for_each(|(_, mut client)| {
client.update(&server, &world.entities, &world.chunks, &world.meta); client.update(
&server,
&world.entities,
&world.spatial_index,
&world.chunks,
&world.meta,
);
}); });
world.entities.update(); world.entities.update();