extend libblock

This commit is contained in:
clasher113 2025-12-27 21:03:43 +02:00
parent 8cd5e38385
commit 892c273e95
3 changed files with 34 additions and 16 deletions

View file

@ -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. The result will use the destination table instead of creating a new one if the optional argument specified.
## Model ## Model and physics
Block model information.
```lua ```lua
-- returns block model type (block/aabb/custom/...) -- 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 -- 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 -- 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 ## Data fields

View file

@ -157,19 +157,20 @@ block.seek_origin(x: int, y: int, z: int) -> int, int, int
```lua ```lua
-- возвращает тип модели блока (block/aabb/custom/...) -- возвращает тип модели блока (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 текстур, назначенных на стороны блока -- возвращает массив из 6 текстур, назначенных на стороны блока
block.get_textures(id: int) -> table<string> block.get_textures(id: int, [опционально] variant_index: int = 0) -> table<string>
-- Возвращает массив из двух векторов (массивов из 3 чисел): -- Возвращает массив из двух векторов (массивов из 3 чисел):
-- 1. Минимальная точка хитбокса -- 1. Минимальная точка хитбокса
-- 2. Размер хитбокса -- 2. Размер хитбокса
-- rotation_index - индекс поворота блока -- 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}
``` ```
## Данные блоков ## Данные блоков

View file

@ -349,11 +349,23 @@ static int l_caption(lua::State* L) {
return 0; 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) { static int l_get_textures(lua::State* L) {
if (auto def = get_block_def(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); lua::createtable(L, 6, 0);
for (size_t i = 0; i < 6; i++) { 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); lua::rawseti(L, i + 1);
} }
return 1; return 1;
@ -364,8 +376,8 @@ static int l_get_textures(lua::State* L) {
static int l_model_name(lua::State* L) { static int l_model_name(lua::State* L) {
if (auto def = get_block_def(L)) { if (auto def = get_block_def(L)) {
// TODO: variant argument const auto& modelName = (def->variants ? def->variants->variants[get_variant_index(L, def, 2)].model.name :
const auto& modelName = def->defaults.model.name; def->defaults.model.name);
if (modelName.empty()) { if (modelName.empty()) {
return lua::pushlstring(L, def->name + ".model"); 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) { static int l_get_model(lua::State* L) {
if (auto def = get_block_def(L)) { if (auto def = get_block_def(L)) {
// TODO: variant argument const BlockModelType modelType = (def->variants ? def->variants->variants[get_variant_index(L, def, 2)].model.type :
return lua::pushlstring(L, BlockModelTypeMeta.getName(def->defaults.model.type)); def->defaults.model.type);
return lua::pushlstring(L, BlockModelTypeMeta.getName(modelType));
} }
return 0; return 0;
} }
@ -385,12 +398,13 @@ static int l_get_model(lua::State* L) {
static int l_get_hitbox(lua::State* L) { static int l_get_hitbox(lua::State* L) {
if (auto def = get_block_def(L)) { if (auto def = get_block_def(L)) {
size_t rotation = lua::tointeger(L, 2); size_t rotation = lua::tointeger(L, 2);
const size_t hitboxIndex = static_cast<size_t>(lua::gettop(L) >= 3 ? lua::tointeger(L, 3) : 0);
if (def->rotatable) { if (def->rotatable) {
rotation %= def->rotations.MAX_COUNT; rotation %= def->rotations.MAX_COUNT;
} else { } else {
rotation = 0; rotation = 0;
} }
auto& hitbox = def->rt.hitboxes[rotation].at(0); auto& hitbox = def->rt.hitboxes[rotation].at(hitboxIndex);
lua::createtable(L, 2, 0); lua::createtable(L, 2, 0);
lua::pushvec3(L, hitbox.min()); lua::pushvec3(L, hitbox.min());