aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWarr1024 <warr1024@gmail.com>2021-07-09 09:08:40 -0400
committerGitHub <noreply@github.com>2021-07-09 09:08:40 -0400
commit52128ae11e8b1a7ce66a87c53f1b15f3aabe69f4 (patch)
treedff52505d4b86b9934cc6f37f9b4ceeca5a54014
parente9bc59e376f88f1d4d1c6d3fedf62d9049e3e60d (diff)
downloadminetest-52128ae11e8b1a7ce66a87c53f1b15f3aabe69f4.tar.gz
minetest-52128ae11e8b1a7ce66a87c53f1b15f3aabe69f4.tar.bz2
minetest-52128ae11e8b1a7ce66a87c53f1b15f3aabe69f4.zip
Add API for mods to hook liquid transformation events (#11405)
Add API for mods to hook liquid transformation events Without this API, there is no reliable way for mods to be notified when liquid transform modifies nodes and mods are forced to poll for changes. This allows mods to detect changes to flowing liquid nodes and liquid renewal using event-driven logic.
-rw-r--r--builtin/game/register.lua1
-rw-r--r--doc/lua_api.txt6
-rw-r--r--src/map.cpp2
-rw-r--r--src/script/cpp_api/s_env.cpp34
-rw-r--r--src/script/cpp_api/s_env.h5
5 files changed, 47 insertions, 1 deletions
diff --git a/builtin/game/register.lua b/builtin/game/register.lua
index c07535855..56e40c75c 100644
--- a/builtin/game/register.lua
+++ b/builtin/game/register.lua
@@ -610,6 +610,7 @@ core.registered_on_modchannel_message, core.register_on_modchannel_message = mak
core.registered_on_player_inventory_actions, core.register_on_player_inventory_action = make_registration()
core.registered_allow_player_inventory_actions, core.register_allow_player_inventory_action = make_registration()
core.registered_on_rightclickplayers, core.register_on_rightclickplayer = make_registration()
+core.registered_on_liquid_transformed, core.register_on_liquid_transformed = make_registration()
--
-- Compatibility for on_mapgen_init()
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index aa59898d7..fc6d077e1 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -4859,6 +4859,12 @@ Call these functions only at load time!
* Called when an incoming mod channel message is received
* You should have joined some channels to receive events.
* If message comes from a server mod, `sender` field is an empty string.
+* `minetest.register_on_liquid_transformed(function(post_list, node_list))`
+ * Called after liquid nodes are modified by the engine's liquid transformation
+ process.
+ * `pos_list` is an array of all modified positions.
+ * `node_list` is an array of the old node that was previously at the position
+ with the corresponding index in pos_list.
Setting-related
---------------
diff --git a/src/map.cpp b/src/map.cpp
index 641287c3d..30ce064d6 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -829,7 +829,7 @@ void Map::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
m_transforming_liquid.push_back(iter);
voxalgo::update_lighting_nodes(this, changed_nodes, modified_blocks);
-
+ env->getScriptIface()->on_liquid_transformed(changed_nodes);
/* ----------------------------------------------------------------------
* Manage the queue so that it does not grow indefinately
diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp
index c4a39a869..c11de3757 100644
--- a/src/script/cpp_api/s_env.cpp
+++ b/src/script/cpp_api/s_env.cpp
@@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapgen/mapgen.h"
#include "lua_api/l_env.h"
#include "server.h"
+#include "script/common/c_content.h"
+
void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
u32 blockseed)
@@ -267,3 +269,35 @@ void ScriptApiEnv::on_emerge_area_completion(
luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);
}
}
+
+void ScriptApiEnv::on_liquid_transformed(
+ const std::vector<std::pair<v3s16, MapNode>> &list)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ // Get core.registered_on_liquid_transformed
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "registered_on_liquid_transformed");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ lua_remove(L, -2);
+
+ // Skip converting list and calling hook if there are
+ // no registered callbacks.
+ if(lua_objlen(L, -1) < 1) return;
+
+ // Convert the list to a pos array and a node array for lua
+ int index = 1;
+ const NodeDefManager *ndef = getEnv()->getGameDef()->ndef();
+ lua_createtable(L, list.size(), 0);
+ lua_createtable(L, list.size(), 0);
+ for(std::pair<v3s16, MapNode> p : list) {
+ lua_pushnumber(L, index);
+ push_v3s16(L, p.first);
+ lua_rawset(L, -4);
+ lua_pushnumber(L, index++);
+ pushnode(L, p.second, ndef);
+ lua_rawset(L, -3);
+ }
+
+ runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
+} \ No newline at end of file
diff --git a/src/script/cpp_api/s_env.h b/src/script/cpp_api/s_env.h
index 232a08aaf..090858f17 100644
--- a/src/script/cpp_api/s_env.h
+++ b/src/script/cpp_api/s_env.h
@@ -21,6 +21,8 @@ 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 <vector>
class ServerEnvironment;
struct ScriptCallbackState;
@@ -41,5 +43,8 @@ public:
void on_emerge_area_completion(v3s16 blockpos, int action,
ScriptCallbackState *state);
+ // Called after liquid transform changes
+ void on_liquid_transformed(const std::vector<std::pair<v3s16, MapNode>> &list);
+
void initializeEnvironment(ServerEnvironment *env);
};