add block.has_tag, item.has_tag

This commit is contained in:
MihailRis 2025-08-30 23:46:45 +03:00
parent 8bdf31d7bb
commit 185b6cc661
7 changed files with 41 additions and 6 deletions

View File

@ -35,13 +35,15 @@ Content::Content(
UptrsMap<std::string, BlockMaterial> blockMaterials, UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::SkeletonConfig> skeletons, UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
ResourceIndicesSet resourceIndices, ResourceIndicesSet resourceIndices,
dv::value defaults dv::value defaults,
std::unordered_map<std::string, int> tags
) )
: indices(std::move(indices)), : indices(std::move(indices)),
packs(std::move(packs)), packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)), blockMaterials(std::move(blockMaterials)),
skeletons(std::move(skeletons)), skeletons(std::move(skeletons)),
defaults(std::move(defaults)), defaults(std::move(defaults)),
tags(std::move(tags)),
blocks(std::move(blocks)), blocks(std::move(blocks)),
items(std::move(items)), items(std::move(items)),
entities(std::move(entities)), entities(std::move(entities)),

View File

@ -176,6 +176,7 @@ class Content {
UptrsMap<std::string, BlockMaterial> blockMaterials; UptrsMap<std::string, BlockMaterial> blockMaterials;
UptrsMap<std::string, rigging::SkeletonConfig> skeletons; UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
dv::value defaults = nullptr; dv::value defaults = nullptr;
std::unordered_map<std::string, int> tags;
public: public:
ContentUnitDefs<Block> blocks; ContentUnitDefs<Block> blocks;
ContentUnitDefs<ItemDef> items; ContentUnitDefs<ItemDef> items;
@ -195,7 +196,8 @@ public:
UptrsMap<std::string, BlockMaterial> blockMaterials, UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::SkeletonConfig> skeletons, UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
ResourceIndicesSet resourceIndices, ResourceIndicesSet resourceIndices,
dv::value defaults dv::value defaults,
std::unordered_map<std::string, int> tags
); );
~Content(); ~Content();
@ -211,6 +213,14 @@ public:
return defaults; return defaults;
} }
int getTagIndex(const std::string& tag) const {
const auto& found = tags.find(tag);
if (found == tags.end()) {
return -1;
}
return found->second;
}
const rigging::SkeletonConfig* getSkeleton(const std::string& id) const; const rigging::SkeletonConfig* getSkeleton(const std::string& id) const;
const rigging::SkeletonConfig& requireSkeleton(const std::string& id) const; const rigging::SkeletonConfig& requireSkeleton(const std::string& id) const;
const BlockMaterial* findBlockMaterial(const std::string& id) const; const BlockMaterial* findBlockMaterial(const std::string& id) const;

View File

@ -29,7 +29,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
def.rt.id = blockDefsIndices.size(); def.rt.id = blockDefsIndices.size();
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission); def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
for (const auto& tag : def.tags) { for (const auto& tag : def.tags) {
def.rt.tags.push_back(tags.add(tag)); def.rt.tags.insert(tags.add(tag));
} }
if (def.variants) { if (def.variants) {
@ -96,7 +96,8 @@ std::unique_ptr<Content> ContentBuilder::build() {
std::move(blockMaterials), std::move(blockMaterials),
std::move(skeletons), std::move(skeletons),
std::move(resourceIndices), std::move(resourceIndices),
std::move(defaults) std::move(defaults),
std::move(tags.map)
); );
// Now, it's time to resolve foreign keys // Now, it's time to resolve foreign keys

View File

@ -3,6 +3,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <set>
#include "data/dv.hpp" #include "data/dv.hpp"
#include "typedefs.hpp" #include "typedefs.hpp"
@ -73,7 +74,7 @@ struct ItemDef {
ItemFuncsSet funcsset {}; ItemFuncsSet funcsset {};
bool emissive = false; bool emissive = false;
std::vector<int> tags; std::set<int> tags;
} rt {}; } rt {};
ItemDef(const std::string& name); ItemDef(const std::string& name);

View File

@ -698,6 +698,15 @@ static int l_reload_script(lua::State* L) {
return 0; return 0;
} }
static int l_has_tag(lua::State* L) {
if (auto def = require_block(L)) {
auto tag = lua::require_string(L, 2);
const auto& tags = def->rt.tags;
return lua::pushboolean(L, tags.find(content->getTagIndex(tag)) != tags.end());
}
return 0;
}
const luaL_Reg blocklib[] = { const luaL_Reg blocklib[] = {
{"index", lua::wrap<l_index>}, {"index", lua::wrap<l_index>},
{"name", lua::wrap<l_get_def>}, {"name", lua::wrap<l_get_def>},
@ -737,5 +746,6 @@ const luaL_Reg blocklib[] = {
{"get_field", lua::wrap<l_get_field>}, {"get_field", lua::wrap<l_get_field>},
{"set_field", lua::wrap<l_set_field>}, {"set_field", lua::wrap<l_set_field>},
{"reload_script", lua::wrap<l_reload_script>}, {"reload_script", lua::wrap<l_reload_script>},
{"has_tag", lua::wrap<l_has_tag>},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -108,6 +108,15 @@ static int l_reload_script(lua::State* L) {
return 0; return 0;
} }
static int l_has_tag(lua::State* L) {
if (auto def = get_item_def(L, 1)) {
auto tag = lua::require_string(L, 2);
const auto& tags = def->rt.tags;
return lua::pushboolean(L, tags.find(content->getTagIndex(tag)) != tags.end());
}
return 0;
}
const luaL_Reg itemlib[] = { const luaL_Reg itemlib[] = {
{"index", lua::wrap<l_index>}, {"index", lua::wrap<l_index>},
{"name", lua::wrap<l_name>}, {"name", lua::wrap<l_name>},
@ -121,5 +130,6 @@ const luaL_Reg itemlib[] = {
{"emission", lua::wrap<l_emission>}, {"emission", lua::wrap<l_emission>},
{"uses", lua::wrap<l_uses>}, {"uses", lua::wrap<l_uses>},
{"reload_script", lua::wrap<l_reload_script>}, {"reload_script", lua::wrap<l_reload_script>},
{"has_tag", lua::wrap<l_has_tag>},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <array> #include <array>
#include <set>
#include "data/dv.hpp" #include "data/dv.hpp"
#include "maths/UVRegion.hpp" #include "maths/UVRegion.hpp"
@ -288,7 +289,7 @@ public:
blockid_t surfaceReplacement = 0; blockid_t surfaceReplacement = 0;
std::vector<int> tags; std::set<int> tags;
} rt {}; } rt {};
Block(const std::string& name); Block(const std::string& name);