feat(client): add center-based chunk loading priority

This commit is contained in:
2026-06-26 11:32:52 +08:00
parent a858774b71
commit fbb6fe2fa6
3 changed files with 28 additions and 3 deletions

View File

@@ -53,9 +53,11 @@ public:
}
private:
enum class ChunkLoadStyle { RANDOM, CENTER };
using ChunkHashMap =
tbb::concurrent_unordered_map<ChunkPos, ClientChunk, ChunkPos::Hash>;
using ChunkPosSet = std::unordered_set<ChunkPos, ChunkPos::Hash>;
using ChunkPosVector = std::vector<ChunkPos>;
ClientPlayer m_player;
ChunkHashMap m_chunks;
std::vector<glm::vec4> m_planes;
@@ -79,6 +81,7 @@ private:
std::atomic<TickType> m_day_tick{6000};
std::atomic<bool> m_requesting_chunk{false};
std::shared_ptr<NetworkClient> m_client;
ChunkLoadStyle m_chunk_load_style{ChunkLoadStyle::CENTER};
void client_run(std::stop_token token);
void set_player_pos();

View File

@@ -131,7 +131,7 @@ private:
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool;
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::RANDOM};
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::CENTER};
PlayerUUIDMap m_uuid_to_name;
tbb::concurrent_unordered_map<std::string, Timer> m_timers;

View File

@@ -301,7 +301,7 @@ void ClientWorld::request_chunk() {
}
}
ChunkPosSet need_send_pos;
ChunkPosVector need_send_pos;
{
std::lock_guard lk(m_chunks_mutex);
for (auto it = m_chunks.begin(); it != m_chunks.end();) {
@@ -315,13 +315,35 @@ void ClientWorld::request_chunk() {
for (auto pos : required_chunks) {
auto it = m_chunks.find(pos);
if (it == m_chunks.end()) {
need_send_pos.emplace(pos);
need_send_pos.emplace_back(pos);
}
}
}
if (need_send_pos.empty()) {
return;
}
using enum ChunkLoadStyle;
switch (m_chunk_load_style) {
case RANDOM:
break;
case CENTER: {
glm::vec3 player_pos = m_player.get_player_pos();
auto dist2 = [player_pos](ChunkPos chunk_pos) {
ChunkPos player_chunk_pos =
get_chunk_pos(player_pos.x, player_pos.z);
float dx = player_chunk_pos.x - chunk_pos.x;
float dz = player_chunk_pos.z - chunk_pos.z;
return dx * dx + dz * dz;
};
std::sort(need_send_pos.begin(), need_send_pos.end(),
[&dist2](const auto& a, const auto& b) {
return dist2(a) < dist2(b);
});
}
}
auto uuid = m_player.get_uuid();
ChunkDataReq req;
for (const auto& pos : need_send_pos) {