diff --git a/doc/en/scripting/builtins/libentities.md b/doc/en/scripting/builtins/libentities.md index 0c24ab90..bd50c0e1 100644 --- a/doc/en/scripting/builtins/libentities.md +++ b/doc/en/scripting/builtins/libentities.md @@ -20,8 +20,17 @@ entities.spawn(name: str, pos: vec3, [optional] args: table) -- Checks the existence of an entity by a unique identifier. entities.exists(uid: int) -> bool --- Returns entity name (string ID). -entities.name(uid: int) -> str +-- Returns entity definition index by UID +entities.get_def(uid: int) -> int + +-- Returns entity definition name by index (string ID). +entities.def_name(id: int) -> str + +-- Returns entity definition index by name (integer ID). +entities.def_index(name: str) -> int + +-- Returns number of available entity definitions +entities.defs_count() -> int -- Returns a table of all loaded entities entities.get_all() -> table diff --git a/doc/ru/scripting/builtins/libentities.md b/doc/ru/scripting/builtins/libentities.md index afeec0a0..ebb23497 100644 --- a/doc/ru/scripting/builtins/libentities.md +++ b/doc/ru/scripting/builtins/libentities.md @@ -20,8 +20,17 @@ entities.spawn(name: str, pos: vec3, [optional] args: table) -- Проверяет наличие сущности по уникальному идентификатору. entities.exists(uid: int) -> bool --- Возвращает имя сущности (строковый ID). -entities.name(uid: int) -> str +-- Возвращает индекс определения сущности по UID +entities.get_def(uid: int) -> int + +-- Возвращает имя определения сущности по индексу (строковый ID). +entities.def_name(id: int) -> str + +-- Возвращает индекс определения сущности по имени (числовой ID). +entities.def_index(name: str) -> int + +-- Возвращает число доступных определений сущностей +entities.defs_count() -> int -- Возвращает таблицу всех загруженных сущностей entities.get_all() -> table diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 55c80bd4..06d99016 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -62,7 +62,7 @@ local Entity = {__index={ get_component=function(self, name) return self.components[name] end, has_component=function(self, name) return self.components[name] ~= nil end, get_uid=function(self) return self.eid end, - get_name=function(self) return entities.name(self.eid) end, + get_def=function(self) return entities.get_def(self.eid) end, }} local entities = {} diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 7687e2ee..c4bdf41a 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -21,7 +21,7 @@ static Block* require_block(lua::State* L) { return indices->blocks.get(id); } -static int l_name(lua::State* L) { +static int l_get_def(lua::State* L) { if (auto def = require_block(L)) { return lua::pushstring(L, def->name); } @@ -382,7 +382,7 @@ static int l_raycast(lua::State* L) { const luaL_Reg blocklib [] = { {"index", lua::wrap}, - {"name", lua::wrap}, + {"name", lua::wrap}, {"material", lua::wrap}, {"caption", lua::wrap}, {"defs_count", lua::wrap}, diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index d044bdcc..0cfa2467 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -12,11 +12,36 @@ using namespace scripting; +static EntityDef* require_entity_def(lua::State* L) { + auto indices = content->getIndices(); + auto id = lua::tointeger(L, 1); + if (static_cast(id) >= indices->entities.count()) { + return nullptr; + } + return indices->entities.get(id); +} + + static int l_exists(lua::State* L) { return lua::pushboolean(L, get_entity(L, 1).has_value()); } -static int l_name(lua::State* L) { +static int l_def_index(lua::State* L) { + auto name = lua::require_string(L, 1); + return lua::pushinteger(L, content->entities.require(name).rt.id); +} + +static int l_def_name(lua::State* L) { + if (auto def = require_entity_def(L)) { + return lua::pushstring(L, def->name); + } + return 0; +} +static int l_defs_count(lua::State* L) { + return lua::pushinteger(L, indices->entities.count()); +} + +static int l_get_def(lua::State* L) { if (auto entity = get_entity(L, 1)) { return lua::pushstring(L, entity->getDef().name); } @@ -158,7 +183,10 @@ static int l_raycast(lua::State* L) { const luaL_Reg entitylib [] = { {"exists", lua::wrap}, - {"name", lua::wrap}, + {"def_index", lua::wrap}, + {"def_name", lua::wrap}, + {"get_def", lua::wrap}, + {"defs_count", lua::wrap}, {"spawn", lua::wrap}, {"despawn", lua::wrap}, {"get_skeleton", lua::wrap},