mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
chore: add clang-format and pre-commit configuration
This commit is contained in:
@@ -1,74 +1,60 @@
|
||||
#include <Cubed/gameplay/biome.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/perlin_noise.hpp>
|
||||
#include "Cubed/gameplay/biome.hpp"
|
||||
|
||||
#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,
|
||||
{0.0f, 0.5f},
|
||||
{0.0f, 0.5f},
|
||||
{0.003f, 0.010f, 0.020f},
|
||||
{62, 8}
|
||||
}
|
||||
};
|
||||
static PlainParams plain{{Biome::PLAIN,
|
||||
{0.0f, 0.5f},
|
||||
{0.0f, 0.5f},
|
||||
{0.003f, 0.010f, 0.020f},
|
||||
{62, 8}}};
|
||||
|
||||
static ForestParams forest {
|
||||
{
|
||||
Biome::FOREST,
|
||||
{0.5f, 1.0f},
|
||||
{0.5f, 1.0f},
|
||||
{0.004f, 0.010f, 0.020f},
|
||||
{64, 12}
|
||||
},
|
||||
0.1f
|
||||
static ForestParams forest{{Biome::FOREST,
|
||||
{0.5f, 1.0f},
|
||||
{0.5f, 1.0f},
|
||||
{0.004f, 0.010f, 0.020f},
|
||||
{64, 12}},
|
||||
0.1f
|
||||
|
||||
};
|
||||
|
||||
static DesertParams desert {
|
||||
{
|
||||
Biome::DESERT,
|
||||
{0.5f, 1.0f},
|
||||
{0.0f, 0.5f},
|
||||
{0.003f, 0.010f, 0.020f},
|
||||
{61, 12}
|
||||
}
|
||||
};
|
||||
static DesertParams desert{{Biome::DESERT,
|
||||
{0.5f, 1.0f},
|
||||
{0.0f, 0.5f},
|
||||
{0.003f, 0.010f, 0.020f},
|
||||
{61, 12}}};
|
||||
|
||||
static MountainParams mountain {
|
||||
{
|
||||
Biome::MOUNTAIN,
|
||||
{0.0f, 0.5f},
|
||||
{0.5f, 1.0f},
|
||||
{0.006f, 0.014f, 0.010f},
|
||||
{70, 70}
|
||||
}
|
||||
};
|
||||
static MountainParams mountain{{Biome::MOUNTAIN,
|
||||
{0.0f, 0.5f},
|
||||
{0.5f, 1.0f},
|
||||
{0.006f, 0.014f, 0.010f},
|
||||
{70, 70}}};
|
||||
|
||||
std::string get_biome_str(Biome biome) {
|
||||
std::string str;
|
||||
using enum Biome;
|
||||
switch (biome) {
|
||||
case PLAIN:
|
||||
str = "Plain";
|
||||
break;
|
||||
case FOREST:
|
||||
str = "Forest";
|
||||
break;
|
||||
case DESERT:
|
||||
str = "Desert";
|
||||
break;
|
||||
case MOUNTAIN:
|
||||
str = "Mountain";
|
||||
break;
|
||||
case NONE:
|
||||
str = "Unknown";
|
||||
break;
|
||||
case PLAIN:
|
||||
str = "Plain";
|
||||
break;
|
||||
case FOREST:
|
||||
str = "Forest";
|
||||
break;
|
||||
case DESERT:
|
||||
str = "Desert";
|
||||
break;
|
||||
case MOUNTAIN:
|
||||
str = "Mountain";
|
||||
break;
|
||||
case NONE:
|
||||
str = "Unknown";
|
||||
break;
|
||||
}
|
||||
return str;
|
||||
};
|
||||
@@ -78,7 +64,7 @@ Biome get_biome_from_noise(float temp, float humid) {
|
||||
float dt = t - ct;
|
||||
float dh = h - ch;
|
||||
float dist = std::sqrt(dt*dt + dh*dh);
|
||||
return std::max(0.0f, 0.5f - dist);
|
||||
return std::max(0.0f, 0.5f - dist);
|
||||
};
|
||||
float w_m = weight(temp, humid, 0.25f, 0.15f);
|
||||
float w_p = weight(temp, humid, 0.50f, 0.40f);
|
||||
@@ -93,35 +79,39 @@ Biome get_biome_from_noise(float temp, float humid) {
|
||||
*/
|
||||
Biome get_biome_from_noise(float temp, float humid) {
|
||||
using enum Biome;
|
||||
if (plain.temp.first <= temp && temp < plain.temp.second && plain.humid.first <= humid && humid < plain.humid.second) {
|
||||
if (plain.temp.first <= temp && temp < plain.temp.second &&
|
||||
plain.humid.first <= humid && humid < plain.humid.second) {
|
||||
return PLAIN;
|
||||
}
|
||||
if (forest.temp.first <= temp && temp < forest.temp.second && forest.humid.first <= humid && humid < forest.humid.second) {
|
||||
if (forest.temp.first <= temp && temp < forest.temp.second &&
|
||||
forest.humid.first <= humid && humid < forest.humid.second) {
|
||||
return FOREST;
|
||||
}
|
||||
if (desert.temp.first <= temp && temp < desert.temp.second && desert.humid.first <= humid && humid < desert.humid.second) {
|
||||
if (desert.temp.first <= temp && temp < desert.temp.second &&
|
||||
desert.humid.first <= humid && humid < desert.humid.second) {
|
||||
return DESERT;
|
||||
}
|
||||
if (mountain.temp.first <= temp && temp <= mountain.temp.second && mountain.humid.first <= humid && humid <= mountain.humid.second) {
|
||||
if (mountain.temp.first <= temp && temp <= mountain.temp.second &&
|
||||
mountain.humid.first <= humid && humid <= mountain.humid.second) {
|
||||
return MOUNTAIN;
|
||||
}
|
||||
Logger::warn("Invail Temp {} or Humid {}", temp, humid);
|
||||
return PLAIN;
|
||||
return PLAIN;
|
||||
}
|
||||
std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
|
||||
using enum Biome;
|
||||
switch (biome) {
|
||||
case PLAIN:
|
||||
return plain.frequencies;
|
||||
case FOREST:
|
||||
return forest.frequencies;
|
||||
case DESERT:
|
||||
return desert.frequencies;
|
||||
case MOUNTAIN:
|
||||
return mountain.frequencies;
|
||||
case NONE:
|
||||
ASSERT_MSG(false, "Chunk Biome is None");
|
||||
throw std::invalid_argument{"Chunk Biome is None"};
|
||||
case PLAIN:
|
||||
return plain.frequencies;
|
||||
case FOREST:
|
||||
return forest.frequencies;
|
||||
case DESERT:
|
||||
return desert.frequencies;
|
||||
case MOUNTAIN:
|
||||
return mountain.frequencies;
|
||||
case NONE:
|
||||
ASSERT_MSG(false, "Chunk Biome is None");
|
||||
throw std::invalid_argument{"Chunk Biome is None"};
|
||||
}
|
||||
Logger::warn("Unknown Biome");
|
||||
return {0.003f, 0.015f, 0.06f};
|
||||
@@ -130,17 +120,17 @@ std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
|
||||
BiomeHeightRange get_biome_height_range(Biome biome) {
|
||||
using enum Biome;
|
||||
switch (biome) {
|
||||
case PLAIN:
|
||||
return plain.height_range;
|
||||
case FOREST:
|
||||
return forest.height_range;
|
||||
case DESERT:
|
||||
return desert.height_range;
|
||||
case MOUNTAIN:
|
||||
return mountain.height_range;
|
||||
case NONE:
|
||||
ASSERT_MSG(false, "Chunk Biome is None");
|
||||
throw std::invalid_argument{"Chunk Biome is None"};
|
||||
case PLAIN:
|
||||
return plain.height_range;
|
||||
case FOREST:
|
||||
return forest.height_range;
|
||||
case DESERT:
|
||||
return desert.height_range;
|
||||
case MOUNTAIN:
|
||||
return mountain.height_range;
|
||||
case NONE:
|
||||
ASSERT_MSG(false, "Chunk Biome is None");
|
||||
throw std::invalid_argument{"Chunk Biome is None"};
|
||||
}
|
||||
Logger::warn("Unknown Biome");
|
||||
return {62, 4};
|
||||
@@ -148,71 +138,61 @@ 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}
|
||||
};
|
||||
static const std::unordered_map<int, Biome> INT_TO_BIOME_MAP{
|
||||
{0, PLAIN}, {1, FOREST}, {2, DESERT}, {3, MOUNTAIN}};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int get_interpolated_height(float world_x, float world_z, float temp, float humid) {
|
||||
|
||||
int get_interpolated_height(float world_x, float world_z, float temp,
|
||||
float humid) {
|
||||
|
||||
auto weight = [](float t, float h, float ct, float ch) -> float {
|
||||
float dt = t - ct;
|
||||
float dh = h - ch;
|
||||
float dist = std::sqrt(dt*dt + dh*dh);
|
||||
return std::max(0.0f, 0.5f - dist);
|
||||
float dist = std::sqrt(dt * dt + dh * dh);
|
||||
return std::max(0.0f, 0.5f - dist);
|
||||
};
|
||||
|
||||
float w_mountain = weight(temp, humid, 0.25f, 0.15f);
|
||||
float w_plain = weight(temp, humid, 0.50f, 0.40f);
|
||||
float w_desert = weight(temp, humid, 0.75f, 0.15f);
|
||||
float w_forest = weight(temp, humid, 0.75f, 0.75f);
|
||||
float w_plain = weight(temp, humid, 0.50f, 0.40f);
|
||||
float w_desert = weight(temp, humid, 0.75f, 0.15f);
|
||||
float w_forest = weight(temp, humid, 0.75f, 0.75f);
|
||||
// adjust transitions between chunks
|
||||
float pow_n = 8.0f; // the larger n is, the purer the biome
|
||||
w_mountain = std::pow(w_mountain, pow_n) * MOUNTAIN_FREQ;
|
||||
w_plain = std::pow(w_plain, pow_n) * PLAIN_FREQ;
|
||||
w_desert = std::pow(w_desert, pow_n) * DESERT_FREQ;
|
||||
w_forest = std::pow(w_forest, pow_n) * FOREST_FREQ;
|
||||
w_plain = std::pow(w_plain, pow_n) * PLAIN_FREQ;
|
||||
w_desert = std::pow(w_desert, pow_n) * DESERT_FREQ;
|
||||
w_forest = std::pow(w_forest, pow_n) * FOREST_FREQ;
|
||||
|
||||
float total = w_mountain + w_plain + w_desert + w_forest;
|
||||
w_mountain /= total; w_plain /= total; w_desert /= total; w_forest /= total;
|
||||
w_mountain /= total;
|
||||
w_plain /= total;
|
||||
w_desert /= total;
|
||||
w_forest /= total;
|
||||
|
||||
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);
|
||||
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;
|
||||
};
|
||||
|
||||
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(Biome::MOUNTAIN) +
|
||||
w_plain * sample_height(Biome::PLAIN) +
|
||||
w_desert * sample_height(Biome::DESERT) +
|
||||
w_forest * sample_height(Biome::FOREST);
|
||||
return static_cast<int>(h);
|
||||
}
|
||||
|
||||
PlainParams& plain_params() {
|
||||
return plain;
|
||||
}
|
||||
ForestParams& forest_params() {
|
||||
return forest;
|
||||
}
|
||||
DesertParams& desert_params() {
|
||||
return desert;
|
||||
}
|
||||
MountainParams& mountain_params() {
|
||||
return mountain;
|
||||
}
|
||||
|
||||
}
|
||||
PlainParams& plain_params() { return plain; }
|
||||
ForestParams& forest_params() { return forest; }
|
||||
DesertParams& desert_params() { return desert; }
|
||||
MountainParams& mountain_params() { return mountain; }
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
Reference in New Issue
Block a user