refactor: biome build (#5)

* refactor: rename Biome to BiomeType

* feat: add biome builder
This commit is contained in:
zhenyan121
2026-05-02 13:40:46 +08:00
committed by GitHub
parent a63dfa7f47
commit a02bfad639
20 changed files with 563 additions and 164 deletions

View File

@@ -111,6 +111,12 @@ add_executable(${PROJECT_NAME}
src/tools/perlin_noise.cpp src/tools/perlin_noise.cpp
src/ui/text.cpp src/ui/text.cpp
src/window.cpp src/window.cpp
src/gameplay/builders/biome_builder.cpp
src/gameplay/builders/plain_builder.cpp
src/gameplay/builders/mountain_builder.cpp
src/gameplay/builders/river_builder.cpp
src/gameplay/builders/desert_builder.cpp
src/gameplay/builders/forest_builder.cpp
) )
if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(CMAKE_BUILD_TYPE STREQUAL "Debug")

View File

@@ -22,6 +22,9 @@ constexpr float DEFAULT_MAX_RUN_SPEED = 7.0f;
constexpr float DEFAULT_ACCELERATION = 10.0f; constexpr float DEFAULT_ACCELERATION = 10.0f;
constexpr float DEFAULT_DECELERATION = 15.0f; constexpr float DEFAULT_DECELERATION = 15.0f;
constexpr float DEFAULT_G = 22.5f; constexpr float DEFAULT_G = 22.5f;
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<std::array<float, CHUCK_SIZE>, CHUCK_SIZE>; using HeightMapArray = std::array<std::array<float, CHUCK_SIZE>, CHUCK_SIZE>;

View File

