Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
fix load amount chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriTimoz committed Nov 18, 2023
1 parent 903eaa1 commit 8409239
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 0 additions & 1 deletion minecraft-server/src/player_handler/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ pub async fn handshake(stream: &mut TcpStream, logged_in_player_info: LoggedInPl
return Err(());
};
debug!("ChunkBatchAcknoledgement received");

Ok((PlayerInfo {
addr: logged_in_player_info.addr,
username: logged_in_player_info.username,
Expand Down
17 changes: 12 additions & 5 deletions minecraft-server/src/player_handler/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl PlayerHandler {

async fn on_move(&mut self) {
let new_center_chunk = self.position.chunk();

// Tell the client which chunk he is in
if new_center_chunk == self.center_chunk { return };
self.send_packet(PlayClientbound::SetCenterChunk { chunk_x: VarInt(new_center_chunk.cx), chunk_z: VarInt(new_center_chunk.cz) }).await;
Expand All @@ -58,8 +57,8 @@ impl PlayerHandler {
let mut loaded_chunks_after = HashSet::new();
for cx in (new_center_chunk.cx - self.render_distance)..=(new_center_chunk.cx + self.render_distance) {
for cz in (new_center_chunk.cz - self.render_distance)..=(new_center_chunk.cz + self.render_distance) {
let dist = (((cx - new_center_chunk.cx).pow(2) + (cz - new_center_chunk.cz).pow(2)) as f32).sqrt();
if dist > self.render_distance as f32 { continue };
let dist = (cx - new_center_chunk.cx).abs() + (cz - new_center_chunk.cz).abs();
if dist > self.render_distance { continue };
loaded_chunks_after.insert(ChunkColumnPosition { cx, cz });
}
}
Expand All @@ -68,10 +67,10 @@ impl PlayerHandler {
if loaded_chunks_after == self.loaded_chunks { return };
let mut newly_loaded_chunks: Vec<_> = loaded_chunks_after.difference(&self.loaded_chunks).cloned().collect();
let unloaded_chunks: Vec<_> = self.loaded_chunks.difference(&loaded_chunks_after).cloned().collect();
for skipped in newly_loaded_chunks.iter().skip(50) {
for skipped in newly_loaded_chunks.iter().skip(5) {
loaded_chunks_after.remove(skipped);
}
newly_loaded_chunks.truncate(50);
newly_loaded_chunks.truncate(5);

// Tell the world about the changes
self.world.update_loaded_chunks(self.info.uuid, loaded_chunks_after.clone()).await;
Expand All @@ -80,6 +79,8 @@ impl PlayerHandler {
let mut heightmaps = HashMap::new();
heightmaps.insert(String::from("MOTION_BLOCKING"), NbtTag::LongArray(vec![0; 37]));
let heightmaps = NbtTag::Compound(heightmaps);
let start_time = std::time::Instant::now();
let mut i = 0;
for newly_loaded_chunk in newly_loaded_chunks {
let mut column = Vec::new();
for cy in -4..20 {
Expand Down Expand Up @@ -109,9 +110,15 @@ impl PlayerHandler {
block_light: Array::default(),
}
};
i += 1;
let elapsed: Duration = start_time.elapsed();
info!("Chunk {} Elapsed: {:?}", i, elapsed);

self.send_packet(chunk_data).await;
info!("sent");
}


// Tell the client to unload chunks
for unloaded_chunk in unloaded_chunks {
self.send_packet(PlayClientbound::UnloadChunk {
Expand Down
4 changes: 2 additions & 2 deletions minecraft-server/src/world/light.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{collections::{BinaryHeap, VecDeque}, ops::AddAssign};
use std::{collections::BinaryHeap, ops::AddAssign};

use minecraft_protocol::ids::blocks::Block;

use crate::prelude::*;
use super::*;

#[derive(Debug, Clone)]
struct SectionLightData(Vec<u8>);
struct SectionLightData(Vec<u8>); // TODO(optimization): Use simd

impl SectionLightData {
pub fn new() -> SectionLightData {
Expand Down
13 changes: 13 additions & 0 deletions minecraft-server/src/world/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,5 +698,18 @@ mod tests {

}

#[test]
fn benchmark_get_block() {

let start_time = std::time::Instant::now();
for _ in 0..441 {
let mut column = ChunkColumn::flat();
}

let elapsed: Duration = start_time.elapsed();
println!("All Elapsed: {:?}", elapsed);
println!("Elapsed: {:?}", elapsed / 441);
}


}

0 comments on commit 8409239

Please sign in to comment.