add VoxelStructure lua usertype

This commit is contained in:
MihailRis
2024-09-19 18:41:34 +03:00
parent bf9f81a98c
commit 88b0f8e3d6
17 changed files with 180 additions and 60 deletions
+14 -2
View File
@@ -66,11 +66,23 @@ dv::value VoxelStructure::serialize() const {
void VoxelStructure::deserialize(const dv::value& src) {
size = glm::ivec3();
dv::get_vec(src, "size", size);
voxels.resize(size.x*size.y*size.z);
auto volume = size.x*size.y*size.z;
voxels.resize(volume);
const auto& voxelsArr = src["voxels"];
for (size_t i = 0; i < size.x*size.y*size.z; i++) {
for (size_t i = 0; i < volume; i++) {
voxels[i].id = voxelsArr[i * 2].asInteger();
voxels[i].state = int2blockstate(voxelsArr[i * 2 + 1].asInteger());
}
}
void VoxelStructure::prepare(const Content& content) {
auto volume = size.x*size.y*size.z;
voxelsRuntime.resize(volume);
for (size_t i = 0; i < volume; i++) {
const auto& name = blockNames[voxels[i].id];
voxelsRuntime[i].id = content.blocks.require(name).rt.id;
voxelsRuntime[i].state = voxels[i].state;
}
}
+16 -1
View File
@@ -11,7 +11,7 @@ inline constexpr int STRUCTURE_FORMAT_VERSION = 1;
class Level;
class Content;
struct VoxelStructure : public Serializable {
class VoxelStructure : public Serializable {
glm::ivec3 size;
/// @brief Structure voxels indexed different to world content
@@ -19,6 +19,9 @@ struct VoxelStructure : public Serializable {
/// @brief Block names are used for indexing
std::vector<std::string> blockNames;
/// @brief Structure voxels built on prepare(...) call
std::vector<voxel> voxelsRuntime;
public:
VoxelStructure() : size() {}
VoxelStructure(
@@ -33,6 +36,18 @@ struct VoxelStructure : public Serializable {
dv::value serialize() const override;
void deserialize(const dv::value& src) override;
/// @brief Build runtime voxel indices
/// @param content world content
void prepare(const Content& content);
static std::unique_ptr<VoxelStructure> create(
Level* level, const glm::ivec3& a, const glm::ivec3& b, bool entities);
const glm::ivec3& getSize() const {
return size;
}
const std::vector<voxel>& getRuntimeVoxels() {
return voxelsRuntime;
}
};
+6 -3
View File
@@ -7,7 +7,8 @@
#include "content/Content.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "world/generator/GeneratorDef.hpp"
#include "GeneratorDef.hpp"
#include "VoxelStructure.hpp"
#include "util/timeutil.hpp"
#include "debug/Logger.hpp"
@@ -47,6 +48,8 @@ WorldGenerator::WorldGenerator(
});
}
WorldGenerator::~WorldGenerator() {}
static inline void generate_pole(
const BlocksLayers& layers,
int top, int bottom,
@@ -119,8 +122,8 @@ std::unique_ptr<ChunkPrototype> WorldGenerator::generatePrototype(
}
return std::make_unique<ChunkPrototype>(
ChunkPrototypeLevel::BIOMES,
nullptr,
std::move(chunkBiomes));
std::move(chunkBiomes),
nullptr);
}
void WorldGenerator::generateHeightmap(
+13 -9
View File
@@ -14,6 +14,7 @@ class Content;
struct GeneratorDef;
class Heightmap;
struct Biome;
class VoxelStructure;
enum class ChunkPrototypeLevel {
BIOMES, HEIGHTMAP
@@ -22,18 +23,19 @@ enum class ChunkPrototypeLevel {
struct ChunkPrototype {
ChunkPrototypeLevel level;
/// @brief chunk heightmap
std::shared_ptr<Heightmap> heightmap;
/// @brief chunk biomes matrix
std::vector<const Biome*> biomes;
/// @brief chunk heightmap
std::shared_ptr<Heightmap> heightmap;
ChunkPrototype(
ChunkPrototypeLevel level,
std::shared_ptr<Heightmap> heightmap,
std::vector<const Biome*> biomes
std::vector<const Biome*> biomes,
std::shared_ptr<Heightmap> heightmap
) : level(level),
heightmap(std::move(heightmap)),
biomes(std::move(biomes)) {};
biomes(std::move(biomes)),
heightmap(std::move(heightmap)) {};
};
/// @brief High-level world generation controller
@@ -49,6 +51,8 @@ class WorldGenerator {
/// @brief Chunk prototypes loading surround map
SurroundMap surroundMap;
std::vector<std::unique_ptr<VoxelStructure>> structures;
/// @brief Generate chunk prototype (see ChunkPrototype)
/// @param x chunk position X divided by CHUNK_W
/// @param z chunk position Y divided by CHUNK_D
@@ -61,15 +65,15 @@ public:
const Content* content,
uint64_t seed
);
virtual ~WorldGenerator() = default;
~WorldGenerator();
virtual void update(int centerX, int centerY, int loadDistance);
void update(int centerX, int centerY, int loadDistance);
/// @brief Generate complete chunk voxels
/// @param voxels destinatiopn chunk voxels buffer
/// @param x chunk position X divided by CHUNK_W
/// @param z chunk position Y divided by CHUNK_D
virtual void generate(voxel* voxels, int x, int z);
void generate(voxel* voxels, int x, int z);
inline static std::string DEFAULT = "core:default";
};