From e7501a17accafb22fdc451aa60024465dff4d6db Mon Sep 17 00:00:00 2001 From: Cogi Asd Date: Mon, 8 Jul 2024 20:20:37 +0300 Subject: [PATCH] ref: libcore, libvecn --- src/logic/scripting/lua/libcore.cpp | 32 ------------ src/logic/scripting/lua/libvecn.cpp | 80 ++++++++++------------------- 2 files changed, 27 insertions(+), 85 deletions(-) diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index 6776cf30..2b3e2ca8 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -190,37 +190,6 @@ static int l_get_generators(lua::State* L) { return 1; } -/// @brief Get engine constants -/// @return A table with engine constants -static int l_get_constants(lua::State* L) { - - lua::createtable(L, 0, 20); - - const std::pair numberConstants[] = { - {"ENGINE_VERSION_MAJOR", ENGINE_VERSION_MAJOR}, - {"ENGINE_VERSION_MINOR", ENGINE_VERSION_MINOR}, - {"BLOCK_AIR", BLOCK_AIR}, - {"ITEM_EMPTY", ITEM_EMPTY}, - {"CHUNK_W", CHUNK_W}, - {"CHUNK_H", CHUNK_H}, - {"CHUNK_D", CHUNK_D}, - {"VOXEL_USER_BITS", VOXEL_USER_BITS}, - {"VOXEL_USER_BITS_OFFSET", VOXEL_USER_BITS_OFFSET}, - {"ITEM_ICON_SIZE", ITEM_ICON_SIZE}, - {"CHUNK_VOL", CHUNK_VOL}, - {"BLOCK_VOID", BLOCK_VOID}, - {"ITEM_VOID", ITEM_VOID}, - {"MAX_BLOCKS", MAX_BLOCKS} - }; - - for (const auto& constant : numberConstants) { - lua::pushnumber(L, constant.second); - lua::setfield(L, constant.first); - } - - return 1; -} - const luaL_Reg corelib [] = { {"new_world", lua::wrap}, {"open_world", lua::wrap}, @@ -235,6 +204,5 @@ const luaL_Reg corelib [] = { {"quit", lua::wrap}, {"get_default_generator", lua::wrap}, {"get_generators", lua::wrap}, - {"get_constants", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index 813aae9b..aa4e6142 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -1,43 +1,41 @@ #include "api_lua.hpp" -#define GLM_ENABLE_EXPERIMENTAL #include #include -#include -template class Operation> +template class Op> static int l_binop(lua::State* L) { uint argc = lua::gettop(L); if (argc != 2 && argc != 3) { throw std::runtime_error("invalid arguments number (2 or 3 expected)"); } - const auto& a = lua::tovec(L, 1); + auto a = lua::tovec(L, 1); if (lua::isnumber(L, 2)) { // scalar second operand overload - const auto& b = lua::tonumber(L, 2); - Operation oper; + auto b = lua::tonumber(L, 2); + Op op; if (argc == 2) { lua::createtable(L, n, 0); for (uint i = 0; i < n; i++) { - lua::pushnumber(L, oper(a[i], b)); + lua::pushnumber(L, op(a[i], b)); lua::rawseti(L, i+1); } return 1; } else { - return lua::setvec(L, 3, oper(a, glm::vec(b))); + return lua::setvec(L, 3, op(a, glm::vec(b))); } } else { - const auto& b = lua::tovec(L, 2); - Operation oper; + auto b = lua::tovec(L, 2); + Op op; if (argc == 2) { lua::createtable(L, n, 0); for (uint i = 0; i < n; i++) { - lua::pushnumber(L, oper(a[i], b[i])); + lua::pushnumber(L, op(a[i], b[i])); lua::rawseti(L, i+1); } return 1; } else { - return lua::setvec(L, 3, oper(a, b)); + return lua::setvec(L, 3, op(a, b)); } } } @@ -45,7 +43,7 @@ static int l_binop(lua::State* L) { template(*func)(const glm::vec&)> static int l_unaryop(lua::State* L) { uint argc = lua::gettop(L); - const auto& vec = func(lua::tovec(L, 1)); + auto vec = func(lua::tovec(L, 1)); switch (argc) { case 1: lua::createtable(L, n, 0); @@ -64,10 +62,11 @@ static int l_unaryop(lua::State* L) { template&)> static int l_scalar_op(lua::State* L) { + auto vec = lua::tovec(L, 1); if (lua::gettop(L) != 1) { throw std::runtime_error("invalid arguments number (1 expected)"); } - return lua::pushnumber(L, func(lua::tovec(L, 1)/*vector a*/)); + return lua::pushnumber(L, func(vec)); } template @@ -93,30 +92,6 @@ static int l_dot(lua::State* L) { lua::tovec(L, 2))); } -template -static int l_rotate(lua::State* L) { - if (lua::gettop(L) != 2) { - throw std::runtime_error("invalid arguments number (2 expected)"); - } - const auto& vec = lua::tovec(L, 1); // vector - const auto& angle = lua::tonumber(L, 2); // scalar (angle) - - const auto _cos = std::cos(angle); - const auto _sin = std::sin(angle); - - glm::vec result_vector; - - switch (n) { - case 2: - result_vector.x = vec.x * _cos - vec.y * _sin; - result_vector.y = vec.x * _sin + vec.y * _cos; - break; - // TODO - } - - return lua::setvec(L, 1, result_vector); -} - template static int l_inverse(lua::State* L) { if (lua::gettop(L) != 1) { @@ -132,7 +107,7 @@ static int l_inverse(lua::State* L) { template static int l_tostring(lua::State* L) { - const auto& vec = lua::tovec(L, 1); + auto vec = lua::tovec(L, 1); if (lua::gettop(L) != 1) { throw std::runtime_error("invalid arguments number (1 expected)"); } @@ -153,13 +128,12 @@ const luaL_Reg vec2lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"norm", lua::wrap>}, - {"len", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, + {"tostring", lua::wrap>}, {"abs", lua::wrap>}, {"round", lua::wrap>}, - {"tostring", lua::wrap>}, - {"inv", lua::wrap>}, - {"rot", lua::wrap>}, + {"inverse", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, {NULL, NULL} @@ -170,12 +144,12 @@ const luaL_Reg vec3lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"norm", lua::wrap>}, - {"len", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, + {"tostring", lua::wrap>}, {"abs", lua::wrap>}, {"round", lua::wrap>}, - {"tostring", lua::wrap>}, - {"inv", lua::wrap>}, + {"inverse", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, {NULL, NULL} @@ -186,12 +160,12 @@ const luaL_Reg vec4lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"abs", lua::wrap>}, - {"norm", lua::wrap>}, - {"len", lua::wrap>}, - {"round", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, {"tostring", lua::wrap>}, - {"inv", lua::wrap>}, + {"abs", lua::wrap>}, + {"round", lua::wrap>}, + {"inverse", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, {NULL, NULL}