create event dispatcher at start

This commit is contained in:
Isse 2024-02-07 10:38:30 +01:00
parent 63a7ecd5d7
commit b891bb038a
4 changed files with 60 additions and 31 deletions

View File

@ -85,7 +85,6 @@ impl ServerEvent for SetLanternEvent {
flicker, flicker,
animated: true, animated: true,
}); });
} }
} }
} }
@ -115,8 +114,9 @@ impl ServerEvent for NpcInteractEvent {
}) })
}; };
if within_range && let Some(agent) = agents if within_range
.get_mut(npc_entity) && agent.target.is_none() && let Some(agent) = agents.get_mut(npc_entity)
&& agent.target.is_none()
{ {
if let Some(interactor_uid) = uids.get(interactor) { if let Some(interactor_uid) = uids.get(interactor) {
agent agent
@ -411,9 +411,14 @@ impl ServerEvent for ToggleSpriteLightEvent {
) { ) {
for ev in events.into_iter() { for ev in events.into_iter() {
if let Some(entity_pos) = positions.get(ev.entity) if let Some(entity_pos) = positions.get(ev.entity)
&& entity_pos.0.distance_squared(ev.pos.as_()) < MAX_INTERACT_RANGE.powi(2) && entity_pos.0.distance_squared(ev.pos.as_()) < MAX_INTERACT_RANGE.powi(2)
&& block_change.can_set_block(ev.pos) { && 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)) { {
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); block_change.set(ev.pos, new_block);
// TODO: Emit outcome // TODO: Emit outcome
} }

View File

@ -7,7 +7,7 @@ use common::event::{
}; };
use common_base::span; use common_base::span;
use common_ecs::{dispatch, System}; 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 use group_manip::update_map_markers;
pub(crate) use trade::cancel_trades_for; pub(crate) use trade::cancel_trades_for;
@ -163,18 +163,10 @@ impl Server {
pub fn handle_events(&mut self) -> Vec<Event> { pub fn handle_events(&mut self) -> Vec<Event> {
let mut frontend_events = Vec::new(); 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"); 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); drop(guard);
span!(guard, "handle serial events"); span!(guard, "handle serial events");
@ -185,4 +177,17 @@ impl Server {
frontend_events frontend_events
} }
pub fn create_event_dispatcher(pools: Arc<rayon::ThreadPool>) -> 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")
}
} }

View File

@ -94,21 +94,28 @@ pub fn handle_mount_volume(
); );
if let Some((mat, _, block)) = block_transform 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 mount_pos = (mat * mount_offset.0.with_w(1.0)).xyz();
let within_range = { let within_range = {
let positions = state.ecs().read_storage::<comp::Pos>(); let positions = state.ecs().read_storage::<comp::Pos>();
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::<Uid>().get(rider).copied(); let maybe_uid = state.ecs().read_storage::<Uid>().get(rider).copied();
if let Some(rider) = maybe_uid && within_range { if let Some(rider) = maybe_uid
let _link_successful = state.link(VolumeMounting { && within_range
pos: volume_pos, {
block, let _link_successful = state
rider, .link(VolumeMounting {
}).is_ok(); pos: volume_pos,
block,
rider,
})
.is_ok();
#[cfg(feature = "worldgen")] #[cfg(feature = "worldgen")]
if _link_successful { if _link_successful {
let uid_allocator = state.ecs().read_resource::<IdMaps>(); let uid_allocator = state.ecs().read_resource::<IdMaps>();
@ -117,13 +124,20 @@ pub fn handle_mount_volume(
&& let Some(volume_pos) = volume_pos.try_map_entity(|uid| { && let Some(volume_pos) = volume_pos.try_map_entity(|uid| {
let entity = uid_allocator.uid_entity(uid)?; let entity = uid_allocator.uid_entity(uid)?;
state.read_storage::<RtSimEntity>().get(entity).map(|v| v.0) state.read_storage::<RtSimEntity>().get(entity).map(|v| v.0)
}) { })
state.ecs().write_resource::<RtSim>().hook_character_mount_volume( {
state
.ecs()
.write_resource::<RtSim>()
.hook_character_mount_volume(
&state.ecs().read_resource::<Arc<world::World>>(), &state.ecs().read_resource::<Arc<world::World>>(),
state.ecs().read_resource::<world::IndexOwned>().as_index_ref(), state
.ecs()
.read_resource::<world::IndexOwned>()
.as_index_ref(),
volume_pos, volume_pos,
rider_actor, rider_actor,
); );
} }
} }
} }

View File

@ -108,7 +108,9 @@ use persistence::{
character_updater::CharacterUpdater, character_updater::CharacterUpdater,
}; };
use prometheus::Registry; 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::{ use std::{
i32, i32,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
@ -230,6 +232,7 @@ pub struct Server {
disconnect_all_clients_requested: bool, disconnect_all_clients_requested: bool,
server_constants: ServerConstants, server_constants: ServerConstants,
event_dispatcher: SendDispatcher<'static>,
} }
impl Server { impl Server {
@ -314,7 +317,7 @@ impl Server {
report_stage(ServerInitStage::StartingSystems); report_stage(ServerInitStage::StartingSystems);
let mut state = State::server( let mut state = State::server(
pools, Arc::clone(&pools),
world.sim().map_size_lg(), world.sim().map_size_lg(),
Arc::clone(&map.default_chunk), Arc::clone(&map.default_chunk),
|dispatcher_builder| { |dispatcher_builder| {
@ -622,6 +625,8 @@ impl Server {
disconnect_all_clients_requested: false, disconnect_all_clients_requested: false,
server_constants, server_constants,
event_dispatcher: Self::create_event_dispatcher(pools),
}; };
debug!(?settings, "created veloren server with"); debug!(?settings, "created veloren server with");