add structure block names (for indexing)

This commit is contained in:
MihailRis 2024-08-24 03:20:16 +03:00
parent 54067d0ab2
commit 4c697c0f46
2 changed files with 42 additions and 4 deletions

View File

@ -1,9 +1,12 @@
#include "Structure.hpp"
#include <unordered_map>
#include <cstring>
#include "data/dynamic.hpp"
#include "data/dynamic_util.hpp"
#include "content/Content.hpp"
#include "voxels/Block.hpp"
#include "voxels/ChunksStorage.hpp"
#include "voxels/VoxelsVolume.hpp"
#include "world/Level.hpp"
@ -15,13 +18,33 @@ std::unique_ptr<Structure> Structure::create(
auto size = glm::abs(a - b);
VoxelsVolume volume(size.x, size.y, size.z);
volume.setPosition(start.x, start.y, start.z);
level->chunksStorage->getVoxels(&volume);
auto volVoxels = volume.getVoxels();
std::vector<voxel> voxels(size.x*size.y*size.z);
std::memcpy(voxels.data(), volVoxels, sizeof(voxel) * voxels.size());
return std::make_unique<Structure>(size, std::move(voxels));
std::vector<std::string> blockNames;
std::unordered_map<blockid_t, blockid_t> blocksRegistered;
auto contentIndices = level->content->getIndices();
for (size_t i = 0 ; i < voxels.size(); i++) {
blockid_t id = volVoxels[i].id;
blockid_t index;
auto found = blocksRegistered.find(id);
if (found == blocksRegistered.end()) {
const auto& def = contentIndices->blocks.require(id);
index = blockNames.size();
blockNames.push_back(def.name);
blocksRegistered[id] = index;
} else {
index = found->second;
}
voxels[i].id = index;
}
return std::make_unique<Structure>(
size, std::move(voxels), std::move(blockNames));
}
std::unique_ptr<dynamic::Map> Structure::serialize() const {
@ -29,6 +52,10 @@ std::unique_ptr<dynamic::Map> Structure::serialize() const {
root->put("version", STRUCTURE_FORMAT_VERSION);
root->put("size", dynamic::to_value(size));
auto& blockNamesArr = root->putList("block-names");
for (const auto& name : blockNames) {
blockNamesArr.put(name);
}
auto& voxelsArr = root->putList("voxels");
for (size_t i = 0; i < voxels.size(); i++) {
voxelsArr.put(static_cast<integer_t>(voxels[i].id));

View File

@ -9,15 +9,26 @@
inline constexpr int STRUCTURE_FORMAT_VERSION = 1;
class Level;
class Content;
struct Structure : public Serializable {
glm::ivec3 size;
/// @brief Structure voxels indexed different to world content
std::vector<voxel> voxels;
/// @brief Block names are used for indexing
std::vector<std::string> blockNames;
Structure() : size() {}
Structure(glm::ivec3 size, std::vector<voxel> voxels)
: size(size), voxels(std::move(voxels)) {}
Structure(
glm::ivec3 size,
std::vector<voxel> voxels,
std::vector<std::string> blockNames
): size(size),
voxels(std::move(voxels)),
blockNames(std::move(blockNames))
{}
std::unique_ptr<dynamic::Map> serialize() const override;
void deserialize(dynamic::Map* src) override;