]> git.lizzy.rs Git - minetest.git/commitdiff
Move the clamping of hp/breath when their maximums change to read_object_properties...
authorBeha <shacknetisp@mail.com>
Tue, 30 Jul 2019 15:29:45 +0000 (11:29 -0400)
committersfan5 <sfan5@live.de>
Tue, 30 Jul 2019 15:29:45 +0000 (17:29 +0200)
This prevents set_properties() calls that have nothing to do with hp_max or breath_max overriding the saved hp before another mod has the chance to set a player's intended hp_max (such as in on_joinplayer).

src/content_sao.cpp
src/script/common/c_content.cpp
src/script/common/c_content.h
src/script/cpp_api/s_entity.cpp
src/script/cpp_api/s_entity.h
src/script/lua_api/l_object.cpp

index 41b1aec18794c65db0bea633ccd167368f4cd3e4..84a09d977f98dde74fc017475975f58107b93af8 100644 (file)
@@ -331,7 +331,7 @@ void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
        if(m_registered){
                // Get properties
                m_env->getScriptIface()->
-                       luaentity_GetProperties(m_id, &m_prop);
+                       luaentity_GetProperties(m_id, this, &m_prop);
                // Initialize HP from properties
                m_hp = m_prop.hp_max;
                // Activate entity, supplying serialized state
index 361db226e050c0f9a0646658b98034e0d7505312..3a9e9e049d1b20f52eccdaf45588706201963cd8 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "common/c_types.h"
 #include "nodedef.h"
 #include "object_properties.h"
+#include "content_sao.h"
 #include "cpp_api/s_node.h"
 #include "lua_api/l_object.h"
 #include "lua_api/l_item.h"
@@ -182,7 +183,7 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
 
 /******************************************************************************/
 void read_object_properties(lua_State *L, int index,
-               ObjectProperties *prop, IItemDefManager *idef)
+               ServerActiveObject *sao, ObjectProperties *prop, IItemDefManager *idef)
 {
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
@@ -190,10 +191,24 @@ void read_object_properties(lua_State *L, int index,
                return;
 
        int hp_max = 0;
-       if (getintfield(L, -1, "hp_max", hp_max))
+       if (getintfield(L, -1, "hp_max", hp_max)) {
                prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
 
-       getintfield(L, -1, "breath_max", prop->breath_max);
+               if (prop->hp_max < sao->getHP()) {
+                       PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
+                       sao->setHP(prop->hp_max, reason);
+                       if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
+                               sao->getEnv()->getGameDef()->SendPlayerHPOrDie((PlayerSAO *)sao, reason);
+               }
+       }
+
+       if (getintfield(L, -1, "breath_max", prop->breath_max)) {
+               if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
+                       PlayerSAO *player = (PlayerSAO *)sao;
+                       if (prop->breath_max < player->getBreath())
+                               player->setBreath(prop->breath_max);
+               }
+       }
        getboolfield(L, -1, "physical", prop->physical);
        getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
 
index f3a653682ab9006b2cf701cd3a06be0babd3d477..9e755682faeba29d2d5a43e9d36036be80868725 100644 (file)
@@ -62,6 +62,7 @@ struct HitParams;
 struct EnumString;
 struct NoiseParams;
 class Schematic;
+class ServerActiveObject;
 
 
 ContentFeatures    read_content_features     (lua_State *L, int index);
@@ -107,6 +108,7 @@ void               push_item_definition_full (lua_State *L,
                                               const ItemDefinition &i);
 
 void               read_object_properties    (lua_State *L, int index,
+                                              ServerActiveObject *sao,
                                               ObjectProperties *prop,
                                               IItemDefManager *idef);
 void               push_object_properties    (lua_State *L,
index 8af9f9bf67c6ba01b3f85045363e345fd69f2248..26c7e8cd4eee3150c4e080644e3218a9c222c481 100644 (file)
@@ -157,7 +157,7 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
 }
 
 void ScriptApiEntity::luaentity_GetProperties(u16 id,
-               ObjectProperties *prop)
+               ServerActiveObject *self, ObjectProperties *prop)
 {
        SCRIPTAPI_PRECHECKHEADER
 
@@ -170,11 +170,11 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
        prop->hp_max = 10;
 
        // Deprecated: read object properties directly
-       read_object_properties(L, -1, prop, getServer()->idef());
+       read_object_properties(L, -1, self, prop, getServer()->idef());
 
        // Read initial_properties
        lua_getfield(L, -1, "initial_properties");
-       read_object_properties(L, -1, prop, getServer()->idef());
+       read_object_properties(L, -1, self, prop, getServer()->idef());
        lua_pop(L, 1);
 }
 
index 966c2745ef54f7682d07ad2788730503912d292c..cc08c46e83e29d2b7155e1f77a21ca44d7029abf 100644 (file)
@@ -35,7 +35,7 @@ class ScriptApiEntity
        void luaentity_Remove(u16 id);
        std::string luaentity_GetStaticdata(u16 id);
        void luaentity_GetProperties(u16 id,
-                       ObjectProperties *prop);
+                       ServerActiveObject *self, ObjectProperties *prop);
        void luaentity_Step(u16 id, float dtime);
        bool luaentity_Punch(u16 id,
                        ServerActiveObject *puncher, float time_from_last_punch,
index 7ff1f58bc21f569fb3ca0051bf57b2e4909e1a06..ad855483462eb68d3bdcedc7ff03ff0fc1667d48 100644 (file)
@@ -757,20 +757,7 @@ int ObjectRef::l_set_properties(lua_State *L)
        if (!prop)
                return 0;
 
-       read_object_properties(L, 2, prop, getServer(L)->idef());
-
-       PlayerSAO *player = getplayersao(ref);
-
-       if (prop->hp_max < co->getHP()) {
-               PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
-               co->setHP(prop->hp_max, reason);
-               if (player)
-                       getServer(L)->SendPlayerHPOrDie(player, reason);
-       }
-
-       if (player && prop->breath_max < player->getBreath())
-               player->setBreath(prop->breath_max);
-
+       read_object_properties(L, 2, co, prop, getServer(L)->idef());
        co->notifyObjectPropertiesModified();
        return 0;
 }