From 8ef288c18997f19681f2a2c0222bb32f133fe335 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 19 Aug 2024 00:49:28 +0300 Subject: [PATCH] refactor lua scripting a bit --- src/logic/scripting/lua/libaudio.cpp | 4 +- src/logic/scripting/lua/libblock.cpp | 28 ++--- src/logic/scripting/lua/libmat4.cpp | 4 +- src/logic/scripting/lua/libplayer.cpp | 14 +-- src/logic/scripting/lua/lua_util.hpp | 159 ++++++++---------------- src/logic/scripting/lua/lua_wrapper.hpp | 85 +++++++++++++ src/logic/scripting/scripting.cpp | 18 +-- 7 files changed, 169 insertions(+), 143 deletions(-) create mode 100644 src/logic/scripting/lua/lua_wrapper.hpp diff --git a/src/logic/scripting/lua/libaudio.cpp b/src/logic/scripting/lua/libaudio.cpp index 03214e6e..ee311585 100644 --- a/src/logic/scripting/lua/libaudio.cpp +++ b/src/logic/scripting/lua/libaudio.cpp @@ -335,7 +335,7 @@ static int l_audio_get_duration(lua::State* L) { static int l_audio_get_position(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - return lua::pushvec3_stack(L, speaker->getPosition()); + return lua::pushvec_stack(L, speaker->getPosition()); } return 0; } @@ -344,7 +344,7 @@ static int l_audio_get_position(lua::State* L) { static int l_audio_get_velocity(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - return lua::pushvec3_stack(L, speaker->getVelocity()); + return lua::pushvec_stack(L, speaker->getVelocity()); } return 0; } diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 145362b7..f346098a 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -57,7 +57,7 @@ static int l_is_extended(lua::State* L) { static int l_get_size(lua::State* L) { if (auto def = require_block(L)) { - return lua::pushivec3_stack(L, def->size.x, def->size.y, def->size.z); + return lua::pushivec_stack(L, glm::ivec3(def->size)); } return 0; } @@ -76,7 +76,7 @@ static int l_seek_origin(lua::State* L) { auto z = lua::tointeger(L, 3); auto vox = level->chunks->get(x, y, z); auto& def = indices->blocks.require(vox->id); - return lua::pushivec3_stack( + return lua::pushivec_stack( L, level->chunks->seekOrigin({x, y, z}, def, vox->state) ); } @@ -117,14 +117,14 @@ static int l_get_x(lua::State* L) { auto z = lua::tointeger(L, 3); auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { - return lua::pushivec3_stack(L, 1, 0, 0); + return lua::pushivec_stack(L, glm::ivec3(1, 0, 0)); } - auto& def = level->content->getIndices()->blocks.require(vox->id); + const auto& def = level->content->getIndices()->blocks.require(vox->id); if (!def.rotatable) { - return lua::pushivec3_stack(L, 1, 0, 0); + return lua::pushivec_stack(L, glm::ivec3(1, 0, 0)); } else { const CoordSystem& rot = def.rotations.variants[vox->state.rotation]; - return lua::pushivec3_stack(L, rot.axisX.x, rot.axisX.y, rot.axisX.z); + return lua::pushivec_stack(L, rot.axisX); } } @@ -134,14 +134,14 @@ static int l_get_y(lua::State* L) { auto z = lua::tointeger(L, 3); auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { - return lua::pushivec3_stack(L, 0, 1, 0); + return lua::pushivec_stack(L, glm::ivec3(0, 1, 0)); } - auto& def = level->content->getIndices()->blocks.require(vox->id); + const auto& def = level->content->getIndices()->blocks.require(vox->id); if (!def.rotatable) { - return lua::pushivec3_stack(L, 0, 1, 0); + return lua::pushivec_stack(L, glm::ivec3(0, 1, 0)); } else { const CoordSystem& rot = def.rotations.variants[vox->state.rotation]; - return lua::pushivec3_stack(L, rot.axisY.x, rot.axisY.y, rot.axisY.z); + return lua::pushivec_stack(L, rot.axisY); } } @@ -151,14 +151,14 @@ static int l_get_z(lua::State* L) { auto z = lua::tointeger(L, 3); auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { - return lua::pushivec3_stack(L, 0, 0, 1); + return lua::pushivec_stack(L, glm::ivec3(0, 0, 1)); } - auto& def = level->content->getIndices()->blocks.require(vox->id); + const auto& def = level->content->getIndices()->blocks.require(vox->id); if (!def.rotatable) { - return lua::pushivec3_stack(L, 0, 0, 1); + return lua::pushivec_stack(L, glm::ivec3(0, 0, 1)); } else { const CoordSystem& rot = def.rotations.variants[vox->state.rotation]; - return lua::pushivec3_stack(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z); + return lua::pushivec_stack(L, rot.axisZ); } } diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libmat4.cpp index 44721ca6..078c229a 100644 --- a/src/logic/scripting/lua/libmat4.cpp +++ b/src/logic/scripting/lua/libmat4.cpp @@ -49,9 +49,9 @@ static int l_mul(lua::State* L) { switch (argc) { case 2: { if (len2 == 4) { - return lua::pushvec4_stack(L, matrix1 * lua::tovec4(L, 2)); + return lua::pushvec(L, matrix1 * lua::tovec4(L, 2)); } else if (len2 == 3) { - return lua::pushvec3_stack( + return lua::pushvec( L, matrix1 * glm::vec4(lua::tovec3(L, 2), 1.0f) ); } diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index 943e12da..fb4c9f0f 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -17,7 +17,7 @@ inline std::shared_ptr get_player(lua::State* L, int idx) { static int l_get_pos(lua::State* L) { if (auto player = get_player(L, 1)) { - return lua::pushvec3_stack(L, player->getPosition()); + return lua::pushvec_stack(L, player->getPosition()); } return 0; } @@ -37,7 +37,7 @@ static int l_set_pos(lua::State* L) { static int l_get_vel(lua::State* L) { if (auto player = get_player(L, 1)) { if (auto hitbox = player->getHitbox()) { - return lua::pushvec3_stack(L, hitbox->velocity); + return lua::pushvec_stack(L, hitbox->velocity); } } return 0; @@ -59,7 +59,7 @@ static int l_set_vel(lua::State* L) { static int l_get_rot(lua::State* L) { if (auto player = get_player(L, 1)) { - return lua::pushvec3_stack(L, player->cam); + return lua::pushvec_stack(L, player->cam); } return 0; } @@ -133,7 +133,7 @@ static int l_get_selected_block(lua::State* L) { if (player->selection.vox.id == BLOCK_VOID) { return 0; } - return lua::pushivec3_stack(L, player->selection.position); + return lua::pushivec_stack(L, player->selection.position); } return 0; } @@ -149,15 +149,13 @@ static int l_get_selected_entity(lua::State* L) { static int l_get_spawnpoint(lua::State* L) { if (auto player = get_player(L, 1)) { - return lua::pushvec3_stack(L, player->getSpawnPoint()); + return lua::pushvec_stack(L, player->getSpawnPoint()); } return 0; } static int l_set_spawnpoint(lua::State* L) { - auto player = get_player(L, 1); - - if (player) { + if (auto player = get_player(L, 1)) { auto x = lua::tonumber(L, 2); auto y = lua::tonumber(L, 3); auto z = lua::tonumber(L, 4); diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 0822e5bc..23ab4c80 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -4,7 +4,7 @@ #include #include -#include "lua_commons.hpp" +#include "lua_wrapper.hpp" #include "lua_custom_types.hpp" #define GLM_ENABLE_EXPERIMENTAL #include @@ -18,68 +18,6 @@ namespace lua { std::string env_name(int env); - template - int wrap(lua_State* L) { - int result = 0; - try { - result = func(L); - } - // transform exception with description into lua_error - catch (std::exception& e) { - luaL_error(L, e.what()); - } - // Rethrow any other exception (lua error for example) - catch (...) { - throw; - } - return result; - } - - inline void pop(lua::State* L, int n = 1) { - lua_pop(L, n); - } - inline void insert(lua::State* L, int idx) { - lua_insert(L, idx); - } - inline void remove(lua::State* L, int idx) { - lua_remove(L, idx); - } - inline int gettop(lua::State* L) { - return lua_gettop(L); - } - inline size_t objlen(lua::State* L, int idx) { - return lua_objlen(L, idx); - } - inline int next(lua::State* L, int idx) { - return lua_next(L, idx); - } - inline int type(lua::State* L, int idx) { - return lua_type(L, idx); - } - inline const char* type_name(lua::State* L, int idx) { - return lua_typename(L, idx); - } - inline int rawget(lua::State* L, int idx = -2) { - lua_rawget(L, idx); - return 1; - } - inline int rawgeti(lua::State* L, int n, int idx = -1) { - lua_rawgeti(L, idx, n); - return 1; - } - inline void rawseti(lua::State* L, int n, int idx = -2) { - lua_rawseti(L, idx, n); - } - - inline int createtable(lua::State* L, int narr, int nrec) { - lua_createtable(L, narr, nrec); - return 1; - } - - inline bool isnil(lua::State* L, int idx) { - return lua_isnil(L, idx); - } - inline bool getglobal(lua::State* L, const std::string& name) { lua_getglobal(L, name.c_str()); if (isnil(L, -1)) { @@ -107,25 +45,8 @@ namespace lua { return true; } - // function wrappers with number of pushed values as return value - - inline int pushnil(lua::State* L) { - lua_pushnil(L); - return 1; - } - - inline int pushinteger(lua::State* L, lua::Integer x) { - lua_pushinteger(L, x); - return 1; - } - - inline int pushnumber(lua::State* L, lua::Number x) { - lua_pushnumber(L, x); - return 1; - } - template - inline int pushvec(lua::State* L, glm::vec vec) { + inline int pushvec(lua::State* L, const glm::vec& vec) { createtable(L, n, 0); for (int i = 0; i < n; i++) { pushnumber(L, vec[i]); @@ -135,7 +56,7 @@ namespace lua { } template - inline int pushivec(lua::State* L, glm::vec vec) { + inline int pushivec(lua::State* L, const glm::vec& vec) { createtable(L, n, 0); for (int i = 0; i < n; i++) { pushinteger(L, vec[i]); @@ -144,34 +65,20 @@ namespace lua { return 1; } - inline int pushivec3_stack( - lua::State* L, lua::Integer x, lua::Integer y, lua::Integer z - ) { - pushinteger(L, x); - pushinteger(L, y); - pushinteger(L, z); - return 3; + template + inline int pushvec_stack(lua::State* L, const glm::vec& vec) { + for (int i = 0; i < n; i++) { + pushnumber(L, vec[i]); + } + return n; } - inline int pushivec3_stack(lua::State* L, glm::ivec3 vec) { - pushinteger(L, vec.x); - pushinteger(L, vec.y); - pushinteger(L, vec.z); - return 3; - } - - inline int pushvec3_stack(lua::State* L, glm::vec3 vec) { - pushnumber(L, vec.x); - pushnumber(L, vec.y); - pushnumber(L, vec.z); - return 3; - } - inline int pushvec4_stack(lua::State* L, glm::vec4 vec) { - pushnumber(L, vec.x); - pushnumber(L, vec.y); - pushnumber(L, vec.z); - pushnumber(L, vec.w); - return 4; + template + inline int pushivec_stack(lua::State* L, const glm::vec& vec) { + for (int i = 0; i < n; i++) { + pushinteger(L, vec[i]); + } + return n; } inline void setmetatable(lua::State* L, int idx = -2) { @@ -637,4 +544,40 @@ namespace lua { luaL_setfuncs(L, libfuncs, 0); setglobal(L, name); } + + inline int requirefield(lua::State* L, const std::string& name, int idx = -1) { + if (getfield(L, name, idx)) { + return 1; + } + throw std::runtime_error("object has no member '"+name+"'"); + } + + inline const char* require_string_field( + lua::State* L, const std::string& name, int idx=-1 + ) { + requirefield(L, name, idx); + auto value = require_string(L, -1); + lua::pop(L); + return value; + } + + inline Integer require_integer_field( + lua::State* L, const std::string& name, int idx=-1 + ) { + requirefield(L, name, idx); + auto value = tointeger(L, -1); + lua::pop(L); + return value; + } + + inline bool get_boolean_field( + lua::State* L, const std::string& name, bool def, int idx=-1 + ) { + if (getfield(L, name, idx)) { + bool value = toboolean(L, -1); + pop(L); + return value; + } + return def; + } } diff --git a/src/logic/scripting/lua/lua_wrapper.hpp b/src/logic/scripting/lua/lua_wrapper.hpp new file mode 100644 index 00000000..ee184cb1 --- /dev/null +++ b/src/logic/scripting/lua/lua_wrapper.hpp @@ -0,0 +1,85 @@ +#pragma once + +#include "lua_commons.hpp" + +namespace lua { + template + int wrap(lua_State* L) { + int result = 0; + try { + result = func(L); + } + // transform exception with description into lua_error + catch (std::exception& e) { + luaL_error(L, e.what()); + } + // Rethrow any other exception (lua error for example) + catch (...) { + throw; + } + return result; + } + + inline void pop(lua::State* L, int n = 1) { + lua_pop(L, n); + } + inline void insert(lua::State* L, int idx) { + lua_insert(L, idx); + } + inline void remove(lua::State* L, int idx) { + lua_remove(L, idx); + } + inline int gettop(lua::State* L) { + return lua_gettop(L); + } + inline size_t objlen(lua::State* L, int idx) { + return lua_objlen(L, idx); + } + inline int next(lua::State* L, int idx) { + return lua_next(L, idx); + } + inline int type(lua::State* L, int idx) { + return lua_type(L, idx); + } + inline const char* type_name(lua::State* L, int idx) { + return lua_typename(L, idx); + } + inline int rawget(lua::State* L, int idx = -2) { + lua_rawget(L, idx); + return 1; + } + inline int rawgeti(lua::State* L, int n, int idx = -1) { + lua_rawgeti(L, idx, n); + return 1; + } + inline void rawseti(lua::State* L, int n, int idx = -2) { + lua_rawseti(L, idx, n); + } + + inline int createtable(lua::State* L, int narr, int nrec) { + lua_createtable(L, narr, nrec); + return 1; + } + + inline bool isnil(lua::State* L, int idx) { + return lua_isnil(L, idx); + } + + // function wrappers with number of pushed values as return value + + inline int pushnil(lua::State* L) { + lua_pushnil(L); + return 1; + } + + inline int pushinteger(lua::State* L, lua::Integer x) { + lua_pushinteger(L, x); + return 1; + } + + inline int pushnumber(lua::State* L, lua::Number x) { + lua_pushnumber(L, x); + return 1; + } + +} diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index aeedc1d9..9fa17a91 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -205,14 +205,14 @@ void scripting::on_blocks_tick(const Block& block, int tps) { void scripting::update_block(const Block& block, int x, int y, int z) { std::string name = block.name + ".update"; lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) { - return lua::pushivec3_stack(L, x, y, z); + return lua::pushivec_stack(L, glm::ivec3(x, y, z)); }); } void scripting::random_update_block(const Block& block, int x, int y, int z) { std::string name = block.name + ".randupdate"; lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) { - return lua::pushivec3_stack(L, x, y, z); + return lua::pushivec_stack(L, glm::ivec3(x, y, z)); }); } @@ -221,13 +221,13 @@ void scripting::on_block_placed( ) { std::string name = block.name + ".placed"; lua::emit_event(lua::get_main_thread(), name, [x, y, z, player](auto L) { - lua::pushivec3_stack(L, x, y, z); + lua::pushivec_stack(L, glm::ivec3(x, y, z)); lua::pushinteger(L, player ? player->getId() : -1); return 4; }); auto world_event_args = [&](lua::State* L) { lua::pushinteger(L, block.rt.id); - lua::pushivec3_stack(L, x, y, z); + lua::pushivec_stack(L, glm::ivec3(x, y, z)); lua::pushinteger(L, player ? player->getId() : -1); return 5; }; @@ -251,7 +251,7 @@ void scripting::on_block_broken( lua::get_main_thread(), name, [x, y, z, player](auto L) { - lua::pushivec3_stack(L, x, y, z); + lua::pushivec_stack(L, glm::ivec3(x, y, z)); lua::pushinteger(L, player ? player->getId() : -1); return 4; } @@ -259,7 +259,7 @@ void scripting::on_block_broken( } auto world_event_args = [&](lua::State* L) { lua::pushinteger(L, block.rt.id); - lua::pushivec3_stack(L, x, y, z); + lua::pushivec_stack(L, glm::ivec3(x, y, z)); lua::pushinteger(L, player ? player->getId() : -1); return 5; }; @@ -279,7 +279,7 @@ bool scripting::on_block_interact( ) { std::string name = block.name + ".interact"; return lua::emit_event(lua::get_main_thread(), name, [pos, player](auto L) { - lua::pushivec3_stack(L, pos.x, pos.y, pos.z); + lua::pushivec_stack(L, pos); lua::pushinteger(L, player->getId()); return 4; }); @@ -302,7 +302,7 @@ bool scripting::on_item_use_on_block( lua::get_main_thread(), name, [ipos, normal, player](auto L) { - lua::pushivec3_stack(L, ipos.x, ipos.y, ipos.z); + lua::pushivec_stack(L, ipos); lua::pushinteger(L, player->getId()); lua::pushivec(L, normal); return 5; @@ -318,7 +318,7 @@ bool scripting::on_item_break_block( lua::get_main_thread(), name, [x, y, z, player](auto L) { - lua::pushivec3_stack(L, x, y, z); + lua::pushivec_stack(L, glm::ivec3(x, y, z)); lua::pushinteger(L, player->getId()); return 4; }