Skip to content

Commit 36ab769

Browse files
committed
Sends chunk when player goes away by a certain threshold distance.
1 parent dff458f commit 36ab769

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

src/net/packets/incoming/set_player_pos_and_rotate.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::net::packets::{ConnectionId, IncomingPacket};
2-
use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS};
2+
use crate::net::systems::chunk_sender::{ChunkSender};
33
use crate::state::GlobalState;
44
use crate::utils::components::rotation::Rotation;
55
use crate::utils::encoding::position::Position;
@@ -27,7 +27,8 @@ impl IncomingPacket for SetPlayerPosAndRotate {
2727
let mut position = component_storage.get_mut::<Position>(my_entity_id).await?;
2828
let mut rotation = component_storage.get_mut::<Rotation>(my_entity_id).await?;
2929

30-
let old_chunk_pos = (position.x >> 4, position.z >> 4);
30+
ChunkSender::send_chunks_to_player_if_needed(state.clone(), my_entity_id, (position.x >> 4, position.z >> 4)).await?;
31+
/*let old_chunk_pos = (position.x >> 4, position.z >> 4);
3132
let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4);
3233
3334
if old_chunk_pos != new_chunk_pos {
@@ -40,7 +41,7 @@ impl IncomingPacket for SetPlayerPosAndRotate {
4041
}
4142
);
4243
}
43-
44+
*/
4445
*position = Position {
4546
x: self.x as i32,
4647
y: self.y as i16,

src/net/packets/incoming/set_player_position.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use tracing::{debug, trace};
1+
use tracing::{trace};
22

33
use ferrumc_macros::{packet, NetDecode};
44

55
use crate::net::packets::{ConnectionId, IncomingPacket};
6-
use crate::net::systems::chunk_sender::{ChunkSender, CHUNK_RADIUS};
6+
use crate::net::systems::chunk_sender::{ChunkSender};
77
use crate::state::GlobalState;
88
use crate::utils::encoding::position::Position;
99

@@ -34,7 +34,9 @@ impl IncomingPacket for SetPlayerPosition {
3434

3535
let mut position = component_storage.get_mut::<Position>(my_entity_id).await?;
3636

37-
let old_chunk_pos = (position.x >> 4, position.z >> 4);
37+
ChunkSender::send_chunks_to_player_if_needed(state.clone(), my_entity_id, (position.x >> 4, position.z >> 4)).await?;
38+
39+
/*let old_chunk_pos = (position.x >> 4, position.z >> 4);
3840
let new_chunk_pos = (self.x as i32 >> 4, self.z as i32 >> 4);
3941
4042
if old_chunk_pos != new_chunk_pos {
@@ -46,7 +48,7 @@ impl IncomingPacket for SetPlayerPosition {
4648
Ok::<(), Error>(())
4749
}
4850
);
49-
}
51+
}*/
5052

5153
*position = Position {
5254
x: self.x as i32,

src/net/systems/chunk_sender.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use crate::net::packets::outgoing::set_center_chunk::SetCenterChunk;
99
use crate::net::systems::System;
1010
use crate::net::{Connection, ConnectionWrapper};
1111
use crate::state::GlobalState;
12+
use crate::utils::components::last_chunk_tx_pos::LastChunkTxPos;
1213
use crate::utils::components::player::Player;
1314
use crate::utils::encoding::position::Position;
1415
use crate::utils::prelude::*;
1516
use ferrumc_macros::AutoGenName;
1617

17-
pub const CHUNK_RADIUS: i32 = 8;
18+
pub const CHUNK_RADIUS: i32 = 32;
1819
const CHUNK_TX_INTERVAL_MS: u64 = 50000;
1920

2021
#[derive(AutoGenName)]
@@ -51,6 +52,39 @@ impl System for ChunkSender {
5152
}
5253

5354
impl ChunkSender {
55+
pub async fn send_chunks_to_player_if_needed(
56+
state: GlobalState,
57+
entity_id: impl TryInto<usize>,
58+
current_pos: (i32, i32),
59+
) -> Result<()> {
60+
let entity_id = entity_id.try_into().map_err(|_| Error::ConversionError)?;
61+
62+
let mut last_chunk_tx_pos = state
63+
.world
64+
.get_component_storage()
65+
.get_mut_or_insert_with::<LastChunkTxPos>(entity_id, Default::default)
66+
.await;
67+
68+
let distance = last_chunk_tx_pos.distance_to(current_pos.0, current_pos.1);
69+
70+
if distance < (CHUNK_RADIUS as f64 / 4f64) {
71+
return Ok(());
72+
}
73+
74+
last_chunk_tx_pos.set_last_chunk_tx_pos(current_pos.0, current_pos.1);
75+
76+
let state_clone = state.clone();
77+
tokio::spawn(
78+
async move {
79+
ChunkSender::send_chunks_to_player(state_clone, entity_id).await?;
80+
81+
Ok::<(), Error>(())
82+
}
83+
);
84+
85+
86+
Ok(())
87+
}
5488
pub async fn send_chunks_to_player(
5589
state: GlobalState,
5690
entity_id: impl TryInto<usize>,
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use ferrumc_macros::{Component, Constructor, Getter};
2+
3+
#[derive(Debug, Default, Component, Getter, Constructor)]
4+
pub struct LastChunkTxPos {
5+
pub x: i32,
6+
pub z: i32,
7+
}
8+
9+
impl LastChunkTxPos {
10+
pub fn set_last_chunk_tx_pos(&mut self, x: i32, z: i32) {
11+
self.x = x;
12+
self.z = z;
13+
}
14+
15+
pub fn distance_to(&self, x: i32, z: i32) -> f64 {
16+
let dx = self.x - x;
17+
let dz = self.z - z;
18+
19+
((dx * dx + dz * dz) as f64).sqrt()
20+
}
21+
}

src/utils/components/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod grounded;
22
pub mod keep_alive;
33
pub mod player;
44
pub mod rotation;
5+
pub mod last_chunk_tx_pos;

0 commit comments

Comments
 (0)