From 9a7fe1bfe9ebfe688d02c98e58a1996cb778f366 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 21 Jun 2026 15:00:50 +0800 Subject: [PATCH] feat(gameplay): add temporary chunk flag to prevent path clearing --- include/Cubed/gameplay/chunk.hpp | 8 ++++---- src/gameplay/cave_carver.cpp | 3 +++ src/gameplay/chunk.cpp | 10 +++++----- src/gameplay/chunk_generator.cpp | 9 +++++++-- src/gameplay/river_worm.cpp | 3 +++ 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/Cubed/gameplay/chunk.hpp b/include/Cubed/gameplay/chunk.hpp index 3a0dab6..166864b 100644 --- a/include/Cubed/gameplay/chunk.hpp +++ b/include/Cubed/gameplay/chunk.hpp @@ -24,7 +24,8 @@ private: std::atomic m_need_upload{true}; std::atomic m_is_on_gen_vertex_data{false}; std::atomic m_gening{false}; - std::atomic m_gen_finish{false}; + std::atomic m_temp_chunk{false}; + std::atomic m_biome = BiomeType::PLAIN; std::mutex m_vertexs_data_mutex; @@ -56,7 +57,7 @@ private: BlockType id); public: - Chunk(World& world, ChunkPos chunk_pos); + Chunk(World& world, ChunkPos chunk_pos, bool temp_chunk = false); ~Chunk(); Chunk(const Chunk&) = delete; Chunk& operator=(const Chunk&) = delete; @@ -128,8 +129,7 @@ public: void set_chunk_block(int index, unsigned id); // ensure thread safe! void gen_chunk(); - - bool is_gen_finish() const; + bool is_temp_chunk() const; ChunkPos chunk_pos() const; BiomeType biome() const; void biome(BiomeType b); diff --git a/src/gameplay/cave_carver.cpp b/src/gameplay/cave_carver.cpp index 2170b2f..2527a2f 100644 --- a/src/gameplay/cave_carver.cpp +++ b/src/gameplay/cave_carver.cpp @@ -49,11 +49,13 @@ void CaveCarver::try_to_add_path(const ChunkPos& chunk_pos, void CaveCarver::cleanup_finished_caves() { std::vector finished_keys; + for (const auto& pair : m_paths) { if (pair.second.is_finished()) { finished_keys.push_back(pair.first); } } + for (const auto& key : finished_keys) { m_paths.erase(key); } @@ -61,4 +63,5 @@ void CaveCarver::cleanup_finished_caves() { int CaveCarver::cave_sum() const { return m_paths.size(); } float& CaveCarver::cave_probability() { return m_cave_probability; } + } // namespace Cubed \ No newline at end of file diff --git a/src/gameplay/chunk.cpp b/src/gameplay/chunk.cpp index cb86f42..24dcdc3 100644 --- a/src/gameplay/chunk.cpp +++ b/src/gameplay/chunk.cpp @@ -8,8 +8,8 @@ namespace Cubed { -Chunk::Chunk(World& world, ChunkPos chunk_pos) - : m_chunk_pos(chunk_pos), m_world(world) { +Chunk::Chunk(World& world, ChunkPos chunk_pos, bool temp_chunk) + : m_temp_chunk(temp_chunk), m_chunk_pos(chunk_pos), m_world(world) { for (int i = 0; i < VERTEX_DATA_SUM; i++) { m_vertex_data.emplace_back(m_world); } @@ -466,7 +466,7 @@ void Chunk::gen_chunk() { } std::vector neighbor; for (int i = 0; i < 4; i++) { - neighbor.emplace_back(m_world, m_chunk_pos + CHUNK_DIR[i]); + neighbor.emplace_back(m_world, m_chunk_pos + CHUNK_DIR[i], true); } for (auto& chunk : neighbor) { chunk.gen_phase_one(); @@ -488,9 +488,9 @@ void Chunk::gen_chunk() { neightbor_blocks[i] = neighbor[i].get_chunk_blocks(); } gen_vertex_data(neightbor_blocks); - m_gen_finish = true; } -bool Chunk::is_gen_finish() const { return m_gen_finish.load(); } // Logger::info("Cross Sum {}", m_cross_vertices_sum.load()); +bool Chunk::is_temp_chunk() const { return m_temp_chunk.load(); } + } // namespace Cubed diff --git a/src/gameplay/chunk_generator.cpp b/src/gameplay/chunk_generator.cpp index 76f58a1..1bb6b5e 100644 --- a/src/gameplay/chunk_generator.cpp +++ b/src/gameplay/chunk_generator.cpp @@ -734,7 +734,9 @@ void ChunkGenerator::generate_cave() { return; blocks[idx] = 0; }); - path.clear_chunk(chunk_pos); + if (!m_chunk.is_temp_chunk()) { + path.clear_chunk(chunk_pos); + } } } @@ -765,8 +767,11 @@ void ChunkGenerator::generate_river() { } blocks[idx] = 7; }); - path.clear_chunk(chunk_pos); + if (!m_chunk.is_temp_chunk()) { + path.clear_chunk(chunk_pos); + } } + if (is_river) { m_chunk.biome(RIVER); } diff --git a/src/gameplay/river_worm.cpp b/src/gameplay/river_worm.cpp index 41c4ca4..a480ce1 100644 --- a/src/gameplay/river_worm.cpp +++ b/src/gameplay/river_worm.cpp @@ -46,11 +46,13 @@ void RiverWorm::try_to_add_path(const ChunkPos& chunk_pos, void RiverWorm::cleanup_finished_rivers() { std::vector finished_keys; + for (const auto& pair : m_paths) { if (pair.second.is_finished()) { finished_keys.push_back(pair.first); } } + for (const auto& key : finished_keys) { m_paths.erase(key); } @@ -58,4 +60,5 @@ void RiverWorm::cleanup_finished_rivers() { int RiverWorm::river_sum() const { return m_paths.size(); } float& RiverWorm::river_probability() { return m_probability; } + } // namespace Cubed \ No newline at end of file