@@ -7,12 +7,7 @@ namespace Cubed {
constexpr float BIOME_NOISE_FREQUENCY = 0.03f; constexpr float BIOME_NOISE_FREQUENCY = 0.03f;
constexpr float PLAIN_FREQ = 0.4f; enum class BiomeType { PLAIN = 0, FOREST, DESERT, MOUNTAIN, RIVER, NONE };
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, RIVER, NONE };
struct BiomeHeightRange { struct BiomeHeightRange {
int base_y; int base_y;
@@ -20,19 +15,23 @@ struct BiomeHeightRange {
}; };
struct BiomeNonAdjacent { struct BiomeNonAdjacent {
Biome first; BiomeType first;
std::vector<Biome> second; std::vector<BiomeType> second;
Biome replace; BiomeType replace;
}; };
static inline const std::vector<BiomeNonAdjacent> NON_ADJACENT{ static inline const std::vector<BiomeNonAdjacent> NON_ADJACENT{
{{Biome::PLAIN, {Biome::DESERT}, Biome::RIVER}, {{BiomeType::PLAIN, {BiomeType::DESERT}, BiomeType::RIVER},
{Biome::FOREST, {Biome::DESERT}, Biome::RIVER}, {BiomeType::FOREST, {BiomeType::DESERT}, BiomeType::RIVER},
{Biome::DESERT, {Biome::MOUNTAIN, Biome::FOREST}, Biome::RIVER}, {BiomeType::DESERT,
{Biome::MOUNTAIN, {Biome::DESERT, Biome::FOREST}, Biome::RIVER}}}; {BiomeType::MOUNTAIN, BiomeType::FOREST},
BiomeType::RIVER},
{BiomeType::MOUNTAIN,
{BiomeType::DESERT, BiomeType::FOREST},
BiomeType::RIVER}}};
struct BaseBiomeParams { struct BaseBiomeParams {
Biome biome; BiomeType biome;
std::pair<float, float> temp; std::pair<float, float> temp;
std::pair<float, float> humid; std::pair<float, float> humid;
std::array<float, 3> frequencies; std::array<float, 3> frequencies;
@@ -51,11 +50,11 @@ struct MountainParams : public BaseBiomeParams {};
struct RiverParams : public BaseBiomeParams {}; struct RiverParams : public BaseBiomeParams {};
std::string get_biome_str(Biome biome); std::string get_biome_str(BiomeType biome);
Biome get_biome_from_noise(float temp, float humid); BiomeType get_biome_from_noise(float temp, float humid);
std::array<float, 3> get_noise_frequencies_for_biome(Biome biome); std::array<float, 3> get_noise_frequencies_for_biome(BiomeType biome);
BiomeHeightRange get_biome_height_range(Biome biome); BiomeHeightRange get_biome_height_range(BiomeType biome);
Biome safe_int_to_biome(int x); BiomeType safe_int_to_biome(int x);
int get_interpolated_height(float world_x, float world_z, float temp, int get_interpolated_height(float world_x, float world_z, float temp,
float humid); float humid);
@@ -64,4 +63,5 @@ ForestParams& forest_params();
DesertParams& desert_params(); DesertParams& desert_params();
MountainParams& mountain_params(); MountainParams& mountain_params();
RiverParams& river_params(); RiverParams& river_params();
} // namespace Cubed } // namespace Cubed

View File

@@ -0,0 +1,16 @@
#pragma once
namespace Cubed {
class ChunkGenerator;
class BiomeBuilder {
public:
BiomeBuilder() = default;
virtual ~BiomeBuilder() = default;
virtual ChunkGenerator& get_chunk_generator() = 0;
virtual void build_biome() = 0;
virtual void build_vegetation() = 0;
protected:
void build_bottom();
};
} // namespace Cubed

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Cubed/gameplay/builders/biome_builder.hpp"
namespace Cubed {
class ChunkGenerator;
class DesertBuilder : public BiomeBuilder {
public:
DesertBuilder(ChunkGenerator& chunk_generator);
void build_biome() override;
ChunkGenerator& get_chunk_generator() override;
void build_vegetation() override;
private:
ChunkGenerator& m_chunk_generator;
void build_blocks();
};
} // namespace Cubed

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Cubed/gameplay/builders/biome_builder.hpp"
namespace Cubed {
class ChunkGenerator;
class ForestBuilder : public BiomeBuilder {
public:
ForestBuilder(ChunkGenerator& chunk_generator);
void build_biome() override;
ChunkGenerator& get_chunk_generator() override;
void build_vegetation() override;
private:
ChunkGenerator& m_chunk_generator;
void build_blocks();
};
} // namespace Cubed

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Cubed/gameplay/builders/biome_builder.hpp"
namespace Cubed {
class ChunkGenerator;
class MountainBuilder : public BiomeBuilder {
public:
MountainBuilder(ChunkGenerator& chunk_generator);
void build_biome() override;
ChunkGenerator& get_chunk_generator() override;
void build_vegetation() override;
private:
ChunkGenerator& m_chunk_generator;
void build_blocks();
};
} // namespace Cubed

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Cubed/gameplay/builders/biome_builder.hpp"
namespace Cubed {
class ChunkGenerator;
class PlainBuilder : public BiomeBuilder {
public:
PlainBuilder(ChunkGenerator& chunk_generator);
void build_biome() override;
ChunkGenerator& get_chunk_generator() override;
void build_vegetation() override;
private:
ChunkGenerator& m_chunk_generator;
void build_blocks();
};
} // namespace Cubed

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Cubed/gameplay/builders/biome_builder.hpp"
namespace Cubed {
class ChunkGenerator;
class RiverBuilder : public BiomeBuilder {
public:
RiverBuilder(ChunkGenerator& chunk_generator);
void build_biome() override;
ChunkGenerator& get_chunk_generator() override;
void build_vegetation() override;
private:
ChunkGenerator& m_chunk_generator;
void build_blocks();
};
} // namespace Cubed

View File

@@ -24,7 +24,7 @@ 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<size_t> m_vertex_sum = 0; std::atomic<size_t> m_vertex_sum = 0;
std::atomic<Biome> m_biome = Biome::PLAIN; std::atomic<BiomeType> m_biome = BiomeType::PLAIN;
std::mutex m_vertexs_data_mutex; std::mutex m_vertexs_data_mutex;
std::unique_ptr<ChunkGenerator> m_generator; std::unique_ptr<ChunkGenerator> m_generator;
@@ -50,7 +50,7 @@ public:
Chunk(Chunk&&) noexcept; Chunk(Chunk&&) noexcept;
Chunk& operator=(Chunk&&) noexcept; Chunk& operator=(Chunk&&) noexcept;
Biome get_biome() const; BiomeType get_biome() const;
ChunkPos get_chunk_pos() const; ChunkPos get_chunk_pos() const;
const std::vector<uint8_t>& get_chunk_blocks() const; const std::vector<uint8_t>& get_chunk_blocks() const;
HeightMapArray get_heightmap() const; HeightMapArray get_heightmap() const;
@@ -94,8 +94,8 @@ public:
void set_chunk_block(int index, unsigned id); void set_chunk_block(int index, unsigned id);
ChunkPos chunk_pos() const; ChunkPos chunk_pos() const;
Biome biome() const; BiomeType biome() const;
void biome(Biome b); void biome(BiomeType b);
HeightMapArray& heightmap(); HeightMapArray& heightmap();
std::vector<uint8_t>& blocks(); std::vector<uint8_t>& blocks();
}; };

