2019-10-20 07:20:21 +00:00
|
|
|
use super::SysTimer;
|
2020-10-17 09:36:44 +00:00
|
|
|
use crate::streams::{GetStream, InGameStream};
|
2019-10-20 05:19:50 +00:00
|
|
|
use common::{
|
|
|
|
comp::{Player, Pos},
|
2020-10-12 08:18:28 +00:00
|
|
|
msg::ServerGeneral,
|
2020-08-29 06:39:16 +00:00
|
|
|
span,
|
2019-10-20 05:19:50 +00:00
|
|
|
state::TerrainChanges,
|
|
|
|
terrain::TerrainGrid,
|
|
|
|
};
|
2019-10-20 07:20:21 +00:00
|
|
|
use specs::{Join, Read, ReadExpect, ReadStorage, System, Write, WriteStorage};
|
2019-10-20 05:19:50 +00:00
|
|
|
|
2020-03-09 03:32:34 +00:00
|
|
|
/// This systems sends new chunks to clients as well as changes to existing
|
|
|
|
/// chunks
|
2019-10-20 05:19:50 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)] // TODO: Pending review in #587
|
2019-10-20 05:19:50 +00:00
|
|
|
type SystemData = (
|
|
|
|
ReadExpect<'a, TerrainGrid>,
|
|
|
|
Read<'a, TerrainChanges>,
|
2019-10-20 07:20:21 +00:00
|
|
|
Write<'a, SysTimer<Self>>,
|
2019-10-20 05:19:50 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
|
|
|
ReadStorage<'a, Player>,
|
2020-10-16 19:37:28 +00:00
|
|
|
WriteStorage<'a, InGameStream>,
|
2019-10-20 05:19:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
2020-10-16 19:37:28 +00:00
|
|
|
(terrain, terrain_changes, mut timer, positions, players, mut in_game_streams): Self::SystemData,
|
2019-10-20 05:19:50 +00:00
|
|
|
) {
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "terrain_sync::Sys::run");
|
2019-10-20 07:20:21 +00:00
|
|
|
timer.start();
|
|
|
|
|
2019-10-20 05:19:50 +00:00
|
|
|
// Sync changed chunks
|
|
|
|
'chunk: for chunk_key in &terrain_changes.modified_chunks {
|
2020-10-18 23:53:19 +00:00
|
|
|
let mut lazy_msg = None;
|
|
|
|
|
2020-10-16 19:37:28 +00:00
|
|
|
for (player, pos, in_game_stream) in (&players, &positions, &mut in_game_streams).join()
|
|
|
|
{
|
2019-10-20 05:19:50 +00:00
|
|
|
if player
|
|
|
|
.view_distance
|
|
|
|
.map(|vd| super::terrain::chunk_in_vd(pos.0, *chunk_key, &terrain, vd))
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2020-10-18 23:53:19 +00:00
|
|
|
if lazy_msg.is_none() {
|
|
|
|
lazy_msg =
|
|
|
|
Some(in_game_stream.prepare(&ServerGeneral::TerrainChunkUpdate {
|
|
|
|
key: *chunk_key,
|
|
|
|
chunk: Ok(Box::new(match terrain.get_key(*chunk_key) {
|
|
|
|
Some(chunk) => chunk.clone(),
|
|
|
|
None => break 'chunk,
|
|
|
|
})),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
lazy_msg
|
|
|
|
.as_ref()
|
|
|
|
.map(|ref msg| in_game_stream.0.send_raw(&msg));
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Don't send all changed blocks to all clients
|
|
|
|
// Sync changed blocks
|
2020-10-18 23:53:19 +00:00
|
|
|
let mut lazy_msg = None;
|
2020-10-16 19:37:28 +00:00
|
|
|
for (player, in_game_stream) in (&players, &mut in_game_streams).join() {
|
2020-10-18 23:53:19 +00:00
|
|
|
if lazy_msg.is_none() {
|
|
|
|
lazy_msg = Some(in_game_stream.prepare(&ServerGeneral::TerrainBlockUpdates(
|
|
|
|
terrain_changes.modified_blocks.clone(),
|
|
|
|
)));
|
|
|
|
}
|
2019-10-20 05:19:50 +00:00
|
|
|
if player.view_distance.is_some() {
|
2020-10-18 23:53:19 +00:00
|
|
|
lazy_msg
|
|
|
|
.as_ref()
|
|
|
|
.map(|ref msg| in_game_stream.0.send_raw(&msg));
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 07:20:21 +00:00
|
|
|
|
|
|
|
timer.end();
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|