Skip to content

Commit c5b4e54

Browse files
Bremawebest31
authored andcommittedMay 15, 2015
Add minetest.register_on_punchplayer
1 parent 86a963c commit c5b4e54

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed
 

‎builtin/game/register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ core.registered_on_crafts, core.register_on_craft = make_registration()
431431
core.registered_craft_predicts, core.register_craft_predict = make_registration()
432432
core.registered_on_protection_violation, core.register_on_protection_violation = make_registration()
433433
core.registered_on_item_eats, core.register_on_item_eat = make_registration()
434+
core.registered_on_punchplayers, core.register_on_punchplayer = make_registration()
434435

435436
--
436437
-- Compatibility for on_mapgen_init()

‎doc/lua_api.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,16 @@ Call these functions only at load time!
17601760
* Called after a new player has been created
17611761
* `minetest.register_on_dieplayer(func(ObjectRef))`
17621762
* Called when a player dies
1763+
* `minetest.register_on_punchplayer(func(player, hitter, time_from_last_punch, tool_capabilities, dir, damage))`
1764+
* Called when a player is punched
1765+
* `player` - ObjectRef - Player that was punched
1766+
* `hitter` - ObjectRef - Player that hit
1767+
* `time_from_last_punch`: Meant for disallowing spamming of clicks (can be nil)
1768+
* `tool_capabilities`: capability table of used tool (can be nil)
1769+
* `dir`: unit vector of direction of punch. Always defined. Points from
1770+
the puncher to the punched.
1771+
* `damage` - number that represents the damage calculated by the engine
1772+
* should return `true` to prevent the default damage mechanism
17631773
* `minetest.register_on_respawnplayer(func(ObjectRef))`
17641774
* Called when player is to be respawned
17651775
* Called _before_ repositioning of player occurs
@@ -2282,7 +2292,6 @@ These functions return the leftover itemstack.
22822292

22832293
* `minetest.forceload_free_block(pos)`
22842294
* stops forceloading the position `pos`
2285-
22862295
Please note that forceloaded areas are saved when the server restarts.
22872296

22882297
minetest.global_exists(name)

‎src/content_sao.cpp

+30-9
Original file line numberDiff line numberDiff line change
@@ -1023,15 +1023,15 @@ int PlayerSAO::punch(v3f dir,
10231023
float time_from_last_punch)
10241024
{
10251025
// It's best that attachments cannot be punched
1026-
if(isAttached())
1026+
if (isAttached())
10271027
return 0;
10281028

1029-
if(!toolcap)
1029+
if (!toolcap)
10301030
return 0;
10311031

10321032
// No effect if PvP disabled
1033-
if(g_settings->getBool("enable_pvp") == false){
1034-
if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER){
1033+
if (g_settings->getBool("enable_pvp") == false) {
1034+
if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
10351035
std::string str = gob_cmd_punched(0, getHP());
10361036
// create message and add to list
10371037
ActiveObjectMessage aom(getId(), true, str);
@@ -1045,14 +1045,35 @@ int PlayerSAO::punch(v3f dir,
10451045

10461046
std::string punchername = "nil";
10471047

1048-
if ( puncher != 0 )
1048+
if (puncher != 0)
10491049
punchername = puncher->getDescription();
10501050

1051-
actionstream<<"Player "<<m_player->getName()<<" punched by "
1052-
<<punchername<<", damage "<<hitparams.hp
1053-
<<" HP"<<std::endl;
1051+
PlayerSAO *playersao = m_player->getPlayerSAO();
1052+
1053+
bool damage_handled = m_env->getScriptIface()->on_punchplayer(playersao,
1054+
puncher, time_from_last_punch, toolcap, dir,
1055+
hitparams.hp);
1056+
1057+
if (!damage_handled) {
1058+
setHP(getHP() - hitparams.hp);
1059+
} else { // override client prediction
1060+
if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
1061+
std::string str = gob_cmd_punched(0, getHP());
1062+
// create message and add to list
1063+
ActiveObjectMessage aom(getId(), true, str);
1064+
m_messages_out.push(aom);
1065+
}
1066+
}
1067+
10541068

1055-
setHP(getHP() - hitparams.hp);
1069+
actionstream << "Player " << m_player->getName() << " punched by "
1070+
<< punchername;
1071+
if (!damage_handled) {
1072+
actionstream << ", damage " << hitparams.hp << " HP";
1073+
} else {
1074+
actionstream << ", damage handled by lua";
1075+
}
1076+
actionstream << std::endl;
10561077

10571078
return hitparams.wear;
10581079
}

‎src/script/cpp_api/s_internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ bool* m_variable;
6161
StackUnroller stack_unroller(L);
6262

6363
#endif /* S_INTERNAL_H_ */
64+

‎src/script/cpp_api/s_player.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include "cpp_api/s_player.h"
2121
#include "cpp_api/s_internal.h"
22+
#include "common/c_converter.h"
23+
#include "common/c_content.h"
2224
#include "util/string.h"
2325

2426
void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
@@ -45,6 +47,28 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
4547
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
4648
}
4749

50+
bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
51+
ServerActiveObject *hitter,
52+
float time_from_last_punch,
53+
const ToolCapabilities *toolcap,
54+
v3f dir,
55+
s16 damage)
56+
{
57+
SCRIPTAPI_PRECHECKHEADER
58+
// Get core.registered_on_punchplayers
59+
lua_getglobal(L, "core");
60+
lua_getfield(L, -1, "registered_on_punchplayers");
61+
// Call callbacks
62+
objectrefGetOrCreate(L, player);
63+
objectrefGetOrCreate(L, hitter);
64+
lua_pushnumber(L, time_from_last_punch);
65+
push_tool_capabilities(L, *toolcap);
66+
push_v3f(L, dir);
67+
lua_pushnumber(L, damage);
68+
script_run_callbacks(L, 6, RUN_CALLBACKS_MODE_OR);
69+
return lua_toboolean(L, -1);
70+
}
71+
4872
bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
4973
{
5074
SCRIPTAPI_PRECHECKHEADER

‎src/script/cpp_api/s_player.h

+5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include <map>
2424

2525
#include "cpp_api/s_base.h"
26+
#include "irr_v3d.h"
2627

28+
struct ToolCapabilities;
2729

2830
class ScriptApiPlayer
2931
: virtual public ScriptApiBase
@@ -38,6 +40,9 @@ class ScriptApiPlayer
3840
void on_joinplayer(ServerActiveObject *player);
3941
void on_leaveplayer(ServerActiveObject *player);
4042
void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
43+
bool on_punchplayer(ServerActiveObject *player,
44+
ServerActiveObject *hitter, float time_from_last_punch,
45+
const ToolCapabilities *toolcap, v3f dir, s16 damage);
4146

4247
void on_playerReceiveFields(ServerActiveObject *player,
4348
const std::string &formname,

1 commit comments

Comments
 (1)

4aiman commented on Jun 3, 2015

@4aiman
Contributor

Yay!!
Now it's time for adding on_right_click to players, inventories to entities and on_open_inventory callback )

Please sign in to comment.