View File

@@ -2,21 +2,17 @@
#include "Cubed/constants.hpp" #include "Cubed/constants.hpp"
#include "Cubed/gameplay/biome.hpp" #include "Cubed/gameplay/biome.hpp"
#include "Cubed/gameplay/builders/biome_builder.hpp"
#include "Cubed/tools/cubed_random.hpp" #include "Cubed/tools/cubed_random.hpp"
#include <atomic> #include <atomic>
#include <memory>
#include <optional> #include <optional>
namespace Cubed { namespace Cubed {
class Chunk; class Chunk;
class ChunkGenerator { 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<std::array<float, CHUCK_SIZE>, CHUCK_SIZE>;
public: public:
ChunkGenerator(Chunk& chunk); ChunkGenerator(Chunk& chunk);
@@ -44,15 +40,22 @@ public:
// Generate Structure // Generate Structure
void generate_vegetation(); void generate_vegetation();
Chunk& chunk();
Random& random();
bool neighbor_river() const;
private: private:
static inline std::atomic<bool> is_init{false}; static inline std::atomic<bool> is_init{false};
static inline unsigned m_generator_seed{0}; static inline unsigned m_generator_seed{0};
static inline std::atomic<bool> is_seed_change{false}; static inline std::atomic<bool> is_seed_change{false};
Chunk& m_chunk; Chunk& m_chunk;
Random m_random; Random m_random;
std::array<Biome, 4> neighbor_biome{Biome::NONE, Biome::NONE, Biome::NONE, std::array<BiomeType, 4> neighbor_biome{BiomeType::NONE, BiomeType::NONE,
Biome::NONE}; BiomeType::NONE, BiomeType::NONE};
std::unique_ptr<BiomeBuilder> m_biome_builder{nullptr};
bool is_neighbor_river = false; bool is_neighbor_river = false;
void make_biome_builder();
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -2,20 +2,18 @@
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
#include "Cubed/tools/perlin_noise.hpp"
#include <cmath>
#include <unordered_map> #include <unordered_map>
namespace Cubed { namespace Cubed {
static PlainParams plain{{Biome::PLAIN, static PlainParams plain{{BiomeType::PLAIN,
{0.0f, 0.5f}, {0.0f, 0.5f},
{0.0f, 0.5f}, {0.0f, 0.5f},
{0.003f, 0.010f, 0.020f}, {0.003f, 0.010f, 0.020f},
{62, 8}}}; {62, 8}}};
static ForestParams forest{{Biome::FOREST, static ForestParams forest{{BiomeType::FOREST,
{0.5f, 1.0f}, {0.5f, 1.0f},
{0.5f, 1.0f}, {0.5f, 1.0f},
{0.004f, 0.010f, 0.020f}, {0.004f, 0.010f, 0.020f},
@@ -24,27 +22,27 @@ static ForestParams forest{{Biome::FOREST,
}; };
static DesertParams desert{{Biome::DESERT, static DesertParams desert{{BiomeType::DESERT,
{0.5f, 1.0f}, {0.5f, 1.0f},
{0.0f, 0.5f}, {0.0f, 0.5f},
{0.003f, 0.010f, 0.020f}, {0.003f, 0.010f, 0.020f},
{61, 12}}}; {61, 12}}};
static MountainParams mountain{{Biome::MOUNTAIN, static MountainParams mountain{{BiomeType::MOUNTAIN,
{0.0f, 0.5f}, {0.0f, 0.5f},
{0.5f, 1.0f}, {0.5f, 1.0f},
{0.006f, 0.014f, 0.010f}, {0.006f, 0.014f, 0.010f},
{70, 70}}}; {70, 70}}};
static RiverParams river{{Biome::RIVER, static RiverParams river{{BiomeType::RIVER,
{-0.1f, -0.1f}, {-0.1f, -0.1f},
{-0.1f, -0.1f}, {-0.1f, -0.1f},
{0.003f, 0.010f, 0.020f}, {0.003f, 0.010f, 0.020f},
{50, 6}}}; {50, 6}}};
std::string get_biome_str(Biome biome) { std::string get_biome_str(BiomeType biome) {
std::string str; std::string str;
using enum Biome; using enum BiomeType;
switch (biome) { switch (biome) {
case PLAIN: case PLAIN:
str = "Plain"; str = "Plain";
@@ -86,8 +84,8 @@ Biome get_biome_from_noise(float temp, float humid) {
return Biome::FOREST; return Biome::FOREST;
} }
*/ */
Biome get_biome_from_noise(float temp, float humid) { BiomeType get_biome_from_noise(float temp, float humid) {
using enum Biome; using enum BiomeType;
if (plain.temp.first <= temp && temp < plain.temp.second && if (plain.temp.first <= temp && temp < plain.temp.second &&
plain.humid.first <= humid && humid < plain.humid.second) { plain.humid.first <= humid && humid < plain.humid.second) {
return PLAIN; return PLAIN;
@@ -107,8 +105,8 @@ Biome get_biome_from_noise(float temp, float humid) {
Logger::warn("Invail Temp {} or Humid {}", temp, humid); Logger::warn("Invail Temp {} or Humid {}", temp, humid);
return PLAIN; return PLAIN;
} }
std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) { std::array<float, 3> get_noise_frequencies_for_biome(BiomeType biome) {
using enum Biome; using enum BiomeType;
switch (biome) { switch (biome) {
case PLAIN: case PLAIN:
return plain.frequencies; return plain.frequencies;
@@ -128,8 +126,8 @@ std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
return {0.003f, 0.015f, 0.06f}; return {0.003f, 0.015f, 0.06f};
} }
BiomeHeightRange get_biome_height_range(Biome biome) { BiomeHeightRange get_biome_height_range(BiomeType biome) {
using enum Biome; using enum BiomeType;
switch (biome) { switch (biome) {
case PLAIN: case PLAIN:
return plain.height_range; return plain.height_range;
@@ -149,16 +147,16 @@ BiomeHeightRange get_biome_height_range(Biome biome) {
return {62, 4}; return {62, 4};
} }
Biome safe_int_to_biome(int x) { BiomeType safe_int_to_biome(int x) {
using enum Biome; using enum BiomeType;
static const std::unordered_map<int, Biome> INT_TO_BIOME_MAP{ static const std::unordered_map<int, BiomeType> INT_TO_BIOME_MAP{
{0, PLAIN}, {1, FOREST}, {2, DESERT}, {3, MOUNTAIN}, {4, RIVER}}; {0, PLAIN}, {1, FOREST}, {2, DESERT}, {3, MOUNTAIN}, {4, RIVER}};
auto it = INT_TO_BIOME_MAP.find(x); auto it = INT_TO_BIOME_MAP.find(x);
ASSERT_MSG(it != INT_TO_BIOME_MAP.end(), ":Can't Find"); ASSERT_MSG(it != INT_TO_BIOME_MAP.end(), ":Can't Find");
return it->second; return it->second;
} }
/*
int get_interpolated_height(float world_x, float world_z, float temp, int get_interpolated_height(float world_x, float world_z, float temp,
float humid) { float humid) {
@@ -186,7 +184,7 @@ int get_interpolated_height(float world_x, float world_z, float temp,
w_desert /= total; w_desert /= total;
w_forest /= total; w_forest /= total;
auto sample_height = [&](Biome b) -> float { auto sample_height = [&](BiomeType b) -> float {
auto range = get_biome_height_range(b); auto range = get_biome_height_range(b);
auto [f1, f2, f3] = get_noise_frequencies_for_biome(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) + float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f, world_z * f1) +
@@ -196,13 +194,13 @@ int get_interpolated_height(float world_x, float world_z, float temp,
return range.base_y + n * range.amplitude; return range.base_y + n * range.amplitude;
}; };
float h = w_mountain * sample_height(Biome::MOUNTAIN) + float h = w_mountain * sample_height(BiomeType::MOUNTAIN) +
w_plain * sample_height(Biome::PLAIN) + w_plain * sample_height(BiomeType::PLAIN) +
w_desert * sample_height(Biome::DESERT) + w_desert * sample_height(BiomeType::DESERT) +
w_forest * sample_height(Biome::FOREST); w_forest * sample_height(BiomeType::FOREST);
return static_cast<int>(h); return static_cast<int>(h);
} }
*/
PlainParams& plain_params() { return plain; } PlainParams& plain_params() { return plain; }
ForestParams& forest_params() { return forest; } ForestParams& forest_params() { return forest; }
DesertParams& desert_params() { return desert; } DesertParams& desert_params() { return desert; }

View File

@@ -0,0 +1,18 @@
#include "Cubed/gameplay/builders/biome_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp"
namespace Cubed {
void BiomeBuilder::build_bottom() {
ChunkGenerator& chunk_generator = get_chunk_generator();
Chunk& chunk = chunk_generator.chunk();
auto& m_blocks = chunk.blocks();
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;
}
}
}
}
} // namespace Cubed

View File

@@ -0,0 +1,55 @@
#include "Cubed/gameplay/builders/desert_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp"
namespace Cubed {
DesertBuilder::DesertBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {}
void DesertBuilder::build_biome() {
BiomeBuilder::build_bottom();
build_blocks();
};
void DesertBuilder::build_blocks() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 3;
}
for (int y = height - 5; y <= height; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 4;
}
}
}
}
void DesertBuilder::build_vegetation() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
if (m_chunk_generator.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;
}
}
}
}
}
ChunkGenerator& DesertBuilder::get_chunk_generator() {
return m_chunk_generator;
};
} // namespace Cubed

View File

@@ -0,0 +1,72 @@
#include "Cubed/gameplay/builders/forest_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/tree.hpp"
namespace Cubed {
ForestBuilder::ForestBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {}
void ForestBuilder::build_biome() {
BiomeBuilder::build_bottom();
build_blocks();
};
void ForestBuilder::build_blocks() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 3;
}
for (int y = height - 5; y < height; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 2;
}
m_blocks[Chunk::get_index(x, height, z)] = 1;
}
}
}
void ForestBuilder::build_vegetation() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_heightmap = m_chunk.heightmap();
auto& m_random = m_chunk_generator.random();
std::array<int, SIZE_X> x_arr;
std::iota(x_arr.begin(), x_arr.end(), 0);
std::shuffle(x_arr.begin(), x_arr.end(), m_random.engine());
std::array<int, SIZE_Z> 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) {
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});
}
}
}
auto& m_blocks = m_chunk.blocks();
if (m_chunk_generator.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;
}
}
}
}
}
ChunkGenerator& ForestBuilder::get_chunk_generator() {
return m_chunk_generator;
};
} // namespace Cubed

