diff --git a/src/world/generator/Structure.cpp b/src/world/generator/Structure.cpp index 1d4837b7..bf5426c7 100644 --- a/src/world/generator/Structure.cpp +++ b/src/world/generator/Structure.cpp @@ -1,9 +1,12 @@ #include "Structure.hpp" +#include #include #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::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 voxels(size.x*size.y*size.z); - std::memcpy(voxels.data(), volVoxels, sizeof(voxel) * voxels.size()); - return std::make_unique(size, std::move(voxels)); + std::vector blockNames; + std::unordered_map 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( + size, std::move(voxels), std::move(blockNames)); } std::unique_ptr Structure::serialize() const { @@ -29,6 +52,10 @@ std::unique_ptr 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(voxels[i].id)); diff --git a/src/world/generator/Structure.hpp b/src/world/generator/Structure.hpp index 93d93a4d..2e932c61 100644 --- a/src/world/generator/Structure.hpp +++ b/src/world/generator/Structure.hpp @@ -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 voxels; + /// @brief Block names are used for indexing + std::vector blockNames; Structure() : size() {} - Structure(glm::ivec3 size, std::vector voxels) - : size(size), voxels(std::move(voxels)) {} + Structure( + glm::ivec3 size, + std::vector voxels, + std::vector blockNames + ): size(size), + voxels(std::move(voxels)), + blockNames(std::move(blockNames)) + {} std::unique_ptr serialize() const override; void deserialize(dynamic::Map* src) override;