[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / Totem.cpp
blobeae836fd563d700f1ee0c60b5a42cd54fa0fe79f
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Totem.h"
20 #include "WorldPacket.h"
21 #include "Log.h"
22 #include "Group.h"
23 #include "Player.h"
24 #include "ObjectMgr.h"
25 #include "SpellMgr.h"
26 #include "CreatureAI.h"
28 Totem::Totem() : Creature(CREATURE_SUBTYPE_TOTEM)
30 m_duration = 0;
31 m_type = TOTEM_PASSIVE;
34 void Totem::Update( uint32 time )
36 Unit *owner = GetOwner();
37 if (!owner || !owner->isAlive() || !isAlive())
39 UnSummon(); // remove self
40 return;
43 if (m_duration <= time)
45 UnSummon(); // remove self
46 return;
48 else
49 m_duration -= time;
51 Creature::Update( time );
54 void Totem::Summon(Unit* owner)
56 owner->GetMap()->Add((Creature*)this);
58 // select totem model in dependent from owner team
59 CreatureInfo const *cinfo = GetCreatureInfo();
60 if(owner->GetTypeId() == TYPEID_PLAYER && cinfo)
62 uint32 display_id = sObjectMgr.ChooseDisplayId(((Player*)owner)->GetTeam(), cinfo);
63 CreatureModelInfo const *minfo = sObjectMgr.GetCreatureModelRandomGender(display_id);
64 if (minfo)
65 display_id = minfo->modelid;
66 SetDisplayId(display_id);
69 AIM_Initialize();
71 if (owner->GetTypeId() == TYPEID_UNIT && ((Creature*)owner)->AI())
72 ((Creature*)owner)->AI()->JustSummoned((Creature*)this);
74 // there are some totems, which exist just for their visual appeareance
75 if (!GetSpell())
76 return;
78 switch(m_type)
80 case TOTEM_PASSIVE:
81 CastSpell(this, GetSpell(), true);
82 break;
83 case TOTEM_STATUE:
84 CastSpell(GetOwner(), GetSpell(), true);
85 break;
86 default: break;
90 void Totem::UnSummon()
92 CombatStop();
93 RemoveAurasDueToSpell(GetSpell());
95 if (Unit *owner = GetOwner())
97 owner->_RemoveTotem(this);
98 owner->RemoveAurasDueToSpell(GetSpell());
100 //remove aura all party members too
101 if (owner->GetTypeId() == TYPEID_PLAYER)
103 ((Player*)owner)->SendAutoRepeatCancel(this);
105 // Not only the player can summon the totem (scripted AI)
106 if (Group *pGroup = ((Player*)owner)->GetGroup())
108 for(GroupReference *itr = pGroup->GetFirstMember(); itr != NULL; itr = itr->next())
110 Player* Target = itr->getSource();
111 if(Target && pGroup->SameSubGroup((Player*)owner, Target))
112 Target->RemoveAurasDueToSpell(GetSpell());
117 if (owner->GetTypeId() == TYPEID_UNIT && ((Creature*)owner)->AI())
118 ((Creature*)owner)->AI()->SummonedCreatureDespawn((Creature*)this);
121 AddObjectToRemoveList();
124 void Totem::SetOwner(uint64 guid)
126 SetCreatorGUID(guid);
127 SetOwnerGUID(guid);
128 if (Unit *owner = GetOwner())
130 setFaction(owner->getFaction());
131 SetLevel(owner->getLevel());
135 Unit *Totem::GetOwner()
137 uint64 ownerid = GetOwnerGUID();
138 if(!ownerid)
139 return NULL;
140 return ObjectAccessor::GetUnit(*this, ownerid);
143 void Totem::SetTypeBySummonSpell(SpellEntry const * spellProto)
145 // Get spell casted by totem
146 SpellEntry const * totemSpell = sSpellStore.LookupEntry(GetSpell());
147 if (totemSpell)
149 // If spell have cast time -> so its active totem
150 if (GetSpellCastTime(totemSpell))
151 m_type = TOTEM_ACTIVE;
153 if(spellProto->SpellIconID == 2056)
154 m_type = TOTEM_STATUE; //Jewelery statue
157 bool Totem::IsImmunedToSpellEffect(SpellEntry const* spellInfo, SpellEffectIndex index) const
159 // TODO: possibly all negative auras immune?
160 switch(spellInfo->Effect[index])
162 case SPELL_EFFECT_ATTACK_ME:
163 return true;
164 default:
165 break;
167 switch(spellInfo->EffectApplyAuraName[index])
169 case SPELL_AURA_PERIODIC_DAMAGE:
170 case SPELL_AURA_PERIODIC_LEECH:
171 case SPELL_AURA_MOD_FEAR:
172 case SPELL_AURA_TRANSFORM:
173 case SPELL_AURA_MOD_TAUNT:
174 return true;
175 default:
176 break;
178 return Creature::IsImmunedToSpellEffect(spellInfo, index);