View File

@@ -0,0 +1,63 @@
#include "Cubed/gameplay/builders/mountain_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp"
namespace Cubed {
MountainBuilder::MountainBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {}
void MountainBuilder::build_biome() {
BiomeBuilder::build_bottom();
build_blocks();
};
void MountainBuilder::build_blocks() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 3;
}
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;
}
}
}
}
void MountainBuilder::build_vegetation() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
if (m_chunk_generator.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;
}
}
}
}
}
ChunkGenerator& MountainBuilder::get_chunk_generator() {
return m_chunk_generator;
};
} // namespace Cubed

View File

@@ -0,0 +1,55 @@
#include "Cubed/gameplay/builders/plain_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp"
namespace Cubed {
PlainBuilder::PlainBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {}
void PlainBuilder::build_biome() {
BiomeBuilder::build_bottom();
build_blocks();
};
void PlainBuilder::build_blocks() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 3;
}
for (int y = height - 5; y < height; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 2;
}
m_blocks[Chunk::get_index(x, height, z)] = 1;
}
}
}
void PlainBuilder::build_vegetation() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
if (m_chunk_generator.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;
}
}
}
}
}
ChunkGenerator& PlainBuilder::get_chunk_generator() {
return m_chunk_generator;
};
} // namespace Cubed

