2020-11-02 18:30:56 +00:00
|
|
|
use crate::{client::Client, presence::Presence};
|
2021-04-20 23:33:42 +00:00
|
|
|
use common::{comp::Pos, terrain::TerrainGrid};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_ecs::{Job, Origin, Phase, System};
|
2021-04-27 23:30:42 +00:00
|
|
|
use common_net::msg::{
|
|
|
|
CompressedData, SerializedTerrainChunk, ServerGeneral, TERRAIN_LOW_BANDWIDTH,
|
|
|
|
};
|
2021-04-06 15:47:03 +00:00
|
|
|
use common_state::TerrainChanges;
|
2021-03-08 08:36:53 +00:00
|
|
|
use specs::{Join, Read, ReadExpect, ReadStorage};
|
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
|
2021-03-04 14:00:16 +00:00
|
|
|
#[derive(Default)]
|
2019-10-20 05:19:50 +00:00
|
|
|
pub struct Sys;
|
2021-03-08 11:13:59 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2021-03-08 08:36:53 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-10-20 05:19:50 +00:00
|
|
|
type SystemData = (
|
|
|
|
ReadExpect<'a, TerrainGrid>,
|
|
|
|
Read<'a, TerrainChanges>,
|
|
|
|
ReadStorage<'a, Pos>,
|
2020-10-30 16:39:53 +00:00
|
|
|
ReadStorage<'a, Presence>,
|
2020-11-02 18:30:56 +00:00
|
|
|
ReadStorage<'a, Client>,
|
2019-10-20 05:19:50 +00:00
|
|
|
);
|
|
|
|
|
2021-03-04 14:00:16 +00:00
|
|
|
const NAME: &'static str = "terrain_sync";
|
|
|
|
const ORIGIN: Origin = Origin::Server;
|
|
|
|
const PHASE: Phase = Phase::Create;
|
|
|
|
|
2019-10-20 05:19:50 +00:00
|
|
|
fn run(
|
2021-03-08 11:13:59 +00:00
|
|
|
_job: &mut Job<Self>,
|
2021-03-08 08:36:53 +00:00
|
|
|
(terrain, terrain_changes, positions, presences, clients): Self::SystemData,
|
2019-10-20 05:19:50 +00:00
|
|
|
) {
|
|
|
|
// Sync changed chunks
|
|
|
|
'chunk: for chunk_key in &terrain_changes.modified_chunks {
|
2021-04-27 23:30:42 +00:00
|
|
|
let mut lazy_msg_hi = None;
|
|
|
|
let mut lazy_msg_lo = None;
|
2020-10-18 23:53:19 +00:00
|
|
|
|
2020-11-02 18:30:56 +00:00
|
|
|
for (presence, pos, client) in (&presences, &positions, &clients).join() {
|
2021-04-27 23:30:42 +00:00
|
|
|
if let Some(participant) = &client.participant {
|
|
|
|
let low_bandwidth = participant.bandwidth() < TERRAIN_LOW_BANDWIDTH;
|
|
|
|
let lazy_msg = if low_bandwidth {
|
|
|
|
&mut lazy_msg_lo
|
|
|
|
} else {
|
|
|
|
&mut lazy_msg_hi
|
|
|
|
};
|
|
|
|
if super::terrain::chunk_in_vd(
|
|
|
|
pos.0,
|
|
|
|
*chunk_key,
|
|
|
|
&terrain,
|
|
|
|
presence.view_distance,
|
|
|
|
) {
|
|
|
|
if lazy_msg.is_none() {
|
|
|
|
*lazy_msg = Some(client.prepare(ServerGeneral::TerrainChunkUpdate {
|
|
|
|
key: *chunk_key,
|
|
|
|
chunk: Ok(match terrain.get_key(*chunk_key) {
|
|
|
|
Some(chunk) => {
|
|
|
|
SerializedTerrainChunk::via_heuristic(&chunk, low_bandwidth)
|
|
|
|
},
|
|
|
|
None => break 'chunk,
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
lazy_msg.as_ref().map(|ref msg| client.send_prepared(&msg));
|
2020-10-18 23:53:19 +00:00
|
|
|
}
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Don't send all changed blocks to all clients
|
|
|
|
// Sync changed blocks
|
2021-04-20 23:33:42 +00:00
|
|
|
if !terrain_changes.modified_blocks.is_empty() {
|
|
|
|
let mut lazy_msg = None;
|
|
|
|
for (_, client) in (&presences, &clients).join() {
|
|
|
|
if lazy_msg.is_none() {
|
|
|
|
lazy_msg = Some(client.prepare(ServerGeneral::TerrainBlockUpdates(
|
2021-04-25 20:18:57 +00:00
|
|
|
CompressedData::compress(&terrain_changes.modified_blocks, 1),
|
2021-04-20 23:33:42 +00:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
lazy_msg.as_ref().map(|ref msg| client.send_prepared(&msg));
|
2020-10-18 23:53:19 +00:00
|
|
|
}
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|