From e74f65eb70077d05d4a212b63d71fd54e42415dd Mon Sep 17 00:00:00 2001 From: Mihail Date: Wed, 31 Jul 2024 13:37:46 +0400 Subject: [PATCH 1/4] pcall(function, varargs) --- res/layouts/console.xml.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/res/layouts/console.xml.lua b/res/layouts/console.xml.lua index e23ce357..819ac1fd 100644 --- a/res/layouts/console.xml.lua +++ b/res/layouts/console.xml.lua @@ -45,8 +45,8 @@ function submit(text) setup_variables() document.log.caret = -1 - local status, result = pcall(function() return console.execute(text) end) - if result ~= nil then + local status, result = pcall(console.execute, text) + if result then console.log(result) end document.prompt.text = "" From f4f8caec435426cec3944fb590bdfa6eb7581de6 Mon Sep 17 00:00:00 2001 From: Mihail Date: Wed, 31 Jul 2024 15:25:58 +0400 Subject: [PATCH 2/4] a lot stuff changed --- res/scripts/stdcmd.lua | 105 ++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/res/scripts/stdcmd.lua b/res/scripts/stdcmd.lua index 1933ae03..95ba2639 100644 --- a/res/scripts/stdcmd.lua +++ b/res/scripts/stdcmd.lua @@ -1,13 +1,13 @@ local SEPARATOR = "________________" -SEPARATOR = SEPARATOR..SEPARATOR..SEPARATOR +SEPARATOR = SEPARATOR .. SEPARATOR .. SEPARATOR function build_scheme(command) - local str = command.name.." " + local str = command.name .. " " for i,arg in ipairs(command.args) do if arg.optional then - str = str.."["..arg.name.."] " + str = str .. "[" .. arg.name .. "] " else - str = str.."<"..arg.name.."> " + str = str .. "<" .. arg.name .. "> " end end return str @@ -16,7 +16,7 @@ end console.add_command( "clear", "Clears the console", - function () + function() local document = Document.new("core:console") document.log.text = "" end @@ -25,8 +25,7 @@ console.add_command( console.add_command( "help name:str=''", "Show help infomation for the specified command", - function (args, kwargs) - + function(args, kwargs) local name = args[1] if #name == 0 then @@ -35,10 +34,10 @@ console.add_command( local str = "Available commands:" for i,k in ipairs(commands) do - str = str.."\n "..build_scheme(console.get_command_info(k)) + str = str .. "\n " .. build_scheme(console.get_command_info(k)) end - return str.."\nuse 'help '" + return str .. "\nuse 'help '" end @@ -49,69 +48,64 @@ console.add_command( end local where = ":" - local str = SEPARATOR.."\n"..command.description.."\n"..name.." " + local str = SEPARATOR .. "\n" .. command.description .. "\n" .. name .. " " for _, arg in ipairs(command.args) do - where = where.."\n "..arg.name.." - "..arg.type + where = where .. "\n " .. arg.name .. " - " .. arg.type if arg.optional then - str = str.."["..arg.name.."] " - where = where.." (optional)" + str = str .. "[" .. arg.name .. "] " + where = where .. " (optional)" else - str = str.."<"..arg.name.."> " + str = str .. "<" .. arg.name .. "> " end end if #command.args > 0 then - str = str.."\nwhere"..where + str = str .. "\nwhere" .. where end - return str.."\n"..SEPARATOR - + return str .. "\n" .. SEPARATOR end ) +local function FormattedTime(seconds, format ) -- from gmod + if not seconds then seconds = 0 end + + local hours = math.floor(seconds / 3600) + local minutes = math.floor((seconds / 60) % 60) + local millisecs = (seconds - math.floor(seconds)) * 100 + seconds = math.floor(seconds % 60) + + if format then + return string.format(format, minutes, seconds, millisecs) + else + return {h = hours, m = minutes, s = seconds, ms = millisecs} + end +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 + local t = FormattedTime(uptime) - return uptime .. " (" .. formatted_uptime .. ")" + formatted_uptime = t.h .. "h " .. t.m .. "m " .. t.s .. "s" + + return formatted_uptime .. " (" .. uptime .. "s)" end ) console.add_command( "tp entity:sel=$entity.id x:num~pos.x y:num~pos.y z:num~pos.z", "Teleport entity", - function (args, kwargs) + function(args, kwargs) local eid, x, y, z = unpack(args) local entity = entities.get(eid) - if entity ~= nil then + if entity then entity.transform:set_pos({x, y, z}) end end @@ -119,53 +113,54 @@ console.add_command( console.add_command( "echo value:str", "Print value to the console", - function (args, kwargs) + function(args, kwargs) return args[1] end ) console.add_command( "time.set value:num", "Set day time [0..1] where 0 is midnight, 0.5 is noon", - function (args, kwargs) - return world.set_day_time(args[1]) + function(args, kwargs) + world.set_day_time(args[1]) + return "Time set to " .. args[1] end ) console.add_command( "blocks.fill id:str x:num~pos.x y:num~pos.y z:num~pos.z w:int h:int d:int", "Fill specified zone with blocks", - function (args, kwargs) + function(args, kwargs) local name, x, y, z, w, h, d = unpack(args) local id = block.index(name) - for ly=0,h-1 do - for lz=0,d-1 do - for lx=0,w-1 do - block.set(x+lx, y+ly, z+lz, id) + for ly = 0, h - 1 do + for lz = 0, d - 1 do + for lx = 0, w - 1 do + block.set(x + lx, y + ly, z + lz, id) end end end - return tostring(w*h*d).." blocks set" + return tostring(w * h * d) .. " blocks set" end ) console.add_command( "player.respawn player:sel=$obj.id", "Respawn player entity", - function (args, kwargs) + function(args, kwargs) local eid = entities.spawn("base:player", {player.get_pos(args[1])}):get_uid() player.set_entity(args[1], eid) - return "spawned new player entity #"..tostring(eid) + return "spawned new player entity #" .. tostring(eid) end ) console.add_command( "entity.despawn entity:sel=$entity.selected", "Despawn entity", - function (args, kwargs) + function(args, kwargs) local eid = args[1] local entity = entities.get(eid) if entity ~= nil then entity:despawn() - return "despawned entity #"..tostring(eid) + return "despawned entity #" .. tostring(eid) end end ) From 3a1ae57bbfc42c9c0c33b36d00fe0f61e76caf7f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 31 Jul 2024 14:45:09 +0300 Subject: [PATCH 3/4] fix: mat4.decompose (-Wmaybe-uninitialized) --- doc/en/scripting/builtins/libmat4.md | 2 +- doc/ru/scripting/builtins/libmat4.md | 2 +- src/logic/scripting/lua/libmat4.cpp | 39 ++++++++++++++-------------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/doc/en/scripting/builtins/libmat4.md b/doc/en/scripting/builtins/libmat4.md index e652d2aa..0c4b437e 100644 --- a/doc/en/scripting/builtins/libmat4.md +++ b/doc/en/scripting/builtins/libmat4.md @@ -111,7 +111,7 @@ mat4.decompose(m: matrix) translation=vec3, skew=vec3, perspective=vec4 -} +} or nil ``` ## Look at point - *mat4.look_at(...)* diff --git a/doc/ru/scripting/builtins/libmat4.md b/doc/ru/scripting/builtins/libmat4.md index e30ca0b6..e6f3a3b1 100644 --- a/doc/ru/scripting/builtins/libmat4.md +++ b/doc/ru/scripting/builtins/libmat4.md @@ -111,7 +111,7 @@ mat4.decompose(m: matrix) translation=vec3, skew=vec3, perspective=vec4 -} +} или nil ``` ## Отслеживание точки *mat4.look_at(...)* diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libmat4.cpp index 8eaa3695..9ab0dde2 100644 --- a/src/logic/scripting/lua/libmat4.cpp +++ b/src/logic/scripting/lua/libmat4.cpp @@ -165,7 +165,7 @@ static int l_transpose(lua::State* L) { /// translation=float[3], /// skew=float[3], /// perspective=float[4] -/// } +/// } or nil static int l_decompose(lua::State* L) { auto matrix = lua::tomat4(L, 1); glm::vec3 scale; @@ -173,35 +173,36 @@ static int l_decompose(lua::State* L) { glm::vec3 translation; glm::vec3 skew; glm::vec4 perspective; - glm::decompose( + if (glm::decompose( matrix, scale, rotation, translation, skew, perspective - ); + )) { + lua::createtable(L, 0, 6); + + lua::pushvec3(L, scale); + lua::setfield(L, "scale"); - lua::createtable(L, 0, 6); - - lua::pushvec3(L, scale); - lua::setfield(L, "scale"); + lua::pushmat4(L, glm::toMat4(rotation)); + lua::setfield(L, "rotation"); - lua::pushmat4(L, glm::toMat4(rotation)); - lua::setfield(L, "rotation"); + lua::pushquat(L, rotation); + lua::setfield(L, "quaternion"); - lua::pushquat(L, rotation); - lua::setfield(L, "quaternion"); + lua::pushvec3(L, translation); + lua::setfield(L, "translation"); - lua::pushvec3(L, translation); - lua::setfield(L, "translation"); + lua::pushvec3(L, skew); + lua::setfield(L, "skew"); - lua::pushvec3(L, skew); - lua::setfield(L, "skew"); - - lua::pushvec4(L, perspective); - lua::setfield(L, "perspective"); - return 1; + lua::pushvec4(L, perspective); + lua::setfield(L, "perspective"); + return 1; + } + return 0; } static int l_look_at(lua::State* L) { From eefaafd1e7c23de9124bab56e77d24b5e4657810 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 31 Jul 2024 14:56:42 +0300 Subject: [PATCH 4/4] update block.compose_state, block.decompose_state semantics --- doc/en/scripting/builtins/libblock.md | 4 ++-- doc/ru/scripting/builtins/libblock.md | 4 ++-- src/logic/scripting/lua/libblock.cpp | 20 ++++++++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/doc/en/scripting/builtins/libblock.md b/doc/en/scripting/builtins/libblock.md index 6346f9d2..2c2c0182 100644 --- a/doc/en/scripting/builtins/libblock.md +++ b/doc/en/scripting/builtins/libblock.md @@ -34,10 +34,10 @@ block.place(x: int, y: int, z: int, id: int, states: int, [optional] playerid: i block.destruct(x: int, y: int, z: int, playerid: int) -- Compose the complete state as an integer -block.compose_state(rotation: int, segment: int, userbits: int) -> int +block.compose_state(state: {rotation: int, segment: int, userbits: int}) -> int -- Decompose the complete state into: rotation, segment, user bits -block.decompose_state(state: int) -> int, int, int +block.decompose_state(state: int) -> {int, int, int} ``` > [!WARNING] diff --git a/doc/ru/scripting/builtins/libblock.md b/doc/ru/scripting/builtins/libblock.md index c89cc5c5..d4d28a28 100644 --- a/doc/ru/scripting/builtins/libblock.md +++ b/doc/ru/scripting/builtins/libblock.md @@ -33,10 +33,10 @@ block.place(x: int, y: int, z: int, id: int, states: int, [optional] playerid: i block.destruct(x: int, y: int, z: int, playerid: int) -- Собирает полное состояние в виде целого числа -block.compose_state(rotation: int, segment: int, userbits: int) -> int +block.compose_state(state: {rotation: int, segment: int, userbits: int}) -> int -- Разбирает полное состояние на: вращение, сегмент, пользовательские биты -block.decompose_state(state: int) -> int, int, int +block.decompose_state(state: int) -> {int, int, int} ``` > [!WARNING] diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index af2863b0..0d24db89 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -381,20 +381,32 @@ static int l_raycast(lua::State* L) { } static int l_compose_state(lua::State* L) { + if (lua::istable(L, 1) || lua::objlen(L, 1) < 3) { + throw std::runtime_error("expected array of 3 integers"); + } blockstate state {}; - state.rotation = lua::tointeger(L, 1); - state.segment = lua::tointeger(L, 2); - state.userbits = lua::tointeger(L, 3); + + lua::rawgeti(L, 1, 1); state.rotation = lua::tointeger(L, -1); lua::pop(L); + lua::rawgeti(L, 2, 1); state.segment = lua::tointeger(L, -1); lua::pop(L); + lua::rawgeti(L, 3, 1); state.userbits = lua::tointeger(L, -1); lua::pop(L); + return lua::pushinteger(L, blockstate2int(state)); } static int l_decompose_state(lua::State* L) { auto stateInt = static_cast(lua::tointeger(L, 1)); auto state = int2blockstate(stateInt); + + lua::createtable(L, 3, 0); lua::pushinteger(L, state.rotation); + lua::rawseti(L, 1); + lua::pushinteger(L, state.segment); + lua::rawseti(L, 2); + lua::pushinteger(L, state.userbits); - return 3; + lua::rawseti(L, 3); + return 1; } const luaL_Reg blocklib [] = {