Directly compare rtism resources to avoid unnecessary hooks

This commit is contained in:
Joshua Barretto 2023-04-11 11:49:10 +01:00
parent 81b52298f0
commit 08afe26112
3 changed files with 7 additions and 4 deletions

View File

@ -233,7 +233,7 @@ pub enum NpcAction {
// Note: the `serde(name = "...")` is to minimise the length of field // Note: the `serde(name = "...")` is to minimise the length of field
// identifiers for the sake of rtsim persistence // identifiers for the sake of rtsim persistence
#[derive(Copy, Clone, Debug, Serialize, Deserialize, enum_map::Enum)] #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, enum_map::Enum)]
pub enum ChunkResource { pub enum ChunkResource {
#[serde(rename = "0")] #[serde(rename = "0")]
Grass, Grass,

View File

@ -700,9 +700,10 @@ impl Server {
fn on_block_update(ecs: &specs::World, changes: Vec<BlockDiff>) { fn on_block_update(ecs: &specs::World, changes: Vec<BlockDiff>) {
// When a resource block updates, inform rtsim // When a resource block updates, inform rtsim
if changes.iter().any(|c| { if changes
c.old.get_rtsim_resource().is_some() || c.new.get_rtsim_resource().is_some() .iter()
}) { .any(|c| c.old.get_rtsim_resource() != c.new.get_rtsim_resource())
{
ecs.write_resource::<rtsim::RtSim>().hook_block_update( ecs.write_resource::<rtsim::RtSim>().hook_block_update(
&ecs.read_resource::<Arc<world::World>>(), &ecs.read_resource::<Arc<world::World>>(),
ecs.read_resource::<world::IndexOwned>().as_index_ref(), ecs.read_resource::<world::IndexOwned>().as_index_ref(),

View File

@ -159,6 +159,8 @@ impl RtSim {
} }
} }
// Note that this hook only needs to be invoked if the block change results in a
// change to the rtsim resource produced by [`Block::get_rtsim_resource`].
pub fn hook_block_update(&mut self, world: &World, index: IndexRef, changes: Vec<BlockDiff>) { pub fn hook_block_update(&mut self, world: &World, index: IndexRef, changes: Vec<BlockDiff>) {
self.state self.state
.emit(event::OnBlockChange { changes }, world, index); .emit(event::OnBlockChange { changes }, world, index);