Removed NPCs that are outside the VD

This commit is contained in:
Joshua Barretto 2019-08-02 18:48:14 +01:00
parent b5d10bfb3e
commit 6ff75dcf0b
2 changed files with 20 additions and 2 deletions

View File

@ -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::<comp::Pos>(),
&self.state.ecs().read_storage::<comp::Agent>(),
)
.join()
.filter(|(_, pos, _)| terrain.get(pos.0.map(|e| e.floor() as i32)).is_err())
.map(|(entity, _, _)| entity)
.collect::<Vec<_>>()
};
for entity in to_delete {
let _ = self.state.ecs_mut().delete_entity(entity);
}
// 7) Finish the tick, pass control back to the frontend.
// Cleanup

View File

@ -132,7 +132,7 @@ impl World {
const SPAWN_RATE: f32 = 0.1;
let supplement = ChunkSupplement {
npcs: if rand::thread_rng().gen::<f32>() < SPAWN_RATE {
npcs: if rand::thread_rng().gen::<f32>() < SPAWN_RATE && sim_chunk.chaos < 0.5 {
vec![NpcInfo {
pos: gen_entity_pos(),
}]