Merge branch 'xvar/reduce-timeofday-sync-frequency' into 'master'

Reduced frequency of TimeOfDay message being sent to clients

See merge request veloren/veloren!2218
This commit is contained in:
Ben Wallis 2021-04-29 13:50:08 +00:00
commit 8165cc4169
3 changed files with 36 additions and 12 deletions

View File

@ -34,7 +34,7 @@ use common::{
grid::Grid, grid::Grid,
outcome::Outcome, outcome::Outcome,
recipe::RecipeBook, recipe::RecipeBook,
resources::{DeltaTime, PlayerEntity, TimeOfDay}, resources::{PlayerEntity, TimeOfDay},
terrain::{ terrain::{
block::Block, map::MapConfig, neighbors, BiomeKind, SitesKind, SpriteKind, TerrainChunk, block::Block, map::MapConfig, neighbors, BiomeKind, SitesKind, SpriteKind, TerrainChunk,
TerrainChunkSize, TerrainChunkSize,
@ -190,6 +190,7 @@ pub struct Client {
loaded_distance: f32, loaded_distance: f32,
pending_chunks: HashMap<Vec2<i32>, Instant>, pending_chunks: HashMap<Vec2<i32>, Instant>,
target_time_of_day: Option<TimeOfDay>,
} }
/// Holds data related to the current players characters, as well as some /// Holds data related to the current players characters, as well as some
@ -693,6 +694,7 @@ impl Client {
loaded_distance: 0.0, loaded_distance: 0.0,
pending_chunks: HashMap::new(), pending_chunks: HashMap::new(),
target_time_of_day: None,
}) })
} }
@ -1455,6 +1457,16 @@ impl Client {
self.invite = None; 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::<TimeOfDay>();
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 // 4) Tick the client's LocalState
self.state.tick( self.state.tick(
dt, dt,
@ -1722,9 +1734,7 @@ impl Client {
} }
}, },
ServerGeneral::TimeOfDay(time_of_day) => { ServerGeneral::TimeOfDay(time_of_day) => {
let dt = self.state.ecs().read_resource::<DeltaTime>().0; self.target_time_of_day = Some(time_of_day);
let mut tod = self.state.ecs_mut().write_resource::<TimeOfDay>();
tod.0 = Lerp::lerp(tod.0, time_of_day.0, dt as f64);
}, },
ServerGeneral::EntitySync(entity_sync_package) => { ServerGeneral::EntitySync(entity_sync_package) => {
self.state self.state

View File

@ -714,6 +714,17 @@ fn handle_time(
server.state.mut_resource::<TimeOfDay>().0 = new_time; server.state.mut_resource::<TimeOfDay>().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::<Client>();
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) = if let Some(new_time) =
NaiveTime::from_num_seconds_from_midnight_opt(((new_time as u64) % 86400) as u32, 0) NaiveTime::from_num_seconds_from_midnight_opt(((new_time as u64) % 86400) as u32, 0)
{ {

View File

@ -356,14 +356,17 @@ impl<'a> System<'a> for Sys {
// Sync resources // Sync resources
// TODO: doesn't really belong in this system (rename system or create another // TODO: doesn't really belong in this system (rename system or create another
// system?) // system?)
let mut tof_lazymsg = None; const TOD_SYNC_FREQ: u64 = 100;
for client in (&clients).join() { if tick % TOD_SYNC_FREQ == 0 {
let msg = tof_lazymsg let mut tod_lazymsg = None;
.unwrap_or_else(|| client.prepare(ServerGeneral::TimeOfDay(*time_of_day))); for client in (&clients).join() {
// We don't care much about stream errors here since they could just represent let msg = tod_lazymsg
// network disconnection, which is handled elsewhere. .unwrap_or_else(|| client.prepare(ServerGeneral::TimeOfDay(*time_of_day)));
let _ = client.send_prepared(&msg); // We don't care much about stream errors here since they could just represent
tof_lazymsg = Some(msg); // network disconnection, which is handled elsewhere.
let _ = client.send_prepared(&msg);
tod_lazymsg = Some(msg);
}
} }
} }
} }