mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-17 16:17:02 +08:00
refactor: biome build (#5)
* refactor: rename Biome to BiomeType * feat: add biome builder
This commit is contained in:
@@ -2,20 +2,18 @@
|
||||
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/perlin_noise.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
static PlainParams plain{{Biome::PLAIN,
|
||||
static PlainParams plain{{BiomeType::PLAIN,
|
||||
{0.0f, 0.5f},
|
||||
{0.0f, 0.5f},
|
||||
{0.003f, 0.010f, 0.020f},
|
||||
{62, 8}}};
|
||||
|
||||
static ForestParams forest{{Biome::FOREST,
|
||||
static ForestParams forest{{BiomeType::FOREST,
|
||||
{0.5f, 1.0f},
|
||||
{0.5f, 1.0f},
|
||||
{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.0f, 0.5f},
|
||||
{0.003f, 0.010f, 0.020f},
|
||||
{61, 12}}};
|
||||
|
||||
static MountainParams mountain{{Biome::MOUNTAIN,
|
||||
static MountainParams mountain{{BiomeType::MOUNTAIN,
|
||||
{0.0f, 0.5f},
|
||||
{0.5f, 1.0f},
|
||||
{0.006f, 0.014f, 0.010f},
|
||||
{70, 70}}};
|
||||
|
||||
static RiverParams river{{Biome::RIVER,
|
||||
static RiverParams river{{BiomeType::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 get_biome_str(BiomeType biome) {
|
||||
std::string str;
|
||||
using enum Biome;
|
||||
using enum BiomeType;
|
||||
switch (biome) {
|
||||
case PLAIN:
|
||||
str = "Plain";
|
||||
@@ -86,8 +84,8 @@ Biome get_biome_from_noise(float temp, float humid) {
|
||||
return Biome::FOREST;
|
||||
}
|
||||
*/
|
||||
Biome get_biome_from_noise(float temp, float humid) {
|
||||
using enum Biome;
|
||||
BiomeType get_biome_from_noise(float temp, float humid) {
|
||||
using enum BiomeType;
|
||||
if (plain.temp.first <= temp && temp < plain.temp.second &&
|
||||
plain.humid.first <= humid && humid < plain.humid.second) {
|
||||
return PLAIN;
|
||||
@@ -107,8 +105,8 @@ Biome get_biome_from_noise(float temp, float humid) {
|
||||
Logger::warn("Invail Temp {} or Humid {}", temp, humid);
|
||||
return PLAIN;
|
||||
}
|
||||
std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
|
||||
using enum Biome;
|
||||
std::array<float, 3> get_noise_frequencies_for_biome(BiomeType biome) {
|
||||
using enum BiomeType;
|
||||
switch (biome) {
|
||||
case PLAIN:
|
||||
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};
|
||||
}
|
||||
|
||||
BiomeHeightRange get_biome_height_range(Biome biome) {
|
||||
using enum Biome;
|
||||
BiomeHeightRange get_biome_height_range(BiomeType biome) {
|
||||
using enum BiomeType;
|
||||
switch (biome) {
|
||||
case PLAIN:
|
||||
return plain.height_range;
|
||||
@@ -149,16 +147,16 @@ BiomeHeightRange get_biome_height_range(Biome biome) {
|
||||
return {62, 4};
|
||||
}
|
||||
|
||||
Biome safe_int_to_biome(int x) {
|
||||
using enum Biome;
|
||||
static const std::unordered_map<int, Biome> INT_TO_BIOME_MAP{
|
||||
BiomeType safe_int_to_biome(int x) {
|
||||
using enum BiomeType;
|
||||
static const std::unordered_map<int, BiomeType> INT_TO_BIOME_MAP{
|
||||
{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");
|
||||
return it->second;
|
||||
}
|
||||
|
||||
/*
|
||||
int get_interpolated_height(float world_x, float world_z, float temp,
|
||||
float humid) {
|
||||
|
||||
@@ -186,7 +184,7 @@ int get_interpolated_height(float world_x, float world_z, float temp,
|
||||
w_desert /= total;
|
||||
w_forest /= total;
|
||||
|
||||
auto sample_height = [&](Biome b) -> float {
|
||||
auto sample_height = [&](BiomeType 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) +
|
||||
@@ -196,13 +194,13 @@ int get_interpolated_height(float world_x, float world_z, float temp,
|
||||
return range.base_y + n * range.amplitude;
|
||||
};
|
||||
|
||||
float h = w_mountain * sample_height(Biome::MOUNTAIN) +
|
||||
w_plain * sample_height(Biome::PLAIN) +
|
||||
w_desert * sample_height(Biome::DESERT) +
|
||||
w_forest * sample_height(Biome::FOREST);
|
||||
float h = w_mountain * sample_height(BiomeType::MOUNTAIN) +
|
||||
w_plain * sample_height(BiomeType::PLAIN) +
|
||||
w_desert * sample_height(BiomeType::DESERT) +
|
||||
w_forest * sample_height(BiomeType::FOREST);
|
||||
return static_cast<int>(h);
|
||||
}
|
||||
|
||||
*/
|
||||
PlainParams& plain_params() { return plain; }
|
||||
ForestParams& forest_params() { return forest; }
|
||||
DesertParams& desert_params() { return desert; }
|
||||
|
||||
18
src/gameplay/builders/biome_builder.cpp
Normal file
18
src/gameplay/builders/biome_builder.cpp
Normal 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
|
||||
55
src/gameplay/builders/desert_builder.cpp
Normal file
55
src/gameplay/builders/desert_builder.cpp
Normal 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
|
||||
72
src/gameplay/builders/forest_builder.cpp
Normal file
72
src/gameplay/builders/forest_builder.cpp
Normal 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
|
||||
63
src/gameplay/builders/mountain_builder.cpp
Normal file
63
src/gameplay/builders/mountain_builder.cpp
Normal 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
|
||||
55
src/gameplay/builders/plain_builder.cpp
Normal file
55
src/gameplay/builders/plain_builder.cpp
Normal 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
|
||||
59
src/gameplay/builders/river_builder.cpp
Normal file
59
src/gameplay/builders/river_builder.cpp
Normal 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
|
||||
@@ -46,7 +46,7 @@ Chunk& Chunk::operator=(Chunk&& other) noexcept {
|
||||
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; }
|
||||
|
||||
@@ -300,9 +300,9 @@ void Chunk::set_chunk_block(int index, unsigned id) {
|
||||
|
||||
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; }
|
||||
std::vector<uint8_t>& Chunk::blocks() { return m_blocks; }
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
#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/tree.hpp"
|
||||
#include "Cubed/tools/cubed_hash.hpp"
|
||||
#include "Cubed/tools/perlin_noise.hpp"
|
||||
|
||||
#include <numeric>
|
||||
namespace Cubed {
|
||||
|
||||
using enum BiomeType;
|
||||
|
||||
constexpr int BLEND_RADIUS = 12;
|
||||
ChunkGenerator::ChunkGenerator(Chunk& chunk) : m_chunk(chunk) {
|
||||
ASSERT_MSG(is_init, "ChunksGenerator is not init");
|
||||
@@ -57,9 +64,9 @@ void ChunkGenerator::resolve_biome_adjacency_conflict(
|
||||
if (chunk == nullptr) {
|
||||
continue;
|
||||
}
|
||||
Biome biome = chunk->get_biome();
|
||||
BiomeType biome = chunk->get_biome();
|
||||
neighbor_biome[i] = biome;
|
||||
if (biome == Biome::RIVER) {
|
||||
if (biome == BiomeType::RIVER) {
|
||||
is_neighbor_river = true;
|
||||
}
|
||||
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_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 [f1, f2, f3] = get_noise_frequencies_for_biome(b);
|
||||
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() {
|
||||
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<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;
|
||||
}
|
||||
}
|
||||
}
|
||||
make_biome_builder();
|
||||
if (!m_biome_builder) {
|
||||
Logger::error("BiomeBuilder is nullptr");
|
||||
return;
|
||||
}
|
||||
m_chunk.blocks().assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0);
|
||||
m_biome_builder->build_biome();
|
||||
}
|
||||
|
||||
void ChunkGenerator::blend_surface_blocks_borders(
|
||||
@@ -344,7 +301,7 @@ 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) {
|
||||
if (m_chunk.biome() == BiomeType::RIVER && final_type == 1) {
|
||||
final_type = 2;
|
||||
}
|
||||
if (is_neighbor_river && final_type == 1) {
|
||||
@@ -371,52 +328,41 @@ 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;
|
||||
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_builder) {
|
||||
Logger::error("BiomeBuilder is nullptr");
|
||||
return;
|
||||
}
|
||||
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
|
||||
Reference in New Issue
Block a user