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/res/modules/vector2.lua b/res/modules/vector2.lua index dad44ee1e..80bf1030b 100644 --- a/res/modules/vector2.lua +++ b/res/modules/vector2.lua @@ -8,7 +8,7 @@ local vector2 = vector2.__index = vector2 --- vec2 cocnrtuct +-- vec2 construct function vector2:new(x, y) if type(x) ~= "number" or type(y) ~= "number" then error("Invalid input argument. Expected two numbers. [ vec2(number, number) ]") @@ -303,11 +303,6 @@ function vector2.__eq(a, b) end --- eq vec -function vector2.__eq(a, b) - return a.x == b.x and a.y == b.y -end - function vector2.__tostring(vcrt) return vcrt:strvec2() end diff --git a/res/modules/vector3.lua b/res/modules/vector3.lua index 855cac3bb..0d9e52e45 100644 --- a/res/modules/vector3.lua +++ b/res/modules/vector3.lua @@ -187,7 +187,7 @@ function vector3:rot(angle, axis, convert2deg) self.y = sx * x + cx * y end elseif axis == nil then - local cosx, sx, cy, sy = math.cos(angle), math.sin(angle), math.cos(angle), math.sin(angle) + local cx, sx, cy, sy = math.cos(angle), math.sin(angle), math.cos(angle), math.sin(angle) local x, y, z = self.x, self.y, self.z self.x = cx * x - sx * y self.y = sx * x + cx * y diff --git a/src/content/ContentBuilder.cpp b/src/content/ContentBuilder.cpp index 66ad1acff..5eb639b1a 100644 --- a/src/content/ContentBuilder.cpp +++ b/src/content/ContentBuilder.cpp @@ -57,7 +57,7 @@ std::unique_ptr ContentBuilder::build() { } } } else { - def.rt.hitboxes->emplace_back(AABB(glm::vec3(1.0f))); + def.rt.hitboxes[0] = def.hitboxes; } blockDefsIndices.push_back(&def); diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 92ff3a370..d33c67ec6 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -383,17 +383,18 @@ void Hud::update(bool visible) { } /// @brief Show inventory on the screen and turn on inventory mode blocking movement -void Hud::openInventory() { - auto& content = frontend.getLevel().content; +void Hud::openInventory(bool playerInventory) { showExchangeSlot(); - inventoryOpen = true; - auto inventory = player.getInventory(); - auto inventoryDocument = assets.get("core:inventory"); - inventoryView = std::dynamic_pointer_cast(inventoryDocument->getRoot()); - inventoryView->bind(inventory, &content); - add(HudElement(HudElementMode::INVENTORY, inventoryDocument, inventoryView, false)); - add(HudElement(HudElementMode::INVENTORY, nullptr, exchangeSlot, false)); + + if (playerInventory) { + auto& content = frontend.getLevel().content; + auto inventory = player.getInventory(); + auto inventoryDocument = assets.get("core:inventory"); + inventoryView = std::dynamic_pointer_cast(inventoryDocument->getRoot()); + inventoryView->bind(inventory, &content); + add(HudElement(HudElementMode::INVENTORY, inventoryDocument, inventoryView, false)); + } } std::shared_ptr Hud::openInventory( @@ -412,11 +413,8 @@ std::shared_ptr Hud::openInventory( } secondUI = secondInvView; - if (playerInventory) { - openInventory(); - } else { - inventoryOpen = true; - } + openInventory(playerInventory); + if (inv == nullptr) { inv = level.inventories->createVirtual(secondInvView->getSlotsCount()); } @@ -444,11 +442,9 @@ void Hud::openInventory( throw std::runtime_error("block UI root element must be 'inventory'"); } secondUI = blockUI; - if (playerInventory) { - openInventory(); - } else { - inventoryOpen = true; - } + + openInventory(playerInventory); + if (blockinv == nullptr) { blockinv = level.inventories->createVirtual(blockUI->getSlotsCount()); } @@ -477,6 +473,7 @@ void Hud::showExchangeSlot() { exchangeSlot->setInteractive(false); exchangeSlot->setZIndex(1); gui.store(SlotView::EXCHANGE_SLOT_NAME, exchangeSlot); + add(HudElement(HudElementMode::INVENTORY, nullptr, exchangeSlot, false)); } void Hud::showOverlay( @@ -486,12 +483,9 @@ void Hud::showOverlay( closeInventory(); } secondUI = doc->getRoot(); - if (playerInventory) { - openInventory(); - } else { - showExchangeSlot(); - inventoryOpen = true; - } + + openInventory(playerInventory); + add(HudElement(HudElementMode::INVENTORY, doc, secondUI, false), args); } diff --git a/src/frontend/hud.hpp b/src/frontend/hud.hpp index 560382d31..ad37d24ab 100644 --- a/src/frontend/hud.hpp +++ b/src/frontend/hud.hpp @@ -156,7 +156,7 @@ public: void setPause(bool pause); /// @brief Show player inventory in inventory-mode - void openInventory(); + void openInventory(bool playerInventory = true); /// @brief Show inventory in inventory-mode /// @param doc ui layout diff --git a/src/graphics/ui/elements/CheckBox.cpp b/src/graphics/ui/elements/CheckBox.cpp index 22741e45d..8694dc085 100644 --- a/src/graphics/ui/elements/CheckBox.cpp +++ b/src/graphics/ui/elements/CheckBox.cpp @@ -25,7 +25,8 @@ void CheckBox::draw(const DrawContext& pctx, const Assets&) { batch->rect(pos.x, pos.y, size.x, size.y); } -void CheckBox::mouseRelease(int, int) { +void CheckBox::mouseRelease(int x, int y) { + UINode::mouseRelease(x, y); checked = !checked; if (consumer) { consumer(checked); 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());