Skip to content

Commit

Permalink
Sends chunk when player goes away by a certain threshold distance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweattypalms committed Sep 9, 2024
1 parent dff458f commit 36ab769
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/net/packets/incoming/set_player_pos_and_rotate.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -27,7 +27,8 @@ impl IncomingPacket for SetPlayerPosAndRotate {
let mut position = component_storage.get_mut::<Position>(my_entity_id).await?;
let mut rotation = component_storage.get_mut::<Rotation>(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 {
Expand All @@ -40,7 +41,7 @@ impl IncomingPacket for SetPlayerPosAndRotate {
}
);
}

*/
*position = Position {
x: self.x as i32,
y: self.y as i16,
Expand Down
10 changes: 6 additions & 4 deletions src/net/packets/incoming/set_player_position.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -34,7 +34,9 @@ impl IncomingPacket for SetPlayerPosition {

let mut position = component_storage.get_mut::<Position>(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 {
Expand All @@ -46,7 +48,7 @@ impl IncomingPacket for SetPlayerPosition {
Ok::<(), Error>(())
}
);
}
}*/

*position = Position {
x: self.x as i32,
Expand Down
36 changes: 35 additions & 1 deletion src/net/systems/chunk_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<usize>,
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::<LastChunkTxPos>(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<usize>,
Expand Down
21 changes: 21 additions & 0 deletions src/utils/components/last_chunk_tx_pos.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
1 change: 1 addition & 0 deletions src/utils/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod grounded;
pub mod keep_alive;
pub mod player;
pub mod rotation;
pub mod last_chunk_tx_pos;

0 comments on commit 36ab769

Please sign in to comment.