Make entity on_punch have same signature and behaviour as player on_punch

This commit is contained in:
sapier 2017-01-21 15:58:07 +01:00
parent 953cbb3b15
commit 814ee971f7
4 changed files with 33 additions and 23 deletions

@ -1392,7 +1392,7 @@ a non-tool item, so that it can do something else than take damage.
On the Lua side, every punch calls: On the Lua side, every punch calls:
entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction) entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction, damage)
This should never be called directly, because damage is usually not handled by This should never be called directly, because damage is usually not handled by
the entity itself. the entity itself.
@ -1403,6 +1403,9 @@ the entity itself.
* `tool_capabilities` can be `nil`. * `tool_capabilities` can be `nil`.
* `direction` is a unit vector, pointing from the source of the punch to * `direction` is a unit vector, pointing from the source of the punch to
the punched object. the punched object.
* `damage` damage that will be done to entity
Return value of this function will determin if damage is done by this function
(retval true) or shall be done by engine (retval false)
To punch an entity/object in Lua, call: To punch an entity/object in Lua, call:

@ -578,6 +578,10 @@ int LuaEntitySAO::punch(v3f dir,
punchitem, punchitem,
time_from_last_punch); time_from_last_punch);
bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0);
if (!damage_handled) {
if (result.did_punch) { if (result.did_punch) {
setHP(getHP() - result.damage); setHP(getHP() - result.damage);
@ -594,12 +598,12 @@ int LuaEntitySAO::punch(v3f dir,
ActiveObjectMessage aom(getId(), true, str); ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push(aom); m_messages_out.push(aom);
} }
}
if (getHP() == 0) if (getHP() == 0)
m_removed = true; m_removed = true;
m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
time_from_last_punch, toolcap, dir);
return result.wear; return result.wear;
} }

@ -224,10 +224,10 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
} }
// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch, // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
// tool_capabilities, direction) // tool_capabilities, direction, damage)
void ScriptApiEntity::luaentity_Punch(u16 id, bool ScriptApiEntity::luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch, ServerActiveObject *puncher, float time_from_last_punch,
const ToolCapabilities *toolcap, v3f dir) const ToolCapabilities *toolcap, v3f dir, s16 damage)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
@ -242,8 +242,8 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
// Get function // Get function
lua_getfield(L, -1, "on_punch"); lua_getfield(L, -1, "on_punch");
if (lua_isnil(L, -1)) { if (lua_isnil(L, -1)) {
lua_pop(L, 2); // Pop on_punch and entitu lua_pop(L, 2); // Pop on_punch and entity
return; return false;
} }
luaL_checktype(L, -1, LUA_TFUNCTION); luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self lua_pushvalue(L, object); // self
@ -251,11 +251,14 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
lua_pushnumber(L, time_from_last_punch); lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap); push_tool_capabilities(L, *toolcap);
push_v3f(L, dir); push_v3f(L, dir);
lua_pushnumber(L, damage);
setOriginFromTable(object); setOriginFromTable(object);
PCALL_RES(lua_pcall(L, 5, 0, error_handler)); PCALL_RES(lua_pcall(L, 6, 0, error_handler));
bool retval = lua_toboolean(L, -1);
lua_pop(L, 2); // Pop object and error handler lua_pop(L, 2); // Pop object and error handler
return retval;
} }
// Calls entity:on_rightclick(ObjectRef clicker) // Calls entity:on_rightclick(ObjectRef clicker)

@ -38,9 +38,9 @@ public:
void luaentity_GetProperties(u16 id, void luaentity_GetProperties(u16 id,
ObjectProperties *prop); ObjectProperties *prop);
void luaentity_Step(u16 id, float dtime); void luaentity_Step(u16 id, float dtime);
void luaentity_Punch(u16 id, bool luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch, ServerActiveObject *puncher, float time_from_last_punch,
const ToolCapabilities *toolcap, v3f dir); const ToolCapabilities *toolcap, v3f dir, s16 damage);
void luaentity_Rightclick(u16 id, void luaentity_Rightclick(u16 id,
ServerActiveObject *clicker); ServerActiveObject *clicker);
}; };