Skip to content

Commit 814ee97

Browse files
author
sapier
committedJan 28, 2017
Make entity on_punch have same signature and behaviour as player on_punch
1 parent 953cbb3 commit 814ee97

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed
 

Diff for: ‎doc/lua_api.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ a non-tool item, so that it can do something else than take damage.
13921392

13931393
On the Lua side, every punch calls:
13941394

1395-
entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
1395+
entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction, damage)
13961396

13971397
This should never be called directly, because damage is usually not handled by
13981398
the entity itself.
@@ -1403,6 +1403,9 @@ the entity itself.
14031403
* `tool_capabilities` can be `nil`.
14041404
* `direction` is a unit vector, pointing from the source of the punch to
14051405
the punched object.
1406+
* `damage` damage that will be done to entity
1407+
Return value of this function will determin if damage is done by this function
1408+
(retval true) or shall be done by engine (retval false)
14061409

14071410
To punch an entity/object in Lua, call:
14081411

Diff for: ‎src/content_sao.cpp

+18-14
Original file line numberDiff line numberDiff line change
@@ -578,28 +578,32 @@ int LuaEntitySAO::punch(v3f dir,
578578
punchitem,
579579
time_from_last_punch);
580580

581-
if (result.did_punch) {
582-
setHP(getHP() - result.damage);
581+
bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
582+
time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0);
583583

584-
if (result.damage > 0) {
585-
std::string punchername = puncher ? puncher->getDescription() : "nil";
584+
if (!damage_handled) {
585+
if (result.did_punch) {
586+
setHP(getHP() - result.damage);
586587

587-
actionstream << getDescription() << " punched by "
588-
<< punchername << ", damage " << result.damage
589-
<< " hp, health now " << getHP() << " hp" << std::endl;
590-
}
588+
if (result.damage > 0) {
589+
std::string punchername = puncher ? puncher->getDescription() : "nil";
591590

592-
std::string str = gob_cmd_punched(result.damage, getHP());
593-
// create message and add to list
594-
ActiveObjectMessage aom(getId(), true, str);
595-
m_messages_out.push(aom);
591+
actionstream << getDescription() << " punched by "
592+
<< punchername << ", damage " << result.damage
593+
<< " hp, health now " << getHP() << " hp" << std::endl;
594+
}
595+
596+
std::string str = gob_cmd_punched(result.damage, getHP());
597+
// create message and add to list
598+
ActiveObjectMessage aom(getId(), true, str);
599+
m_messages_out.push(aom);
600+
}
596601
}
597602

598603
if (getHP() == 0)
599604
m_removed = true;
600605

601-
m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
602-
time_from_last_punch, toolcap, dir);
606+
603607

604608
return result.wear;
605609
}

Diff for: ‎src/script/cpp_api/s_entity.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
224224
}
225225

226226
// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
227-
// tool_capabilities, direction)
228-
void ScriptApiEntity::luaentity_Punch(u16 id,
227+
// tool_capabilities, direction, damage)
228+
bool ScriptApiEntity::luaentity_Punch(u16 id,
229229
ServerActiveObject *puncher, float time_from_last_punch,
230-
const ToolCapabilities *toolcap, v3f dir)
230+
const ToolCapabilities *toolcap, v3f dir, s16 damage)
231231
{
232232
SCRIPTAPI_PRECHECKHEADER
233233

@@ -242,20 +242,23 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
242242
// Get function
243243
lua_getfield(L, -1, "on_punch");
244244
if (lua_isnil(L, -1)) {
245-
lua_pop(L, 2); // Pop on_punch and entitu
246-
return;
245+
lua_pop(L, 2); // Pop on_punch and entity
246+
return false;
247247
}
248248
luaL_checktype(L, -1, LUA_TFUNCTION);
249249
lua_pushvalue(L, object); // self
250250
objectrefGetOrCreate(L, puncher); // Clicker reference
251251
lua_pushnumber(L, time_from_last_punch);
252252
push_tool_capabilities(L, *toolcap);
253253
push_v3f(L, dir);
254+
lua_pushnumber(L, damage);
254255

255256
setOriginFromTable(object);
256-
PCALL_RES(lua_pcall(L, 5, 0, error_handler));
257+
PCALL_RES(lua_pcall(L, 6, 0, error_handler));
257258

259+
bool retval = lua_toboolean(L, -1);
258260
lua_pop(L, 2); // Pop object and error handler
261+
return retval;
259262
}
260263

261264
// Calls entity:on_rightclick(ObjectRef clicker)

Diff for: ‎src/script/cpp_api/s_entity.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class ScriptApiEntity
3838
void luaentity_GetProperties(u16 id,
3939
ObjectProperties *prop);
4040
void luaentity_Step(u16 id, float dtime);
41-
void luaentity_Punch(u16 id,
41+
bool luaentity_Punch(u16 id,
4242
ServerActiveObject *puncher, float time_from_last_punch,
43-
const ToolCapabilities *toolcap, v3f dir);
43+
const ToolCapabilities *toolcap, v3f dir, s16 damage);
4444
void luaentity_Rightclick(u16 id,
4545
ServerActiveObject *clicker);
4646
};

0 commit comments

Comments
 (0)
Please sign in to comment.