From 892c273e9587bd04813d0fde3e753c5fd67a01d2 Mon Sep 17 00:00:00 2001 From: clasher113 Date: Sat, 27 Dec 2025 21:03:43 +0200 Subject: [PATCH] extend libblock --- doc/en/scripting/builtins/libblock.md | 15 +++++++------ doc/ru/scripting/builtins/libblock.md | 9 ++++---- src/logic/scripting/lua/libs/libblock.cpp | 26 +++++++++++++++++------ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/doc/en/scripting/builtins/libblock.md b/doc/en/scripting/builtins/libblock.md index 4ad46f14a..c93735f78 100644 --- a/doc/en/scripting/builtins/libblock.md +++ b/doc/en/scripting/builtins/libblock.md @@ -154,19 +154,22 @@ The function returns a table with the results or nil if the ray does not hit any The result will use the destination table instead of creating a new one if the optional argument specified. -## Model - -Block model information. +## Model and physics ```lua -- returns block model type (block/aabb/custom/...) -block.get_model(id: int) -> str +block.get_model(id: int, [optional] variant_index: int = 0) -> str -- returns block model name -block.model_name(id: int) -> str +block.model_name(id: int, [optional] variant_index: int = 0) -> str -- returns array of 6 textures assigned to sides of block -block.get_textures(id: int) -> string table +block.get_textures(id: int, [optional] variant_index: int = 0) -> string table + +-- returns array of two vectors (arrays of 3 numbers): +-- 1. Minimum point of the hitbox +-- 2. Hitbox size +block.get_hitbox(id: int, rotation_index: int, [optional] hitbox_index: int = 0) -> {vec3, vec3} ``` ## Data fields diff --git a/doc/ru/scripting/builtins/libblock.md b/doc/ru/scripting/builtins/libblock.md index 1dabdb7e8..4e20e696c 100644 --- a/doc/ru/scripting/builtins/libblock.md +++ b/doc/ru/scripting/builtins/libblock.md @@ -157,19 +157,20 @@ block.seek_origin(x: int, y: int, z: int) -> int, int, int ```lua -- возвращает тип модели блока (block/aabb/custom/...) -block.get_model(id: int) -> string +block.get_model(id: int, [опционально] variant_index: int = 0) -> string -- возвращает имя модели блока -block.model_name(id: int) -> string +block.model_name(id: int, [опционально] variant_index: int = 0) -> string -- возвращает массив из 6 текстур, назначенных на стороны блока -block.get_textures(id: int) -> table +block.get_textures(id: int, [опционально] variant_index: int = 0) -> table -- Возвращает массив из двух векторов (массивов из 3 чисел): -- 1. Минимальная точка хитбокса -- 2. Размер хитбокса -- rotation_index - индекс поворота блока -block.get_hitbox(id: int, rotation_index: int) -> {vec3, vec3} +-- hitbox_index - индекс хитбокса +block.get_hitbox(id: int, rotation_index: int, [опционально] hitbox_index: int = 0) -> {vec3, vec3} ``` ## Данные блоков diff --git a/src/logic/scripting/lua/libs/libblock.cpp b/src/logic/scripting/lua/libs/libblock.cpp index fa525eb0f..b87020536 100644 --- a/src/logic/scripting/lua/libs/libblock.cpp +++ b/src/logic/scripting/lua/libs/libblock.cpp @@ -349,11 +349,23 @@ static int l_caption(lua::State* L) { return 0; } +static lua::Integer get_variant_index(lua::State* L, const Block* const block, int argumentIndex) { + const auto variantIndex = lua::gettop(L) >= argumentIndex ? lua::tointeger(L, argumentIndex) : 0; + const size_t variantsSize = block->variants->variants.size(); + if (variantIndex < 0 || variantIndex >= variantsSize) { + throw std::out_of_range( + "variant index out of bounds [0, " + std::to_string(variantsSize - 1) + "]"); + } + return variantIndex; +} + static int l_get_textures(lua::State* L) { if (auto def = get_block_def(L)) { + const auto& textureFaces = (def->variants ? def->variants->variants[get_variant_index(L, def, 2)].textureFaces : + def->defaults.textureFaces); lua::createtable(L, 6, 0); for (size_t i = 0; i < 6; i++) { - lua::pushstring(L, def->defaults.textureFaces[i]); // TODO: variant argument + lua::pushstring(L, textureFaces[i]); lua::rawseti(L, i + 1); } return 1; @@ -364,8 +376,8 @@ static int l_get_textures(lua::State* L) { static int l_model_name(lua::State* L) { if (auto def = get_block_def(L)) { - // TODO: variant argument - const auto& modelName = def->defaults.model.name; + const auto& modelName = (def->variants ? def->variants->variants[get_variant_index(L, def, 2)].model.name : + def->defaults.model.name); if (modelName.empty()) { return lua::pushlstring(L, def->name + ".model"); } @@ -376,8 +388,9 @@ static int l_model_name(lua::State* L) { static int l_get_model(lua::State* L) { if (auto def = get_block_def(L)) { - // TODO: variant argument - return lua::pushlstring(L, BlockModelTypeMeta.getName(def->defaults.model.type)); + const BlockModelType modelType = (def->variants ? def->variants->variants[get_variant_index(L, def, 2)].model.type : + def->defaults.model.type); + return lua::pushlstring(L, BlockModelTypeMeta.getName(modelType)); } return 0; } @@ -385,12 +398,13 @@ static int l_get_model(lua::State* L) { static int l_get_hitbox(lua::State* L) { if (auto def = get_block_def(L)) { size_t rotation = lua::tointeger(L, 2); + const size_t hitboxIndex = static_cast(lua::gettop(L) >= 3 ? lua::tointeger(L, 3) : 0); if (def->rotatable) { rotation %= def->rotations.MAX_COUNT; } else { rotation = 0; } - auto& hitbox = def->rt.hitboxes[rotation].at(0); + auto& hitbox = def->rt.hitboxes[rotation].at(hitboxIndex); lua::createtable(L, 2, 0); lua::pushvec3(L, hitbox.min());