diff --git a/src/net/packets/incoming/set_player_pos_and_rotate.rs b/src/net/packets/incoming/set_player_pos_and_rotate.rs index 42370374..869a48a5 100644 --- a/src/net/packets/incoming/set_player_pos_and_rotate.rs +++ b/src/net/packets/incoming/set_player_pos_and_rotate.rs @@ -1,5 +1,5 @@ use crate::net::packets::{ConnectionId, IncomingPacket}; -use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS}; +use crate::net::systems::chunk_sender::{ChunkSender}; use crate::state::GlobalState; use crate::utils::components::rotation::Rotation; use crate::utils::encoding::position::Position; @@ -27,7 +27,8 @@ impl IncomingPacket for SetPlayerPosAndRotate { let mut position = component_storage.get_mut::(my_entity_id).await?; let mut rotation = component_storage.get_mut::(my_entity_id).await?; - let old_chunk_pos = (position.x >> 4, position.z >> 4); + ChunkSender::send_chunks_to_player_if_needed(state.clone(), my_entity_id, (position.x >> 4, position.z >> 4)).await?; + /*let old_chunk_pos = (position.x >> 4, position.z >> 4); let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4); if old_chunk_pos != new_chunk_pos { @@ -40,7 +41,7 @@ impl IncomingPacket for SetPlayerPosAndRotate { } ); } - +*/ *position = Position { x: self.x as i32, y: self.y as i16, diff --git a/src/net/packets/incoming/set_player_position.rs b/src/net/packets/incoming/set_player_position.rs index c4a1c2d4..e3b0716b 100644 --- a/src/net/packets/incoming/set_player_position.rs +++ b/src/net/packets/incoming/set_player_position.rs @@ -1,9 +1,9 @@ -use tracing::{debug, trace}; +use tracing::{trace}; use ferrumc_macros::{packet, NetDecode}; use crate::net::packets::{ConnectionId, IncomingPacket}; -use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS}; +use crate::net::systems::chunk_sender::{ChunkSender}; use crate::state::GlobalState; use crate::utils::encoding::position::Position; @@ -34,7 +34,9 @@ impl IncomingPacket for SetPlayerPosition { let mut position = component_storage.get_mut::(my_entity_id).await?; - let old_chunk_pos = (position.x >> 4, position.z >> 4); + ChunkSender::send_chunks_to_player_if_needed(state.clone(), my_entity_id, (position.x >> 4, position.z >> 4)).await?; + + /*let old_chunk_pos = (position.x >> 4, position.z >> 4); let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4); if old_chunk_pos != new_chunk_pos { @@ -46,7 +48,7 @@ impl IncomingPacket for SetPlayerPosition { Ok::<(), Error>(()) } ); - } + }*/ *position = Position { x: self.x as i32, diff --git a/src/net/systems/chunk_sender.rs b/src/net/systems/chunk_sender.rs index 61e41202..0c502f57 100644 --- a/src/net/systems/chunk_sender.rs +++ b/src/net/systems/chunk_sender.rs @@ -9,12 +9,13 @@ use crate::net::packets::outgoing::set_center_chunk::SetCenterChunk; use crate::net::systems::System; use crate::net::{Connection, ConnectionWrapper}; use crate::state::GlobalState; +use crate::utils::components::last_chunk_tx_pos::LastChunkTxPos; use crate::utils::components::player::Player; use crate::utils::encoding::position::Position; use crate::utils::prelude::*; use ferrumc_macros::AutoGenName; -pub const CHUNK_RADIUS: i32 = 8; +pub const CHUNK_RADIUS: i32 = 32; const CHUNK_TX_INTERVAL_MS: u64 = 50000; #[derive(AutoGenName)] @@ -51,6 +52,39 @@ impl System for ChunkSender { } impl ChunkSender { + pub async fn send_chunks_to_player_if_needed( + state: GlobalState, + entity_id: impl TryInto, + current_pos: (i32, i32), + ) -> Result<()> { + let entity_id = entity_id.try_into().map_err(|_| Error::ConversionError)?; + + let mut last_chunk_tx_pos = state + .world + .get_component_storage() + .get_mut_or_insert_with::(entity_id, Default::default) + .await; + + let distance = last_chunk_tx_pos.distance_to(current_pos.0, current_pos.1); + + if distance < (CHUNK_RADIUS as f64 / 4f64) { + return Ok(()); + } + + last_chunk_tx_pos.set_last_chunk_tx_pos(current_pos.0, current_pos.1); + + let state_clone = state.clone(); + tokio::spawn( + async move { + ChunkSender::send_chunks_to_player(state_clone, entity_id).await?; + + Ok::<(), Error>(()) + } + ); + + + Ok(()) + } pub async fn send_chunks_to_player( state: GlobalState, entity_id: impl TryInto, diff --git a/src/utils/components/last_chunk_tx_pos.rs b/src/utils/components/last_chunk_tx_pos.rs new file mode 100644 index 00000000..97f51fb0 --- /dev/null +++ b/src/utils/components/last_chunk_tx_pos.rs @@ -0,0 +1,21 @@ +use ferrumc_macros::{Component, Constructor, Getter}; + +#[derive(Debug, Default, Component, Getter, Constructor)] +pub struct LastChunkTxPos { + pub x: i32, + pub z: i32, +} + +impl LastChunkTxPos { + pub fn set_last_chunk_tx_pos(&mut self, x: i32, z: i32) { + self.x = x; + self.z = z; + } + + pub fn distance_to(&self, x: i32, z: i32) -> f64 { + let dx = self.x - x; + let dz = self.z - z; + + ((dx * dx + dz * dz) as f64).sqrt() + } +} diff --git a/src/utils/components/mod.rs b/src/utils/components/mod.rs index f96d4f9f..db5efd02 100644 --- a/src/utils/components/mod.rs +++ b/src/utils/components/mod.rs @@ -2,3 +2,4 @@ pub mod grounded; pub mod keep_alive; pub mod player; pub mod rotation; +pub mod last_chunk_tx_pos; \ No newline at end of file