root/trunk/src/game/Totem.cpp @ 126

Revision 126, 5.3 kB (checked in by yumileroy, 17 years ago)

[svn] * Fixed startup error flood if creature model id is 0
* Fixed totems using proper model ids broken after recent change
* Set pet grid activity state to that of caster upon summoning
* Fix a possible crash in ObjectAccessor?
note to self: don't commit anything without 3 days testing. ever. after this one ofc.

Original author: w12x
Date: 2008-10-27 15:28:04-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Totem.h"
22#include "WorldPacket.h"
23#include "MapManager.h"
24#include "Log.h"
25#include "Group.h"
26#include "Player.h"
27#include "ObjectMgr.h"
28#include "SpellMgr.h"
29
30Totem::Totem() : Creature()
31{
32    m_isTotem = true;
33    m_duration = 0;
34    m_type = TOTEM_PASSIVE;
35}
36
37void Totem::Update( uint32 time )
38{
39    Unit *owner = GetOwner();
40    if (!owner || !owner->isAlive() || !this->isAlive())
41    {
42        UnSummon();                                         // remove self
43        return;
44    }
45
46    if (m_duration <= time)
47    {
48        UnSummon();                                         // remove self
49        return;
50    }
51    else
52        m_duration -= time;
53
54    Creature::Update( time );
55}
56
57void Totem::Summon(Unit* owner)
58{
59    CreatureInfo const *cinfo = GetCreatureInfo();
60    if (owner->GetTypeId()==TYPEID_PLAYER && cinfo)
61    {
62        uint32 modelid = 0;
63        if(((Player*)owner)->GetTeam() == HORDE)
64        {
65            if(cinfo->Modelid3)
66                modelid = cinfo->Modelid3;
67            else if(cinfo->Modelid4)
68                modelid = cinfo->Modelid4;
69        }
70        else
71        {
72            if(cinfo->Modelid1)
73                modelid = cinfo->Modelid1;
74            else if(cinfo->Modelid2)
75                modelid = cinfo->Modelid2;
76        }
77        if (modelid)
78            SetDisplayId(modelid);
79        else
80            sLog.outErrorDb("Totem::Summon: Missing modelid information for entry %u, team %u, totem will use default values.",GetEntry(),((Player*)owner)->GetTeam());
81    }
82
83    // Only add if a display exists.
84    sLog.outDebug("AddObject at Totem.cpp line 49");
85    SetInstanceId(owner->GetInstanceId());
86    owner->GetMap()->Add((Creature*)this);
87
88    WorldPacket data(SMSG_GAMEOBJECT_SPAWN_ANIM_OBSOLETE, 8);
89    data << GetGUID();
90    SendMessageToSet(&data,true);
91
92    AIM_Initialize();
93
94    switch(m_type)
95    {
96        case TOTEM_PASSIVE: CastSpell(this, GetSpell(), true); break;
97        case TOTEM_STATUE:  CastSpell(GetOwner(), GetSpell(), true); break;
98        default: break;
99    }
100}
101
102void Totem::UnSummon()
103{
104    SendObjectDeSpawnAnim(GetGUID());
105
106    CombatStop();
107    RemoveAurasDueToSpell(GetSpell());
108    Unit *owner = this->GetOwner();
109    if (owner)
110    {
111        // clear owenr's totem slot
112        for(int i = 0; i < MAX_TOTEM; ++i)
113        {
114            if(owner->m_TotemSlot[i]==GetGUID())
115            {
116                owner->m_TotemSlot[i] = 0;
117                break;
118            }
119        }
120
121        owner->RemoveAurasDueToSpell(GetSpell());
122
123        //remove aura all party members too
124        Group *pGroup = NULL;
125        if (owner->GetTypeId() == TYPEID_PLAYER)
126        {
127            // Not only the player can summon the totem (scripted AI)
128            pGroup = ((Player*)owner)->GetGroup();
129            if (pGroup)
130            {
131                for(GroupReference *itr = pGroup->GetFirstMember(); itr != NULL; itr = itr->next())
132                {
133                    Player* Target = itr->getSource();
134                    if(Target && pGroup->SameSubGroup((Player*)owner, Target))
135                        Target->RemoveAurasDueToSpell(GetSpell());
136                }
137            }
138        }
139    }
140
141    CleanupsBeforeDelete();
142    AddObjectToRemoveList();
143}
144
145void Totem::SetOwner(uint64 guid)
146{
147    SetUInt64Value(UNIT_FIELD_SUMMONEDBY, guid);
148    SetUInt64Value(UNIT_FIELD_CREATEDBY, guid);
149    Unit *owner = this->GetOwner();
150    if (owner)
151    {
152        this->setFaction(owner->getFaction());
153        this->SetLevel(owner->getLevel());
154    }
155}
156
157Unit *Totem::GetOwner()
158{
159    uint64 ownerid = GetOwnerGUID();
160    if(!ownerid)
161        return NULL;
162    return ObjectAccessor::GetUnit(*this, ownerid);
163}
164
165void Totem::SetTypeBySummonSpell(SpellEntry const * spellProto)
166{
167    // Get spell casted by totem
168    SpellEntry const * totemSpell = sSpellStore.LookupEntry(GetSpell());
169    if (totemSpell)
170    {
171        // If spell have cast time -> so its active totem
172        if (GetSpellCastTime(totemSpell))
173            m_type = TOTEM_ACTIVE;
174    }
175    if(spellProto->SpellIconID==2056)
176        m_type = TOTEM_STATUE;                              //Jewelery statue
177}
178
179bool Totem::IsImmunedToSpell(SpellEntry const* spellInfo, bool useCharges)
180{
181    for (int i=0;i<3;i++)
182    {
183        switch(spellInfo->EffectApplyAuraName[i])
184        {
185            case SPELL_AURA_PERIODIC_DAMAGE:
186            case SPELL_AURA_PERIODIC_LEECH:
187                return true;
188            default:
189                continue;
190        }
191    }
192    return Creature::IsImmunedToSpell(spellInfo, useCharges);
193}
Note: See TracBrowser for help on using the browser.