aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBeha <shacknetisp@mail.com>2019-07-30 11:29:45 -0400
committersfan5 <sfan5@live.de>2019-07-30 17:29:45 +0200
commitb8aaef704d3e29c43edc3312a3c5e25516d9c864 (patch)
tree9640e90c11c60982c0c2655c9a5f16864d649744 /src
parent2dd645134eaab7611d167e2cb8469242e642c7ef (diff)
downloadminetest-b8aaef704d3e29c43edc3312a3c5e25516d9c864.tar.gz
minetest-b8aaef704d3e29c43edc3312a3c5e25516d9c864.tar.bz2
minetest-b8aaef704d3e29c43edc3312a3c5e25516d9c864.zip
Move the clamping of hp/breath when their maximums change to read_object_properties(). (#8689)
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).
Diffstat (limited to 'src')
-rw-r--r--src/content_sao.cpp2
-rw-r--r--src/script/common/c_content.cpp21
-rw-r--r--src/script/common/c_content.h2
-rw-r--r--src/script/cpp_api/s_entity.cpp6
-rw-r--r--src/script/cpp_api/s_entity.h2
-rw-r--r--src/script/lua_api/l_object.cpp15
6 files changed, 26 insertions, 22 deletions
diff --git a/src/content_sao.cpp b/src/content_sao.cpp
index 41b1aec18..84a09d977 100644
--- a/src/content_sao.cpp
+++ b/src/content_sao.cpp
@@ -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
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index 361db226e..3a9e9e049 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -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);
diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h
index f3a653682..9e755682f 100644
--- a/src/script/common/c_content.h
+++ b/src/script/common/c_content.h
@@ -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,
diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp
index 8af9f9bf6..26c7e8cd4 100644
--- a/src/script/cpp_api/s_entity.cpp
+++ b/src/script/cpp_api/s_entity.cpp
@@ -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);
}
diff --git a/src/script/cpp_api/s_entity.h b/src/script/cpp_api/s_entity.h
index 966c2745e..cc08c46e8 100644
--- a/src/script/cpp_api/s_entity.h
+++ b/src/script/cpp_api/s_entity.h
@@ -35,7 +35,7 @@ public:
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,
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index 7ff1f58bc..ad8554834 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -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;
}