feat: add BlockType

This commit is contained in:
2026-05-22 15:57:28 +08:00
parent 1a26474a05
commit b824504502
6 changed files with 34 additions and 28 deletions

View File

@@ -51,7 +51,9 @@ BiomeType Chunk::get_biome() const { return m_biome.load(); }
ChunkPos Chunk::get_chunk_pos() const { return m_chunk_pos; }
const std::vector<uint8_t>& Chunk::get_chunk_blocks() const { return m_blocks; }
const std::vector<BlockType>& Chunk::get_chunk_blocks() const {
return m_blocks;
}
HeightMapArray Chunk::get_heightmap() const {
// Logger::info("Chunk pos {} {} in get_heightmap this {}", m_chunk_pos.x,
@@ -76,7 +78,7 @@ int Chunk::get_index(const glm::vec3& pos) {
}
void Chunk::gen_vertex_data(
const std::array<const std::vector<uint8_t>*, 4>& neighbor_block) {
const std::array<const std::vector<BlockType>*, 4>& neighbor_block) {
if (m_is_on_gen_vertex_data) {
return;
}
@@ -116,7 +118,7 @@ void Chunk::gen_vertex_data(
World::chunk_pos(world_nx, world_nz);
auto is_cull =
[&](const std::vector<uint8_t>* chunk_blocks) {
[&](const std::vector<BlockType>* chunk_blocks) {
if (chunk_blocks == nullptr) {
return false;
}
@@ -252,7 +254,8 @@ void Chunk::gen_phase_five() {
}
void Chunk::gen_phase_six(
const std::array<std::optional<std::vector<uint8_t>>, 4>& neighbor_block) {
const std::array<std::optional<std::vector<BlockType>>, 4>&
neighbor_block) {
if (!m_generator) {
Logger::error("ChunkGenerator is Nullptr");
return;
@@ -308,7 +311,7 @@ BiomeType Chunk::biome() const { return m_biome; }
void Chunk::biome(BiomeType b) { m_biome = b; }
HeightMapArray& Chunk::heightmap() { return m_heightmap; }
std::vector<uint8_t>& Chunk::blocks() { return m_blocks; }
std::vector<BlockType>& Chunk::blocks() { return m_blocks; }
World& Chunk::world() { return m_world; }
unsigned Chunk::seed() const {
if (m_seed == 0) {

View File

@@ -366,7 +366,8 @@ void ChunkGenerator::generate_terrain_blocks() {
}
void ChunkGenerator::blend_surface_blocks_borders(
const std::array<std::optional<std::vector<uint8_t>>, 4>& neighbor_block) {
const std::array<std::optional<std::vector<BlockType>>, 4>&
neighbor_block) {
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
@@ -374,8 +375,8 @@ void ChunkGenerator::blend_surface_blocks_borders(
// Helper lambda: get top block type from a neighbor's block data at (nx,
// nz)
auto get_top_block_from_neighbor = [&](const std::vector<uint8_t>& blocks,
int nx, int nz) -> uint8_t {
auto get_top_block_from_neighbor = [&](const std::vector<BlockType>& blocks,
int nx, int nz) -> BlockType {
// Search from topmost y downwards for the first non-zero block
for (int y = WORLD_HEIGHT - 1; y >= 0; --y) {
int idx = Chunk::get_index(
@@ -392,7 +393,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
for (int x = 0; x < CHUNK_SIZE; ++x) {
for (int z = 0; z < CHUNK_SIZE; ++z) {
// Get the current top block type of this column from m_blocks
uint8_t type_self = 0;
BlockType type_self = 0;
int top_y = -1;
top_y = m_heightmap[x][z];
type_self = m_blocks[Chunk::get_index(x, top_y, z)];
@@ -401,7 +402,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
continue; // no block? skip
// Weight map: type -> total weight
std::unordered_map<uint8_t, float> weights;
std::unordered_map<BlockType, float> weights;
weights[type_self] = 1.0f; // self weight
// --- Right neighbor (index 0) ---
@@ -410,7 +411,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
t = t * t * (3.0f - 2.0f * t); // smoothstep
if (t > 0.0f) {
uint8_t type_neighbor =
BlockType type_neighbor =
get_top_block_from_neighbor(*neighbor_block[0], 0, z);
weights[type_neighbor] += t;
}
@@ -422,7 +423,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
t = t * t * (3.0f - 2.0f * t);
if (t > 0.0f) {
uint8_t type_neighbor = get_top_block_from_neighbor(
BlockType type_neighbor = get_top_block_from_neighbor(
*neighbor_block[1], CHUNK_SIZE - 1, z);
weights[type_neighbor] += t;
}
@@ -434,7 +435,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
t = t * t * (3.0f - 2.0f * t);
if (t > 0.0f) {
uint8_t type_neighbor =
BlockType type_neighbor =
get_top_block_from_neighbor(*neighbor_block[2], x, 0);
weights[type_neighbor] += t;
}
@@ -446,14 +447,14 @@ void ChunkGenerator::blend_surface_blocks_borders(
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
t = t * t * (3.0f - 2.0f * t);
if (t > 0.0f) {
uint8_t type_neighbor = get_top_block_from_neighbor(
BlockType type_neighbor = get_top_block_from_neighbor(
*neighbor_block[3], x, CHUNK_SIZE - 1);
weights[type_neighbor] += t;
}
}
// Find type with maximum total weight
uint8_t final_type = type_self;
BlockType final_type = type_self;
float max_weight = weights[type_self];
for (const auto& [type, w] : weights) {
if (w > max_weight) {

View File

@@ -10,7 +10,7 @@
namespace Cubed {
struct ChunkRenderData {
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
std::array<const std::vector<BlockType>*, 4> neighbor_block;
Chunk* chunk;
};
@@ -230,7 +230,7 @@ void World::init_chunks() {
for (auto& [pos, chunks] : temp_neighbor) {
chunks.gen_phase_five();
}
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_block;
std::array<std::optional<std::vector<BlockType>>, 4> neighbor_block;
for (auto& [pos, chunks] : m_chunks) {
for (int i = 0; i < 4; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
@@ -456,7 +456,7 @@ void World::gen_chunks_internal() {
for (auto& [pos, chunks] : temp_neighbor) {
chunks.gen_phase_five();
}
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_blocks_data;
std::array<std::optional<std::vector<BlockType>>, 4> neighbor_blocks_data;
for (auto& [pos, chunks] : new_chunks) {
{
// std::lock_guard lk(m_chunks_mutex);
@@ -477,7 +477,7 @@ void World::gen_chunks_internal() {
}
m_chunk_gen_fraction = 0.6f;
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
std::array<const std::vector<BlockType>*, 4> neighbor_block;
for (auto& [pos, chunk] : new_chunks) {
for (int i = 0; i < 4; i++) {
auto it = new_chunks_neighbor.find(pos + CHUNK_DIR[i]);
@@ -784,7 +784,7 @@ void World::update(float delta_time) {
for (auto& [pos, chunk] : m_chunks) {
if (chunk.is_dirty()) {
// the curial fator influence
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
std::array<const std::vector<BlockType>*, 4> neighbor_block;
for (int i = 0; i < 4; i++) {
auto it = m_chunks.find(pos + CHUNK_DIR[i]);
if (it != m_chunks.end()) {