aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrandon <brandon@bremaweb.com>2014-05-16 23:47:12 -0500
committerest31 <MTest31@outlook.com>2015-05-15 11:09:55 +0200
commitc5b4e541749c50805519ce040d98a0a8e5e0ec03 (patch)
treebbc735720d6ac2e3b27c07c0b6d01ef586d61211 /src
parent86a963caca9604ad57904e9acd9bef7c46ca47d8 (diff)
downloadminetest-c5b4e541749c50805519ce040d98a0a8e5e0ec03.tar.gz
minetest-c5b4e541749c50805519ce040d98a0a8e5e0ec03.tar.bz2
minetest-c5b4e541749c50805519ce040d98a0a8e5e0ec03.zip
Add minetest.register_on_punchplayer
Diffstat (limited to 'src')
-rw-r--r--src/content_sao.cpp39
-rw-r--r--src/script/cpp_api/s_internal.h1
-rw-r--r--src/script/cpp_api/s_player.cpp24
-rw-r--r--src/script/cpp_api/s_player.h5
4 files changed, 60 insertions, 9 deletions
diff --git a/src/content_sao.cpp b/src/content_sao.cpp
index 0aae7bbc2..c7f4b60c7 100644
--- a/src/content_sao.cpp
+++ b/src/content_sao.cpp
@@ -1023,15 +1023,15 @@ int PlayerSAO::punch(v3f dir,
float time_from_last_punch)
{
// It's best that attachments cannot be punched
- if(isAttached())
+ if (isAttached())
return 0;
- if(!toolcap)
+ if (!toolcap)
return 0;
// No effect if PvP disabled
- if(g_settings->getBool("enable_pvp") == false){
- if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER){
+ if (g_settings->getBool("enable_pvp") == false) {
+ if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
std::string str = gob_cmd_punched(0, getHP());
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
@@ -1045,14 +1045,35 @@ int PlayerSAO::punch(v3f dir,
std::string punchername = "nil";
- if ( puncher != 0 )
+ if (puncher != 0)
punchername = puncher->getDescription();
- actionstream<<"Player "<<m_player->getName()<<" punched by "
- <<punchername<<", damage "<<hitparams.hp
- <<" HP"<<std::endl;
+ PlayerSAO *playersao = m_player->getPlayerSAO();
+
+ bool damage_handled = m_env->getScriptIface()->on_punchplayer(playersao,
+ puncher, time_from_last_punch, toolcap, dir,
+ hitparams.hp);
+
+ if (!damage_handled) {
+ setHP(getHP() - hitparams.hp);
+ } else { // override client prediction
+ if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
+ std::string str = gob_cmd_punched(0, getHP());
+ // create message and add to list
+ ActiveObjectMessage aom(getId(), true, str);
+ m_messages_out.push(aom);
+ }
+ }
+
- setHP(getHP() - hitparams.hp);
+ actionstream << "Player " << m_player->getName() << " punched by "
+ << punchername;
+ if (!damage_handled) {
+ actionstream << ", damage " << hitparams.hp << " HP";
+ } else {
+ actionstream << ", damage handled by lua";
+ }
+ actionstream << std::endl;
return hitparams.wear;
}
diff --git a/src/script/cpp_api/s_internal.h b/src/script/cpp_api/s_internal.h
index 10ee1a7de..9999a584a 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 81bfd4505..d56766824 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,28 @@ 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,
+ s16 damage)
+{
+ SCRIPTAPI_PRECHECKHEADER
+ // Get core.registered_on_punchplayers
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "registered_on_punchplayers");
+ // Call callbacks
+ objectrefGetOrCreate(L, player);
+ objectrefGetOrCreate(L, hitter);
+ lua_pushnumber(L, time_from_last_punch);
+ push_tool_capabilities(L, *toolcap);
+ push_v3f(L, dir);
+ lua_pushnumber(L, damage);
+ script_run_callbacks(L, 6, RUN_CALLBACKS_MODE_OR);
+ return lua_toboolean(L, -1);
+}
+
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 c77d397c4..a0f764cd5 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 <map>
#include "cpp_api/s_base.h"
+#include "irr_v3d.h"
+struct ToolCapabilities;
class ScriptApiPlayer
: virtual public ScriptApiBase
@@ -38,6 +40,9 @@ public:
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, s16 damage);
void on_playerReceiveFields(ServerActiveObject *player,
const std::string &formname,