View File

@@ -0,0 +1,59 @@
#include "Cubed/gameplay/builders/river_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp"
namespace Cubed {
RiverBuilder::RiverBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {}
void RiverBuilder::build_biome() {
BiomeBuilder::build_bottom();
build_blocks();
};
void RiverBuilder::build_blocks() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 3;
}
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;
}
}
}
}
}
void RiverBuilder::build_vegetation() {
auto& m_chunk = m_chunk_generator.chunk();
auto& m_blocks = m_chunk.blocks();
auto& m_heightmap = m_chunk.heightmap();
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;
}
}
}
}
ChunkGenerator& RiverBuilder::get_chunk_generator() {
return m_chunk_generator;
};
} // namespace Cubed

View File

@@ -46,7 +46,7 @@ Chunk& Chunk::operator=(Chunk&& other) noexcept {
return *this; return *this;
} }
Biome Chunk::get_biome() const { return m_biome.load(); } BiomeType Chunk::get_biome() const { return m_biome.load(); }
ChunkPos Chunk::get_chunk_pos() const { return m_chunk_pos; } ChunkPos Chunk::get_chunk_pos() const { return m_chunk_pos; }
@@ -300,9 +300,9 @@ void Chunk::set_chunk_block(int index, unsigned id) {
ChunkPos Chunk::chunk_pos() const { return m_chunk_pos; } ChunkPos Chunk::chunk_pos() const { return m_chunk_pos; }
Biome Chunk::biome() const { return m_biome; } BiomeType Chunk::biome() const { return m_biome; }
void Chunk::biome(Biome b) { m_biome = b; } void Chunk::biome(BiomeType b) { m_biome = b; }
HeightMapArray& Chunk::heightmap() { return m_heightmap; } HeightMapArray& Chunk::heightmap() { return m_heightmap; }
std::vector<uint8_t>& Chunk::blocks() { return m_blocks; } std::vector<uint8_t>& Chunk::blocks() { return m_blocks; }

View File

@@ -1,12 +1,19 @@
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/builders/desert_builder.hpp"
#include "Cubed/gameplay/builders/forest_builder.hpp"
#include "Cubed/gameplay/builders/mountain_builder.hpp"
#include "Cubed/gameplay/builders/plain_builder.hpp"
#include "Cubed/gameplay/builders/river_builder.hpp"
#include "Cubed/gameplay/chunk.hpp" #include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/tree.hpp" #include "Cubed/gameplay/tree.hpp"
#include "Cubed/tools/cubed_hash.hpp" #include "Cubed/tools/cubed_hash.hpp"
#include "Cubed/tools/perlin_noise.hpp" #include "Cubed/tools/perlin_noise.hpp"
#include <numeric>
namespace Cubed { namespace Cubed {
using enum BiomeType;
constexpr int BLEND_RADIUS = 12; constexpr int BLEND_RADIUS = 12;
ChunkGenerator::ChunkGenerator(Chunk& chunk) : m_chunk(chunk) { ChunkGenerator::ChunkGenerator(Chunk& chunk) : m_chunk(chunk) {
ASSERT_MSG(is_init, "ChunksGenerator is not init"); ASSERT_MSG(is_init, "ChunksGenerator is not init");
@@ -57,9 +64,9 @@ void ChunkGenerator::resolve_biome_adjacency_conflict(
if (chunk == nullptr) { if (chunk == nullptr) {
continue; continue;
} }
Biome biome = chunk->get_biome(); BiomeType biome = chunk->get_biome();
neighbor_biome[i] = biome; neighbor_biome[i] = biome;
if (biome == Biome::RIVER) { if (biome == BiomeType::RIVER) {
is_neighbor_river = true; is_neighbor_river = true;
} }
for (const auto& non : NON_ADJACENT) { for (const auto& non : NON_ADJACENT) {
@@ -89,7 +96,7 @@ void ChunkGenerator::generate_heightmap() {
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE); float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE);
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE); float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE);
auto sample_height = [&](Biome b) -> float { auto sample_height = [&](BiomeType b) -> float {
auto range = get_biome_height_range(b); auto range = get_biome_height_range(b);
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b); auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f, float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f,
@@ -185,63 +192,13 @@ void ChunkGenerator::blend_heightmap_boundaries(
} }
void ChunkGenerator::generate_terrain_blocks() { void ChunkGenerator::generate_terrain_blocks() {
auto& m_blocks = m_chunk.blocks(); make_biome_builder();
auto& m_heightmap = m_chunk.heightmap(); if (!m_biome_builder) {
auto m_biome = m_chunk.biome(); Logger::error("BiomeBuilder is nullptr");
// bottom return;
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<int>(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 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;
}
for (int y = height; y <= height; y++) {
m_blocks[Chunk::get_index(x, y, z)] = 1;
}
}
}
} }
m_chunk.blocks().assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0);
m_biome_builder->build_biome();
} }
void ChunkGenerator::blend_surface_blocks_borders( void ChunkGenerator::blend_surface_blocks_borders(
@@ -344,7 +301,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
// Update the top block if the type changed // Update the top block if the type changed
if (final_type != type_self) { if (final_type != type_self) {
// top block // top block
if (m_chunk.biome() == Biome::RIVER && final_type == 1) { if (m_chunk.biome() == BiomeType::RIVER && final_type == 1) {
final_type = 2; final_type = 2;
} }
if (is_neighbor_river && final_type == 1) { if (is_neighbor_river && final_type == 1) {
@@ -371,52 +328,41 @@ void ChunkGenerator::blend_surface_blocks_borders(
} }
void ChunkGenerator::generate_vegetation() { void ChunkGenerator::generate_vegetation() {
auto m_biome = m_chunk.biome();
auto& m_blocks = m_chunk.blocks(); if (!m_biome_builder) {
auto& m_heightmap = m_chunk.heightmap(); Logger::error("BiomeBuilder is nullptr");
if (m_biome == Biome::FOREST) { return;
std::array<int, SIZE_X> x_arr;
std::iota(x_arr.begin(), x_arr.end(), 0);
std::shuffle(x_arr.begin(), x_arr.end(), m_random.engine());
std::array<int, SIZE_Z> 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) {
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;
}
}
} }
m_biome_builder->build_vegetation();
}
void ChunkGenerator::make_biome_builder() {
auto biome = m_chunk.biome();
switch (biome) {
case PLAIN:
m_biome_builder = std::make_unique<PlainBuilder>(*this);
break;
case DESERT:
m_biome_builder = std::make_unique<DesertBuilder>(*this);
break;
case FOREST:
m_biome_builder = std::make_unique<ForestBuilder>(*this);
break;
case MOUNTAIN:
m_biome_builder = std::make_unique<MountainBuilder>(*this);
break;
case RIVER:
m_biome_builder = std::make_unique<RiverBuilder>(*this);
break;
case NONE:
m_biome_builder = nullptr;
break;
} }
} }
Chunk& ChunkGenerator::chunk() { return m_chunk; }
Random& ChunkGenerator::random() { return m_random; }
bool ChunkGenerator::neighbor_river() const { return is_neighbor_river; }
} // namespace Cubed } // namespace Cubed