From 9c45f187f83087a7174eb9dc09e88cf4406ad3b0 Mon Sep 17 00:00:00 2001 From: Cogi Asd Date: Mon, 8 Jul 2024 14:54:19 +0300 Subject: [PATCH] ref vectors --- src/logic/scripting/lua/libvecn.cpp | 40 +++++++++++++++++++---------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index 38e9c0c8..a6d1ef18 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -9,10 +9,10 @@ static int l_binop(lua::State* L) { if (argc != 2 && argc != 3) { throw std::runtime_error("invalid arguments number (2 or 3 expected)"); } - auto a = lua::tovec(L, 1); + const auto& a = lua::tovec(L, 1); if (lua::isnumber(L, 2)) { // scalar second operand overload - auto b = lua::tonumber(L, 2); + const auto& b = lua::tonumber(L, 2); Operation op; if (argc == 2) { lua::createtable(L, n, 0); @@ -25,7 +25,7 @@ static int l_binop(lua::State* L) { return lua::setvec(L, 3, op(a, glm::vec(b))); } } else { - auto b = lua::tovec(L, 2); + const auto& b = lua::tovec(L, 2); Operation op; if (argc == 2) { lua::createtable(L, n, 0); @@ -43,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); - auto vec = func(lua::tovec(L, 1)); + const auto& vec = func(lua::tovec(L, 1)); switch (argc) { case 1: lua::createtable(L, n, 0); @@ -73,8 +73,8 @@ static int l_pow(lua::State* L) { if (lua::gettop(L) != 2) { throw std::runtime_error("invalid arguments number (2 expected)"); } - auto a = lua::tovec(L, 1); //vector - auto b = lua::tonumber(L, 2); //scalar (pow) + const auto& a = lua::tovec(L, 1); //vector + const auto& b = lua::tonumber(L, 2); //scalar (pow) glm::vec result_vector; for (int i = 0; i < n; i++) { result_vector[i] = pow(a[i], b); @@ -84,21 +84,30 @@ static int l_pow(lua::State* L) { template static int l_dot(lua::State* L) { - if (lua::gettop(L)!= 2) { + if (lua::gettop(L) != 2) { throw std::runtime_error("invalid arguments number (2 expected)"); } - auto a = lua::tovec(L, 1); - auto b = lua::tovec(L, 2); - float result_vector = 0; - for (int i = 0; i < n; i++) { - result_vector += a[i] * b[i]; + return lua::pushnumber(L, glm::dot(lua::tovec(L, 1), // vector a + lua::tovec(L, 2) // vector b + )); +} + +template +static int l_round(lua::State* L) { + if (lua::gettop(L)!= 1) { + throw std::runtime_error("invalid arguments number (1 expected)"); } - return lua::pushnumber(L, result_vector); + const auto& vec = lua::tovec(L, 1); + glm::vec rounded_vector; + for (int i = 0; i < n; i++) { + rounded_vector[i] = std::round(vec[i]); + } + return lua::setvec(L, 1, rounded_vector); } template static int l_tostring(lua::State* L) { - auto vec = lua::tovec(L, 1); + const auto& vec = lua::tovec(L, 1); if (lua::gettop(L) != 1) { throw std::runtime_error("invalid arguments number (1 expected)"); } @@ -125,6 +134,7 @@ const luaL_Reg vec2lib [] = { {"tostring", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, + {"round", lua::wrap>}, {NULL, NULL} }; @@ -139,6 +149,7 @@ const luaL_Reg vec3lib [] = { {"tostring", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, + {"round", lua::wrap>}, {NULL, NULL} }; @@ -153,5 +164,6 @@ const luaL_Reg vec4lib [] = { {"tostring", lua::wrap>}, {"pow", lua::wrap>}, {"dot", lua::wrap>}, + {"round", lua::wrap>}, {NULL, NULL} }; \ No newline at end of file