Make entity on_punch have same signature and behaviour as player on_punch
authorsapier <sapier at gmx dot net>
Sat, 21 Jan 2017 14:58:07 +0000 (15:58 +0100)
committersapier <sapier at gmx dot net>
Sat, 28 Jan 2017 14:57:54 +0000 (15:57 +0100)
doc/lua_api.txt
src/content_sao.cpp
src/script/cpp_api/s_entity.cpp
src/script/cpp_api/s_entity.h

index ee7d57c2f936cd7ab22c682f929ff947e2b82bf6..ff745c1c2173b420622760a1b27b74b771b0569f 100644 (file)
@@ -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:
 
-    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
 the entity itself.
@@ -1403,6 +1403,9 @@ the entity itself.
 * `tool_capabilities` can be `nil`.
 * `direction` is a unit vector, pointing from the source of the punch to
    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:
 
index 35133490ed553c905cbab6eb5e00a9d72e88a0e1..7a1171eb70427790afa07ce28985e980fe88c25c 100644 (file)
@@ -578,28 +578,32 @@ int LuaEntitySAO::punch(v3f dir,
                        punchitem,
                        time_from_last_punch);
 
-       if (result.did_punch) {
-               setHP(getHP() - result.damage);
+       bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
+                       time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0);
 
-               if (result.damage > 0) {
-                       std::string punchername = puncher ? puncher->getDescription() : "nil";
+       if (!damage_handled) {
+               if (result.did_punch) {
+                       setHP(getHP() - result.damage);
 
-                       actionstream << getDescription() << " punched by "
-                                       << punchername << ", damage " << result.damage
-                                       << " hp, health now " << getHP() << " hp" << std::endl;
-               }
+                       if (result.damage > 0) {
+                               std::string punchername = puncher ? puncher->getDescription() : "nil";
 
-               std::string str = gob_cmd_punched(result.damage, getHP());
-               // create message and add to list
-               ActiveObjectMessage aom(getId(), true, str);
-               m_messages_out.push(aom);
+                               actionstream << getDescription() << " punched by "
+                                               << punchername << ", damage " << result.damage
+                                               << " hp, health now " << getHP() << " hp" << std::endl;
+                       }
+
+                       std::string str = gob_cmd_punched(result.damage, getHP());
+                       // create message and add to list
+                       ActiveObjectMessage aom(getId(), true, str);
+                       m_messages_out.push(aom);
+               }
        }
 
        if (getHP() == 0)
                m_removed = true;
 
-       m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
-                       time_from_last_punch, toolcap, dir);
+
 
        return result.wear;
 }
index 378a6bf0964204ca614760b4462d3c087748698e..fcd8dd4a07033c8ba18a9a599c65d58c9bf59c32 100644 (file)
@@ -224,10 +224,10 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
 }
 
 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
-//                       tool_capabilities, direction)
-void ScriptApiEntity::luaentity_Punch(u16 id,
+//                       tool_capabilities, direction, damage)
+bool ScriptApiEntity::luaentity_Punch(u16 id,
                ServerActiveObject *puncher, float time_from_last_punch,
-               const ToolCapabilities *toolcap, v3f dir)
+               const ToolCapabilities *toolcap, v3f dir, s16 damage)
 {
        SCRIPTAPI_PRECHECKHEADER
 
@@ -242,8 +242,8 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
        // Get function
        lua_getfield(L, -1, "on_punch");
        if (lua_isnil(L, -1)) {
-               lua_pop(L, 2); // Pop on_punch and entitu
-               return;
+               lua_pop(L, 2); // Pop on_punch and entity
+               return false;
        }
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_pushvalue(L, object);  // self
@@ -251,11 +251,14 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
        lua_pushnumber(L, time_from_last_punch);
        push_tool_capabilities(L, *toolcap);
        push_v3f(L, dir);
+       lua_pushnumber(L, damage);
 
        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
+       return retval;
 }
 
 // Calls entity:on_rightclick(ObjectRef clicker)
index 8df9d7f0014e8f422a5949dafc97806eb4f86536..4e2a056bbb4cc10a001c943ca387216dd4549bda 100644 (file)
@@ -38,9 +38,9 @@ public:
        void luaentity_GetProperties(u16 id,
                        ObjectProperties *prop);
        void luaentity_Step(u16 id, float dtime);
-       void luaentity_Punch(u16 id,
+       bool luaentity_Punch(u16 id,
                        ServerActiveObject *puncher, float time_from_last_punch,
-                       const ToolCapabilities *toolcap, v3f dir);
+                       const ToolCapabilities *toolcap, v3f dir, s16 damage);
        void luaentity_Rightclick(u16 id,
                        ServerActiveObject *clicker);
 };