mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
refactor(gameplay): pass new_chunks vector as parameter instead of member variable
This commit is contained in:
@@ -118,7 +118,6 @@ private:
|
|||||||
// key = uuid
|
// key = uuid
|
||||||
PlayerHashMap m_players;
|
PlayerHashMap m_players;
|
||||||
ChunkHashMap m_chunks;
|
ChunkHashMap m_chunks;
|
||||||
NewChunkVector m_new_chunks;
|
|
||||||
|
|
||||||
CaveCarver m_cave_carcer;
|
CaveCarver m_cave_carcer;
|
||||||
RiverWorm m_river_worm;
|
RiverWorm m_river_worm;
|
||||||
@@ -142,7 +141,6 @@ private:
|
|||||||
std::atomic<bool> m_tick_running{true};
|
std::atomic<bool> m_tick_running{true};
|
||||||
std::atomic<int> m_per_tick_time = DEFAULT_PER_TICK_TIME; // ms
|
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;
|
mutable std::shared_mutex m_player_mutex;
|
||||||
std::mutex m_need_gen_queue_mutex;
|
std::mutex m_need_gen_queue_mutex;
|
||||||
std::condition_variable_any m_gen_cv;
|
std::condition_variable_any m_gen_cv;
|
||||||
@@ -168,7 +166,7 @@ private:
|
|||||||
const std::optional<std::string>& uuid);
|
const std::optional<std::string>& uuid);
|
||||||
void sync_and_collect_missing_chunks(std::vector<ChunkPos>&,
|
void sync_and_collect_missing_chunks(std::vector<ChunkPos>&,
|
||||||
const ChunkPosSet&);
|
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 wait_all_chunk_tasks();
|
||||||
|
|
||||||
void update_ref_count(const ChunkPosSet& old, const ChunkPosSet& now);
|
void update_ref_count(const ChunkPosSet& old, const ChunkPosSet& now);
|
||||||
|
|||||||
@@ -237,25 +237,22 @@ void ServerWorld::gen_chunks_internal(const std::string& uuid) {
|
|||||||
ASSERT_MSG(!required_chunks_set.empty(), "required chunks is empty!!");
|
ASSERT_MSG(!required_chunks_set.empty(), "required chunks is empty!!");
|
||||||
|
|
||||||
Logger::info("New Gen Chunks Sum: {}", need_gen_chunks_pos.size());
|
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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
submit_new_chunks(uuid, new_chunks);
|
||||||
// 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);
|
|
||||||
m_chunk_gen_finished = true;
|
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;
|
using enum ChunkLoadStyle;
|
||||||
std::lock_guard lock(m_new_chunk_mutex);
|
|
||||||
auto pool_ptr = m_gen_thread_pool.load();
|
auto pool_ptr = m_gen_thread_pool.load();
|
||||||
if (!pool_ptr) {
|
if (!pool_ptr) {
|
||||||
return;
|
return;
|
||||||
@@ -306,7 +303,7 @@ void ServerWorld::submit_new_chunks(const std::string& uuid) {
|
|||||||
switch (m_chunk_load_style) {
|
switch (m_chunk_load_style) {
|
||||||
case RANDOM:
|
case RANDOM:
|
||||||
// Enqueue directly in random order
|
// Enqueue directly in random order
|
||||||
for (auto& task : m_new_chunks) {
|
for (auto& task : new_chunks) {
|
||||||
|
|
||||||
pool_ptr->enqueue([&task, this]() {
|
pool_ptr->enqueue([&task, this]() {
|
||||||
std::unique_ptr<ServerChunk> chunk{std::move(task.chunk)};
|
std::unique_ptr<ServerChunk> chunk{std::move(task.chunk)};
|
||||||
@@ -317,7 +314,7 @@ void ServerWorld::submit_new_chunks(const std::string& uuid) {
|
|||||||
break;
|
break;
|
||||||
case CENTER: {
|
case CENTER: {
|
||||||
std::vector<std::pair<ChunkPos, PendingChunk*>> tasks;
|
std::vector<std::pair<ChunkPos, PendingChunk*>> tasks;
|
||||||
for (auto& task : m_new_chunks) {
|
for (auto& task : new_chunks) {
|
||||||
|
|
||||||
tasks.emplace_back(task.pos, &task);
|
tasks.emplace_back(task.pos, &task);
|
||||||
}
|
}
|
||||||
@@ -342,8 +339,6 @@ void ServerWorld::submit_new_chunks(const std::string& uuid) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_new_chunks.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerWorld::start_gen_thread() {
|
void ServerWorld::start_gen_thread() {
|
||||||
|
|||||||
Reference in New Issue
Block a user