From 3c20e8ed4eb5ee934528e6d0d447bb355b92465d Mon Sep 17 00:00:00 2001 From: Ben Wallis Date: Wed, 28 Apr 2021 22:26:09 +0100 Subject: [PATCH] Reduced sync of TimeOfDay to once per 100 ticks --- client/src/lib.rs | 18 ++++++++++++++---- server/src/cmd.rs | 11 +++++++++++ server/src/sys/entity_sync.rs | 19 +++++++++++-------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index a9e1fc582a..a7ef53d2bd 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -34,7 +34,7 @@ use common::{ grid::Grid, outcome::Outcome, recipe::RecipeBook, - resources::{DeltaTime, PlayerEntity, TimeOfDay}, + resources::{PlayerEntity, TimeOfDay}, terrain::{ block::Block, map::MapConfig, neighbors, BiomeKind, SitesKind, SpriteKind, TerrainChunk, TerrainChunkSize, @@ -190,6 +190,7 @@ pub struct Client { loaded_distance: f32, pending_chunks: HashMap, Instant>, + target_time_of_day: Option, } /// Holds data related to the current players characters, as well as some @@ -693,6 +694,7 @@ impl Client { loaded_distance: 0.0, pending_chunks: HashMap::new(), + target_time_of_day: None, }) } @@ -1449,6 +1451,16 @@ impl Client { self.invite = None; } + // Lerp towards the target time of day - this ensures a smooth transition for + // large jumps in TimeOfDay such as when using /time + if let Some(target_tod) = self.target_time_of_day { + let mut tod = self.state.ecs_mut().write_resource::(); + tod.0 = Lerp::lerp(tod.0, target_tod.0, dt.as_secs_f64()); + if tod.0 >= target_tod.0 { + self.target_time_of_day = None; + } + } + // 4) Tick the client's LocalState self.state.tick( dt, @@ -1716,9 +1728,7 @@ impl Client { } }, ServerGeneral::TimeOfDay(time_of_day) => { - let dt = self.state.ecs().read_resource::().0; - let mut tod = self.state.ecs_mut().write_resource::(); - tod.0 = Lerp::lerp(tod.0, time_of_day.0, dt as f64); + self.target_time_of_day = Some(time_of_day); }, ServerGeneral::EntitySync(entity_sync_package) => { self.state diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 9c24866507..47081c6a48 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -714,6 +714,17 @@ fn handle_time( server.state.mut_resource::().0 = new_time; + // Update all clients with the new TimeOfDay (without this they would have to + // wait for the next 100th tick to receive the update). + let mut tod_lazymsg = None; + let clients = server.state.ecs().read_storage::(); + for client in (&clients).join() { + let msg = tod_lazymsg + .unwrap_or_else(|| client.prepare(ServerGeneral::TimeOfDay(TimeOfDay(new_time)))); + let _ = client.send_prepared(&msg); + tod_lazymsg = Some(msg); + } + if let Some(new_time) = NaiveTime::from_num_seconds_from_midnight_opt(((new_time as u64) % 86400) as u32, 0) { diff --git a/server/src/sys/entity_sync.rs b/server/src/sys/entity_sync.rs index 12a33c4570..964df88283 100644 --- a/server/src/sys/entity_sync.rs +++ b/server/src/sys/entity_sync.rs @@ -356,14 +356,17 @@ impl<'a> System<'a> for Sys { // Sync resources // TODO: doesn't really belong in this system (rename system or create another // system?) - let mut tof_lazymsg = None; - for client in (&clients).join() { - let msg = tof_lazymsg - .unwrap_or_else(|| client.prepare(ServerGeneral::TimeOfDay(*time_of_day))); - // We don't care much about stream errors here since they could just represent - // network disconnection, which is handled elsewhere. - let _ = client.send_prepared(&msg); - tof_lazymsg = Some(msg); + const TOD_SYNC_FREQ: u64 = 100; + if tick % TOD_SYNC_FREQ == 0 { + let mut tod_lazymsg = None; + for client in (&clients).join() { + let msg = tod_lazymsg + .unwrap_or_else(|| client.prepare(ServerGeneral::TimeOfDay(*time_of_day))); + // We don't care much about stream errors here since they could just represent + // network disconnection, which is handled elsewhere. + let _ = client.send_prepared(&msg); + tod_lazymsg = Some(msg); + } } } }