add structure block names (for indexing)
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
#include "Structure.hpp"
|
#include "Structure.hpp"
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "data/dynamic.hpp"
|
#include "data/dynamic.hpp"
|
||||||
#include "data/dynamic_util.hpp"
|
#include "data/dynamic_util.hpp"
|
||||||
|
#include "content/Content.hpp"
|
||||||
|
#include "voxels/Block.hpp"
|
||||||
#include "voxels/ChunksStorage.hpp"
|
#include "voxels/ChunksStorage.hpp"
|
||||||
#include "voxels/VoxelsVolume.hpp"
|
#include "voxels/VoxelsVolume.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
@@ -15,13 +18,33 @@ std::unique_ptr<Structure> Structure::create(
|
|||||||
auto size = glm::abs(a - b);
|
auto size = glm::abs(a - b);
|
||||||
|
|
||||||
VoxelsVolume volume(size.x, size.y, size.z);
|
VoxelsVolume volume(size.x, size.y, size.z);
|
||||||
|
volume.setPosition(start.x, start.y, start.z);
|
||||||
level->chunksStorage->getVoxels(&volume);
|
level->chunksStorage->getVoxels(&volume);
|
||||||
|
|
||||||
auto volVoxels = volume.getVoxels();
|
auto volVoxels = volume.getVoxels();
|
||||||
std::vector<voxel> voxels(size.x*size.y*size.z);
|
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 {
|
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("version", STRUCTURE_FORMAT_VERSION);
|
||||||
root->put("size", dynamic::to_value(size));
|
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");
|
auto& voxelsArr = root->putList("voxels");
|
||||||
for (size_t i = 0; i < voxels.size(); i++) {
|
for (size_t i = 0; i < voxels.size(); i++) {
|
||||||
voxelsArr.put(static_cast<integer_t>(voxels[i].id));
|
voxelsArr.put(static_cast<integer_t>(voxels[i].id));
|
||||||
|
|||||||
@@ -9,15 +9,26 @@
|
|||||||
inline constexpr int STRUCTURE_FORMAT_VERSION = 1;
|
inline constexpr int STRUCTURE_FORMAT_VERSION = 1;
|
||||||
|
|
||||||
class Level;
|
class Level;
|
||||||
|
class Content;
|
||||||
|
|
||||||
struct Structure : public Serializable {
|
struct Structure : public Serializable {
|
||||||
glm::ivec3 size;
|
glm::ivec3 size;
|
||||||
|
|
||||||
|
/// @brief Structure voxels indexed different to world content
|
||||||
std::vector<voxel> voxels;
|
std::vector<voxel> voxels;
|
||||||
|
/// @brief Block names are used for indexing
|
||||||
|
std::vector<std::string> blockNames;
|
||||||
|
|
||||||
Structure() : size() {}
|
Structure() : size() {}
|
||||||
|
|
||||||
Structure(glm::ivec3 size, std::vector<voxel> voxels)
|
Structure(
|
||||||
: size(size), voxels(std::move(voxels)) {}
|
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;
|
std::unique_ptr<dynamic::Map> serialize() const override;
|
||||||
void deserialize(dynamic::Map* src) override;
|
void deserialize(dynamic::Map* src) override;
|
||||||
|
|||||||
Reference in New Issue
Block a user