Skip to content

Commit

Permalink
feat(Core/GameObject): Implement OnGameObjectModifyHealth() hook (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
KJack authored Sep 28, 2023
1 parent 2779833 commit c25d0b3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/server/game/Entities/GameObject/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,14 @@ void GameObject::ModifyHealth(int32 change, Unit* attackerOrHealer /*= nullptr*/
if (!IsDestructibleBuilding())
return;

if (!m_goValue.Building.MaxHealth || !change)
// if this building doesn't have health, return
if (!m_goValue.Building.MaxHealth)
return;

sScriptMgr->OnGameObjectModifyHealth(this, attackerOrHealer, change, sSpellMgr->GetSpellInfo(spellId));

// if the health isn't being changed, return
if (!change)
return;

if (!m_allowModifyDestructibleBuilding)
Expand Down
15 changes: 15 additions & 0 deletions src/server/game/Scripting/ScriptDefines/GameObjectScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ void ScriptMgr::OnGameObjectDamaged(GameObject* go, Player* player)
}
}

void ScriptMgr::OnGameObjectModifyHealth(GameObject* go, Unit* attackerOrHealer, int32& change, SpellInfo const* spellInfo)
{
ASSERT(go);

ExecuteScript<AllGameObjectScript>([&](AllGameObjectScript* script)
{
script->OnGameObjectModifyHealth(go, attackerOrHealer, change, spellInfo);
});

if (auto tempScript = ScriptRegistry<GameObjectScript>::GetScriptById(go->GetScriptId()))
{
tempScript->OnModifyHealth(go, attackerOrHealer, change, spellInfo);
}
}

void ScriptMgr::OnGameObjectLootStateChanged(GameObject* go, uint32 state, Unit* unit)
{
ASSERT(go);
Expand Down
7 changes: 7 additions & 0 deletions src/server/game/Scripting/ScriptMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,9 @@ class AllGameObjectScript : public ScriptObject
// Called when the game object is damaged (destructible buildings only).
virtual void OnGameObjectDamaged(GameObject* /*go*/, Player* /*player*/) { }

// Called when the health of a game object is modified (destructible buildings only).
virtual void OnGameObjectModifyHealth(GameObject* /*go*/, Unit* /*attackerOrHealer*/, int32& /*change*/, SpellInfo const* /*spellInfo*/) { }

// Called when the game object loot state is changed.
virtual void OnGameObjectLootStateChanged(GameObject* /*go*/, uint32 /*state*/, Unit* /*unit*/) { }

Expand Down Expand Up @@ -787,6 +790,9 @@ class GameObjectScript : public ScriptObject, public UpdatableScript<GameObject>
// Called when the game object is damaged (destructible buildings only).
virtual void OnDamaged(GameObject* /*go*/, Player* /*player*/) { }

// Called when the health of a game object is modified (destructible buildings only).
virtual void OnModifyHealth(GameObject* /*go*/, Unit* /*attackerOrHealer*/, int32& /*change*/, SpellInfo const* /*spellInfo*/) { }

// Called when the game object loot state is changed.
virtual void OnLootStateChanged(GameObject* /*go*/, uint32 /*state*/, Unit* /*unit*/) { }

Expand Down Expand Up @@ -2228,6 +2234,7 @@ class ScriptMgr
uint32 GetDialogStatus(Player* player, GameObject* go);
void OnGameObjectDestroyed(GameObject* go, Player* player);
void OnGameObjectDamaged(GameObject* go, Player* player);
void OnGameObjectModifyHealth(GameObject* go, Unit* attackerOrHealer, int32& change, SpellInfo const* spellInfo);
void OnGameObjectLootStateChanged(GameObject* go, uint32 state, Unit* unit);
void OnGameObjectStateChanged(GameObject* go, uint32 state);
void OnGameObjectUpdate(GameObject* go, uint32 diff);
Expand Down

0 comments on commit c25d0b3

Please sign in to comment.