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,
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
@ -412,8 +412,13 @@ 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)) {
&& 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
}

View File

@ -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<Event> {
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<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
&& 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::<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();
if let Some(rider) = maybe_uid && within_range {
let _link_successful = state.link(VolumeMounting {
if let Some(rider) = maybe_uid
&& within_range
{
let _link_successful = state
.link(VolumeMounting {
pos: volume_pos,
block,
rider,
}).is_ok();
})
.is_ok();
#[cfg(feature = "worldgen")]
if _link_successful {
let uid_allocator = state.ecs().read_resource::<IdMaps>();
@ -117,10 +124,17 @@ 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::<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::<world::IndexOwned>().as_index_ref(),
state
.ecs()
.read_resource::<world::IndexOwned>()
.as_index_ref(),
volume_pos,
rider_actor,
);

View File

@ -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");