2021-04-28 15:36:07 +00:00
|
|
|
use super::terrain::LazyTerrainMessage;
|
|
|
|
use crate::{client::Client, metrics::NetworkRequestMetrics, 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-28 15:36:07 +00:00
|
|
|
use common_net::msg::{CompressedData, ServerGeneral};
|
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 {
|
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>,
|
2021-04-28 15:36:07 +00:00
|
|
|
ReadExpect<'a, NetworkRequestMetrics>,
|
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-04-28 15:36:07 +00:00
|
|
|
(terrain, terrain_changes, positions, presences, clients, network_metrics): Self::SystemData,
|
2019-10-20 05:19:50 +00:00
|
|
|
) {
|
|
|
|
// Sync changed chunks
|
|
|
|
'chunk: for chunk_key in &terrain_changes.modified_chunks {
|
2021-04-28 15:36:07 +00:00
|
|
|
let mut lazy_msg = LazyTerrainMessage::new();
|
2020-11-02 18:30:56 +00:00
|
|
|
for (presence, pos, client) in (&presences, &positions, &clients).join() {
|
2021-04-28 15:36:07 +00:00
|
|
|
if super::terrain::chunk_in_vd(pos.0, *chunk_key, &terrain, presence.view_distance)
|
|
|
|
{
|
2021-05-01 18:28:20 +00:00
|
|
|
if let Err(()) = lazy_msg.prepare_and_send(
|
|
|
|
&network_metrics,
|
2021-07-11 18:41:52 +00:00
|
|
|
client,
|
|
|
|
presence,
|
2021-05-01 18:28:20 +00:00
|
|
|
chunk_key,
|
|
|
|
|| terrain.get_key(*chunk_key).ok_or(()),
|
|
|
|
) {
|
2021-04-28 15:36:07 +00:00
|
|
|
break 'chunk;
|
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
|
|
|
)));
|
|
|
|
}
|
2021-07-11 18:41:52 +00:00
|
|
|
lazy_msg.as_ref().map(|msg| client.send_prepared(msg));
|
2020-10-18 23:53:19 +00:00
|
|
|
}
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|