Updated Copyright year to 2013
[getmangos.git] / src / game / Totem.cpp
blobca238811687052691e47b88f5030b6b118448c4b
1 /*
2 * Copyright (C) 2005-2013 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"
27 #include "InstanceData.h"
29 Totem::Totem() : Creature(CREATURE_SUBTYPE_TOTEM)
31 m_duration = 0;
32 m_type = TOTEM_PASSIVE;
35 bool Totem::Create(uint32 guidlow, CreatureCreatePos& cPos, CreatureInfo const* cinfo, Unit* owner)
37 SetMap(cPos.GetMap());
38 SetPhaseMask(cPos.GetPhaseMask(), false);
40 Team team = owner->GetTypeId() == TYPEID_PLAYER ? ((Player*)owner)->GetTeam() : TEAM_NONE;
42 if (!CreateFromProto(guidlow, cinfo, team))
43 return false;
45 // special model selection case for totems
46 if (owner->GetTypeId() == TYPEID_PLAYER)
48 if (uint32 modelid_race = sObjectMgr.GetModelForRace(GetNativeDisplayId(), owner->getRaceMask()))
49 SetDisplayId(modelid_race);
52 cPos.SelectFinalPoint(this);
54 // totem must be at same Z in case swimming caster and etc.
55 if (fabs(cPos.m_pos.z - owner->GetPositionZ()) > 5.0f)
56 cPos.m_pos.z = owner->GetPositionZ();
58 if (!cPos.Relocate(this))
59 return false;
61 // Notify the map's instance data.
62 // Only works if you create the object in it, not if it is moves to that map.
63 // Normally non-players do not teleport to other maps.
64 if (InstanceData* iData = GetMap()->GetInstanceData())
65 iData->OnCreatureCreate(this);
67 LoadCreatureAddon(false);
69 return true;
72 void Totem::Update(uint32 update_diff, uint32 time)
74 Unit* owner = GetOwner();
75 if (!owner || !owner->isAlive() || !isAlive())
77 UnSummon(); // remove self
78 return;
81 if (m_duration <= update_diff)
83 UnSummon(); // remove self
84 return;
86 else
87 m_duration -= update_diff;
89 Creature::Update(update_diff, time);
92 void Totem::Summon(Unit* owner)
94 AIM_Initialize();
95 owner->GetMap()->Add((Creature*)this);
97 if (owner->GetTypeId() == TYPEID_UNIT && ((Creature*)owner)->AI())
98 ((Creature*)owner)->AI()->JustSummoned((Creature*)this);
100 // there are some totems, which exist just for their visual appeareance
101 if (!GetSpell())
102 return;
104 switch (m_type)
106 case TOTEM_PASSIVE:
107 CastSpell(this, GetSpell(), true);
108 break;
109 case TOTEM_STATUE:
110 CastSpell(GetOwner(), GetSpell(), true);
111 break;
112 default: break;
116 void Totem::UnSummon()
118 CombatStop();
119 RemoveAurasDueToSpell(GetSpell());
121 if (Unit* owner = GetOwner())
123 owner->_RemoveTotem(this);
124 owner->RemoveAurasDueToSpell(GetSpell());
126 // remove aura all party members too
127 if (owner->GetTypeId() == TYPEID_PLAYER)
129 ((Player*)owner)->SendAutoRepeatCancel(this);
131 // Not only the player can summon the totem (scripted AI)
132 if (Group* pGroup = ((Player*)owner)->GetGroup())
134 for (GroupReference* itr = pGroup->GetFirstMember(); itr != NULL; itr = itr->next())
136 Player* Target = itr->getSource();
137 if (Target && pGroup->SameSubGroup((Player*)owner, Target))
138 Target->RemoveAurasDueToSpell(GetSpell());
143 if (owner->GetTypeId() == TYPEID_UNIT && ((Creature*)owner)->AI())
144 ((Creature*)owner)->AI()->SummonedCreatureDespawn((Creature*)this);
147 // any totem unsummon look like as totem kill, req. for proper animation
148 if (isAlive())
149 SetDeathState(DEAD);
151 AddObjectToRemoveList();
154 void Totem::SetOwner(Unit* owner)
156 SetCreatorGuid(owner->GetObjectGuid());
157 SetOwnerGuid(owner->GetObjectGuid());
158 setFaction(owner->getFaction());
159 SetLevel(owner->getLevel());
162 Unit* Totem::GetOwner()
164 if (ObjectGuid ownerGuid = GetOwnerGuid())
165 return ObjectAccessor::GetUnit(*this, ownerGuid);
167 return NULL;
170 void Totem::SetTypeBySummonSpell(SpellEntry const* spellProto)
172 // Get spell casted by totem
173 SpellEntry const* totemSpell = sSpellStore.LookupEntry(GetSpell());
174 if (totemSpell)
176 // If spell have cast time -> so its active totem
177 if (GetSpellCastTime(totemSpell))
178 m_type = TOTEM_ACTIVE;
180 if (spellProto->SpellIconID == 2056)
181 m_type = TOTEM_STATUE; // Jewelery statue
184 bool Totem::IsImmuneToSpellEffect(SpellEntry const* spellInfo, SpellEffectIndex index, bool castOnSelf) const
186 SpellEffectEntry const* spellEffect = spellInfo->GetSpellEffect(index);
187 if(spellEffect)
189 // TODO: possibly all negative auras immune?
190 switch(spellEffect->Effect)
192 case SPELL_EFFECT_ATTACK_ME:
193 return true;
194 default:
195 break;
197 switch(spellEffect->EffectApplyAuraName)
199 case SPELL_AURA_PERIODIC_DAMAGE:
200 case SPELL_AURA_PERIODIC_LEECH:
201 case SPELL_AURA_MOD_FEAR:
202 case SPELL_AURA_TRANSFORM:
203 case SPELL_AURA_MOD_TAUNT:
204 return true;
205 default:
206 break;
210 return Creature::IsImmuneToSpellEffect(spellInfo, index, castOnSelf);