From 810ce672dacf72f1d03449e6096e814426646cd4 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Wed, 1 Jul 2026 10:06:33 +0800 Subject: [PATCH] refactor(gameplay): pass new_chunks vector as parameter instead of member variable --- include/Cubed/gameplay/server_world.hpp | 4 +-- src/gameplay/server_world.cpp | 39 +++++++++++-------------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/include/Cubed/gameplay/server_world.hpp b/include/Cubed/gameplay/server_world.hpp index 0a33808..d0f9758 100644 --- a/include/Cubed/gameplay/server_world.hpp +++ b/include/Cubed/gameplay/server_world.hpp @@ -118,7 +118,6 @@ private: // key = uuid PlayerHashMap m_players; ChunkHashMap m_chunks; - NewChunkVector m_new_chunks; CaveCarver m_cave_carcer; RiverWorm m_river_worm; @@ -142,7 +141,6 @@ private: std::atomic m_tick_running{true}; std::atomic m_per_tick_time = DEFAULT_PER_TICK_TIME; // ms - std::shared_mutex m_new_chunk_mutex; mutable std::shared_mutex m_player_mutex; std::mutex m_need_gen_queue_mutex; std::condition_variable_any m_gen_cv; @@ -168,7 +166,7 @@ private: const std::optional& uuid); void sync_and_collect_missing_chunks(std::vector&, const ChunkPosSet&); - void submit_new_chunks(const std::string& uuid); + void submit_new_chunks(const std::string& uuid, NewChunkVector& new_chunks); // void wait_all_chunk_tasks(); void update_ref_count(const ChunkPosSet& old, const ChunkPosSet& now); diff --git a/src/gameplay/server_world.cpp b/src/gameplay/server_world.cpp index 3473422..c587d64 100644 --- a/src/gameplay/server_world.cpp +++ b/src/gameplay/server_world.cpp @@ -237,25 +237,22 @@ void ServerWorld::gen_chunks_internal(const std::string& uuid) { ASSERT_MSG(!required_chunks_set.empty(), "required chunks is empty!!"); Logger::info("New Gen Chunks Sum: {}", need_gen_chunks_pos.size()); - { - std::lock_guard lock(m_new_chunk_mutex); - if (need_gen_chunks_pos.empty() && m_new_chunks.empty()) { - m_could_gen = true; - return; - } + if (need_gen_chunks_pos.empty()) { + m_could_gen = true; + + return; + } + NewChunkVector new_chunks; + + // Create new chunk + + for (auto& pos : need_gen_chunks_pos) { + new_chunks.emplace_back( + pos, std::make_unique(ServerChunk(*this, pos))); } - { - // Create new chunk - std::lock_guard lock(m_new_chunk_mutex); - for (auto& pos : need_gen_chunks_pos) { - m_new_chunks.emplace_back( - pos, std::make_unique(ServerChunk(*this, pos))); - } - } - - submit_new_chunks(uuid); + submit_new_chunks(uuid, new_chunks); m_chunk_gen_finished = true; } @@ -296,9 +293,9 @@ void ServerWorld::sync_and_collect_missing_chunks( } } -void ServerWorld::submit_new_chunks(const std::string& uuid) { +void ServerWorld::submit_new_chunks(const std::string& uuid, + NewChunkVector& new_chunks) { using enum ChunkLoadStyle; - std::lock_guard lock(m_new_chunk_mutex); auto pool_ptr = m_gen_thread_pool.load(); if (!pool_ptr) { return; @@ -306,7 +303,7 @@ void ServerWorld::submit_new_chunks(const std::string& uuid) { switch (m_chunk_load_style) { case RANDOM: // Enqueue directly in random order - for (auto& task : m_new_chunks) { + for (auto& task : new_chunks) { pool_ptr->enqueue([&task, this]() { std::unique_ptr chunk{std::move(task.chunk)}; @@ -317,7 +314,7 @@ void ServerWorld::submit_new_chunks(const std::string& uuid) { break; case CENTER: { std::vector> tasks; - for (auto& task : m_new_chunks) { + for (auto& task : new_chunks) { tasks.emplace_back(task.pos, &task); } @@ -342,8 +339,6 @@ void ServerWorld::submit_new_chunks(const std::string& uuid) { } } break; } - - m_new_chunks.clear(); } void ServerWorld::start_gen_thread() {