From 9a43940bafe89a00d7e92efbb6e59488e53f9753 Mon Sep 17 00:00:00 2001 From: Cogi Asd Date: Mon, 8 Jul 2024 14:32:59 +0300 Subject: [PATCH] ref stdcmd.lua, libvecn.cpp --- res/scripts/stdcmd.lua | 54 ++++++++++++++++++++++++-- src/logic/scripting/lua/libvecn.cpp | 60 +++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 16 deletions(-) diff --git a/res/scripts/stdcmd.lua b/res/scripts/stdcmd.lua index 33f5721b..4368174b 100644 --- a/res/scripts/stdcmd.lua +++ b/res/scripts/stdcmd.lua @@ -26,24 +26,34 @@ console.add_command( "help name:str=''", "Show help infomation for the specified command", function (args, kwargs) + local name = args[1] if #name == 0 then + local commands = console.get_commands_list() table.sort(commands) local str = "Available commands:" + for i,k in ipairs(commands) do str = str.."\n "..build_scheme(console.get_command_info(k)) end + return str.."\nuse 'help '" + end + local command = console.get_command_info(name) + if command == nil then return string.format("command %q not found", name) end - local where = "" + + local where = ":" local str = SEPARATOR.."\n"..command.description.."\n"..name.." " - for i,arg in ipairs(command.args) do + + for _, arg in ipairs(command.args) do where = where.."\n "..arg.name.." - "..arg.type + if arg.optional then str = str.."["..arg.name.."] " where = where.." (optional)" @@ -51,11 +61,47 @@ console.add_command( str = str.."<"..arg.name.."> " end end - if #command.args then + + if #command.args > 0 then str = str.."\nwhere"..where end - + return str.."\n"..SEPARATOR + + end +) + +console.add_command( + "time.uptime", + "Get time elapsed since the engine started", + function() + + local uptime = time.uptime() + local years = math.floor(uptime / 31536000) + local days = math.floor((uptime % 31536000) / 86400) % 365 + local hours = math.floor((uptime % 86400) / 3600) % 24 + local minutes = math.floor((uptime % 3600) / 60) % 60 + local seconds = math.floor(uptime % 60) + + local formatted_uptime = "" + + if years > 0 then + formatted_uptime = formatted_uptime .. years .. "y " + end + if days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. days .. "d " + end + if hours > 0 or days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. hours .. "h " + end + if minutes > 0 or hours > 0 or days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. minutes .. "m " + end + if seconds > 0 or minutes > 0 or hours > 0 or days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. seconds .. "s" + end + + return uptime .. " (" .. formatted_uptime .. ")" end ) diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index 0837a03b..38e9c0c8 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -3,7 +3,7 @@ #include #include -template class Op> +template class Operation> static int l_binop(lua::State* L) { uint argc = lua::gettop(L); if (argc != 2 && argc != 3) { @@ -13,7 +13,7 @@ static int l_binop(lua::State* L) { if (lua::isnumber(L, 2)) { // scalar second operand overload auto b = lua::tonumber(L, 2); - Op op; + Operation op; if (argc == 2) { lua::createtable(L, n, 0); for (uint i = 0; i < n; i++) { @@ -26,7 +26,7 @@ static int l_binop(lua::State* L) { } } else { auto b = lua::tovec(L, 2); - Op op; + Operation op; if (argc == 2) { lua::createtable(L, n, 0); for (uint i = 0; i < n; i++) { @@ -62,11 +62,38 @@ 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(vec)); + return lua::pushnumber(L, func(lua::tovec(L, 1)/*vector a*/)); +} + +template +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) + 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); +} + +template +static int l_dot(lua::State* L) { + 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, result_vector); } template @@ -92,9 +119,12 @@ const luaL_Reg vec2lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"norm", lua::wrap>}, - {"len", lua::wrap>}, + {"norm", lua::wrap>}, + {"len", lua::wrap>}, + {"abs", lua::wrap>}, {"tostring", lua::wrap>}, + {"pow", lua::wrap>}, + {"dot", lua::wrap>}, {NULL, NULL} }; @@ -103,9 +133,12 @@ const luaL_Reg vec3lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"norm", lua::wrap>}, - {"len", lua::wrap>}, + {"norm", lua::wrap>}, + {"len", lua::wrap>}, + {"abs", lua::wrap>}, {"tostring", lua::wrap>}, + {"pow", lua::wrap>}, + {"dot", lua::wrap>}, {NULL, NULL} }; @@ -114,8 +147,11 @@ const luaL_Reg vec4lib [] = { {"sub", lua::wrap>}, {"mul", lua::wrap>}, {"div", lua::wrap>}, - {"norm", lua::wrap>}, - {"len", lua::wrap>}, + {"abs", lua::wrap>}, + {"norm", lua::wrap>}, + {"len", lua::wrap>}, {"tostring", lua::wrap>}, + {"pow", lua::wrap>}, + {"dot", lua::wrap>}, {NULL, NULL} -}; +}; \ No newline at end of file