refactor(gameplay): pass new_chunks vector as parameter instead of member variable

This commit is contained in:
2026-07-01 10:06:33 +08:00
parent f8e2ff5ce3
commit 810ce672da
2 changed files with 18 additions and 25 deletions

View File

@@ -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<bool> m_tick_running{true};
std::atomic<int> 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<std::string>& uuid);
void sync_and_collect_missing_chunks(std::vector<ChunkPos>&,
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);

View File

@@ -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>(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>(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<ServerChunk> chunk{std::move(task.chunk)};
@@ -317,7 +314,7 @@ void ServerWorld::submit_new_chunks(const std::string& uuid) {
break;
case CENTER: {
std::vector<std::pair<ChunkPos, PendingChunk*>> 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() {