From b891bb038a8ebb53ccd587b57db4f673752043ec Mon Sep 17 00:00:00 2001 From: Isse Date: Wed, 7 Feb 2024 10:38:30 +0100 Subject: [PATCH] create event dispatcher at start --- server/src/events/interaction.rs | 17 +++++++++----- server/src/events/mod.rs | 27 ++++++++++++++--------- server/src/events/mounting.rs | 38 ++++++++++++++++++++++---------- server/src/lib.rs | 9 ++++++-- 4 files changed, 60 insertions(+), 31 deletions(-) diff --git a/server/src/events/interaction.rs b/server/src/events/interaction.rs index 8617a1ec6d..e24937a18c 100755 --- a/server/src/events/interaction.rs +++ b/server/src/events/interaction.rs @@ -85,7 +85,6 @@ impl ServerEvent for SetLanternEvent { flicker, animated: true, }); - } } } @@ -115,8 +114,9 @@ impl ServerEvent for NpcInteractEvent { }) }; - if within_range && let Some(agent) = agents - .get_mut(npc_entity) && agent.target.is_none() + if within_range + && let Some(agent) = agents.get_mut(npc_entity) + && agent.target.is_none() { if let Some(interactor_uid) = uids.get(interactor) { agent @@ -411,9 +411,14 @@ impl ServerEvent for ToggleSpriteLightEvent { ) { for ev in events.into_iter() { if let Some(entity_pos) = positions.get(ev.entity) - && entity_pos.0.distance_squared(ev.pos.as_()) < MAX_INTERACT_RANGE.powi(2) - && block_change.can_set_block(ev.pos) { - if let Some(new_block) = terrain.get(ev.pos).ok().and_then(|block| block.with_toggle_light(ev.enable)) { + && entity_pos.0.distance_squared(ev.pos.as_()) < MAX_INTERACT_RANGE.powi(2) + && block_change.can_set_block(ev.pos) + { + if let Some(new_block) = terrain + .get(ev.pos) + .ok() + .and_then(|block| block.with_toggle_light(ev.enable)) + { block_change.set(ev.pos, new_block); // TODO: Emit outcome } diff --git a/server/src/events/mod.rs b/server/src/events/mod.rs index 97bcb2b200..8a63485f75 100644 --- a/server/src/events/mod.rs +++ b/server/src/events/mod.rs @@ -7,7 +7,7 @@ use common::event::{ }; use common_base::span; use common_ecs::{dispatch, System}; -use specs::{DispatcherBuilder, Entity as EcsEntity, ReadExpect, WorldExt}; +use specs::{shred::SendDispatcher, DispatcherBuilder, Entity as EcsEntity, ReadExpect, WorldExt}; pub use group_manip::update_map_markers; pub(crate) use trade::cancel_trades_for; @@ -163,18 +163,10 @@ impl Server { pub fn handle_events(&mut self) -> Vec { let mut frontend_events = Vec::new(); - span!(guard, "create event dispatcher"); - // Run systems to handle events. - // Create and run a dispatcher for ecs systems. - let mut dispatch_builder = - DispatcherBuilder::new().with_pool(Arc::clone(self.state.thread_pool())); - register_event_systems(&mut dispatch_builder); - // This dispatches all the systems in parallel. - let mut dispatcher = dispatch_builder.build(); - drop(guard); span!(guard, "run event systems"); - dispatcher.dispatch(self.state.ecs()); + // This dispatches all the systems in parallel. + self.event_dispatcher.dispatch(self.state.ecs()); drop(guard); span!(guard, "handle serial events"); @@ -185,4 +177,17 @@ impl Server { frontend_events } + + pub fn create_event_dispatcher(pools: Arc) -> SendDispatcher<'static> { + span!(_guard, "create event dispatcher"); + // Run systems to handle events. + // Create and run a dispatcher for ecs systems. + let mut dispatch_builder = DispatcherBuilder::new().with_pool(pools); + register_event_systems(&mut dispatch_builder); + dispatch_builder + .build() + .try_into_sendable() + .ok() + .expect("This should be sendable") + } } diff --git a/server/src/events/mounting.rs b/server/src/events/mounting.rs index dca0fae144..6e579484b8 100644 --- a/server/src/events/mounting.rs +++ b/server/src/events/mounting.rs @@ -94,21 +94,28 @@ pub fn handle_mount_volume( ); if let Some((mat, _, block)) = block_transform - && let Some(mount_offset) = block.mount_offset() { + && let Some(mount_offset) = block.mount_offset() + { let mount_pos = (mat * mount_offset.0.with_w(1.0)).xyz(); let within_range = { let positions = state.ecs().read_storage::(); - positions.get(rider).map_or(false, |pos| pos.0.distance_squared(mount_pos) < MAX_SPRITE_MOUNT_RANGE.powi(2)) + positions.get(rider).map_or(false, |pos| { + pos.0.distance_squared(mount_pos) < MAX_SPRITE_MOUNT_RANGE.powi(2) + }) }; let maybe_uid = state.ecs().read_storage::().get(rider).copied(); - if let Some(rider) = maybe_uid && within_range { - let _link_successful = state.link(VolumeMounting { - pos: volume_pos, - block, - rider, - }).is_ok(); + if let Some(rider) = maybe_uid + && within_range + { + let _link_successful = state + .link(VolumeMounting { + pos: volume_pos, + block, + rider, + }) + .is_ok(); #[cfg(feature = "worldgen")] if _link_successful { let uid_allocator = state.ecs().read_resource::(); @@ -117,13 +124,20 @@ pub fn handle_mount_volume( && let Some(volume_pos) = volume_pos.try_map_entity(|uid| { let entity = uid_allocator.uid_entity(uid)?; state.read_storage::().get(entity).map(|v| v.0) - }) { - state.ecs().write_resource::().hook_character_mount_volume( + }) + { + state + .ecs() + .write_resource::() + .hook_character_mount_volume( &state.ecs().read_resource::>(), - state.ecs().read_resource::().as_index_ref(), + state + .ecs() + .read_resource::() + .as_index_ref(), volume_pos, rider_actor, - ); + ); } } } diff --git a/server/src/lib.rs b/server/src/lib.rs index f70cd28782..a17a441b51 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -108,7 +108,9 @@ use persistence::{ character_updater::CharacterUpdater, }; use prometheus::Registry; -use specs::{Builder, Entity as EcsEntity, Entity, Join, LendJoin, WorldExt}; +use specs::{ + shred::SendDispatcher, Builder, Entity as EcsEntity, Entity, Join, LendJoin, WorldExt, +}; use std::{ i32, ops::{Deref, DerefMut}, @@ -230,6 +232,7 @@ pub struct Server { disconnect_all_clients_requested: bool, server_constants: ServerConstants, + event_dispatcher: SendDispatcher<'static>, } impl Server { @@ -314,7 +317,7 @@ impl Server { report_stage(ServerInitStage::StartingSystems); let mut state = State::server( - pools, + Arc::clone(&pools), world.sim().map_size_lg(), Arc::clone(&map.default_chunk), |dispatcher_builder| { @@ -622,6 +625,8 @@ impl Server { disconnect_all_clients_requested: false, server_constants, + + event_dispatcher: Self::create_event_dispatcher(pools), }; debug!(?settings, "created veloren server with");