diff --git a/CMakeLists.txt b/CMakeLists.txt index 26bed19..5eb4925 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,7 @@ add_executable(${PROJECT_NAME} src/dev_panel.cpp src/gameplay/biome.cpp src/gameplay/chunk.cpp + src/gameplay/chunk_generator.cpp src/gameplay/player.cpp src/gameplay/tree.cpp src/gameplay/world.cpp diff --git a/include/Cubed/gameplay/biome.hpp b/include/Cubed/gameplay/biome.hpp index 5bc4f93..6ce0c5f 100644 --- a/include/Cubed/gameplay/biome.hpp +++ b/include/Cubed/gameplay/biome.hpp @@ -20,6 +20,7 @@ enum class Biome { NONE }; + struct BiomeHeightRange { int base_y; int amplitude; @@ -31,6 +32,13 @@ struct BiomeNonAdjacent { Biome replace; }; +static inline const std::vector NON_ADJACENT {{ + {Biome::PLAIN, {Biome::NONE}, Biome::PLAIN}, + {Biome::FOREST, {Biome::DESERT}, Biome::PLAIN}, + {Biome::DESERT, {Biome::MOUNTAIN, Biome::FOREST}, Biome::PLAIN}, + {Biome::MOUNTAIN, {Biome::DESERT}, Biome::PLAIN} +}}; + struct BaseBiomeParams { Biome biome; std::pair temp; diff --git a/include/Cubed/gameplay/chunk.hpp b/include/Cubed/gameplay/chunk.hpp index d7b497a..4b9469d 100644 --- a/include/Cubed/gameplay/chunk.hpp +++ b/include/Cubed/gameplay/chunk.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -19,22 +20,17 @@ private: static constexpr int SIZE_X = CHUCK_SIZE; static constexpr int SIZE_Y = WORLD_SIZE_Y; static constexpr int SIZE_Z = CHUCK_SIZE; - - static inline const std::vector NON_ADJACENT {{ - {Biome::PLAIN, {Biome::NONE}, Biome::PLAIN}, - {Biome::FOREST, {Biome::DESERT}, Biome::PLAIN}, - {Biome::DESERT, {Biome::MOUNTAIN, Biome::FOREST}, Biome::PLAIN}, - {Biome::MOUNTAIN, {Biome::DESERT}, Biome::PLAIN} - } - }; + using HeightMapArray = std::array, SIZE_X>; std::atomic m_dirty {false}; std::atomic m_need_upload{true}; std::atomic m_is_on_gen_vertex_data {false}; std::atomic m_vertex_sum = 0; + std::atomic m_biome = Biome::PLAIN; std::mutex m_vertexs_data_mutex; - std::atomic m_biome = Biome::PLAIN; + std::unique_ptr m_generator; + ChunkPos m_chunk_pos; World& m_world; HeightMapArray m_heightmap; @@ -57,25 +53,25 @@ public: Chunk& operator=(Chunk&&) noexcept; Biome get_biome() const; - + ChunkPos get_chunk_pos() const; const std::vector& get_chunk_blocks() const; HeightMapArray get_heightmap() const; static int get_index(int x, int y, int z); static int get_index(const glm::vec3& pos); // Init Chunk - // Generate Biome + // Determine biome from temperature and humidity noise void gen_phase_one(); - // Adjust Biome + // Resolve biome adjacency conflicts with neighbor chunks void gen_phase_two(const std::array& adj_chunks); - // Generate Heightmap + // Generate heightmap using biome-specific noise void gen_phase_three(); - // Adjust Height + // Blend heightmap with neighbors for smooth transitions void gen_phase_four(const std::array, 4>& neighbor_heightmap); - // Generate Block + // Generate terrain blocks from heightmap and biome void gen_phase_five(); - // Adjust Block; + // Blend surface blocks at chunk borders with neighbors void gen_phase_six(const std::array>, 4>& neighbor_block); - // Generate Structure + // Generate biome-specific vegetation/structures void gen_phase_seven(); //void gen_vertex_data(); // 0 : (1, 0) @@ -96,6 +92,11 @@ public: void set_chunk_block(int index, unsigned id); + ChunkPos chunk_pos() const; + Biome biome() const; + void biome(Biome b); + HeightMapArray& heightmap(); + std::vector& blocks(); }; diff --git a/include/Cubed/gameplay/chunk_generator.hpp b/include/Cubed/gameplay/chunk_generator.hpp new file mode 100644 index 0000000..2c4d4cb --- /dev/null +++ b/include/Cubed/gameplay/chunk_generator.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include + +#include +#include +namespace Cubed { + + +class Chunk; + +class ChunkGenerator { + static constexpr int SIZE_X = CHUCK_SIZE; + static constexpr int SIZE_Y = WORLD_SIZE_Y; + static constexpr int SIZE_Z = CHUCK_SIZE; + using HeightMapArray = std::array, CHUCK_SIZE>; +public: + ChunkGenerator(Chunk& chunk); + + static void init(); + static void reload(); + static const unsigned& seed(); + static void seed(unsigned s); + + // Generate Biome + void assign_chunk_biome(); + // Adjust Biome + void resolve_biome_adjacency_conflict(const std::array& adj_chunks); + // Generate Heightmap + void generate_heightmap(); + // Adjust Height + void blend_heightmap_boundaries(const std::array, 4>& neighbor_heightmap); + // Generate Block + void generate_terrain_blocks(); + // Adjust Block; + void blend_surface_blocks_borders(const std::array>, 4>& neighbor_block); + // Generate Structure + void generate_vegetation(); + +private: + static inline std::atomic is_init {false}; + static inline unsigned m_generator_seed {0}; + static inline std::atomic is_seed_change {false}; + Chunk& m_chunk; + Random m_random; +}; + + +} \ No newline at end of file diff --git a/include/Cubed/tools/cubed_hash.hpp b/include/Cubed/tools/cubed_hash.hpp index 8a3b6da..3adfed4 100644 --- a/include/Cubed/tools/cubed_hash.hpp +++ b/include/Cubed/tools/cubed_hash.hpp @@ -1,6 +1,6 @@ #pragma once #include - +#include namespace Cubed { @@ -8,7 +8,26 @@ namespace HASH { inline std::size_t str(std::string_view value) { return std::hash{}(value); } - + inline uint32_t mix_hash(int32_t a, int32_t b, uint32_t fixed_seed) { + uint32_t h = fixed_seed; + + h ^= (uint32_t)a * 0xcc9e2d51u; + h = (h << 15) | (h >> 17); // rotl 15 + h *= 0x1b873593u; + + h ^= (uint32_t)b * 0xcc9e2d51u; + h = (h << 15) | (h >> 17); // rotl 15 + h *= 0x1b873593u; + + // Finalization(avalanche) + h ^= h >> 16; + h *= 0x85ebca6bu; + h ^= h >> 13; + h *= 0xc2b2ae35u; + h ^= h >> 16; + + return h; + } } } \ No newline at end of file diff --git a/include/Cubed/tools/cubed_random.hpp b/include/Cubed/tools/cubed_random.hpp index 3d7e6f8..c7cfb0a 100644 --- a/include/Cubed/tools/cubed_random.hpp +++ b/include/Cubed/tools/cubed_random.hpp @@ -4,16 +4,15 @@ namespace Cubed { class Random { public: - static unsigned get_base_seed(); - static unsigned get_thread_seed(); - static Random& get(); + Random(); bool random_bool(double probability); std::mt19937& engine(); unsigned seed(); + void init(unsigned seed); + private: - Random(); unsigned int m_seed = 0; std::mt19937 m_engine; }; diff --git a/include/Cubed/tools/perlin_noise.hpp b/include/Cubed/tools/perlin_noise.hpp index 426497d..d28d21f 100644 --- a/include/Cubed/tools/perlin_noise.hpp +++ b/include/Cubed/tools/perlin_noise.hpp @@ -7,16 +7,12 @@ namespace Cubed { class PerlinNoise { public: - static void init(); + static void init(unsigned seed); static float noise(float x, float y, float z); - static void reload(); - static const unsigned& seed(); - static void seed(unsigned seed); + static void reload(unsigned seed); private: static inline std::atomic is_init = false; static inline std::vector p; - static inline unsigned m_seed = 0; - static inline bool is_seed_change = false; static float fade(float t); static float lerp(float t, float a, float b); static float grad(int hash, float x, float y, float z); diff --git a/src/app.cpp b/src/app.cpp index b43fa6a..d418421 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -52,7 +52,7 @@ void App::init() { glfwSetScrollCallback(m_window.get_glfw_window(), mouse_scroll_callback); glfwSetCursorEnterCallback(m_window.get_glfw_window(), cursor_enter_callback); glfwSetCharCallback(m_window.get_glfw_window(), char_callback); - PerlinNoise::init(); + ChunkGenerator::init(); m_renderer.init(); Logger::info("Renderer Init Success"); diff --git a/src/dev_panel.cpp b/src/dev_panel.cpp index 2089b37..54f5c08 100644 --- a/src/dev_panel.cpp +++ b/src/dev_panel.cpp @@ -212,12 +212,15 @@ void DevPanel::show_world_tab_item() { ImGuiInputTextFlags_EnterReturnsTrue, filter_unsigned)) { - PerlinNoise::seed(static_cast(std::strtoul(perlin_noise_input_buffer, nullptr, 10))); + ChunkGenerator::seed(static_cast(std::strtoul(perlin_noise_input_buffer, nullptr, 10))); m_text_editing.perlin_seed = false; + m_player->set_player_pos({0.0f, 255.0f, 0.0f}); + m_app.world().rebuild_world(); + } } if (!m_text_editing.perlin_seed) { - ImGui::Text("Perlin Noise Seed: %u", PerlinNoise::seed()); + ImGui::Text("ChunkGenerator Seed: %u", ChunkGenerator::seed()); if (ImGui::IsItemClicked()) { m_text_editing.perlin_seed = true; } diff --git a/src/gameplay/chunk.cpp b/src/gameplay/chunk.cpp index c1bfeaa..dd60995 100644 --- a/src/gameplay/chunk.cpp +++ b/src/gameplay/chunk.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -7,7 +6,6 @@ #include #include -#include #include namespace Cubed { @@ -62,6 +60,10 @@ Biome Chunk::get_biome() const { return m_biome.load(); } +ChunkPos Chunk::get_chunk_pos() const { + return m_chunk_pos; +} + const std::vector& Chunk::get_chunk_blocks() const{ return m_blocks; } @@ -214,290 +216,62 @@ size_t Chunk::get_vertex_sum() const { void Chunk::gen_phase_one() { - float x = static_cast(m_chunk_pos.x); - float z = static_cast(m_chunk_pos.z); - float temp = PerlinNoise::noise(x * BIOME_NOISE_FREQUENCY, 0.0f, z * BIOME_NOISE_FREQUENCY); - float humid = PerlinNoise::noise(x * BIOME_NOISE_FREQUENCY, 1.0f, z * BIOME_NOISE_FREQUENCY); - m_biome = get_biome_from_noise(temp, humid); + m_generator = std::make_unique(*this); + if (!m_generator) { + Logger::error("ChunkGenerator is Nullptr"); + return; + } + m_generator->assign_chunk_biome(); } void Chunk::gen_phase_two(const std::array& adj_chunks) { - for (auto& chunk : adj_chunks) { - if (chunk == nullptr) { - continue; - } - Biome biome = chunk->get_biome(); - for (const auto& non : NON_ADJACENT) { - if (m_biome != non.first) { - continue; - } - for (auto b : non.second) { - if (b == biome) { - m_biome = non.replace; - return; - } - } - } + if (!m_generator) { + Logger::error("ChunkGenerator is Nullptr"); + return; } + m_generator->resolve_biome_adjacency_conflict(adj_chunks); } void Chunk::gen_phase_three() { - for (int x = 0; x < CHUCK_SIZE; x++) { - for (int z = 0; z < CHUCK_SIZE; z++) { - - float world_x = static_cast(x + m_chunk_pos.x * CHUCK_SIZE); - float world_z = static_cast(z + m_chunk_pos.z * CHUCK_SIZE); - - auto sample_height = [&](Biome b) -> float { - auto range = get_biome_height_range(b); - auto [f1, f2, f3] = get_noise_frequencies_for_biome(b); - float n = - 1.00f * PerlinNoise::noise(world_x * f1, 0.5f, world_z * f1) + - 0.50f * PerlinNoise::noise(world_x * f2, 0.5f, world_z * f2) + - 0.25f * PerlinNoise::noise(world_x * f3, 0.5f, world_z * f3); - n /= 1.75f; - return range.base_y + n * range.amplitude; - }; - m_heightmap[x][z] = sample_height(m_biome); - } + if (!m_generator) { + Logger::error("ChunkGenerator is Nullptr"); + return; } + m_generator->generate_heightmap(); } void Chunk::gen_phase_four(const std::array, 4>& neighbor_heightmap) { - // Width of interpolation influence (in number of cells) - constexpr int BLEND_RADIUS = 12; - - for (int x = 0; x < SIZE_X; x++) { - for (int z = 0; z < SIZE_Z; z++) { - float h = static_cast(m_heightmap[x][z]); - float total_weight = 1.0f; - float blended = h; - - // --- Right neighbor neighbor[0]: (1, 0) --- - // Blend when x is close to SIZE_X-1 - if (neighbor_heightmap[0] != std::nullopt) { - int dist = (SIZE_X - 1) - x; // distance from right border - if (dist < BLEND_RADIUS) { - // Neighbor's boundary row is its x=0 column - float neighbor_h = static_cast((*neighbor_heightmap[0])[0][z]); - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; // larger weight when closer - // Use smoothstep for a more natural transition - t = t * t * (3.0f - 2.0f * t); - blended += t * neighbor_h; - total_weight += t; - } - } - - // --- Left neighbor neighbor[1]: (-1, 0) --- - if (neighbor_heightmap[1] != std::nullopt) { - int dist = x; // distance from left border - if (dist < BLEND_RADIUS) { - float neighbor_h = static_cast((*neighbor_heightmap[1])[SIZE_X - 1][z]); - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; - t = t * t * (3.0f - 2.0f * t); - blended += t * neighbor_h; - total_weight += t; - } - } - - // --- Front neighbor neighbor[2]: (0, 1) --- - if (neighbor_heightmap[2] != std::nullopt) { - int dist = (SIZE_Z - 1) - z; - if (dist < BLEND_RADIUS) { - float neighbor_h = static_cast((*neighbor_heightmap[2])[x][0]); - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; - t = t * t * (3.0f - 2.0f * t); - blended += t * neighbor_h; - total_weight += t; - } - } - - // --- Back neighbor neighbor[3]: (0, -1) --- - if (neighbor_heightmap[3] != std::nullopt) { - int dist = z; - if (dist < BLEND_RADIUS) { - float neighbor_h = static_cast((*neighbor_heightmap[3])[x][SIZE_Z - 1]); - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; - t = t * t * (3.0f - 2.0f * t); - blended += t * neighbor_h; - total_weight += t; - } - } - - m_heightmap[x][z] = static_cast(blended / total_weight); - } + if (!m_generator) { + Logger::error("ChunkGenerator is Nullptr"); + return; } + m_generator->blend_heightmap_boundaries(neighbor_heightmap); } void Chunk::gen_phase_five() { - // bottom - m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0); - for (int x = 0; x < CHUCK_SIZE; x++) { - for (int y = 0; y < 5; y++) { - for (int z = 0; z < CHUCK_SIZE; z++) { - m_blocks[get_index(x, y, z)] = 3; - } - } + if (!m_generator) { + Logger::error("ChunkGenerator is Nullptr"); + return; } - - for (int x = 0; x < CHUCK_SIZE; x++) { - for (int z = 0; z < CHUCK_SIZE; z++) { - int height = static_cast(m_heightmap[x][z]); - for (int y = 5; y < height - 5; y++) { - m_blocks[get_index(x, y, z)] = 3; - } - if (m_biome == Biome::MOUNTAIN) { - for (int y = height - 5; y <= height - 1; y++) { - if (y > 110) { - m_blocks[get_index(x, y, z)] = 3; - } else { - m_blocks[get_index(x, y, z)] = 2; - } - - } - if (height > 110) { - m_blocks[get_index(x, height - 1, z)] = 3; - } else { - m_blocks[get_index(x, height - 1, z)] = 1; - } - } else if (m_biome == Biome::DESERT) { - for (int y = height - 5; y <= height; y++) { - m_blocks[get_index(x, y, z)] = 4; - } - } else { - for (int y = height - 5; y <= height - 1; y++) { - m_blocks[get_index(x, y, z)] = 2; - } - for (int y = height; y <= height; y++) { - m_blocks[get_index(x, y, z)] = 1; - } - } - } - } - + m_generator->generate_terrain_blocks(); } void Chunk::gen_phase_six(const std::array>, 4>& neighbor_block) { - constexpr int BLEND_RADIUS = 12; - constexpr int WORLD_HEIGHT = WORLD_SIZE_Y; - - // Helper lambda: get top block type from a neighbor's block data at (nx, nz) - auto get_top_block_from_neighbor = [&](const std::vector& blocks, int nx, int nz) -> uint8_t { - // Search from topmost y downwards for the first non-zero block - for (int y = WORLD_HEIGHT - 1; y >= 0; --y) { - int idx = get_index(nx, y, nz); // linear index: y * area + z * size + x - if (idx >= 0 && idx < static_cast(blocks.size()) && blocks[idx] != 0) { - return blocks[idx]; - } - } - return 0; // fallback, should not happen for valid chunks - }; - - // For each column (x, z) - for (int x = 0; x < CHUCK_SIZE; ++x) { - for (int z = 0; z < CHUCK_SIZE; ++z) { - // Get the current top block type of this column from m_blocks - uint8_t type_self = 0; - int top_y = -1; - top_y = m_heightmap[x][z]; - type_self = m_blocks[get_index(x, top_y, z)]; - - if (top_y == -1) continue; // no block? skip - - // Weight map: type -> total weight - std::unordered_map weights; - weights[type_self] = 1.0f; // self weight - - // --- Right neighbor (index 0) --- - if (neighbor_block[0] && x >= CHUCK_SIZE - BLEND_RADIUS) { - int dist = (CHUCK_SIZE - 1) - x; - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; - t = t * t * (3.0f - 2.0f * t); // smoothstep - if (t > 0.0f) { - uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[0], 0, z); - weights[type_neighbor] += t; - } - } - - // --- Left neighbor (index 1) --- - if (neighbor_block[1] && x < BLEND_RADIUS) { - int dist = x; - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; - t = t * t * (3.0f - 2.0f * t); - if (t > 0.0f) { - uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[1], CHUCK_SIZE - 1, z); - weights[type_neighbor] += t; - } - } - - // --- Front neighbor (index 2) --- - if (neighbor_block[2] && z >= CHUCK_SIZE - BLEND_RADIUS) { - int dist = (CHUCK_SIZE - 1) - z; - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; - t = t * t * (3.0f - 2.0f * t); - if (t > 0.0f) { - uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[2], x, 0); - weights[type_neighbor] += t; - } - } - - // --- Back neighbor (index 3) --- - if (neighbor_block[3] && z < BLEND_RADIUS) { - int dist = z; - float t = 1.0f - static_cast(dist) / BLEND_RADIUS; - t = t * t * (3.0f - 2.0f * t); - if (t > 0.0f) { - uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[3], x, CHUCK_SIZE - 1); - weights[type_neighbor] += t; - } - } - - // Find type with maximum total weight - uint8_t final_type = type_self; - float max_weight = weights[type_self]; - for (const auto& [type, w] : weights) { - if (w > max_weight) { - max_weight = w; - final_type = type; - } - } - - // Update the top block if the type changed - if (final_type != type_self) { - m_blocks[get_index(x, top_y, z)] = final_type; - unsigned fill_type = 2; - if (final_type == 1) { - fill_type = 2; - } else if (final_type == 4) { - fill_type = 4; - } - for (int y = top_y - 5; y < top_y; y++) { - m_blocks[get_index(x, y, z)] = fill_type; - } - } - } + if (!m_generator) { + Logger::error("ChunkGenerator is Nullptr"); + return; } + m_generator->blend_surface_blocks_borders(neighbor_block); } void Chunk::gen_phase_seven() { - if (m_biome == Biome::FOREST) { - std::array x_arr; - std::iota(x_arr.begin(), x_arr.end(), 0); - std::shuffle(x_arr.begin(), x_arr.end(), Cubed::Random::get().engine()); - std::array z_arr; - std::iota(z_arr.begin(), z_arr.end(), 0); - std::shuffle(z_arr.begin(), z_arr.end(), Cubed::Random::get().engine()); - for (auto x : x_arr) { - for (auto z : z_arr) { - if (Cubed::Random::get().random_bool(forest_params().tree_frequency)) { - build_tree(*this, {x, static_cast(m_heightmap[x][z]), z}); - } - - } - } + if (!m_generator) { + Logger::error("ChunkGenerator is Nullptr"); + return; } - + m_generator->generate_vegetation(); mark_dirty(); + m_generator = nullptr; } void Chunk::upload_to_gpu() { @@ -540,4 +314,22 @@ void Chunk::set_chunk_block(int index ,unsigned id) { mark_dirty(); } +ChunkPos Chunk::chunk_pos() const{ + return m_chunk_pos; +} + +Biome Chunk::biome() const{ + return m_biome; +} + +void Chunk::biome(Biome b) { + m_biome = b; +} + +HeightMapArray& Chunk::heightmap() { + return m_heightmap; +} +std::vector& Chunk::blocks() { + return m_blocks; +} } diff --git a/src/gameplay/chunk_generator.cpp b/src/gameplay/chunk_generator.cpp new file mode 100644 index 0000000..82029b6 --- /dev/null +++ b/src/gameplay/chunk_generator.cpp @@ -0,0 +1,347 @@ +#include + +#include +#include +#include +#include +namespace Cubed { + +ChunkGenerator::ChunkGenerator(Chunk& chunk) : + m_chunk(chunk) +{ + ASSERT_MSG(is_init, "ChunksGenerator is not init"); + ChunkPos pos = m_chunk.get_chunk_pos(); + unsigned seed = HASH::mix_hash(pos.x, pos.z, m_generator_seed); + m_random.init(seed); +} + +void ChunkGenerator::init() { + std::random_device d; + m_generator_seed = d(); + Logger::info("Chunk Generator Seed {}", m_generator_seed); + PerlinNoise::init(m_generator_seed); + is_init = true; +} + +void ChunkGenerator::reload() { + if (!is_seed_change) { + return; + } + PerlinNoise::reload(m_generator_seed); + is_seed_change = false; +} + +const unsigned& ChunkGenerator::seed() { + return m_generator_seed; +} +void ChunkGenerator::seed(unsigned s) { + is_seed_change = true; + m_generator_seed = s; +} + + +void ChunkGenerator::assign_chunk_biome() { + auto m_chunk_pos = m_chunk.chunk_pos(); + float x = static_cast(m_chunk_pos.x); + float z = static_cast(m_chunk_pos.z); + float temp = PerlinNoise::noise(x * BIOME_NOISE_FREQUENCY, 0.0f, z * BIOME_NOISE_FREQUENCY); + float humid = PerlinNoise::noise(x * BIOME_NOISE_FREQUENCY, 1.0f, z * BIOME_NOISE_FREQUENCY); + auto biome = get_biome_from_noise(temp, humid); + m_chunk.biome(biome); +} + +void ChunkGenerator::resolve_biome_adjacency_conflict(const std::array& adj_chunks) { + auto m_biome = m_chunk.biome(); + for (auto& chunk : adj_chunks) { + if (chunk == nullptr) { + continue; + } + Biome biome = chunk->get_biome(); + for (const auto& non : NON_ADJACENT) { + if (m_biome != non.first) { + continue; + } + for (auto b : non.second) { + if (b == biome) { + m_biome = non.replace; + return; + } + } + } + } +} + +void ChunkGenerator::generate_heightmap() { + + auto m_chunk_pos = m_chunk.chunk_pos(); + auto& m_heightmap = m_chunk.heightmap(); + auto m_biome = m_chunk.biome(); + + for (int x = 0; x < CHUCK_SIZE; x++) { + for (int z = 0; z < CHUCK_SIZE; z++) { + + float world_x = static_cast(x + m_chunk_pos.x * CHUCK_SIZE); + float world_z = static_cast(z + m_chunk_pos.z * CHUCK_SIZE); + + auto sample_height = [&](Biome b) -> float { + auto range = get_biome_height_range(b); + auto [f1, f2, f3] = get_noise_frequencies_for_biome(b); + float n = + 1.00f * PerlinNoise::noise(world_x * f1, 0.5f, world_z * f1) + + 0.50f * PerlinNoise::noise(world_x * f2, 0.5f, world_z * f2) + + 0.25f * PerlinNoise::noise(world_x * f3, 0.5f, world_z * f3); + n /= 1.75f; + return range.base_y + n * range.amplitude; + }; + m_heightmap[x][z] = sample_height(m_biome); + } + } +} + +void ChunkGenerator::blend_heightmap_boundaries(const std::array, 4>& neighbor_heightmap) { + auto& m_heightmap = m_chunk.heightmap(); + + // Width of interpolation influence (in number of cells) + constexpr int BLEND_RADIUS = 12; + + for (int x = 0; x < SIZE_X; x++) { + for (int z = 0; z < SIZE_Z; z++) { + float h = static_cast(m_heightmap[x][z]); + float total_weight = 1.0f; + float blended = h; + + // --- Right neighbor neighbor[0]: (1, 0) --- + // Blend when x is close to SIZE_X-1 + if (neighbor_heightmap[0] != std::nullopt) { + int dist = (SIZE_X - 1) - x; // distance from right border + if (dist < BLEND_RADIUS) { + // Neighbor's boundary row is its x=0 column + float neighbor_h = static_cast((*neighbor_heightmap[0])[0][z]); + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; // larger weight when closer + // Use smoothstep for a more natural transition + t = t * t * (3.0f - 2.0f * t); + blended += t * neighbor_h; + total_weight += t; + } + } + + // --- Left neighbor neighbor[1]: (-1, 0) --- + if (neighbor_heightmap[1] != std::nullopt) { + int dist = x; // distance from left border + if (dist < BLEND_RADIUS) { + float neighbor_h = static_cast((*neighbor_heightmap[1])[SIZE_X - 1][z]); + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; + t = t * t * (3.0f - 2.0f * t); + blended += t * neighbor_h; + total_weight += t; + } + } + + // --- Front neighbor neighbor[2]: (0, 1) --- + if (neighbor_heightmap[2] != std::nullopt) { + int dist = (SIZE_Z - 1) - z; + if (dist < BLEND_RADIUS) { + float neighbor_h = static_cast((*neighbor_heightmap[2])[x][0]); + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; + t = t * t * (3.0f - 2.0f * t); + blended += t * neighbor_h; + total_weight += t; + } + } + + // --- Back neighbor neighbor[3]: (0, -1) --- + if (neighbor_heightmap[3] != std::nullopt) { + int dist = z; + if (dist < BLEND_RADIUS) { + float neighbor_h = static_cast((*neighbor_heightmap[3])[x][SIZE_Z - 1]); + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; + t = t * t * (3.0f - 2.0f * t); + blended += t * neighbor_h; + total_weight += t; + } + } + + m_heightmap[x][z] = static_cast(blended / total_weight); + } + } +} + +void ChunkGenerator::generate_terrain_blocks() { + auto& m_blocks = m_chunk.blocks(); + auto& m_heightmap = m_chunk.heightmap(); + auto m_biome = m_chunk.biome(); + // bottom + m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0); + for (int x = 0; x < CHUCK_SIZE; x++) { + for (int y = 0; y < 5; y++) { + for (int z = 0; z < CHUCK_SIZE; z++) { + m_blocks[Chunk::get_index(x, y, z)] = 3; + } + } + } + + for (int x = 0; x < CHUCK_SIZE; x++) { + for (int z = 0; z < CHUCK_SIZE; z++) { + int height = static_cast(m_heightmap[x][z]); + for (int y = 5; y < height - 5; y++) { + m_blocks[Chunk::get_index(x, y, z)] = 3; + } + if (m_biome == Biome::MOUNTAIN) { + for (int y = height - 5; y <= height - 1; y++) { + if (y > 110) { + m_blocks[Chunk::get_index(x, y, z)] = 3; + } else { + m_blocks[Chunk::get_index(x, y, z)] = 2; + } + + } + if (height > 110) { + m_blocks[Chunk::get_index(x, height - 1, z)] = 3; + } else { + m_blocks[Chunk::get_index(x, height - 1, z)] = 1; + } + } else if (m_biome == Biome::DESERT) { + for (int y = height - 5; y <= height; y++) { + m_blocks[Chunk::get_index(x, y, z)] = 4; + } + } else { + for (int y = height - 5; y <= height - 1; y++) { + m_blocks[Chunk::get_index(x, y, z)] = 2; + } + for (int y = height; y <= height; y++) { + m_blocks[Chunk::get_index(x, y, z)] = 1; + } + } + } + } +} + +void ChunkGenerator::blend_surface_blocks_borders(const std::array>, 4>& neighbor_block) { + auto& m_blocks = m_chunk.blocks(); + auto& m_heightmap = m_chunk.heightmap(); + + constexpr int BLEND_RADIUS = 12; + constexpr int WORLD_HEIGHT = WORLD_SIZE_Y; + + // Helper lambda: get top block type from a neighbor's block data at (nx, nz) + auto get_top_block_from_neighbor = [&](const std::vector& blocks, int nx, int nz) -> uint8_t { + // 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(nx, y, nz); // linear index: y * area + z * size + x + if (idx >= 0 && idx < static_cast(blocks.size()) && blocks[idx] != 0) { + return blocks[idx]; + } + } + return 0; // fallback, should not happen for valid chunks + }; + + // For each column (x, z) + for (int x = 0; x < CHUCK_SIZE; ++x) { + for (int z = 0; z < CHUCK_SIZE; ++z) { + // Get the current top block type of this column from m_blocks + uint8_t type_self = 0; + int top_y = -1; + top_y = m_heightmap[x][z]; + type_self = m_blocks[Chunk::get_index(x, top_y, z)]; + + if (top_y == -1) continue; // no block? skip + + // Weight map: type -> total weight + std::unordered_map weights; + weights[type_self] = 1.0f; // self weight + + // --- Right neighbor (index 0) --- + if (neighbor_block[0] && x >= CHUCK_SIZE - BLEND_RADIUS) { + int dist = (CHUCK_SIZE - 1) - x; + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; + t = t * t * (3.0f - 2.0f * t); // smoothstep + if (t > 0.0f) { + uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[0], 0, z); + weights[type_neighbor] += t; + } + } + + // --- Left neighbor (index 1) --- + if (neighbor_block[1] && x < BLEND_RADIUS) { + int dist = x; + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; + t = t * t * (3.0f - 2.0f * t); + if (t > 0.0f) { + uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[1], CHUCK_SIZE - 1, z); + weights[type_neighbor] += t; + } + } + + // --- Front neighbor (index 2) --- + if (neighbor_block[2] && z >= CHUCK_SIZE - BLEND_RADIUS) { + int dist = (CHUCK_SIZE - 1) - z; + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; + t = t * t * (3.0f - 2.0f * t); + if (t > 0.0f) { + uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[2], x, 0); + weights[type_neighbor] += t; + } + } + + // --- Back neighbor (index 3) --- + if (neighbor_block[3] && z < BLEND_RADIUS) { + int dist = z; + float t = 1.0f - static_cast(dist) / BLEND_RADIUS; + t = t * t * (3.0f - 2.0f * t); + if (t > 0.0f) { + uint8_t type_neighbor = get_top_block_from_neighbor(*neighbor_block[3], x, CHUCK_SIZE - 1); + weights[type_neighbor] += t; + } + } + + // Find type with maximum total weight + uint8_t final_type = type_self; + float max_weight = weights[type_self]; + for (const auto& [type, w] : weights) { + if (w > max_weight) { + max_weight = w; + final_type = type; + } + } + + // Update the top block if the type changed + if (final_type != type_self) { + m_blocks[Chunk::get_index(x, top_y, z)] = final_type; + unsigned fill_type = 2; + if (final_type == 1) { + fill_type = 2; + } else if (final_type == 4) { + fill_type = 4; + } + for (int y = top_y - 5; y < top_y; y++) { + m_blocks[Chunk::get_index(x, y, z)] = fill_type; + } + } + } + } +} + +void ChunkGenerator::generate_vegetation() { + auto m_biome = m_chunk.biome(); + auto& m_heightmap = m_chunk.heightmap(); + if (m_biome == Biome::FOREST) { + std::array x_arr; + std::iota(x_arr.begin(), x_arr.end(), 0); + std::shuffle(x_arr.begin(), x_arr.end(), m_random.engine()); + std::array z_arr; + std::iota(z_arr.begin(), z_arr.end(), 0); + std::shuffle(z_arr.begin(), z_arr.end(), m_random.engine()); + for (auto x : x_arr) { + for (auto z : z_arr) { + if (m_random.random_bool(forest_params().tree_frequency)) { + build_tree(m_chunk, {x, static_cast(m_heightmap[x][z]), z}); + } + + } + } + } +} + + + +} \ No newline at end of file diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index a1cf37e..b7cd755 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -722,7 +722,7 @@ void World::rebuild_world() { m_new_chunk_queue.clear(); } m_could_gen = true; - PerlinNoise::reload(); + ChunkGenerator::reload(); start_gen_thread(); need_gen(); diff --git a/src/main.cpp b/src/main.cpp index 8d70a54..54d1a9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,10 @@ #include int main(int argc, char** argv) { + + static_assert(sizeof(int) == sizeof(int32_t)); + static_assert(sizeof(unsigned int) == sizeof(uint32_t)); + return Cubed::App::start_cubed_application(argc, argv); } diff --git a/src/tools/cubed_random.cpp b/src/tools/cubed_random.cpp index 04c363d..f0f6a59 100644 --- a/src/tools/cubed_random.cpp +++ b/src/tools/cubed_random.cpp @@ -2,33 +2,11 @@ #include -#include namespace Cubed { -unsigned Random::get_base_seed() { - static unsigned base = [] { - std::random_device rd; - return rd(); - }(); - return base; -} - -unsigned Random::get_thread_seed() { - static std::atomic counter{0}; - thread_local static unsigned seed = get_base_seed() + counter.fetch_add(1); - return seed; -} - Random::Random() { - m_seed = get_thread_seed(); - Logger::info("Seed: {}", m_seed); - m_engine.seed(m_seed); -} - -Random& Random::get() { - thread_local Random instance; - return instance; + } bool Random::random_bool(double probability) { @@ -44,6 +22,9 @@ unsigned Random::seed() { return m_seed; } - +void Random::init(unsigned seed) { + m_seed = seed; + m_engine.seed(seed); +} } \ No newline at end of file diff --git a/src/tools/perlin_noise.cpp b/src/tools/perlin_noise.cpp index 2d66ff4..95ee7e9 100644 --- a/src/tools/perlin_noise.cpp +++ b/src/tools/perlin_noise.cpp @@ -10,16 +10,14 @@ namespace Cubed { -void PerlinNoise::init() { +void PerlinNoise::init(unsigned seed) { p.resize(256); std::iota(p.begin(), p.end(), 0); - auto seed = std::random_device{}(); Logger::info("Init Perlin Noise With Seed {}", seed); std::shuffle(p.begin(), p.end(), std::mt19937(seed)); p.insert(p.end(), p.begin(), p.end()); is_init = true; - m_seed = seed; } float PerlinNoise::noise(float x, float y, float z) { @@ -74,27 +72,15 @@ float PerlinNoise::grad(int hash, float x, float y, float z) { return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } -void PerlinNoise::reload() { - if (!is_seed_change) { - Logger::warn("Seed Not Change"); - return; - } +void PerlinNoise::reload(unsigned seed) { is_init = false; p.resize(256); std::iota(p.begin(), p.end(), 0); - Logger::info("Reload Perlin Noise With Seed {}", m_seed); - std::shuffle(p.begin(), p.end(), std::mt19937(m_seed)); + Logger::info("Reload Perlin Noise With Seed {}", seed); + std::shuffle(p.begin(), p.end(), std::mt19937(seed)); p.insert(p.end(), p.begin(), p.end()); is_init = true; - is_seed_change = false; } -const unsigned& PerlinNoise::seed() { - return m_seed; -} -void PerlinNoise::seed(unsigned seed) { - m_seed = seed; - is_seed_change = true; -} } \ No newline at end of file