diff --git a/server/src/lib.rs b/server/src/lib.rs index 81dc112430..f8c47f60d3 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -54,6 +54,7 @@ use common::{ }, outcome::Outcome, recipe::default_recipe_book, + spiral::Spiral2d, state::{State, TimeOfDay}, sync::WorldSyncExt, terrain::TerrainChunkSize, @@ -525,7 +526,14 @@ impl Server { !&self.state.ecs().read_storage::(), ) .join() - .filter(|(_, pos, _)| terrain.get(pos.0.map(|e| e.floor() as i32)).is_err()) + .filter(|(_, pos, _)| { + let chunk_key = terrain.pos_key(pos.0.map(|e| e.floor() as i32)); + // Check not only this chunk, but also all neighbours to avoid immediate + // despawning if the entity walks outside of a valid chunk + // briefly. If the entity isn't even near a loaded chunk then we get + // rid of it. + Spiral2d::new().all(|offs| terrain.get_key(chunk_key + offs).is_none()) + }) .map(|(entity, _, _)| entity) .collect::>() };