feat(gameplay): add temporary chunk flag to prevent path clearing

This commit is contained in:
2026-06-21 15:00:50 +08:00
parent 8929af888a
commit 9a7fe1bfe9
5 changed files with 22 additions and 11 deletions

View File

@@ -24,7 +24,8 @@ private:
std::atomic<bool> m_need_upload{true}; std::atomic<bool> m_need_upload{true};
std::atomic<bool> m_is_on_gen_vertex_data{false}; std::atomic<bool> m_is_on_gen_vertex_data{false};
std::atomic<bool> m_gening{false}; std::atomic<bool> m_gening{false};
std::atomic<bool> m_gen_finish{false}; std::atomic<bool> m_temp_chunk{false};
std::atomic<BiomeType> m_biome = BiomeType::PLAIN; std::atomic<BiomeType> m_biome = BiomeType::PLAIN;
std::mutex m_vertexs_data_mutex; std::mutex m_vertexs_data_mutex;
@@ -56,7 +57,7 @@ private:
BlockType id); BlockType id);
public: public:
Chunk(World& world, ChunkPos chunk_pos); Chunk(World& world, ChunkPos chunk_pos, bool temp_chunk = false);
~Chunk(); ~Chunk();
Chunk(const Chunk&) = delete; Chunk(const Chunk&) = delete;
Chunk& operator=(const Chunk&) = delete; Chunk& operator=(const Chunk&) = delete;
@@ -128,8 +129,7 @@ public:
void set_chunk_block(int index, unsigned id); void set_chunk_block(int index, unsigned id);
// ensure thread safe! // ensure thread safe!
void gen_chunk(); void gen_chunk();
bool is_temp_chunk() const;
bool is_gen_finish() const;
ChunkPos chunk_pos() const; ChunkPos chunk_pos() const;
BiomeType biome() const; BiomeType biome() const;
void biome(BiomeType b); void biome(BiomeType b);

View File

@@ -49,11 +49,13 @@ void CaveCarver::try_to_add_path(const ChunkPos& chunk_pos,
void CaveCarver::cleanup_finished_caves() { void CaveCarver::cleanup_finished_caves() {
std::vector<unsigned int> finished_keys; std::vector<unsigned int> finished_keys;
for (const auto& pair : m_paths) { for (const auto& pair : m_paths) {
if (pair.second.is_finished()) { if (pair.second.is_finished()) {
finished_keys.push_back(pair.first); finished_keys.push_back(pair.first);
} }
} }
for (const auto& key : finished_keys) { for (const auto& key : finished_keys) {
m_paths.erase(key); m_paths.erase(key);
} }
@@ -61,4 +63,5 @@ void CaveCarver::cleanup_finished_caves() {
int CaveCarver::cave_sum() const { return m_paths.size(); } int CaveCarver::cave_sum() const { return m_paths.size(); }
float& CaveCarver::cave_probability() { return m_cave_probability; } float& CaveCarver::cave_probability() { return m_cave_probability; }
} // namespace Cubed } // namespace Cubed

View File

@@ -8,8 +8,8 @@
namespace Cubed { namespace Cubed {
Chunk::Chunk(World& world, ChunkPos chunk_pos) Chunk::Chunk(World& world, ChunkPos chunk_pos, bool temp_chunk)
: m_chunk_pos(chunk_pos), m_world(world) { : m_temp_chunk(temp_chunk), m_chunk_pos(chunk_pos), m_world(world) {
for (int i = 0; i < VERTEX_DATA_SUM; i++) { for (int i = 0; i < VERTEX_DATA_SUM; i++) {
m_vertex_data.emplace_back(m_world); m_vertex_data.emplace_back(m_world);
} }
@@ -466,7 +466,7 @@ void Chunk::gen_chunk() {
} }
std::vector<Chunk> neighbor; std::vector<Chunk> neighbor;
for (int i = 0; i < 4; i++) { 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) { for (auto& chunk : neighbor) {
chunk.gen_phase_one(); chunk.gen_phase_one();
@@ -488,9 +488,9 @@ void Chunk::gen_chunk() {
neightbor_blocks[i] = neighbor[i].get_chunk_blocks(); neightbor_blocks[i] = neighbor[i].get_chunk_blocks();
} }
gen_vertex_data(neightbor_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()); // Logger::info("Cross Sum {}", m_cross_vertices_sum.load());
bool Chunk::is_temp_chunk() const { return m_temp_chunk.load(); }
} // namespace Cubed } // namespace Cubed

View File

@@ -734,8 +734,10 @@ void ChunkGenerator::generate_cave() {
return; return;
blocks[idx] = 0; blocks[idx] = 0;
}); });
if (!m_chunk.is_temp_chunk()) {
path.clear_chunk(chunk_pos); path.clear_chunk(chunk_pos);
} }
}
} }
void ChunkGenerator::generate_river() { void ChunkGenerator::generate_river() {
@@ -765,8 +767,11 @@ void ChunkGenerator::generate_river() {
} }
blocks[idx] = 7; blocks[idx] = 7;
}); });
if (!m_chunk.is_temp_chunk()) {
path.clear_chunk(chunk_pos); path.clear_chunk(chunk_pos);
} }
}
if (is_river) { if (is_river) {
m_chunk.biome(RIVER); m_chunk.biome(RIVER);
} }

View File

@@ -46,11 +46,13 @@ void RiverWorm::try_to_add_path(const ChunkPos& chunk_pos,
void RiverWorm::cleanup_finished_rivers() { void RiverWorm::cleanup_finished_rivers() {
std::vector<unsigned> finished_keys; std::vector<unsigned> finished_keys;
for (const auto& pair : m_paths) { for (const auto& pair : m_paths) {
if (pair.second.is_finished()) { if (pair.second.is_finished()) {
finished_keys.push_back(pair.first); finished_keys.push_back(pair.first);
} }
} }
for (const auto& key : finished_keys) { for (const auto& key : finished_keys) {
m_paths.erase(key); m_paths.erase(key);
} }
@@ -58,4 +60,5 @@ void RiverWorm::cleanup_finished_rivers() {
int RiverWorm::river_sum() const { return m_paths.size(); } int RiverWorm::river_sum() const { return m_paths.size(); }
float& RiverWorm::river_probability() { return m_probability; } float& RiverWorm::river_probability() { return m_probability; }
} // namespace Cubed } // namespace Cubed