refactor & add structures.json

This commit is contained in:
MihailRis
2024-09-24 04:05:13 +03:00
parent 519ebc05f1
commit 323c2f2935
20 changed files with 153 additions and 43 deletions
+11
View File
@@ -0,0 +1,11 @@
#include "GeneratorDef.hpp"
#include "VoxelFragment.hpp"
GeneratingVoxelStructure::GeneratingVoxelStructure(
VoxelStructureMeta meta,
std::unique_ptr<VoxelFragment> structure
) : structure(std::move(structure)), meta(std::move(meta)) {}
GeneratorDef::GeneratorDef(std::string name) : name(std::move(name)) {}
+22 -3
View File
@@ -1,14 +1,20 @@
#pragma once
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include <unordered_map>
#include "typedefs.hpp"
#include "maths/Heightmap.hpp"
#include "StructurePlacement.hpp"
class Content;
class VoxelStructure;
class VoxelFragment;
struct VoxelStructureMeta {
std::string name;
};
struct BlocksLayer {
/// @brief Layer block
@@ -102,7 +108,7 @@ public:
virtual ~GeneratorScript() = default;
/// @brief Load all structures
virtual std::vector<std::shared_ptr<VoxelStructure>> loadStructures() = 0;
virtual std::vector<std::shared_ptr<VoxelFragment>> loadStructures() = 0;
/// @brief Generates a heightmap with values in range 0..1
/// @param offset position of the heightmap in the world
@@ -133,12 +139,25 @@ public:
virtual void prepare(const Content* content) = 0;
};
struct GeneratingVoxelStructure {
VoxelStructureMeta meta;
std::unique_ptr<VoxelFragment> structure;
GeneratingVoxelStructure(
VoxelStructureMeta meta,
std::unique_ptr<VoxelFragment> structure
);
};
/// @brief Generator information
struct GeneratorDef {
/// @brief Generator full name - packid:name
std::string name;
std::unique_ptr<GeneratorScript> script;
GeneratorDef(std::string name) : name(std::move(name)) {}
std::unordered_map<std::string, size_t> structuresIndices;
std::vector<std::unique_ptr<GeneratingVoxelStructure>> structures;
GeneratorDef(std::string name);
GeneratorDef(const GeneratorDef&) = delete;
};
@@ -1,4 +1,4 @@
#include "VoxelStructure.hpp"
#include "VoxelFragment.hpp"
#include <unordered_map>
#include <cstring>
@@ -10,7 +10,7 @@
#include "voxels/VoxelsVolume.hpp"
#include "world/Level.hpp"
std::unique_ptr<VoxelStructure> VoxelStructure::create(
std::unique_ptr<VoxelFragment> VoxelFragment::create(
Level* level, const glm::ivec3& a, const glm::ivec3& b, bool entities
) {
auto start = glm::min(a, b);
@@ -43,11 +43,11 @@ std::unique_ptr<VoxelStructure> VoxelStructure::create(
voxels[i].state = volVoxels[i].state;
}
return std::make_unique<VoxelStructure>(
return std::make_unique<VoxelFragment>(
size, std::move(voxels), std::move(blockNames));
}
dv::value VoxelStructure::serialize() const {
dv::value VoxelFragment::serialize() const {
auto root = dv::object();
root["version"] = STRUCTURE_FORMAT_VERSION;
root["size"] = dv::to_value(size);
@@ -64,7 +64,7 @@ dv::value VoxelStructure::serialize() const {
return root;
}
void VoxelStructure::deserialize(const dv::value& src) {
void VoxelFragment::deserialize(const dv::value& src) {
size = glm::ivec3();
dv::get_vec(src, "size", size);
@@ -83,7 +83,7 @@ void VoxelStructure::deserialize(const dv::value& src) {
}
}
void VoxelStructure::prepare(const Content& content) {
void VoxelFragment::prepare(const Content& content) {
auto volume = size.x*size.y*size.z;
voxelsRuntime.resize(volume);
for (size_t i = 0; i < volume; i++) {
@@ -93,7 +93,7 @@ void VoxelStructure::prepare(const Content& content) {
}
}
std::unique_ptr<VoxelStructure> VoxelStructure::rotated(const Content& content) const {
std::unique_ptr<VoxelFragment> VoxelFragment::rotated(const Content& content) const {
std::vector<voxel> newVoxels(voxels.size());
for (int y = 0; y < size.y; y++) {
@@ -115,7 +115,7 @@ std::unique_ptr<VoxelStructure> VoxelStructure::rotated(const Content& content)
}
}
}
auto newStructure = std::make_unique<VoxelStructure>(
auto newStructure = std::make_unique<VoxelFragment>(
// swap X and Z on 90 deg. rotation
glm::ivec3(size.z, size.y, size.x),
std::move(newVoxels),
@@ -11,7 +11,7 @@ inline constexpr int STRUCTURE_FORMAT_VERSION = 1;
class Level;
class Content;
class VoxelStructure : public Serializable {
class VoxelFragment : public Serializable {
glm::ivec3 size;
/// @brief Structure voxels indexed different to world content
@@ -22,9 +22,9 @@ class VoxelStructure : public Serializable {
/// @brief Structure voxels built on prepare(...) call
std::vector<voxel> voxelsRuntime;
public:
VoxelStructure() : size() {}
VoxelFragment() : size() {}
VoxelStructure(
VoxelFragment(
glm::ivec3 size,
std::vector<voxel> voxels,
std::vector<std::string> blockNames
@@ -41,9 +41,9 @@ public:
void prepare(const Content& content);
/// @brief Create structure copy rotated 90 deg. clockwise
std::unique_ptr<VoxelStructure> rotated(const Content& content) const;
std::unique_ptr<VoxelFragment> rotated(const Content& content) const;
static std::unique_ptr<VoxelStructure> create(
static std::unique_ptr<VoxelFragment> create(
Level* level, const glm::ivec3& a, const glm::ivec3& b, bool entities);
const glm::ivec3& getSize() const {
+3 -1
View File
@@ -7,7 +7,7 @@
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "GeneratorDef.hpp"
#include "VoxelStructure.hpp"
#include "VoxelFragment.hpp"
#include "util/timeutil.hpp"
#include "util/listutil.hpp"
#include "debug/Logger.hpp"
@@ -259,6 +259,8 @@ void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
}
}
}
// TODO: put automatic placement here
for (const auto& placement : prototype.structures) {
if (placement.structure < 0 || placement.structure >= structures.size()) {
+2 -2
View File
@@ -16,7 +16,7 @@ class Content;
struct GeneratorDef;
class Heightmap;
struct Biome;
class VoxelStructure;
class VoxelFragment;
enum class ChunkPrototypeLevel {
VOID=0, BIOMES, HEIGHTMAP, STRUCTURES
@@ -47,7 +47,7 @@ class WorldGenerator {
/// @brief Chunk prototypes loading surround map
SurroundMap surroundMap;
std::vector<std::array<std::shared_ptr<VoxelStructure>, 4>> structures;
std::vector<std::array<std::shared_ptr<VoxelFragment>, 4>> structures;
/// @brief Generate chunk prototype (see ChunkPrototype)
/// @param x chunk position X divided by CHUNK_W