Parcourir la source

Handle lua entity HP changes correctly (like punches)

fixes #11975
sfan5 il y a 2 ans
Parent
commit
303329f2d6
2 fichiers modifiés avec 21 ajouts et 10 suppressions
  1. 14 10
      src/server/luaentity_sao.cpp
  2. 7 0
      src/server/luaentity_sao.h

+ 14 - 10
src/server/luaentity_sao.cpp

@@ -337,19 +337,9 @@ u32 LuaEntitySAO::punch(v3f dir,
 		if (result.did_punch) {
 			setHP((s32)getHP() - result.damage,
 				PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
-
-			// create message and add to list
-			sendPunchCommand();
 		}
 	}
 
-	if (getHP() == 0 && !isGone()) {
-		clearParentAttachment();
-		clearChildAttachments();
-		m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
-		markForRemoval();
-	}
-
 	actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
 			", hp=" << puncher->getHP() << ") punched " <<
 			getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
@@ -402,6 +392,20 @@ std::string LuaEntitySAO::getDescription()
 void LuaEntitySAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
 {
 	m_hp = rangelim(hp, 0, U16_MAX);
+
+	sendPunchCommand();
+
+	if (m_hp == 0 && !isGone()) {
+		clearParentAttachment();
+		clearChildAttachments();
+		if (m_registered) {
+			ServerActiveObject *killer = nullptr;
+			if (reason.type == PlayerHPChangeReason::PLAYER_PUNCH)
+				killer = reason.object;
+			m_env->getScriptIface()->luaentity_on_death(m_id, killer);
+		}
+		markForRemoval();
+	}	
 }
 
 u16 LuaEntitySAO::getHP() const

+ 7 - 0
src/server/luaentity_sao.h

@@ -36,23 +36,30 @@ public:
 	{
 	}
 	~LuaEntitySAO();
+
 	ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_LUAENTITY; }
 	ActiveObjectType getSendType() const { return ACTIVEOBJECT_TYPE_GENERIC; }
 	virtual void addedToEnvironment(u32 dtime_s);
 	void step(float dtime, bool send_recommended);
 	std::string getClientInitializationData(u16 protocol_version);
+
 	bool isStaticAllowed() const { return m_prop.static_save; }
 	bool shouldUnload() const { return true; }
 	void getStaticData(std::string *result) const;
+
 	u32 punch(v3f dir, const ToolCapabilities *toolcap = nullptr,
 			ServerActiveObject *puncher = nullptr,
 			float time_from_last_punch = 1000000.0f,
 			u16 initial_wear = 0);
+
 	void rightClick(ServerActiveObject *clicker);
+
 	void setPos(const v3f &pos);
 	void moveTo(v3f pos, bool continuous);
 	float getMinimumSavedMovement();
+
 	std::string getDescription();
+
 	void setHP(s32 hp, const PlayerHPChangeReason &reason);
 	u16 getHP() const;