aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoïc Blot <nerzhul@users.noreply.github.com>2017-07-27 11:32:35 +0200
committerGitHub <noreply@github.com>2017-07-27 11:32:35 +0200
commit550c0404a8b6fbee857003c961bb802121e88f51 (patch)
tree1b408c5cbb94b8211636d621eab2622e65906137
parentba959ce27f20d0887fc94da4e40880749a493cde (diff)
downloadminetest-550c0404a8b6fbee857003c961bb802121e88f51.tar.gz
minetest-550c0404a8b6fbee857003c961bb802121e88f51.tar.bz2
minetest-550c0404a8b6fbee857003c961bb802121e88f51.zip
Add LuaEntity on_death callback (#6177)
Add LuaEntity on_death callback This fixes #5474
-rw-r--r--doc/lua_api.txt3
-rw-r--r--games/minimal/mods/experimental/init.lua15
-rw-r--r--src/content_sao.cpp4
-rw-r--r--src/script/cpp_api/s_entity.cpp28
-rw-r--r--src/script/cpp_api/s_entity.h1
5 files changed, 44 insertions, 7 deletions
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 2a092300e..b9dc1b47f 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -3864,6 +3864,9 @@ Registered entities
* `tool_capabilities`: capability table of used tool (can be `nil`)
* `dir`: unit vector of direction of punch. Always defined. Points from
the puncher to the punched.
+ `on_death(self, killer)`
+ * Called when the object dies.
+ * `killer`: an `ObjectRef` (can be `nil`)
* `on_rightclick(self, clicker)`
* `get_staticdata(self)`
* Should return a string that will be passed to `on_activate` when
diff --git a/games/minimal/mods/experimental/init.lua b/games/minimal/mods/experimental/init.lua
index 6e0fb1738..ba7a89018 100644
--- a/games/minimal/mods/experimental/init.lua
+++ b/games/minimal/mods/experimental/init.lua
@@ -317,6 +317,9 @@ minetest.register_entity("experimental:testentity", {
self.object:remove()
hitter:add_to_inventory('craft testobject1 1')
end,
+ on_death = function(self, killer)
+ print("testentity.on_death")
+ end
})
--
@@ -398,11 +401,11 @@ minetest.register_abm({
if ncpos ~= nil then
return
end
-
+
if pos.x % 16 ~= 8 or pos.z % 16 ~= 8 then
return
end
-
+
pos.y = pos.y + 1
n = minetest.get_node(pos)
print(dump(n))
@@ -431,7 +434,7 @@ minetest.register_abm({
return
end
nctime = clock
-
+
s0 = ncstuff[ncq]
ncq = s0[1]
s1 = ncstuff[ncq]
@@ -477,15 +480,15 @@ minetest.register_node("experimental:tester_node_1", {
experimental.print_to_everything("incorrect metadata found")
end
end,
-
+
on_destruct = function(pos)
experimental.print_to_everything("experimental:tester_node_1:on_destruct("..minetest.pos_to_string(pos)..")")
end,
-
+
after_destruct = function(pos)
experimental.print_to_everything("experimental:tester_node_1:after_destruct("..minetest.pos_to_string(pos)..")")
end,
-
+
after_dig_node = function(pos, oldnode, oldmetadata, digger)
experimental.print_to_everything("experimental:tester_node_1:after_dig_node("..minetest.pos_to_string(pos)..")")
end,
diff --git a/src/content_sao.cpp b/src/content_sao.cpp
index df5cae34f..5ba4f1ad1 100644
--- a/src/content_sao.cpp
+++ b/src/content_sao.cpp
@@ -582,8 +582,10 @@ int LuaEntitySAO::punch(v3f dir,
}
}
- if (getHP() == 0)
+ if (getHP() == 0) {
m_removed = true;
+ m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
+ }
diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp
index 4c1e296d4..a97b0c12c 100644
--- a/src/script/cpp_api/s_entity.cpp
+++ b/src/script/cpp_api/s_entity.cpp
@@ -262,6 +262,34 @@ bool ScriptApiEntity::luaentity_Punch(u16 id,
return retval;
}
+bool ScriptApiEntity::luaentity_on_death(u16 id, ServerActiveObject *killer)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ int error_handler = PUSH_ERROR_HANDLER(L);
+
+ // Get core.luaentities[id]
+ luaentity_get(L, id);
+ int object = lua_gettop(L);
+ // State: object is at top of stack
+ // Get function
+ lua_getfield(L, -1, "on_death");
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop on_death and entity
+ return false;
+ }
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+ lua_pushvalue(L, object); // self
+ objectrefGetOrCreate(L, killer); // killer reference
+
+ setOriginFromTable(object);
+ PCALL_RES(lua_pcall(L, 6, 1, error_handler));
+
+ bool retval = lua_toboolean(L, -1);
+ lua_pop(L, 2); // Pop object and error handler
+ return retval;
+}
+
// Calls entity:on_rightclick(ObjectRef clicker)
void ScriptApiEntity::luaentity_Rightclick(u16 id,
ServerActiveObject *clicker)
diff --git a/src/script/cpp_api/s_entity.h b/src/script/cpp_api/s_entity.h
index 4e2a056bb..07b06edff 100644
--- a/src/script/cpp_api/s_entity.h
+++ b/src/script/cpp_api/s_entity.h
@@ -41,6 +41,7 @@ public:
bool luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch,
const ToolCapabilities *toolcap, v3f dir, s16 damage);
+ bool luaentity_on_death(u16 id, ServerActiveObject *killer);
void luaentity_Rightclick(u16 id,
ServerActiveObject *clicker);
};