diff options
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/common/c_converter.h | 9 | ||||
-rw-r--r-- | src/script/cpp_api/s_env.cpp | 33 | ||||
-rw-r--r-- | src/script/cpp_api/s_env.h | 7 |
3 files changed, 49 insertions, 0 deletions
diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index 2af726d16..f1e4e47ec 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -121,3 +121,12 @@ void warn_if_field_exists(lua_State *L, int table, size_t write_array_slice_float(lua_State *L, int table_index, float *data, v3u16 data_size, v3u16 slice_offset, v3u16 slice_size); + +// This must match the implementation in builtin/game/misc_s.lua +// Note that this returns a floating point result as Lua integers are 32-bit +inline lua_Number hash_node_position(v3s16 pos) +{ + return (((s64)pos.Z + 0x8000L) << 32) + | (((s64)pos.Y + 0x8000L) << 16) + | ((s64)pos.X + 0x8000L); +} diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp index e49113405..3cbb13cd2 100644 --- a/src/script/cpp_api/s_env.cpp +++ b/src/script/cpp_api/s_env.cpp @@ -299,3 +299,36 @@ void ScriptApiEnv::on_liquid_transformed( runCallbacks(2, RUN_CALLBACKS_MODE_FIRST); } + +void ScriptApiEnv::on_mapblocks_changed(const std::unordered_set<v3s16> &set) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_mapblocks_changed + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_mapblocks_changed"); + luaL_checktype(L, -1, LUA_TTABLE); + lua_remove(L, -2); + + // Convert the set to a set of position hashes + lua_createtable(L, 0, set.size()); + for(const v3s16 &p : set) { + lua_pushnumber(L, hash_node_position(p)); + lua_pushboolean(L, true); + lua_rawset(L, -3); + } + lua_pushinteger(L, set.size()); + + runCallbacks(2, RUN_CALLBACKS_MODE_FIRST); +} + +bool ScriptApiEnv::has_on_mapblocks_changed() +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_mapblocks_changed + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_mapblocks_changed"); + luaL_checktype(L, -1, LUA_TTABLE); + return lua_objlen(L, -1) > 0; +} diff --git a/src/script/cpp_api/s_env.h b/src/script/cpp_api/s_env.h index 9a50a01cc..bc4c4cd4d 100644 --- a/src/script/cpp_api/s_env.h +++ b/src/script/cpp_api/s_env.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "cpp_api/s_base.h" #include "irr_v3d.h" #include "mapnode.h" +#include <unordered_set> #include <vector> class ServerEnvironment; @@ -48,5 +49,11 @@ public: // Called after liquid transform changes void on_liquid_transformed(const std::vector<std::pair<v3s16, MapNode>> &list); + // Called after mapblock changes + void on_mapblocks_changed(const std::unordered_set<v3s16> &set); + + // Determines whether there are any on_mapblocks_changed callbacks + bool has_on_mapblocks_changed(); + void initializeEnvironment(ServerEnvironment *env); }; |