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

@@ -22,6 +22,9 @@ constexpr float DEFAULT_MAX_RUN_SPEED = 7.0f;
constexpr float DEFAULT_ACCELERATION = 10.0f;
constexpr float DEFAULT_DECELERATION = 15.0f;
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>;

View File

@@ -7,12 +7,7 @@ namespace Cubed {
constexpr float BIOME_NOISE_FREQUENCY = 0.03f;
constexpr float PLAIN_FREQ = 0.4f;
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 };
enum class BiomeType { PLAIN = 0, FOREST, DESERT, MOUNTAIN, RIVER, NONE };
struct BiomeHeightRange {
int base_y;
@@ -20,19 +15,23 @@ struct BiomeHeightRange {
};
struct BiomeNonAdjacent {
Biome first;
std::vector<Biome> second;
Biome replace;
BiomeType first;
std::vector<BiomeType> second;
BiomeType replace;
};
static inline const std::vector<BiomeNonAdjacent> NON_ADJACENT{
{{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}}};
{{BiomeType::PLAIN, {BiomeType::DESERT}, BiomeType::RIVER},
{BiomeType::FOREST, {BiomeType::DESERT}, BiomeType::RIVER},
{BiomeType::DESERT,
{BiomeType::MOUNTAIN, BiomeType::FOREST},
BiomeType::RIVER},
{BiomeType::MOUNTAIN,
{BiomeType::DESERT, BiomeType::FOREST},
BiomeType::RIVER}}};
struct BaseBiomeParams {
Biome biome;
BiomeType biome;
std::pair<float, float> temp;
std::pair<float, float> humid;
std::array<float, 3> frequencies;
@@ -51,11 +50,11 @@ 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);
BiomeHeightRange get_biome_height_range(Biome biome);
Biome safe_int_to_biome(int x);
std::string get_biome_str(BiomeType biome);
BiomeType get_biome_from_noise(float temp, float humid);
std::array<float, 3> get_noise_frequencies_for_biome(BiomeType biome);
BiomeHeightRange get_biome_height_range(BiomeType biome);
BiomeType safe_int_to_biome(int x);
int get_interpolated_height(float world_x, float world_z, float temp,
float humid);
@@ -64,4 +63,5 @@ ForestParams& forest_params();
DesertParams& desert_params();
MountainParams& mountain_params();
RiverParams& river_params();
} // 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_is_on_gen_vertex_data{false};
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::unique_ptr<ChunkGenerator> m_generator;
@@ -50,7 +50,7 @@ public:
Chunk(Chunk&&) noexcept;
Chunk& operator=(Chunk&&) noexcept;
Biome get_biome() const;
BiomeType get_biome() const;
ChunkPos get_chunk_pos() const;
const std::vector<uint8_t>& get_chunk_blocks() const;
HeightMapArray get_heightmap() const;
@@ -94,8 +94,8 @@ public:
void set_chunk_block(int index, unsigned id);
ChunkPos chunk_pos() const;
Biome biome() const;
void biome(Biome b);
BiomeType biome() const;
void biome(BiomeType b);
HeightMapArray& heightmap();
std::vector<uint8_t>& blocks();
};

View File

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