refactor WorldGenerator & update test generator script

This commit is contained in:
MihailRis 2024-08-23 03:07:02 +03:00
parent f2395dede8
commit f40ff7cd28
4 changed files with 23 additions and 18 deletions

View File

@ -19,7 +19,7 @@ biomes = {
{block="base:stone", height=-1}, {block="base:stone", height=-1},
{block="base:bazalt", height=1}, {block="base:bazalt", height=1},
}, },
plant_chance = 0.5, plant_chance = 0.3,
plants = { plants = {
{block="base:grass", weight=1}, {block="base:grass", weight=1},
{block="base:flower", weight=0.03}, {block="base:flower", weight=0.03},

View File

@ -29,7 +29,8 @@ ChunksController::ChunksController(Level* level, uint padding)
padding(padding), padding(padding),
generator(std::make_unique<WorldGenerator>( generator(std::make_unique<WorldGenerator>(
level->content->generators.require(level->getWorld()->getGenerator()), level->content->generators.require(level->getWorld()->getGenerator()),
level->content level->content,
level->getWorld()->getSeed()
)) {} )) {}
ChunksController::~ChunksController() = default; ChunksController::~ChunksController() = default;
@ -116,7 +117,7 @@ void ChunksController::createChunk(int x, int z) {
auto& chunkFlags = chunk->flags; auto& chunkFlags = chunk->flags;
if (!chunkFlags.loaded) { if (!chunkFlags.loaded) {
generator->generate(chunk->voxels, x, z, level->getWorld()->getSeed()); generator->generate(chunk->voxels, x, z);
chunkFlags.unsaved = true; chunkFlags.unsaved = true;
} }
chunk->updateHeights(); chunk->updateHeights();

View File

@ -7,23 +7,24 @@
#include "content/Content.hpp" #include "content/Content.hpp"
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
#include "voxels/Chunk.hpp" #include "voxels/Chunk.hpp"
#include "voxels/voxel.hpp"
#include "world/generator/GeneratorDef.hpp" #include "world/generator/GeneratorDef.hpp"
#include "util/timeutil.hpp"
static inline constexpr uint MAX_PARAMETERS = 16; static inline constexpr uint MAX_PARAMETERS = 16;
WorldGenerator::WorldGenerator(const GeneratorDef& def, const Content* content) WorldGenerator::WorldGenerator(
: def(def), content(content) { const GeneratorDef& def, const Content* content, uint64_t seed
)
: def(def), content(content), seed(seed) {
voxel voxels[CHUNK_VOL];
} }
static inline void generate_pole( static inline void generate_pole(
const BlocksLayers& layers, const BlocksLayers& layers,
int top, int top, int bottom,
int bottom,
int seaLevel, int seaLevel,
voxel* voxels, voxel* voxels,
int x, int x, int z
int z
) { ) {
uint y = top; uint y = top;
uint layerExtension = 0; uint layerExtension = 0;
@ -75,11 +76,8 @@ static inline const Biome* choose_biome(
return chosenBiome; return chosenBiome;
} }
#include "util/timeutil.hpp" void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
void WorldGenerator::generate( // timeutil::ScopeLogTimer log(555);
voxel* voxels, int chunkX, int chunkZ, uint64_t seed
) {
timeutil::ScopeLogTimer log(555);
auto heightmap = def.script->generateHeightmap( auto heightmap = def.script->generateHeightmap(
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed {chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed
); );

View File

@ -1,8 +1,10 @@
#pragma once #pragma once
#include <string> #include <string>
#include <memory>
#include "typedefs.hpp" #include "typedefs.hpp"
#include "voxels/voxel.hpp"
struct voxel; struct voxel;
class Content; class Content;
@ -12,10 +14,15 @@ struct GeneratorDef;
class WorldGenerator { class WorldGenerator {
const GeneratorDef& def; const GeneratorDef& def;
const Content* content; const Content* content;
uint64_t seed;
public: public:
/// @param def generator definition
/// @param content world content
/// @param seed world seed
WorldGenerator( WorldGenerator(
const GeneratorDef& def, const GeneratorDef& def,
const Content* content const Content* content,
uint64_t seed
); );
virtual ~WorldGenerator() = default; virtual ~WorldGenerator() = default;
@ -23,8 +30,7 @@ public:
/// @param voxels destinatiopn chunk voxels buffer /// @param voxels destinatiopn chunk voxels buffer
/// @param x chunk position X divided by CHUNK_W /// @param x chunk position X divided by CHUNK_W
/// @param z chunk position Y divided by CHUNK_D /// @param z chunk position Y divided by CHUNK_D
/// @param seed world seed virtual void generate(voxel* voxels, int x, int z);
virtual void generate(voxel* voxels, int x, int z, uint64_t seed);
inline static std::string DEFAULT = "core:default"; inline static std::string DEFAULT = "core:default";
}; };