From 5ede84edc71558c371cef249c187e7193a8d5fcd Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 25 Jun 2024 17:25:42 +0300 Subject: [PATCH 1/3] add vec2, vec3, vec4 libraries --- src/logic/scripting/lua/api_lua.hpp | 3 ++ src/logic/scripting/lua/libmat4.cpp | 4 +-- src/logic/scripting/lua/libvecn.cpp | 43 ++++++++++++++++++++++++++ src/logic/scripting/lua/lua_engine.cpp | 3 ++ src/logic/scripting/lua/lua_util.hpp | 15 ++------- 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 src/logic/scripting/lua/libvecn.cpp diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index 83130ec2..592340c3 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -22,6 +22,9 @@ extern const luaL_Reg packlib []; extern const luaL_Reg playerlib []; extern const luaL_Reg timelib []; extern const luaL_Reg tomllib []; +extern const luaL_Reg vec2lib []; +extern const luaL_Reg vec3lib []; +extern const luaL_Reg vec4lib []; extern const luaL_Reg worldlib []; // Lua Overrides diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libmat4.cpp index 16cc2098..17ab752b 100644 --- a/src/logic/scripting/lua/libmat4.cpp +++ b/src/logic/scripting/lua/libmat4.cpp @@ -60,9 +60,9 @@ static int l_mul(lua::State* L) { } case 3: { if (len2 == 4) { - return lua::setvec4(L, 3, matrix1 * lua::tovec4(L, 2)); + return lua::setvec(L, 3, matrix1 * lua::tovec4(L, 2)); } else if (len2 == 3) { - return lua::setvec3(L, 3, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f)); + return lua::setvec(L, 3, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f)); } return lua::setmat4(L, 3, matrix1 * lua::tomat4(L, 2)); } diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp new file mode 100644 index 00000000..62f4b163 --- /dev/null +++ b/src/logic/scripting/lua/libvecn.cpp @@ -0,0 +1,43 @@ +#include "api_lua.hpp" + +#include + +template< + uint n, + glm::vec(*tofunc)(lua::State*, int), + int(*setfunc)(lua::State*, int, glm::vec) +> +static int l_add(lua::State* L) { + uint argc = lua::gettop(L); + auto a = tofunc(L, 1); + auto b = tofunc(L, 2); + switch (argc) { + case 2: + lua::createtable(L, n, 0); + for (uint i = 0; i < n; i++) { + lua::pushnumber(L, a[i]+b[i]); + lua::rawseti(L, i+1); + } + return 1; + case 3: + return setfunc(L, 3, a + b); + default: { + throw std::runtime_error("invalid arguments number (2 or 3 expected)"); + } + } +} + +const luaL_Reg vec2lib [] = { + {"add", lua::wrap>>}, + {NULL, NULL} +}; + +const luaL_Reg vec3lib [] = { + {"add", lua::wrap>>}, + {NULL, NULL} +}; + +const luaL_Reg vec4lib [] = { + {"add", lua::wrap>>}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index a64216a6..92e59285 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -42,6 +42,9 @@ static void create_libs(lua::State* L) { openlib(L, "player", playerlib); openlib(L, "time", timelib); openlib(L, "toml", tomllib); + openlib(L, "vec2", vec2lib); + openlib(L, "vec3", vec3lib); + openlib(L, "vec4", vec4lib); openlib(L, "world", worldlib); addfunc(L, "print", lua::wrap); diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index a774d283..308ef83e 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -221,19 +221,10 @@ namespace lua { } return 1; } - /// @brief pushes vector table to the stack and updates it with glm vec4 - inline int setvec4(lua::State* L, int idx, glm::vec4 vec) { + template + inline int setvec(lua::State* L, int idx, glm::vec vec) { pushvalue(L, idx); - for (uint i = 0; i < 4; i++) { - pushnumber(L, vec[i]); - rawseti(L, i+1); - } - return 1; - } - /// @brief pushes vector table to the stack and updates it with glm vec3 - inline int setvec3(lua::State* L, int idx, glm::vec3 vec) { - pushvalue(L, idx); - for (uint i = 0; i < 3; i++) { + for (int i = 0; i < n; i++) { pushnumber(L, vec[i]); rawseti(L, i+1); } From c7db73e25b75092466451b171a65b55637bb2e3a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 25 Jun 2024 18:24:48 +0300 Subject: [PATCH 2/3] add vecn.sub(...), .mul(...), .div(...), .length(...), .normalize(...) --- src/logic/scripting/lua/libvecn.cpp | 67 ++++++++++++++++++++++------ src/logic/scripting/lua/lua_util.hpp | 16 +++++++ 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index 62f4b163..a2a8a4a5 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -2,42 +2,83 @@ #include -template< - uint n, - glm::vec(*tofunc)(lua::State*, int), - int(*setfunc)(lua::State*, int, glm::vec) -> -static int l_add(lua::State* L) { +template class Op> +static int l_binop(lua::State* L) { uint argc = lua::gettop(L); - auto a = tofunc(L, 1); - auto b = tofunc(L, 2); + auto a = lua::tovec(L, 1); + auto b = lua::tovec(L, 2); + Op op; switch (argc) { case 2: lua::createtable(L, n, 0); for (uint i = 0; i < n; i++) { - lua::pushnumber(L, a[i]+b[i]); + lua::pushnumber(L, op(a[i], b[i])); lua::rawseti(L, i+1); } return 1; case 3: - return setfunc(L, 3, a + b); + return lua::setvec(L, 3, op(a, b)); default: { throw std::runtime_error("invalid arguments number (2 or 3 expected)"); } } } +template +static int l_normalize(lua::State* L) { + uint argc = lua::gettop(L); + auto vec = glm::normalize(lua::tovec(L, 1)); + switch (argc) { + case 1: + lua::createtable(L, n, 0); + for (uint i = 0; i < n; i++) { + lua::pushnumber(L, vec[i]); + lua::rawseti(L, i+1); + } + return 1; + case 3: + return lua::setvec(L, 3, vec); + default: { + throw std::runtime_error("invalid arguments number (1 or 2 expected)"); + } + } +} + +template +static int l_len(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, glm::length(vec)); +} + const luaL_Reg vec2lib [] = { - {"add", lua::wrap>>}, + {"add", lua::wrap>}, + {"sub", lua::wrap>}, + {"mul", lua::wrap>}, + {"div", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, {NULL, NULL} }; const luaL_Reg vec3lib [] = { - {"add", lua::wrap>>}, + {"add", lua::wrap>}, + {"sub", lua::wrap>}, + {"mul", lua::wrap>}, + {"div", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, {NULL, NULL} }; const luaL_Reg vec4lib [] = { - {"add", lua::wrap>>}, + {"add", lua::wrap>}, + {"sub", lua::wrap>}, + {"mul", lua::wrap>}, + {"div", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 308ef83e..82e71d24 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -328,6 +328,22 @@ namespace lua { setglobal(L, name); } + template + inline glm::vec tovec(lua::State* L, int idx) { + pushvalue(L, idx); + if (!istable(L, idx) || objlen(L, idx) < n) { + throw std::runtime_error("value must be an array of "+std::to_string(n)+" numbers"); + } + glm::vec vec; + for (int i = 0; i < n; i++) { + rawgeti(L, 1); + vec[i] = tonumber(L, -1); + pop(L); + } + pop(L); + return vec; + } + inline glm::vec2 tovec2(lua::State* L, int idx) { pushvalue(L, idx); if (!istable(L, idx) || objlen(L, idx) < 2) { From 0c4b9784669d60f503d01490b958532df9231b26 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 25 Jun 2024 18:37:57 +0300 Subject: [PATCH 3/3] refactor libvecn --- src/logic/scripting/lua/libvecn.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index a2a8a4a5..61155148 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -24,10 +24,10 @@ static int l_binop(lua::State* L) { } } -template -static int l_normalize(lua::State* L) { +template(*func)(const glm::vec&)> +static int l_unaryop(lua::State* L) { uint argc = lua::gettop(L); - auto vec = glm::normalize(lua::tovec(L, 1)); + auto vec = func(lua::tovec(L, 1)); switch (argc) { case 1: lua::createtable(L, n, 0); @@ -44,13 +44,13 @@ static int l_normalize(lua::State* L) { } } -template -static int l_len(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, glm::length(vec)); + return lua::pushnumber(L, func(vec)); } const luaL_Reg vec2lib [] = { @@ -58,8 +58,8 @@ const luaL_Reg vec2lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"normalize", lua::wrap>}, - {"length", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, {NULL, NULL} }; @@ -68,8 +68,8 @@ const luaL_Reg vec3lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"normalize", lua::wrap>}, - {"length", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, {NULL, NULL} }; @@ -78,7 +78,7 @@ const luaL_Reg vec4lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"normalize", lua::wrap>}, - {"length", lua::wrap>}, + {"normalize", lua::wrap>}, + {"length", lua::wrap>}, {NULL, NULL} };