Compare commits

3 Commits

Author SHA1 Message Date
65bafe9141 fix: safe_int_to_biome not include river 2026-05-01 16:43:58 +08:00
f94ef61d40 fix: duplicate mountain terrain generation 2026-05-01 16:35:52 +08:00
9a4b814f4d feat: add river biome 2026-05-01 16:15:48 +08:00
14 changed files with 127 additions and 23 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 874 B

View File

@@ -4,8 +4,9 @@ namespace Cubed {
constexpr int WORLD_SIZE_Y = 256;
constexpr int CHUCK_SIZE = 16;
constexpr int SEA_LEVEL = 64;
constexpr int MAX_BLOCK_NUM = 7;
constexpr int MAX_BLOCK_NUM = 8;
constexpr int MAX_UI_NUM = 1;
constexpr int MAX_BLOCK_STATUS = 1;
constexpr int MAX_BIOME_SUM = 4;

View File

@@ -12,7 +12,7 @@ constexpr float FOREST_FREQ = 1.2f;
constexpr float DESERT_FREQ = 1.2f;
constexpr float MOUNTAIN_FREQ = 2.0f;
enum class Biome { PLAIN = 0, FOREST, DESERT, MOUNTAIN, NONE };
enum class Biome { PLAIN = 0, FOREST, DESERT, MOUNTAIN, RIVER, NONE };
struct BiomeHeightRange {
int base_y;
@@ -26,10 +26,10 @@ struct BiomeNonAdjacent {
};
static inline const std::vector<BiomeNonAdjacent> 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}}};
{{Biome::PLAIN, {Biome::DESERT}, Biome::RIVER},
{Biome::FOREST, {Biome::DESERT}, Biome::RIVER},
{Biome::DESERT, {Biome::MOUNTAIN, Biome::FOREST}, Biome::RIVER},
{Biome::MOUNTAIN, {Biome::DESERT, Biome::FOREST}, Biome::RIVER}}};
struct BaseBiomeParams {
Biome biome;
@@ -49,6 +49,8 @@ struct DesertParams : public BaseBiomeParams {};
struct MountainParams : public BaseBiomeParams {};
struct RiverParams : public BaseBiomeParams {};
std::string get_biome_str(Biome biome);
Biome get_biome_from_noise(float temp, float humid);
std::array<float, 3> get_noise_frequencies_for_biome(Biome biome);
@@ -61,5 +63,5 @@ PlainParams& plain_params();
ForestParams& forest_params();
DesertParams& desert_params();
MountainParams& mountain_params();
RiverParams& river_params();
} // namespace Cubed

View File

@@ -39,7 +39,7 @@ struct LookBlock {
};
constexpr std::array<std::string_view, MAX_BLOCK_NUM> BLOCK_REISTER{
"air", "grass_block", "dirt", "stone", "sand", "log", "leaf"};
"air", "grass_block", "dirt", "stone", "sand", "log", "leaf", "water"};
const std::array<bool, MAX_BLOCK_NUM> TRANSPARENT_MAP{
true, false, false, false, false, false, true};

View File

@@ -1,6 +1,7 @@
#pragma once
#include "Cubed/constants.hpp"
#include "Cubed/gameplay/biome.hpp"
#include "Cubed/tools/cubed_random.hpp"
#include <atomic>
@@ -49,6 +50,9 @@ private:
static inline std::atomic<bool> is_seed_change{false};
Chunk& m_chunk;
Random m_random;
std::array<Biome, 4> neighbor_biome{Biome::NONE, Biome::NONE, Biome::NONE,
Biome::NONE};
bool is_neighbor_river = false;
};
} // namespace Cubed

View File

@@ -223,6 +223,32 @@ void DevPanel::show_biome_table_bar() {
AMPLITUDE_MIN, AMPLITUDE_MAX);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("River")) {
ImGui::SliderFloat("MinTemp##river", &river_params().temp.first,
TEMP_MIN, TEMP_MAX);
ImGui::SliderFloat("MaxTemp##river", &river_params().temp.second,
TEMP_MIN, TEMP_MAX);
ImGui::SliderFloat("MinHumid##river", &river_params().humid.first,
HUMID_MIN, HUMID_MAX);
ImGui::SliderFloat("MaxHumid##river", &river_params().humid.second,
HUMID_MIN, HUMID_MAX);
ImGui::SliderFloat("Freq One##river",
&river_params().frequencies[0], FREQ1_MIN,
FREQ1_MAX);
ImGui::SliderFloat("Freq Two##river",
&river_params().frequencies[1], FREQ2_MIN,
FREQ2_MAX);
ImGui::SliderFloat("Freq Three##river",
&river_params().frequencies[2], FREQ3_MIN,
FREQ3_MAX);
ImGui::SliderInt("Base Y##river",
&river_params().height_range.base_y,
HEIGHT_BASE_MIN, HEIGHT_BASE_MAX);
ImGui::SliderInt("Amplitude##river",
&river_params().height_range.amplitude,
AMPLITUDE_MIN, AMPLITUDE_MAX);
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
}

