2022-05-11 19:50:14 +00:00
|
|
|
use crate::{chunk_serialize::ChunkSendEntry, client::Client, presence::Presence};
|
2022-05-11 15:52:22 +00:00
|
|
|
use common::{comp::Pos, event::EventBus, 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;
|
2022-05-11 15:52:22 +00:00
|
|
|
use specs::{Entities, 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 = (
|
2022-05-11 15:52:22 +00:00
|
|
|
Entities<'a>,
|
2019-10-20 05:19:50 +00:00
|
|
|
ReadExpect<'a, TerrainGrid>,
|
|
|
|
Read<'a, TerrainChanges>,
|
2022-05-11 19:50:14 +00:00
|
|
|
ReadExpect<'a, EventBus<ChunkSendEntry>>,
|
2019-10-20 05:19:50 +00:00
|
|
|
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>,
|
2022-05-11 15:52:22 +00:00
|
|
|
(entities, terrain, terrain_changes, chunk_send_bus, positions, presences, clients): Self::SystemData,
|
2019-10-20 05:19:50 +00:00
|
|
|
) {
|
2022-05-11 15:52:22 +00:00
|
|
|
let mut chunk_send_emitter = chunk_send_bus.emitter();
|
|
|
|
|
2019-10-20 05:19:50 +00:00
|
|
|
// Sync changed chunks
|
2022-05-08 23:11:46 +00:00
|
|
|
for chunk_key in &terrain_changes.modified_chunks {
|
2022-05-11 15:52:22 +00:00
|
|
|
for (entity, presence, pos) in (&entities, &presences, &positions).join() {
|
2022-08-21 04:48:51 +00:00
|
|
|
if super::terrain::chunk_in_vd(
|
|
|
|
pos.0,
|
|
|
|
*chunk_key,
|
|
|
|
&terrain,
|
|
|
|
presence.view_distance.current(),
|
|
|
|
) {
|
2022-05-11 19:50:14 +00:00
|
|
|
chunk_send_emitter.emit(ChunkSendEntry {
|
2022-05-11 15:52:22 +00:00
|
|
|
entity,
|
|
|
|
chunk_key: *chunk_key,
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|