From f3ba2540e31c87b294c52722f064e815c0939fc9 Mon Sep 17 00:00:00 2001 From: Brandon Date: Fri, 16 May 2014 23:47:12 -0500 Subject: [PATCH] add minetest.register_on_punchplayer --- builtin/game/register.lua | 1 + doc/lua_api.txt | 10 +++++++++- src/content_sao.cpp | 9 ++++++++- src/script/cpp_api/s_internal.h | 1 + src/script/cpp_api/s_player.cpp | 23 +++++++++++++++++++++++ src/script/cpp_api/s_player.h | 3 +++ 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/builtin/game/register.lua b/builtin/game/register.lua index cb084024169d..a6f6fa3c7c85 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -431,6 +431,7 @@ core.registered_on_crafts, core.register_on_craft = make_registration() core.registered_craft_predicts, core.register_craft_predict = make_registration() core.registered_on_protection_violation, core.register_on_protection_violation = make_registration() core.registered_on_item_eats, core.register_on_item_eat = make_registration() +core.registered_on_punchplayer, core.register_on_punchplayer = make_registration() -- -- Compatibility for on_mapgen_init() diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 8fda137ae89a..872cab252834 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2278,8 +2278,16 @@ These functions return the leftover itemstack. * `minetest.forceload_free_block(pos)` * stops forceloading the position `pos` - Please note that forceloaded areas are saved when the server restarts. +* `minetest.register_on_punchplayer(func(player, hitter, time_from_last_punch, tool_capabilities, dir))` + * Called when a player is punched + * `player` - ObjectRef - Player that was punched + * `hitter` - ObjectRef - Player that hit + * `time_from_last_punch`: Meant for disallowing spamming of clicks (can be nil) + * `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. + * should return `true` to prevent the default damage mechanism minetest.global_exists(name) ^ Checks if a global variable has been set, without triggering a warning. diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 51f074f7cf08..613c2c618655 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -1041,7 +1041,14 @@ int PlayerSAO::punch(v3f dir, <getPlayerSAO(); + + bool damage_handled = m_env->getScriptIface()->on_punchplayer(playersao, puncher, + time_from_last_punch, toolcap, dir); + + if (!damage_handled) { + setHP(getHP() - hitparams.hp); + } return hitparams.wear; } diff --git a/src/script/cpp_api/s_internal.h b/src/script/cpp_api/s_internal.h index 10ee1a7de149..9999a584a772 100644 --- a/src/script/cpp_api/s_internal.h +++ b/src/script/cpp_api/s_internal.h @@ -61,3 +61,4 @@ bool* m_variable; StackUnroller stack_unroller(L); #endif /* S_INTERNAL_H_ */ + diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp index 81bfd45058b9..c97385d65cc1 100644 --- a/src/script/cpp_api/s_player.cpp +++ b/src/script/cpp_api/s_player.cpp @@ -19,6 +19,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "cpp_api/s_player.h" #include "cpp_api/s_internal.h" +#include "common/c_converter.h" +#include "common/c_content.h" #include "util/string.h" void ScriptApiPlayer::on_newplayer(ServerActiveObject *player) @@ -45,6 +47,27 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player) script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST); } +bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player, + ServerActiveObject *hitter, + float time_from_last_punch, + const ToolCapabilities *toolcap, + v3f dir) +{ + SCRIPTAPI_PRECHECKHEADER + // Get core.registered_on_dieplayers + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_punchplayer"); + // Call callbacks + objectrefGetOrCreate(L, player); + objectrefGetOrCreate(L, hitter); + lua_pushnumber(L, time_from_last_punch); + push_tool_capabilities(L, *toolcap); + push_v3f(L, dir); + script_run_callbacks(L, 5, RUN_CALLBACKS_MODE_LAST); + bool damage_handled = lua_toboolean(L, -1); + return damage_handled; +} + bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h index c77d397c408f..c630997b5a99 100644 --- a/src/script/cpp_api/s_player.h +++ b/src/script/cpp_api/s_player.h @@ -23,7 +23,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "cpp_api/s_base.h" +#include "irr_v3d.h" +struct ToolCapabilities; class ScriptApiPlayer : virtual public ScriptApiBase @@ -38,6 +40,7 @@ class ScriptApiPlayer void on_joinplayer(ServerActiveObject *player); void on_leaveplayer(ServerActiveObject *player); void on_cheat(ServerActiveObject *player, const std::string &cheat_type); + bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,float time_from_last_punch, const ToolCapabilities *toolcap, v3f dir); void on_playerReceiveFields(ServerActiveObject *player, const std::string &formname,