View File

@@ -19,7 +19,7 @@ static ForestParams forest{{Biome::FOREST,
{0.5f, 1.0f},
{0.5f, 1.0f},
{0.004f, 0.010f, 0.020f},
{62, 12}},
{62, 8}},
0.1f
};
@@ -36,6 +36,12 @@ static MountainParams mountain{{Biome::MOUNTAIN,
{0.006f, 0.014f, 0.010f},
{70, 70}}};
static RiverParams river{{Biome::RIVER,
{-0.1f, -0.1f},
{-0.1f, -0.1f},
{0.003f, 0.010f, 0.020f},
{50, 6}}};
std::string get_biome_str(Biome biome) {
std::string str;
using enum Biome;
@@ -52,6 +58,9 @@ std::string get_biome_str(Biome biome) {
case MOUNTAIN:
str = "Mountain";
break;
case RIVER:
str = "River";
break;
case NONE:
str = "Unknown";
break;
@@ -109,6 +118,8 @@ std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
return desert.frequencies;
case MOUNTAIN:
return mountain.frequencies;
case RIVER:
return river.frequencies;
case NONE:
ASSERT_MSG(false, "Chunk Biome is None");
throw std::invalid_argument{"Chunk Biome is None"};
@@ -128,6 +139,8 @@ BiomeHeightRange get_biome_height_range(Biome biome) {
return desert.height_range;
case MOUNTAIN:
return mountain.height_range;
case RIVER:
return river.height_range;
case NONE:
ASSERT_MSG(false, "Chunk Biome is None");
throw std::invalid_argument{"Chunk Biome is None"};
@@ -139,7 +152,7 @@ BiomeHeightRange get_biome_height_range(Biome biome) {
Biome safe_int_to_biome(int x) {
using enum Biome;
static const std::unordered_map<int, Biome> INT_TO_BIOME_MAP{
{0, PLAIN}, {1, FOREST}, {2, DESERT}, {3, MOUNTAIN}};
{0, PLAIN}, {1, FOREST}, {2, DESERT}, {3, MOUNTAIN}, {4, RIVER}};
auto it = INT_TO_BIOME_MAP.find(x);
ASSERT_MSG(it != INT_TO_BIOME_MAP.end(), ":Can't Find");
@@ -194,5 +207,5 @@ PlainParams& plain_params() { return plain; }
ForestParams& forest_params() { return forest; }
DesertParams& desert_params() { return desert; }
MountainParams& mountain_params() { return mountain; }
RiverParams& river_params() { return river; }
} // namespace Cubed

View File

@@ -7,7 +7,7 @@
#include <numeric>
namespace Cubed {
constexpr int BLEND_RADIUS = 12;
ChunkGenerator::ChunkGenerator(Chunk& chunk) : m_chunk(chunk) {
ASSERT_MSG(is_init, "ChunksGenerator is not init");
ChunkPos pos = m_chunk.get_chunk_pos();
@@ -52,11 +52,16 @@ void ChunkGenerator::assign_chunk_biome() {
void ChunkGenerator::resolve_biome_adjacency_conflict(
const std::array<const Chunk*, 4>& adj_chunks) {
auto m_biome = m_chunk.biome();
for (auto& chunk : adj_chunks) {
for (int i = 0; i < 4; i++) {
auto& chunk = adj_chunks[i];
if (chunk == nullptr) {
continue;
}
Biome biome = chunk->get_biome();
neighbor_biome[i] = biome;
if (biome == Biome::RIVER) {
is_neighbor_river = true;
}
for (const auto& non : NON_ADJACENT) {
if (m_biome != non.first) {
continue;
@@ -104,9 +109,8 @@ void ChunkGenerator::generate_heightmap() {
void ChunkGenerator::blend_heightmap_boundaries(
const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap) {
auto& m_heightmap = m_chunk.heightmap();
auto m_biome = m_chunk.biome();
// 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++) {
@@ -116,7 +120,8 @@ void ChunkGenerator::blend_heightmap_boundaries(
// --- Right neighbor neighbor[0]: (1, 0) ---
// Blend when x is close to SIZE_X-1
if (neighbor_heightmap[0] != std::nullopt) {
if (neighbor_heightmap[0] != std::nullopt &&
neighbor_biome[0] != m_biome) {
int dist = (SIZE_X - 1) - x; // distance from right border
if (dist < BLEND_RADIUS) {
// Neighbor's boundary row is its x=0 column
@@ -133,7 +138,8 @@ void ChunkGenerator::blend_heightmap_boundaries(
}
// --- Left neighbor neighbor[1]: (-1, 0) ---
if (neighbor_heightmap[1] != std::nullopt) {
if (neighbor_heightmap[1] != std::nullopt &&
neighbor_biome[1] != m_biome) {
int dist = x; // distance from left border
if (dist < BLEND_RADIUS) {
float neighbor_h = static_cast<float>(
@@ -146,7 +152,8 @@ void ChunkGenerator::blend_heightmap_boundaries(
}
// --- Front neighbor neighbor[2]: (0, 1) ---
if (neighbor_heightmap[2] != std::nullopt) {
if (neighbor_heightmap[2] != std::nullopt &&
neighbor_biome[2] != m_biome) {
int dist = (SIZE_Z - 1) - z;
if (dist < BLEND_RADIUS) {
float neighbor_h =
@@ -159,7 +166,8 @@ void ChunkGenerator::blend_heightmap_boundaries(
}
// --- Back neighbor neighbor[3]: (0, -1) ---
if (neighbor_heightmap[3] != std::nullopt) {
if (neighbor_heightmap[3] != std::nullopt &&
neighbor_biome[3] != m_biome) {
int dist = z;
if (dist < BLEND_RADIUS) {
float neighbor_h = static_cast<float>(
@@ -213,6 +221,17 @@ void ChunkGenerator::generate_terrain_blocks() {
for (int y = height - 5; y <= height; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 4;
}
} else if (m_biome == Biome::RIVER) {
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++) {
if (y >= SEA_LEVEL - 1) {
m_blocks[Chunk::get_index(x, y, z)] = 1;
} else {
m_blocks[Chunk::get_index(x, y, z)] = 2;
}
}
} else {
for (int y = height - 5; y <= height - 1; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 2;
@@ -230,7 +249,6 @@ void ChunkGenerator::blend_surface_blocks_borders(
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,
@@ -325,7 +343,19 @@ void ChunkGenerator::blend_surface_blocks_borders(
// Update the top block if the type changed
if (final_type != type_self) {
// top block
if (m_chunk.biome() == Biome::RIVER && final_type == 1) {
final_type = 2;
}
if (is_neighbor_river && final_type == 1) {
if (top_y < SEA_LEVEL) {
final_type = 2;
} else {
final_type = 1;
}
}
m_blocks[Chunk::get_index(x, top_y, z)] = final_type;
// bottom block
unsigned fill_type = 2;
if (final_type == 1) {
fill_type = 2;
@@ -342,6 +372,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
void ChunkGenerator::generate_vegetation() {
auto m_biome = m_chunk.biome();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
if (m_biome == Biome::FOREST) {
std::array<int, SIZE_X> x_arr;
@@ -352,9 +383,36 @@ void ChunkGenerator::generate_vegetation() {
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<int>(m_heightmap[x][z]), z});
int y = static_cast<int>(m_heightmap[x][z]);
if (m_random.random_bool(forest_params().tree_frequency) &&
y >= SEA_LEVEL) {
build_tree(m_chunk, {x, y, z});
}
}
}
}
if (m_biome == Biome::RIVER) {
for (int x = 0; x < SIZE_X; x++) {
for (int z = 0; z < SIZE_Z; z++) {
int height = static_cast<int>(m_heightmap[x][z]);
if (height >= SEA_LEVEL) {
continue;
}
for (int y = height + 1; y < SEA_LEVEL; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 7;
}
}
}
}
if (is_neighbor_river) {
for (int x = 0; x < SIZE_X; x++) {
for (int z = 0; z < SIZE_Z; z++) {
int height = static_cast<int>(m_heightmap[x][z]);
if (height >= SEA_LEVEL) {
continue;
}
for (int y = height + 1; y < SEA_LEVEL; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 7;
}
}
}