PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/server/game/Spells/Auras/SpellAuras.h

https://github.com/Archives/TrinityCore
C Header | 264 lines | 202 code | 44 blank | 18 comment | 14 complexity | a94dbad3e2283f662f4cfc7db2d62e31 MD5 | raw file
  1. /*
  2. * Copyright (C) 2008 - 2011 Trinity <http://www.trinitycore.org/>
  3. *
  4. * Copyright (C) 2010 - 2011 Myth Project <http://bitbucket.org/sun/myth-core/>
  5. *
  6. * Myth Project's source is based on the Trinity Project source, you can find the
  7. * link to that easily in Trinity Copyrights. Myth Project is a private community.
  8. * To get access, you either have to donate or pass a developer test.
  9. * You can't share Myth Project's sources! Only for personal use.
  10. */
  11. #ifndef TRINITY_SPELLAURAS_H
  12. #define TRINITY_SPELLAURAS_H
  13. #include "SpellAuraDefines.h"
  14. class Unit;
  15. struct SpellEntry;
  16. struct SpellModifier;
  17. struct ProcTriggerSpell;
  18. struct SpellProcEntry;
  19. // forward decl
  20. class AuraEffect;
  21. class Aura;
  22. class DynamicObject;
  23. class AuraScript;
  24. class ProcInfo;
  25. // update aura target map every 500 ms instead of every update - reduce amount of grid searcher calls
  26. #define UPDATE_TARGET_MAP_INTERVAL 500
  27. class AuraApplication
  28. {
  29. friend void Unit::_ApplyAura(AuraApplication * aurApp, uint8 effMask);
  30. friend void Unit::_UnapplyAura(AuraApplicationMap::iterator &i, AuraRemoveMode removeMode);
  31. friend void Unit::_ApplyAuraEffect(Aura * aura, uint8 effIndex);
  32. friend void Unit::RemoveAura(AuraApplication * aurApp, AuraRemoveMode mode);
  33. friend AuraApplication * Unit::_CreateAuraApplication(Aura * aura, uint8 effMask);
  34. private:
  35. Unit* const m_target;
  36. Aura * const m_base;
  37. uint8 m_slot; // Aura slot on unit
  38. uint8 m_flags; // Aura info flag
  39. uint8 m_effectsToApply; // Used only at spell hit to determine which effect should be applied
  40. AuraRemoveMode m_removeMode:8; // Store info for know remove aura reason
  41. bool m_needClientUpdate:1;
  42. explicit AuraApplication(Unit* target, Unit* caster, Aura * base, uint8 effMask);
  43. void _Remove();
  44. private:
  45. void _InitFlags(Unit* caster, uint8 effMask);
  46. void _HandleEffect(uint8 effIndex, bool apply);
  47. public:
  48. Unit* GetTarget() const { return m_target; }
  49. Aura * GetBase() const { return m_base; }
  50. uint8 GetSlot() const { return m_slot; }
  51. uint8 GetFlags() const { return m_flags; }
  52. uint8 GetEffectMask() const { return m_flags & (AFLAG_EFF_INDEX_0 | AFLAG_EFF_INDEX_1 | AFLAG_EFF_INDEX_2); }
  53. bool HasEffect(uint8 effect) const { ASSERT(effect < MAX_SPELL_EFFECTS); return m_flags & (1<<effect); }
  54. bool IsPositive() const { return m_flags & AFLAG_POSITIVE; }
  55. bool IsSelfcasted() const { return m_flags & AFLAG_CASTER; }
  56. uint8 GetEffectsToApply() const { return m_effectsToApply; }
  57. void SetRemoveMode(AuraRemoveMode mode) { m_removeMode = mode; }
  58. AuraRemoveMode GetRemoveMode() const {return m_removeMode;}
  59. void SetNeedClientUpdate() { m_needClientUpdate = true;}
  60. bool IsNeedClientUpdate() const { return m_needClientUpdate;}
  61. void BuildUpdatePacket(ByteBuffer& data, bool remove) const;
  62. void ClientUpdate(bool remove = false);
  63. };
  64. class Aura
  65. {
  66. friend Aura * Unit::_TryStackingOrRefreshingExistingAura(SpellEntry const* newAura, uint8 effMask, Unit* caster, int32 *baseAmount, Item* castItem, uint64 casterGUID);
  67. public:
  68. typedef std::map<uint64, AuraApplication *> ApplicationMap;
  69. static uint8 BuildEffectMaskForOwner(SpellEntry const* spellProto, uint8 avalibleEffectMask, WorldObject* owner);
  70. static Aura* TryRefreshStackOrCreate(SpellEntry const* spellproto, uint8 tryEffMask, WorldObject* owner, Unit* caster, int32* baseAmount = NULL, Item* castItem = NULL, uint64 casterGUID = 0, bool* refresh = NULL);
  71. static Aura* TryCreate(SpellEntry const* spellproto, uint8 effMask, WorldObject * owner, Unit* caster, int32 *baseAmount = NULL, Item* castItem = NULL, uint64 casterGUID = 0);
  72. static Aura* Create(SpellEntry const* spellproto, uint8 effMask, WorldObject* owner, Unit* caster, int32* baseAmount, Item* castItem, uint64 casterGUID);
  73. explicit Aura(SpellEntry const* spellproto, WorldObject * owner, Unit* caster, Item* castItem, uint64 casterGUID);
  74. void _InitEffects(uint8 effMask, Unit* caster, int32 *baseAmount);
  75. virtual ~Aura();
  76. SpellEntry const* GetSpellProto() const { return m_spellProto; }
  77. uint32 GetId() const{ return GetSpellProto()->Id; }
  78. uint64 GetCastItemGUID() const { return m_castItemGuid; }
  79. uint64 const& GetCasterGUID() const { return m_casterGuid; }
  80. Unit* GetCaster() const;
  81. WorldObject * GetOwner() const { return m_owner; }
  82. Unit* GetUnitOwner() const { ASSERT(GetType() == UNIT_AURA_TYPE); return (Unit*)m_owner; }
  83. DynamicObject * GetDynobjOwner() const { ASSERT(GetType() == DYNOBJ_AURA_TYPE); return (DynamicObject*)m_owner; }
  84. AuraObjectType GetType() const;
  85. virtual void _ApplyForTarget(Unit* target, Unit* caster, AuraApplication * auraApp);
  86. virtual void _UnapplyForTarget(Unit* target, Unit* caster, AuraApplication * auraApp);
  87. void _Remove(AuraRemoveMode removeMode);
  88. virtual void Remove(AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT) = 0;
  89. virtual void FillTargetMap(std::map<Unit *, uint8> & targets, Unit* caster) = 0;
  90. void UpdateTargetMap(Unit* caster, bool apply = true);
  91. void _RegisterForTargets() {Unit* caster = GetCaster(); UpdateTargetMap(caster, false);}
  92. void ApplyForTargets() {Unit* caster = GetCaster(); UpdateTargetMap(caster, true);}
  93. void _ApplyEffectForTargets(uint8 effIndex);
  94. void UpdateOwner(uint32 diff, WorldObject * owner);
  95. void Update(uint32 diff, Unit* caster);
  96. time_t GetApplyTime() const { return m_applyTime; }
  97. int32 GetMaxDuration() const { return m_maxDuration; }
  98. void SetMaxDuration(int32 duration) { m_maxDuration = duration; }
  99. int32 CalcMaxDuration() const { return CalcMaxDuration(GetCaster()); }
  100. int32 CalcMaxDuration(Unit* caster) const;
  101. int32 GetDuration() const { return m_duration; }
  102. void SetDuration(int32 duration, bool withMods = false);
  103. void RefreshDuration();
  104. void RefreshTimers();
  105. bool IsExpired() const { return !GetDuration();}
  106. bool IsPermanent() const { return GetMaxDuration() == -1; }
  107. uint8 GetCharges() const { return m_procCharges; }
  108. void SetCharges(uint8 charges);
  109. uint8 CalcMaxCharges(Unit* caster) const;
  110. uint8 CalcMaxCharges() const { return CalcMaxCharges(GetCaster()); }
  111. bool ModCharges(int32 num, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT);
  112. bool DropCharge(AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT) { return ModCharges(-1, removeMode); }
  113. uint8 GetStackAmount() const { return m_stackAmount; }
  114. void SetStackAmount(uint8 num);
  115. bool ModStackAmount(int32 num, AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT);
  116. void RefreshSpellMods();
  117. uint8 GetCasterLevel() const { return m_casterLevel; }
  118. bool IsPassive() const;
  119. bool IsDeathPersistent() const;
  120. bool IsRemovedOnShapeLost(Unit* target) const { return (GetCasterGUID() == target->GetGUID() && m_spellProto->Stances && !(m_spellProto->AttributesEx2 & SPELL_ATTR2_NOT_NEED_SHAPESHIFT) && !(m_spellProto->Attributes & SPELL_ATTR0_NOT_SHAPESHIFT)); }
  121. bool CanBeSaved() const;
  122. bool IsRemoved() const { return m_isRemoved; }
  123. bool CanBeSentToClient() const;
  124. // Single cast aura helpers
  125. bool IsSingleTarget() const {return m_isSingleTarget;}
  126. void SetIsSingleTarget(bool val) { m_isSingleTarget = val;}
  127. void UnregisterSingleTarget();
  128. void SetLoadedState(int32 maxduration, int32 duration, int32 charges, uint8 stackamount, uint8 recalculateMask, int32 * amount);
  129. // helpers for aura effects
  130. bool HasEffect(uint8 effIndex) const { return bool(GetEffect(effIndex)); }
  131. bool HasEffectType(AuraType type) const;
  132. AuraEffect * GetEffect(uint8 effIndex) const { ASSERT (effIndex < MAX_SPELL_EFFECTS); return m_effects[effIndex]; }
  133. uint8 GetEffectMask() const { uint8 effMask = 0; for (uint8 i = 0; i < MAX_SPELL_EFFECTS; ++i) if (m_effects[i]) effMask |= 1<<i; return effMask; }
  134. void RecalculateAmountOfEffects();
  135. void HandleAllEffects(AuraApplication * aurApp, uint8 mode, bool apply);
  136. // Helpers for targets
  137. ApplicationMap const & GetApplicationMap() {return m_applications;}
  138. void GetApplicationList(std::list<AuraApplication*> & applicationList) const;
  139. const AuraApplication * GetApplicationOfTarget (uint64 const & guid) const { ApplicationMap::const_iterator itr = m_applications.find(guid); if (itr != m_applications.end()) return itr->second; return NULL; }
  140. AuraApplication * GetApplicationOfTarget (uint64 const & guid) { ApplicationMap::iterator itr = m_applications.find(guid); if (itr != m_applications.end()) return itr->second; return NULL; }
  141. bool IsAppliedOnTarget(uint64 const & guid) const { return m_applications.find(guid) != m_applications.end(); }
  142. void SetNeedClientUpdateForTargets() const;
  143. void HandleAuraSpecificMods(AuraApplication const* aurApp, Unit* caster, bool apply, bool onReapply);
  144. bool CanBeAppliedOn(Unit* target);
  145. bool CheckAreaTarget(Unit* target);
  146. // Proc system
  147. bool IsProcOnCooldown() const;
  148. void AddProcCooldown(uint32 msec);
  149. bool IsUsingCharges() const { return m_isUsingCharges; }
  150. void SetUsingCharges(bool val) { m_isUsingCharges = val; }
  151. void PrepareProcToTrigger();
  152. bool IsProcTriggeredOnEvent(AuraApplication* aurApp, ProcEventInfo& eventInfo) const;
  153. float CalcProcChance(SpellProcEntry const& procEntry, ProcEventInfo& eventInfo) const;
  154. void TriggerProcOnEvent(AuraApplication* aurApp, ProcEventInfo& eventInfo);
  155. // AuraScript
  156. void LoadScripts();
  157. bool CallScriptCheckAreaTargetHandlers(Unit* target);
  158. bool CallScriptEffectApplyHandlers(AuraEffect const* aurEff, AuraApplication const* aurApp, AuraEffectHandleModes mode);
  159. bool CallScriptEffectRemoveHandlers(AuraEffect const* aurEff, AuraApplication const* aurApp, AuraEffectHandleModes mode);
  160. void CallScriptAfterEffectApplyHandlers(AuraEffect const* aurEff, AuraApplication const* aurApp, AuraEffectHandleModes mode);
  161. void CallScriptAfterEffectRemoveHandlers(AuraEffect const* aurEff, AuraApplication const* aurApp, AuraEffectHandleModes mode);
  162. bool CallScriptEffectPeriodicHandlers(AuraEffect const* aurEff, AuraApplication const* aurApp);
  163. void CallScriptEffectUpdatePeriodicHandlers(AuraEffect * aurEff);
  164. void CallScriptEffectCalcAmountHandlers(AuraEffect const* aurEff, int32 & amount, bool & canBeRecalculated);
  165. void CallScriptEffectCalcPeriodicHandlers(AuraEffect const* aurEff, bool & isPeriodic, int32 & amplitude);
  166. void CallScriptEffectCalcSpellModHandlers(AuraEffect const* aurEff, SpellModifier *& spellMod);
  167. void CallScriptEffectAbsorbHandlers(AuraEffect * aurEff, AuraApplication const* aurApp, DamageInfo & dmgInfo, uint32 & absorbAmount, bool & defaultPrevented);
  168. void CallScriptEffectAfterAbsorbHandlers(AuraEffect * aurEff, AuraApplication const* aurApp, DamageInfo & dmgInfo, uint32 & absorbAmount);
  169. void CallScriptEffectManaShieldHandlers(AuraEffect * aurEff, AuraApplication const* aurApp, DamageInfo & dmgInfo, uint32 & absorbAmount, bool & defaultPrevented);
  170. void CallScriptEffectAfterManaShieldHandlers(AuraEffect * aurEff, AuraApplication const* aurApp, DamageInfo & dmgInfo, uint32 & absorbAmount);
  171. std::list<AuraScript *> m_loadedScripts;
  172. private:
  173. void _DeleteRemovedApplications();
  174. protected:
  175. SpellEntry const* const m_spellProto;
  176. uint64 const m_casterGuid;
  177. uint64 const m_castItemGuid; // it is NOT safe to keep a pointer to the item because it may get deleted
  178. time_t const m_applyTime;
  179. WorldObject * const m_owner; //
  180. int32 m_maxDuration; // Max aura duration
  181. int32 m_duration; // Current time
  182. int32 m_timeCla; // Timer for power per sec calcultion
  183. int32 m_updateTargetMapInterval; // Timer for UpdateTargetMapOfEffect
  184. uint8 const m_casterLevel; // Aura level (store caster level for correct show level dep amount)
  185. uint8 m_procCharges; // Aura charges (0 for infinite)
  186. uint8 m_stackAmount; // Aura stack amount
  187. AuraEffect * m_effects[3];
  188. ApplicationMap m_applications;
  189. bool m_isRemoved:1;
  190. bool m_isSingleTarget:1; // true if it's a single target spell and registered at caster - can change at spell steal for example
  191. bool m_isUsingCharges:1;
  192. private:
  193. Unit::AuraApplicationList m_removedApplications;
  194. };
  195. class UnitAura : public Aura
  196. {
  197. friend Aura * Aura::Create(SpellEntry const* spellproto, uint8 effMask, WorldObject * owner, Unit* caster, int32 *baseAmount, Item* castItem, uint64 casterGUID);
  198. protected:
  199. explicit UnitAura(SpellEntry const* spellproto, uint8 effMask, WorldObject * owner, Unit* caster, int32 *baseAmount, Item* castItem, uint64 casterGUID);
  200. public:
  201. void _ApplyForTarget(Unit* target, Unit* caster, AuraApplication * aurApp);
  202. void _UnapplyForTarget(Unit* target, Unit* caster, AuraApplication * aurApp);
  203. void Remove(AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT);
  204. void FillTargetMap(std::map<Unit *, uint8> & targets, Unit* caster);
  205. // Allow Apply Aura Handler to modify and access m_AuraDRGroup
  206. void SetDiminishGroup(DiminishingGroup group) { m_AuraDRGroup = group; }
  207. DiminishingGroup GetDiminishGroup() const { return m_AuraDRGroup; }
  208. private:
  209. DiminishingGroup m_AuraDRGroup:8; // Diminishing
  210. };
  211. class DynObjAura : public Aura
  212. {
  213. friend Aura * Aura::Create(SpellEntry const* spellproto, uint8 effMask, WorldObject * owner, Unit* caster, int32 *baseAmount, Item* castItem, uint64 casterGUID);
  214. protected:
  215. explicit DynObjAura(SpellEntry const* spellproto, uint8 effMask, WorldObject * owner, Unit* caster, int32 *baseAmount, Item* castItem, uint64 casterGUID);
  216. public:
  217. void Remove(AuraRemoveMode removeMode = AURA_REMOVE_BY_DEFAULT);
  218. void FillTargetMap(std::map<Unit *, uint8> & targets, Unit* caster);
  219. };
  220. #endif