2020-11-02 18:30:56 +00:00
|
|
|
use crate::{client::Client, presence::Presence};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common::{comp::Pos, terrain::TerrainGrid};
|
|
|
|
use common_ecs::{Job, Origin, Phase, System};
|
2020-12-13 17:11:55 +00:00
|
|
|
use common_net::msg::ServerGeneral;
|
2020-12-01 12:13:07 +00:00
|
|
|
use common_sys::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 {
|
2020-10-18 23:53:19 +00:00
|
|
|
let mut lazy_msg = None;
|
|
|
|
|
2020-11-02 18:30:56 +00:00
|
|
|
for (presence, pos, client) in (&presences, &positions, &clients).join() {
|
2020-10-30 16:39:53 +00:00
|
|
|
if super::terrain::chunk_in_vd(pos.0, *chunk_key, &terrain, presence.view_distance)
|
2019-10-20 05:19:50 +00:00
|
|
|
{
|
2020-10-18 23:53:19 +00:00
|
|
|
if lazy_msg.is_none() {
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg = Some(client.prepare(ServerGeneral::TerrainChunkUpdate {
|
|
|
|
key: *chunk_key,
|
|
|
|
chunk: Ok(Box::new(match terrain.get_key(*chunk_key) {
|
|
|
|
Some(chunk) => chunk.clone(),
|
|
|
|
None => break 'chunk,
|
|
|
|
})),
|
|
|
|
}));
|
2020-10-18 23:53:19 +00:00
|
|
|
}
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg.as_ref().map(|ref msg| client.send_prepared(&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-11-02 18:30:56 +00:00
|
|
|
for (_, client) in (&presences, &clients).join() {
|
2020-10-18 23:53:19 +00:00
|
|
|
if lazy_msg.is_none() {
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg = Some(client.prepare(ServerGeneral::TerrainBlockUpdates(
|
2020-10-18 23:53:19 +00:00
|
|
|
terrain_changes.modified_blocks.clone(),
|
|
|
|
)));
|
|
|
|
}
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg.as_ref().map(|ref msg| client.send_prepared(&msg));
|
2019-10-20 05:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|