add block/item tags

This commit is contained in:
MihailRis 2025-08-30 23:26:07 +03:00
parent d59a901ae0
commit 8bdf31d7bb
9 changed files with 53 additions and 1 deletions

View File

@ -1,4 +1,5 @@
{
"texture": "coal_ore",
"tags": ["base:ore"],
"base:durability": 16.0
}

View File

@ -7,5 +7,6 @@
"obstacle": false,
"selectable": false,
"replaceable": true,
"translucent": true
"translucent": true,
"tags": ["base:liquid"]
}

View File

@ -28,6 +28,9 @@ std::unique_ptr<Content> ContentBuilder::build() {
// Generating runtime info
def.rt.id = blockDefsIndices.size();
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
for (const auto& tag : def.tags) {
def.rt.tags.push_back(tags.add(tag));
}
if (def.variants) {
for (auto& variant : def.variants->variants) {

View File

@ -62,6 +62,27 @@ public:
}
};
struct TagsIndices {
int nextIndex = 1;
std::unordered_map<std::string, int> map;
int add(const std::string& tag) {
const auto& found = map.find(tag);
if (found != map.end()) {
return found->second;
}
return map[tag] = nextIndex++;
}
int indexOf(const std::string& tag) {
const auto& found = map.find(tag);
if (found == map.end()) {
return -1;
}
return found->second;
}
};
class ContentBuilder {
UptrsMap<std::string, BlockMaterial> blockMaterials;
UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
@ -74,6 +95,7 @@ public:
ContentUnitBuilder<GeneratorDef> generators {allNames, ContentType::GENERATOR};
ResourceIndicesSet resourceIndices {};
dv::value defaults = nullptr;
TagsIndices tags {};
~ContentBuilder();

View File

@ -89,6 +89,7 @@ template<> void ContentUnitLoader<Block>::loadUnit(
) {
auto root = io::read_json(file);
process_properties(def, name, root);
process_tags(def, root);
if (root.has("parent")) {
const auto& parentName = root["parent"].asString();

View File

@ -21,3 +21,17 @@ inline void process_properties(T& def, const std::string& name, const dv::value&
process_method(def.properties, suffix, field, value);
}
}
template <typename T>
inline void process_tags(T& def, const dv::value& root) {
if (!root.has("tags")) {
return;
}
const auto& tags = root["tags"];
for (const auto& tagValue : tags) {
if (!tagValue.isString()) {
continue;
}
def.tags.push_back(tagValue.asString());
}
}

View File

@ -19,6 +19,7 @@ template<> void ContentUnitLoader<ItemDef>::loadUnit(
) {
auto root = io::read_json(file);
process_properties(def, name, root);
process_tags(def, root);
if (root.has("parent")) {
const auto& parentName = root["parent"].asString();

View File

@ -2,6 +2,7 @@
#include <glm/glm.hpp>
#include <string>
#include <vector>
#include "data/dv.hpp"
#include "typedefs.hpp"
@ -64,11 +65,15 @@ struct ItemDef {
std::string scriptFile;
std::vector<std::string> tags;
struct {
itemid_t id;
blockid_t placingBlock;
ItemFuncsSet funcsset {};
bool emissive = false;
std::vector<int> tags;
} rt {};
ItemDef(const std::string& name);

View File

@ -261,6 +261,8 @@ public:
std::unique_ptr<Variants> variants;
std::vector<std::string> tags;
/// @brief Runtime indices (content indexing results)
struct {
/// @brief block runtime integer id
@ -285,6 +287,8 @@ public:
itemid_t pickingItem = 0;
blockid_t surfaceReplacement = 0;
std::vector<int> tags;
} rt {};
Block(const std::string& name);