diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 680ace15..8bdafb22 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -13,29 +13,32 @@ #include "../../../content/Content.hpp" #include "../../../logic/BlocksController.hpp" -int l_block_name(lua_State* L) { +static Block* require_block(lua_State* L) { auto indices = scripting::content->getIndices(); lua_Integer id = lua_tointeger(L, 1); if (static_cast(id) >= indices->countBlockDefs()) { - return 0; + return nullptr; } - auto def = indices->getBlockDef(id); - lua_pushstring(L, def->name.c_str()); - return 1; + return indices->getBlockDef(id); } -int l_block_material(lua_State* L) { - auto indices = scripting::content->getIndices(); - lua_Integer id = lua_tointeger(L, 1); - if (static_cast(id) >= indices->countBlockDefs()) { - return 0; +static int l_name(lua_State* L) { + if (auto def = require_block(L)) { + lua_pushstring(L, def->name.c_str()); + return 1; } - auto def = indices->getBlockDef(id); - lua_pushstring(L, def->material.c_str()); - return 1; + return 0; } -int l_is_solid_at(lua_State* L) { +static int l_material(lua_State* L) { + if (auto def = require_block(L)) { + lua_pushstring(L, def->material.c_str()); + return 1; + } + return 0; +} + +static int l_is_solid_at(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -44,18 +47,53 @@ int l_is_solid_at(lua_State* L) { return 1; } -int l_blocks_count(lua_State* L) { +static int l_count(lua_State* L) { lua_pushinteger(L, scripting::indices->countBlockDefs()); return 1; } -int l_block_index(lua_State* L) { +static int l_index(lua_State* L) { std::string name = lua_tostring(L, 1); lua_pushinteger(L, scripting::content->requireBlock(name).rt.id); return 1; } -int l_set_block(lua_State* L) { +static int l_is_extended(lua_State* L) { + if (auto def = require_block(L)) { + lua_pushboolean(L, def->rt.extended); + return 1; + } + return 0; +} + +static int l_get_size(lua_State* L) { + if (auto def = require_block(L)) { + return lua::pushivec3(L, def->size.x, def->size.y, def->size.z); + } + return 0; +} + +static int l_is_segment(lua_State* L) { + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); + auto vox = scripting::level->chunks->get(x, y, z); + + lua_pushboolean(L, vox->state.segment); + return 1; +} + +static int l_seek_origin(lua_State* L) { + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); + auto vox = scripting::level->chunks->get(x, y, z); + auto def = scripting::indices->getBlockDef(vox->id); + + return lua::pushivec3(L, scripting::level->chunks->seekOrigin({x, y, z}, def, vox->state)); +} + +static int l_set(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -76,7 +114,7 @@ int l_set_block(lua_State* L) { return 0; } -int l_get_block(lua_State* L) { +static int l_get(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -86,7 +124,7 @@ int l_get_block(lua_State* L) { return 1; } -int l_get_block_x(lua_State* L) { +static int l_get_x(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -103,7 +141,7 @@ int l_get_block_x(lua_State* L) { } } -int l_get_block_y(lua_State* L) { +static int l_get_y(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -120,7 +158,7 @@ int l_get_block_y(lua_State* L) { } } -int l_get_block_z(lua_State* L) { +static int l_get_z(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -137,7 +175,7 @@ int l_get_block_z(lua_State* L) { } } -int l_get_block_rotation(lua_State* L) { +static int l_get_rotation(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -147,7 +185,7 @@ int l_get_block_rotation(lua_State* L) { return 1; } -int l_set_block_rotation(lua_State* L) { +static int l_set_rotation(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -156,7 +194,7 @@ int l_set_block_rotation(lua_State* L) { return 0; } -int l_get_block_states(lua_State* L) { +static int l_get_states(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -166,7 +204,7 @@ int l_get_block_states(lua_State* L) { return 1; } -int l_set_block_states(lua_State* L) { +static int l_set_states(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -182,7 +220,7 @@ int l_set_block_states(lua_State* L) { return 0; } -int l_get_block_user_bits(lua_State* L) { +static int l_get_user_bits(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -200,7 +238,7 @@ int l_get_block_user_bits(lua_State* L) { return 1; } -int l_set_block_user_bits(lua_State* L) { +static int l_set_user_bits(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -223,7 +261,7 @@ int l_set_block_user_bits(lua_State* L) { return 0; } -int l_is_replaceable_at(lua_State* L) { +static int l_is_replaceable_at(lua_State* L) { lua_Integer x = lua_tointeger(L, 1); lua_Integer y = lua_tointeger(L, 2); lua_Integer z = lua_tointeger(L, 3); @@ -232,35 +270,36 @@ int l_is_replaceable_at(lua_State* L) { return 1; } -int l_block_caption(lua_State* L) { - auto indices = scripting::content->getIndices(); - lua_Integer id = lua_tointeger(L, 1); - if (static_cast(id) >= indices->countBlockDefs()) { - return 0; +static int l_caption(lua_State* L) { + if (auto def = require_block(L)) { + lua_pushstring(L, def->caption.c_str()); + return 1; } - auto def = indices->getBlockDef(id); - lua_pushstring(L, def->caption.c_str()); - return 1; + return 0; } const luaL_Reg blocklib [] = { - {"index", lua_wrap_errors}, - {"name", lua_wrap_errors}, - {"material", lua_wrap_errors}, - {"caption", lua_wrap_errors}, - {"defs_count", lua_wrap_errors}, + {"index", lua_wrap_errors}, + {"name", lua_wrap_errors}, + {"material", lua_wrap_errors}, + {"caption", lua_wrap_errors}, + {"defs_count", lua_wrap_errors}, {"is_solid_at", lua_wrap_errors}, {"is_replaceable_at", lua_wrap_errors}, - {"set", lua_wrap_errors}, - {"get", lua_wrap_errors}, - {"get_X", lua_wrap_errors}, - {"get_Y", lua_wrap_errors}, - {"get_Z", lua_wrap_errors}, - {"get_states", lua_wrap_errors}, - {"set_states", lua_wrap_errors}, - {"get_rotation", lua_wrap_errors}, - {"set_rotation", lua_wrap_errors}, - {"get_user_bits", lua_wrap_errors}, - {"set_user_bits", lua_wrap_errors}, + {"set", lua_wrap_errors}, + {"get", lua_wrap_errors}, + {"get_X", lua_wrap_errors}, + {"get_Y", lua_wrap_errors}, + {"get_Z", lua_wrap_errors}, + {"get_states", lua_wrap_errors}, + {"set_states", lua_wrap_errors}, + {"get_rotation", lua_wrap_errors}, + {"set_rotation", lua_wrap_errors}, + {"get_user_bits", lua_wrap_errors}, + {"set_user_bits", lua_wrap_errors}, + {"is_extended", lua_wrap_errors}, + {"get_size", lua_wrap_errors}, + {"is_segment", lua_wrap_errors}, + {"seek_origin", lua_wrap_errors}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 0c4de9f3..799bd3d4 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -21,6 +21,13 @@ namespace lua { return 3; } + inline int pushivec3(lua_State* L, glm::ivec3 vec) { + lua_pushinteger(L, vec.x); + lua_pushinteger(L, vec.y); + lua_pushinteger(L, vec.z); + return 3; + } + inline int pushvec3(lua_State* L, glm::vec3 vec) { lua_pushnumber(L, vec.x); lua_pushnumber(L, vec.y);