From c8dc491e4e6248f574296c196a19aa8a84145c79 Mon Sep 17 00:00:00 2001 From: Cogi Asd Date: Mon, 8 Jul 2024 21:43:54 +0300 Subject: [PATCH] add: overloading for functions in libvecn.cpp --- src/logic/scripting/lua/libvecn.cpp | 35 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index aa4e6142..64a416fb 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -71,31 +71,44 @@ static int l_scalar_op(lua::State* L) { template static int l_pow(lua::State* L) { - if (lua::gettop(L) != 2) { + uint argc = lua::gettop(L); + if (argc != 2) { throw std::runtime_error("invalid arguments number (2 expected)"); } 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); + if (lua::isnumber(L, 2)) { // scalar second operand overload + 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); + } + return lua::setvec(L, 1, result_vector); + } else { + const auto& b = lua::tovec(L, 2); //vector + glm::vec result_vector; + for (int i = 0; i < n; i++) { + result_vector[i] = pow(a[i], b[i]); + } + return lua::setvec(L, 1, result_vector); } - return lua::setvec(L, 1, result_vector); } template static int l_dot(lua::State* L) { - if (lua::gettop(L) != 2) { + uint argc = lua::gettop(L); + if (argc != 2) { throw std::runtime_error("invalid arguments number (2 expected)"); } - return lua::pushnumber(L, glm::dot(lua::tovec(L, 1), - lua::tovec(L, 2))); + const auto& a = lua::tovec(L, 1); + const auto& b = lua::tovec(L, 2); + return lua::pushnumber(L, glm::dot(a, b)); } template static int l_inverse(lua::State* L) { - if (lua::gettop(L) != 1) { - throw std::runtime_error("invalid arguments number (2 expected)"); + uint argc = lua::gettop(L); + if (argc != 1) { + throw std::runtime_error("invalid arguments number (1 expected)"); } const auto& _vector = lua::tovec(L, 1); //vector glm::vec result_vector;