diff --git a/server/src/lib.rs b/server/src/lib.rs index 2ca2234fda..cea5ef493a 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -19,8 +19,8 @@ use common::{ net::PostOffice, state::{State, TimeOfDay, Uid}, terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainMap}, - vol::VolSize, vol::Vox, + vol::{ReadVol, VolSize}, }; use log::debug; use rand::Rng; @@ -363,6 +363,7 @@ impl Server { } } } + // Sync changed blocks let msg = ServerMsg::TerrainBlockUpdates(self.state.terrain_changes().modified_blocks.clone()); @@ -377,6 +378,23 @@ impl Server { } } + // Remove NPCs that are outside the view distances of all players + let to_delete = { + let terrain = self.state.terrain(); + ( + &self.state.ecs().entities(), + &self.state.ecs().read_storage::(), + &self.state.ecs().read_storage::(), + ) + .join() + .filter(|(_, pos, _)| terrain.get(pos.0.map(|e| e.floor() as i32)).is_err()) + .map(|(entity, _, _)| entity) + .collect::>() + }; + for entity in to_delete { + let _ = self.state.ecs_mut().delete_entity(entity); + } + // 7) Finish the tick, pass control back to the frontend. // Cleanup diff --git a/world/src/lib.rs b/world/src/lib.rs index 927b65103c..5887b27a8c 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -132,7 +132,7 @@ impl World { const SPAWN_RATE: f32 = 0.1; let supplement = ChunkSupplement { - npcs: if rand::thread_rng().gen::() < SPAWN_RATE { + npcs: if rand::thread_rng().gen::() < SPAWN_RATE && sim_chunk.chaos < 0.5 { vec![NpcInfo { pos: gen_entity_pos(), }]