From 7132ef41361700d9f8210835163fb8ff7a150b04 Mon Sep 17 00:00:00 2001 From: Imbris Date: Sun, 8 Mar 2020 23:32:34 -0400 Subject: [PATCH] Rearrange some operations in the server tick to reduce clientside latency of ServerEvent mediated effects --- CHANGELOG.md | 1 + client/src/lib.rs | 2 +- common/src/state.rs | 51 ++++++++++++++++-------- server/src/lib.rs | 73 ++++++++++++++++++++++------------ server/src/sys/mod.rs | 35 +++++++++------- server/src/sys/terrain_sync.rs | 8 +--- server/src/sys/waypoint.rs | 10 ++++- 7 files changed, 114 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37648ad8bf..8cdf3cf0c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed highlighting of non-collectible sprites - Fixed /give_exp ignoring player argument - Extend run sfx to small animals to prevent sneak attacks by geese. +- Decreased clientside latency of ServerEvent mediated effects (e.g. projectiles, inventory operations, etc) ### Removed diff --git a/client/src/lib.rs b/client/src/lib.rs index 09841e3fa7..f2bc898373 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -410,7 +410,7 @@ impl Client { // 3) Update client local data // 4) Tick the client's LocalState - self.state.tick(dt, add_foreign_systems); + self.state.tick(dt, add_foreign_systems, true); // 5) Terrain let pos = self diff --git a/common/src/state.rs b/common/src/state.rs index c657785b25..0fbd3202d0 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -286,8 +286,35 @@ impl State { } } + // Run RegionMap tick to update entity region occupancy + pub fn update_region_map(&self) { + self.ecs.write_resource::().tick( + self.ecs.read_storage::(), + self.ecs.read_storage::(), + self.ecs.entities(), + ); + } + + // Apply terrain changes + pub fn apply_terrain_changes(&self) { + let mut terrain = self.ecs.write_resource::(); + let mut modified_blocks = std::mem::replace( + &mut self.ecs.write_resource::().blocks, + Default::default(), + ); + // Apply block modifications + // Only include in `TerrainChanges` if successful + modified_blocks.retain(|pos, block| terrain.set(*pos, *block).is_ok()); + self.ecs.write_resource::().modified_blocks = modified_blocks; + } + /// Execute a single tick, simulating the game state by the given duration. - pub fn tick(&mut self, dt: Duration, add_foreign_systems: impl Fn(&mut DispatcherBuilder)) { + pub fn tick( + &mut self, + dt: Duration, + add_foreign_systems: impl Fn(&mut DispatcherBuilder), + update_terrain_and_regions: bool, + ) { // Change the time accordingly. self.ecs.write_resource::().0 += dt.as_secs_f64() * DAY_CYCLE_FACTOR; self.ecs.write_resource::