add plants

This commit is contained in:
MihailRis
2024-08-20 20:46:23 +03:00
parent 6efc942a1d
commit 5bdabaea42
5 changed files with 101 additions and 10 deletions
+43 -2
View File
@@ -35,15 +35,56 @@ struct BlocksLayers {
struct BiomeParameter {
/// @brief Central parameter value for the biome
float origin;
float value;
/// @brief Parameter score multiplier
/// (the higher the value, the greater the chance of biome choosing)
/// (the higher the weight, the greater the chance of biome choosing)
float weight;
};
struct PlantEntry {
/// @brief Plant block id
std::string block;
/// @brief Plant weight
float weight;
struct {
blockid_t id;
} rt;
};
struct BiomePlants {
static inline float MIN_CHANCE = 0.000001f;
/// @brief Plant entries sorted by weight descending.
std::vector<PlantEntry> plants;
/// @brief Sum of weight values
float weightsSum;
/// @brief Plant generation chance
float chance;
/// @brief Choose plant based on weight
/// @param rand some random value in range [0, 1)
/// @return index of chosen plant block
inline blockid_t choose(float rand) const {
if (plants.empty() || rand > chance || chance < MIN_CHANCE) {
return 0;
}
rand = rand / chance;
rand *= weightsSum;
for (const auto& plant : plants) {
rand -= plant.weight;
if (rand <= 0.0f) {
return plant.rt.id;
}
}
return plants[plants.size()-1].rt.id;
}
};
struct Biome {
std::string name;
std::vector<BiomeParameter> parameters;
BiomePlants plants;
BlocksLayers groundLayers;
BlocksLayers seaLayers;
};
+14 -1
View File
@@ -3,6 +3,7 @@
#include <cstring>
#include <iostream>
#include "maths/util.hpp"
#include "content/Content.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
@@ -63,7 +64,7 @@ static inline const Biome* choose_biome(
for (const auto& biome : biomes) {
float score = 0.0f;
for (uint i = 0; i < paramsCount; i++) {
score += glm::abs((params[i] - biome.parameters[i].origin) /
score += glm::abs((params[i] - biome.parameters[i].value) /
biome.parameters[i].weight);
}
if (score < chosenScore) {
@@ -92,6 +93,9 @@ void WorldGenerator::generate(
std::memset(voxels, 0, sizeof(voxel) * CHUNK_VOL);
PseudoRandom plantsRand;
plantsRand.setSeed(chunkX, chunkZ);
for (uint z = 0; z < CHUNK_D; z++) {
for (uint x = 0; x < CHUNK_W; x++) {
const Biome* biome = choose_biome(biomes, biomeParams, x, z);
@@ -104,6 +108,15 @@ void WorldGenerator::generate(
generate_pole(seaLayers, seaLevel, height, seaLevel, voxels, x, z);
generate_pole(groundLayers, height, 0, seaLevel, voxels, x, z);
if (height+1 > seaLevel) {
// TODO: add underwater plants support
float rand = (plantsRand.randU32() % RAND_MAX) / static_cast<float>(RAND_MAX);
blockid_t plant = biome->plants.choose(rand);
if (plant) {
voxels[vox_index(x, height+1, z)].id = plant;
}
}
}
}
}