Reduced sync of TimeOfDay to once per 100 ticks

This commit is contained in:
Ben Wallis 2021-04-28 22:26:09 +01:00
parent aa6145c9b5
commit 3c20e8ed4e
3 changed files with 36 additions and 12 deletions

View File

@ -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<Vec2<i32>, Instant>,
target_time_of_day: Option<TimeOfDay>,
}
/// 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::<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
self.state.tick(
dt,
@ -1716,9 +1728,7 @@ impl Client {
}
},
ServerGeneral::TimeOfDay(time_of_day) => {
let dt = self.state.ecs().read_resource::<DeltaTime>().0;
let mut tod = self.state.ecs_mut().write_resource::<TimeOfDay>();
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

View File

@ -714,6 +714,17 @@ fn handle_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) =
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
// 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);
}
}
}
}