root/trunk/src/game/Pet.cpp @ 250

Revision 250, 62.0 kB (checked in by yumileroy, 17 years ago)

*Update aura stacking check. By QAston.
*Update pet autocast check. By qubix.

Original author: megamage
Date: 2008-11-17 17:59:33-06: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 "Common.h"
22#include "Database/DatabaseEnv.h"
23#include "Log.h"
24#include "WorldSession.h"
25#include "WorldPacket.h"
26#include "ObjectMgr.h"
27#include "SpellMgr.h"
28#include "Pet.h"
29#include "MapManager.h"
30#include "Formulas.h"
31#include "SpellAuras.h"
32#include "CreatureAI.h"
33#include "Unit.h"
34#include "Util.h"
35
36char const* petTypeSuffix[MAX_PET_TYPE] =
37{
38    "'s Minion",                                            // SUMMON_PET
39    "'s Pet",                                               // HUNTER_PET
40    "'s Guardian",                                          // GUARDIAN_PET
41    "'s Companion"                                          // MINI_PET
42};
43
44//numbers represent minutes * 100 while happy (you get 100 loyalty points per min while happy)
45uint32 const LevelUpLoyalty[6] =
46{
47    5500,
48    11500,
49    17000,
50    23500,
51    31000,
52    39500,
53};
54
55uint32 const LevelStartLoyalty[6] =
56{
57    2000,
58    4500,
59    7000,
60    10000,
61    13500,
62    17500,
63};
64
65Pet::Pet(PetType type) : Creature()
66{
67    m_isPet = true;
68    m_name = "Pet";
69    m_petType = type;
70
71    m_removed = false;
72    m_regenTimer = 4000;
73    m_happinessTimer = 7500;
74    m_loyaltyTimer = 12000;
75    m_duration = 0;
76    m_bonusdamage = 0;
77
78    m_loyaltyPoints = 0;
79    m_TrainingPoints = 0;
80    m_resetTalentsCost = 0;
81    m_resetTalentsTime = 0;
82
83    m_auraUpdateMask = 0;
84
85    // pets always have a charminfo, even if they are not actually charmed
86    CharmInfo* charmInfo = InitCharmInfo(this);
87
88    if(type == MINI_PET)                                    // always passive
89        charmInfo->SetReactState(REACT_PASSIVE);
90    else if(type == GUARDIAN_PET)                           // always aggressive
91        charmInfo->SetReactState(REACT_AGGRESSIVE);
92
93    m_spells.clear();
94    m_Auras.clear();
95    m_CreatureSpellCooldowns.clear();
96    m_CreatureCategoryCooldowns.clear();
97    m_autospells.clear();
98    m_declinedname = NULL;
99    m_isActive = true;
100}
101
102Pet::~Pet()
103{
104    if(m_uint32Values)                                      // only for fully created Object
105    {
106        for (PetSpellMap::iterator i = m_spells.begin(); i != m_spells.end(); ++i)
107            delete i->second;
108        ObjectAccessor::Instance().RemoveObject(this);
109    }
110
111    delete m_declinedname;
112}
113
114void Pet::AddToWorld()
115{
116    ///- Register the pet for guid lookup
117    if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
118    Unit::AddToWorld();
119}
120
121void Pet::RemoveFromWorld()
122{
123    ///- Remove the pet from the accessor
124    if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
125    ///- Don't call the function for Creature, normal mobs + totems go in a different storage
126    Unit::RemoveFromWorld();
127}
128
129bool Pet::LoadPetFromDB( Unit* owner, uint32 petentry, uint32 petnumber, bool current )
130{
131    uint32 ownerid = owner->GetGUIDLow();
132
133    QueryResult *result;
134
135    if(petnumber)
136        // known petnumber entry                  0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
137        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND id = '%u'",ownerid, petnumber);
138    else if(current)
139        // current pet (slot 0)                   0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
140        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND slot = '0'",ownerid );
141    else if(petentry)
142        // known petentry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
143        //                                        0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
144        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND entry = '%u' AND (slot = '0' OR slot = '3') ",ownerid, petentry );
145    else
146        // any current or other non-stabled pet (for hunter "call pet")
147        //                                        0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
148        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND (slot = '0' OR slot = '3') ",ownerid);
149
150    if(!result)
151        return false;
152
153    Field *fields = result->Fetch();
154
155    // update for case of current pet "slot = 0"
156    petentry = fields[1].GetUInt32();
157    if(!petentry)
158    {
159        delete result;
160        return false;
161    }
162
163    uint32 summon_spell_id = fields[21].GetUInt32();
164    SpellEntry const* spellInfo = sSpellStore.LookupEntry(summon_spell_id);
165
166    bool is_temporary_summoned = spellInfo && GetSpellDuration(spellInfo) > 0;
167
168    // check temporary summoned pets like mage water elemental
169    if(current && is_temporary_summoned)
170    {
171        delete result;
172        return false;
173    }
174
175    Map *map = owner->GetMap();
176    uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
177    uint32 pet_number = fields[0].GetUInt32();
178    if(!Create(guid, map, petentry, pet_number))
179    {
180        delete result;
181        return false;
182    }
183
184    float px, py, pz;
185    owner->GetClosePoint(px, py, pz,GetObjectSize(),PET_FOLLOW_DIST,PET_FOLLOW_ANGLE);
186
187    Relocate(px, py, pz, owner->GetOrientation());
188
189    if(!IsPositionValid())
190    {
191        sLog.outError("ERROR: Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
192            GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
193        delete result;
194        return false;
195    }
196
197    setPetType(PetType(fields[22].GetUInt8()));
198    SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE,owner->getFaction());
199    SetUInt32Value(UNIT_CREATED_BY_SPELL, summon_spell_id);
200
201    CreatureInfo const *cinfo = GetCreatureInfo();
202    if(cinfo->type == CREATURE_TYPE_CRITTER)
203    {
204        AIM_Initialize();
205        map->Add((Creature*)this);
206        delete result;
207        return true;
208    }
209    if(getPetType()==HUNTER_PET || (getPetType()==SUMMON_PET && cinfo->type == CREATURE_TYPE_DEMON && owner->getClass() == CLASS_WARLOCK))
210        m_charmInfo->SetPetNumber(pet_number, true);
211    else
212        m_charmInfo->SetPetNumber(pet_number, false);
213    SetUInt64Value(UNIT_FIELD_SUMMONEDBY, owner->GetGUID());
214    SetDisplayId(fields[3].GetUInt32());
215    SetNativeDisplayId(fields[3].GetUInt32());
216    uint32 petlevel=fields[4].GetUInt32();
217    SetUInt32Value(UNIT_NPC_FLAGS , 0);
218    SetName(fields[11].GetString());
219
220    switch(getPetType())
221    {
222
223        case SUMMON_PET:
224            petlevel=owner->getLevel();
225
226            SetUInt32Value(UNIT_FIELD_BYTES_0,2048);
227            SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
228                                                            // this enables popup window (pet dismiss, cancel)
229            break;
230        case HUNTER_PET:
231            SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
232            SetByteValue(UNIT_FIELD_BYTES_1, 1, fields[8].GetUInt32());
233            SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
234            SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_UNK3 | UNIT_BYTE2_FLAG_AURAS | UNIT_BYTE2_FLAG_UNK5 );
235
236            if(fields[12].GetBool())
237                SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_NOT_ALLOWED);
238            else
239                SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
240
241            SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
242                                                            // this enables popup window (pet abandon, cancel)
243            SetTP(fields[9].GetInt32());
244            SetMaxPower(POWER_HAPPINESS,GetCreatePowers(POWER_HAPPINESS));
245            SetPower(   POWER_HAPPINESS,fields[15].GetUInt32());
246            setPowerType(POWER_FOCUS);
247            break;
248        default:
249            sLog.outError("Pet have incorrect type (%u) for pet loading.",getPetType());
250    }
251    InitStatsForLevel( petlevel);
252    SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
253    SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[5].GetUInt32());
254    SetUInt64Value(UNIT_FIELD_CREATEDBY, owner->GetGUID());
255
256    m_charmInfo->SetReactState( ReactStates( fields[6].GetUInt8() ));
257    m_loyaltyPoints = fields[7].GetInt32();
258
259    uint32 savedhealth = fields[13].GetUInt32();
260    uint32 savedmana = fields[14].GetUInt32();
261
262    // set current pet as current
263    if(fields[10].GetUInt32() != 0)
264    {
265        CharacterDatabase.BeginTransaction();
266        CharacterDatabase.PExecute("UPDATE character_pet SET slot = '3' WHERE owner = '%u' AND slot = '0' AND id <> '%u'",ownerid, m_charmInfo->GetPetNumber());
267        CharacterDatabase.PExecute("UPDATE character_pet SET slot = '0' WHERE owner = '%u' AND id = '%u'",ownerid, m_charmInfo->GetPetNumber());
268        CharacterDatabase.CommitTransaction();
269    }
270
271    if(!is_temporary_summoned)
272    {
273        // permanent controlled pets store state in DB
274        Tokens tokens = StrSplit(fields[16].GetString(), " ");
275
276        if(tokens.size() != 20)
277        {
278            delete result;
279            return false;
280        }
281
282        int index;
283        Tokens::iterator iter;
284        for(iter = tokens.begin(), index = 0; index < 10; ++iter, ++index )
285        {
286            m_charmInfo->GetActionBarEntry(index)->Type = atol((*iter).c_str());
287            ++iter;
288            m_charmInfo->GetActionBarEntry(index)->SpellOrAction = atol((*iter).c_str());
289            // patch for old data where some spells have ACT_DECIDE but should have ACT_CAST
290            // so overwrite old state
291            SpellEntry const *spellInfo = sSpellStore.LookupEntry(m_charmInfo->GetActionBarEntry(index)->SpellOrAction);
292            if (spellInfo && spellInfo->AttributesEx & SPELL_ATTR_EX_PET_NOT_AUTOCAST)
293                m_charmInfo->GetActionBarEntry(index)->Type = ACT_CAST;
294        }
295
296        //init teach spells
297        tokens = StrSplit(fields[17].GetString(), " ");
298        for (iter = tokens.begin(), index = 0; index < 4; ++iter, ++index)
299        {
300            uint32 tmp = atol((*iter).c_str());
301
302            ++iter;
303
304            if(tmp)
305                AddTeachSpell(tmp, atol((*iter).c_str()));
306            else
307                break;
308        }
309    }
310
311    // since last save (in seconds)
312    uint32 timediff = (time(NULL) - fields[18].GetUInt32());
313
314    delete result;
315
316    //load spells/cooldowns/auras
317    SetCanModifyStats(true);
318    _LoadAuras(timediff);
319
320    //init AB
321    if(is_temporary_summoned)
322    {
323        // Temporary summoned pets always have initial spell list at load
324        InitPetCreateSpells();
325    }
326    else
327    {
328        LearnPetPassives();
329        CastPetAuras(current);
330    }
331
332    if(getPetType() == SUMMON_PET && !current)              //all (?) summon pets come with full health when called, but not when they are current
333    {
334        SetHealth(GetMaxHealth());
335        SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
336    }
337    else
338    {
339        SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
340        SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
341    }
342
343    AIM_Initialize();
344    map->Add((Creature*)this);
345
346    // Spells should be loaded after pet is added to map, because in CanCast is check on it
347    _LoadSpells();
348    _LoadSpellCooldowns();
349
350    owner->SetPet(this);                                    // in DB stored only full controlled creature
351    sLog.outDebug("New Pet has guid %u", GetGUIDLow());
352
353    if(owner->GetTypeId() == TYPEID_PLAYER)
354    {
355        ((Player*)owner)->PetSpellInitialize();
356        if(((Player*)owner)->GetGroup())
357            ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_PET);
358    }
359
360    if(owner->GetTypeId() == TYPEID_PLAYER && getPetType() == HUNTER_PET)
361    {
362        result = CharacterDatabase.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'",owner->GetGUIDLow(),GetCharmInfo()->GetPetNumber());
363
364        if(result)
365        {
366            if(m_declinedname)
367                delete m_declinedname;
368
369            m_declinedname = new DeclinedName;
370            Field *fields = result->Fetch();
371            for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
372            {
373                m_declinedname->name[i] = fields[i].GetCppString();
374            }
375        }
376    }
377
378    return true;
379}
380
381void Pet::SavePetToDB(PetSaveMode mode)
382{
383    if(!GetEntry())
384        return;
385
386    // save only fully controlled creature
387    if(!isControlled())
388        return;
389
390    uint32 curhealth = GetHealth();
391    uint32 curmana = GetPower(POWER_MANA);
392
393    switch(mode)
394    {
395        case PET_SAVE_IN_STABLE_SLOT_1:
396        case PET_SAVE_IN_STABLE_SLOT_2:
397        case PET_SAVE_NOT_IN_SLOT:
398        {
399            RemoveAllAuras();
400
401            //only alive hunter pets get auras saved, the others don't
402            if(!(getPetType() == HUNTER_PET && isAlive()))
403                m_Auras.clear();
404        }
405        default:
406            break;
407    }
408
409    _SaveSpells();
410    _SaveSpellCooldowns();
411    _SaveAuras();
412
413    switch(mode)
414    {
415        case PET_SAVE_AS_CURRENT:
416        case PET_SAVE_IN_STABLE_SLOT_1:
417        case PET_SAVE_IN_STABLE_SLOT_2:
418        case PET_SAVE_NOT_IN_SLOT:
419        {
420            uint32 loyalty =1;
421            if(getPetType()!=HUNTER_PET)
422                loyalty = GetLoyaltyLevel();
423
424            uint32 owner = GUID_LOPART(GetOwnerGUID());
425            std::string name = m_name;
426            CharacterDatabase.escape_string(name);
427            CharacterDatabase.BeginTransaction();
428            // remove current data
429            CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner,m_charmInfo->GetPetNumber() );
430
431            // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
432            if(mode!=PET_SAVE_NOT_IN_SLOT)
433                CharacterDatabase.PExecute("UPDATE character_pet SET slot = 3 WHERE owner = '%u' AND slot = '%u'", owner, uint32(mode) );
434
435            // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
436            if(getPetType()==HUNTER_PET && (mode==PET_SAVE_AS_CURRENT||mode==PET_SAVE_NOT_IN_SLOT))
437                CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '0' OR slot = '3')", owner );
438            // save pet
439            std::ostringstream ss;
440            ss  << "INSERT INTO character_pet ( id, entry,  owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata,TeachSpelldata,savetime,resettalents_cost,resettalents_time,CreatedBySpell,PetType) "
441                << "VALUES ("
442                << m_charmInfo->GetPetNumber() << ", "
443                << GetEntry() << ", "
444                << owner << ", "
445                << GetNativeDisplayId() << ", "
446                << getLevel() << ", "
447                << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE) << ", "
448                << uint32(m_charmInfo->GetReactState()) << ", "
449                << m_loyaltyPoints << ", "
450                << GetLoyaltyLevel() << ", "
451                << m_TrainingPoints << ", "
452                << uint32(mode) << ", '"
453                << name.c_str() << "', "
454                << uint32((GetByteValue(UNIT_FIELD_BYTES_2, 2) == UNIT_RENAME_ALLOWED)?0:1) << ", "
455                << (curhealth<1?1:curhealth) << ", "
456                << curmana << ", "
457                << GetPower(POWER_HAPPINESS) << ", '";
458
459            for(uint32 i = 0; i < 10; i++)
460                ss << uint32(m_charmInfo->GetActionBarEntry(i)->Type) << " " << uint32(m_charmInfo->GetActionBarEntry(i)->SpellOrAction) << " ";
461            ss << "', '";
462
463            //save spells the pet can teach to it's Master
464            {
465                int i = 0;
466                for(TeachSpellMap::iterator itr = m_teachspells.begin(); i < 4 && itr != m_teachspells.end(); ++i, ++itr)
467                    ss << itr->first << " " << itr->second << " ";
468                for(; i < 4; ++i)
469                    ss << uint32(0) << " " << uint32(0) << " ";
470            }
471
472            ss  << "', "
473                << time(NULL) << ", "
474                << uint32(m_resetTalentsCost) << ", "
475                << uint64(m_resetTalentsTime) << ", "
476                << GetUInt32Value(UNIT_CREATED_BY_SPELL) << ", "
477                << uint32(getPetType()) << ")";
478
479            CharacterDatabase.Execute( ss.str().c_str() );
480
481            CharacterDatabase.CommitTransaction();
482            break;
483        }
484        case PET_SAVE_AS_DELETED:
485        {
486            RemoveAllAuras();
487            uint32 owner = GUID_LOPART(GetOwnerGUID());
488            DeleteFromDB(m_charmInfo->GetPetNumber());
489            break;
490        }
491        default:
492            sLog.outError("Unknown pet save/remove mode: %d",mode);
493    }
494}
495
496void Pet::DeleteFromDB(uint32 guidlow)
497{
498    CharacterDatabase.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow);
499    CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow);
500    CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow);
501    CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow);
502    CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow);
503}
504
505void Pet::setDeathState(DeathState s)                       // overwrite virtual Creature::setDeathState and Unit::setDeathState
506{
507    Creature::setDeathState(s);
508    if(getDeathState()==CORPSE)
509    {
510        //remove summoned pet (no corpse)
511        if(getPetType()==SUMMON_PET)
512            Remove(PET_SAVE_NOT_IN_SLOT);
513        // other will despawn at corpse desppawning (Pet::Update code)
514        else
515        {
516            // pet corpse non lootable and non skinnable
517            SetUInt32Value( UNIT_DYNAMIC_FLAGS, 0x00 );
518            RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
519
520             //lose happiness when died and not in BG/Arena
521            MapEntry const* mapEntry = sMapStore.LookupEntry(GetMapId());
522            if(!mapEntry || (mapEntry->map_type != MAP_ARENA && mapEntry->map_type != MAP_BATTLEGROUND))
523                ModifyPower(POWER_HAPPINESS, -HAPPINESS_LEVEL_SIZE);
524
525            SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_DISABLE_ROTATE);
526        }
527    }
528    else if(getDeathState()==ALIVE)
529    {
530        RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_DISABLE_ROTATE);
531        CastPetAuras(true);
532    }
533}
534
535void Pet::Update(uint32 diff)
536{
537    if(m_removed)                                           // pet already removed, just wait in remove queue, no updates
538        return;
539
540    switch( m_deathState )
541    {
542        case CORPSE:
543        {
544            if( m_deathTimer <= diff )
545            {
546                assert(getPetType()!=SUMMON_PET && "Must be already removed.");
547                Remove(PET_SAVE_NOT_IN_SLOT);               //hunters' pets never get removed because of death, NEVER!
548                return;
549            }
550            break;
551        }
552        case ALIVE:
553        {
554            // unsummon pet that lost owner
555            Unit* owner = GetOwner();
556            if(!owner || (!IsWithinDistInMap(owner, OWNER_MAX_DISTANCE) && !isPossessed()) || isControlled() && !owner->GetPetGUID())
557            {
558                Remove(PET_SAVE_NOT_IN_SLOT, true);
559                return;
560            }
561
562            if(isControlled())
563            {
564                if( owner->GetPetGUID() != GetGUID() )
565                {
566                    Remove(getPetType()==HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
567                    return;
568                }
569            }
570
571            if(m_duration > 0)
572            {
573                if(m_duration > diff)
574                    m_duration -= diff;
575                else
576                {
577                    Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
578                    return;
579                }
580            }
581
582            if(getPetType() != HUNTER_PET)
583                break;
584
585            //regenerate Focus
586            if(m_regenTimer <= diff)
587            {
588                RegenerateFocus();
589                m_regenTimer = 4000;
590            }
591            else
592                m_regenTimer -= diff;
593
594            if(m_happinessTimer <= diff)
595            {
596                LooseHappiness();
597                m_happinessTimer = 7500;
598            }
599            else
600                m_happinessTimer -= diff;
601
602            if(m_loyaltyTimer <= diff)
603            {
604                TickLoyaltyChange();
605                m_loyaltyTimer = 12000;
606            }
607            else
608                m_loyaltyTimer -= diff;
609
610            break;
611        }
612        default:
613            break;
614    }
615    Creature::Update(diff);
616}
617
618void Pet::RegenerateFocus()
619{
620    uint32 curValue = GetPower(POWER_FOCUS);
621    uint32 maxValue = GetMaxPower(POWER_FOCUS);
622
623    if (curValue >= maxValue)
624        return;
625
626    float addvalue = 24 * sWorld.getRate(RATE_POWER_FOCUS);
627
628    AuraList const& ModPowerRegenPCTAuras = GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
629    for(AuraList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
630        if ((*i)->GetModifier()->m_miscvalue == POWER_FOCUS)
631            addvalue *= ((*i)->GetModifier()->m_amount + 100) / 100.0f;
632
633    ModifyPower(POWER_FOCUS, (int32)addvalue);
634}
635
636void Pet::LooseHappiness()
637{
638    uint32 curValue = GetPower(POWER_HAPPINESS);
639    if (curValue <= 0)
640        return;
641    int32 addvalue = (140 >> GetLoyaltyLevel()) * 125;      //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
642    if(isInCombat())                                        //we know in combat happiness fades faster, multiplier guess
643        addvalue = int32(addvalue * 1.5);
644    ModifyPower(POWER_HAPPINESS, -addvalue);
645}
646
647void Pet::ModifyLoyalty(int32 addvalue)
648{
649    uint32 loyaltylevel = GetLoyaltyLevel();
650
651    if(addvalue > 0)                                        //only gain influenced, not loss
652        addvalue = int32((float)addvalue * sWorld.getRate(RATE_LOYALTY));
653
654    if(loyaltylevel >= BEST_FRIEND && (addvalue + m_loyaltyPoints) > int32(GetMaxLoyaltyPoints(loyaltylevel)))
655        return;
656
657    m_loyaltyPoints += addvalue;
658
659    if(m_loyaltyPoints < 0)
660    {
661        if(loyaltylevel > REBELLIOUS)
662        {
663            //level down
664            --loyaltylevel;
665            SetLoyaltyLevel(LoyaltyLevel(loyaltylevel));
666            m_loyaltyPoints = GetStartLoyaltyPoints(loyaltylevel);
667            SetTP(m_TrainingPoints - int32(getLevel()));
668        }
669        else
670        {
671            m_loyaltyPoints = 0;
672            Unit* owner = GetOwner();
673            if(owner && owner->GetTypeId() == TYPEID_PLAYER)
674            {
675                WorldPacket data(SMSG_PET_BROKEN, 0);
676                ((Player*)owner)->GetSession()->SendPacket(&data);
677
678                //run away
679                ((Player*)owner)->RemovePet(this,PET_SAVE_AS_DELETED);
680            }
681        }
682    }
683    //level up
684    else if(m_loyaltyPoints > int32(GetMaxLoyaltyPoints(loyaltylevel)))
685    {
686        ++loyaltylevel;
687        SetLoyaltyLevel(LoyaltyLevel(loyaltylevel));
688        m_loyaltyPoints = GetStartLoyaltyPoints(loyaltylevel);
689        SetTP(m_TrainingPoints + getLevel());
690    }
691}
692
693void Pet::TickLoyaltyChange()
694{
695    int32 addvalue;
696
697    switch(GetHappinessState())
698    {
699        case HAPPY:   addvalue =  20; break;
700        case CONTENT: addvalue =  10; break;
701        case UNHAPPY: addvalue = -20; break;
702        default:
703            return;
704    }
705    ModifyLoyalty(addvalue);
706}
707
708void Pet::KillLoyaltyBonus(uint32 level)
709{
710    if(level > 100)
711        return;
712
713                                                            //at lower levels gain is faster | the lower loyalty the more loyalty is gained
714    uint32 bonus = uint32(((100 - level) / 10) + (6 - GetLoyaltyLevel()));
715    ModifyLoyalty(bonus);
716}
717
718HappinessState Pet::GetHappinessState()
719{
720    if(GetPower(POWER_HAPPINESS) < HAPPINESS_LEVEL_SIZE)
721        return UNHAPPY;
722    else if(GetPower(POWER_HAPPINESS) >= HAPPINESS_LEVEL_SIZE * 2)
723        return HAPPY;
724    else
725        return CONTENT;
726}
727
728void Pet::SetLoyaltyLevel(LoyaltyLevel level)
729{
730    SetByteValue(UNIT_FIELD_BYTES_1, 1, level);
731}
732
733bool Pet::CanTakeMoreActiveSpells(uint32 spellid)
734{
735    uint8  activecount = 1;
736    uint32 chainstartstore[ACTIVE_SPELLS_MAX];
737
738    if(IsPassiveSpell(spellid))
739        return true;
740
741    chainstartstore[0] = spellmgr.GetFirstSpellInChain(spellid);
742
743    for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
744    {
745        if(IsPassiveSpell(itr->first))
746            continue;
747
748        uint32 chainstart = spellmgr.GetFirstSpellInChain(itr->first);
749
750        uint8 x;
751
752        for(x = 0; x < activecount; x++)
753        {
754            if(chainstart == chainstartstore[x])
755                break;
756        }
757
758        if(x == activecount)                                //spellchain not yet saved -> add active count
759        {
760            ++activecount;
761            if(activecount > ACTIVE_SPELLS_MAX)
762                return false;
763            chainstartstore[x] = chainstart;
764        }
765    }
766    return true;
767}
768
769bool Pet::HasTPForSpell(uint32 spellid)
770{
771    int32 neededtrainp = GetTPForSpell(spellid);
772    if((m_TrainingPoints - neededtrainp < 0 || neededtrainp < 0) && neededtrainp != 0)
773        return false;
774    return true;
775}
776
777int32 Pet::GetTPForSpell(uint32 spellid)
778{
779    uint32 basetrainp = 0;
780
781    SkillLineAbilityMap::const_iterator lower = spellmgr.GetBeginSkillLineAbilityMap(spellid);
782    SkillLineAbilityMap::const_iterator upper = spellmgr.GetEndSkillLineAbilityMap(spellid);
783    for(SkillLineAbilityMap::const_iterator _spell_idx = lower; _spell_idx != upper; ++_spell_idx)
784    {
785        if(!_spell_idx->second->reqtrainpoints)
786            return 0;
787
788        basetrainp = _spell_idx->second->reqtrainpoints;
789        break;
790    }
791
792    uint32 spenttrainp = 0;
793    uint32 chainstart = spellmgr.GetFirstSpellInChain(spellid);
794
795    for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
796    {
797        if(itr->second->state == PETSPELL_REMOVED)
798            continue;
799
800        if(spellmgr.GetFirstSpellInChain(itr->first) == chainstart)
801        {
802            SkillLineAbilityMap::const_iterator _lower = spellmgr.GetBeginSkillLineAbilityMap(itr->first);
803            SkillLineAbilityMap::const_iterator _upper = spellmgr.GetEndSkillLineAbilityMap(itr->first);
804
805            for(SkillLineAbilityMap::const_iterator _spell_idx2 = _lower; _spell_idx2 != _upper; ++_spell_idx2)
806            {
807                if(_spell_idx2->second->reqtrainpoints > spenttrainp)
808                {
809                    spenttrainp = _spell_idx2->second->reqtrainpoints;
810                    break;
811                }
812            }
813        }
814    }
815
816    return int32(basetrainp) - int32(spenttrainp);
817}
818
819uint32 Pet::GetMaxLoyaltyPoints(uint32 level)
820{
821    return LevelUpLoyalty[level - 1];
822}
823
824uint32 Pet::GetStartLoyaltyPoints(uint32 level)
825{
826    return LevelStartLoyalty[level - 1];
827}
828
829void Pet::SetTP(int32 TP)
830{
831    m_TrainingPoints = TP;
832    SetUInt32Value(UNIT_TRAINING_POINTS, (uint32)GetDispTP());
833}
834
835int32 Pet::GetDispTP()
836{
837    if(getPetType()!= HUNTER_PET)
838        return(0);
839    if(m_TrainingPoints < 0)
840        return -m_TrainingPoints;
841    else
842        return -(m_TrainingPoints + 1);
843}
844
845void Pet::Remove(PetSaveMode mode, bool returnreagent)
846{
847    Unit* owner = GetOwner();
848
849    if(owner)
850    {
851        if(owner->GetTypeId()==TYPEID_PLAYER)
852        {
853            ((Player*)owner)->RemovePet(this,mode,returnreagent);
854            return;
855        }
856
857        // only if current pet in slot
858        if(owner->GetPetGUID()==GetGUID())
859            owner->SetPet(0);
860    }
861
862    CleanupsBeforeDelete();
863    AddObjectToRemoveList();
864    m_removed = true;
865}
866
867void Pet::GivePetXP(uint32 xp)
868{
869    if(getPetType() != HUNTER_PET)
870        return;
871
872    if ( xp < 1 )
873        return;
874
875    if(!isAlive())
876        return;
877
878    uint32 level = getLevel();
879
880    // XP to money conversion processed in Player::RewardQuest
881    if(level >= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
882        return;
883
884    uint32 curXP = GetUInt32Value(UNIT_FIELD_PETEXPERIENCE);
885    uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
886    uint32 newXP = curXP + xp;
887
888    if(newXP >= nextLvlXP && level+1 > GetOwner()->getLevel())
889    {
890        SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, nextLvlXP-1);
891        return;
892    }
893
894    while( newXP >= nextLvlXP && level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) )
895    {
896        newXP -= nextLvlXP;
897
898        SetLevel( level + 1 );
899        SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, uint32((Trinity::XP::xp_to_level(level+1))/4));
900
901        level = getLevel();
902        nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
903        GivePetLevel(level);
904    }
905
906    SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, newXP);
907
908    if(getPetType() == HUNTER_PET)
909        KillLoyaltyBonus(level);
910}
911
912void Pet::GivePetLevel(uint32 level)
913{
914    if(!level)
915        return;
916
917    InitStatsForLevel( level);
918
919    SetTP(m_TrainingPoints + (GetLoyaltyLevel() - 1));
920}
921
922bool Pet::CreateBaseAtCreature(Creature* creature)
923{
924    if(!creature)
925    {
926        sLog.outError("CRITICAL ERROR: NULL pointer parsed into CreateBaseAtCreature()");
927        return false;
928    }
929    uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
930
931    sLog.outBasic("SetInstanceID()");
932    SetInstanceId(creature->GetInstanceId());
933
934    sLog.outBasic("Create pet");
935    uint32 pet_number = objmgr.GeneratePetNumber();
936    if(!Create(guid, creature->GetMap(), creature->GetEntry(), pet_number))
937        return false;
938
939    Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
940
941    if(!IsPositionValid())
942    {
943        sLog.outError("ERROR: Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
944            GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
945        return false;
946    }
947
948    CreatureInfo const *cinfo = GetCreatureInfo();
949    if(!cinfo)
950    {
951        sLog.outError("ERROR: CreateBaseAtCreature() failed, creatureInfo is missing!");
952        return false;
953    }
954
955    if(cinfo->type == CREATURE_TYPE_CRITTER)
956    {
957        setPetType(MINI_PET);
958        return true;
959    }
960    SetDisplayId(creature->GetDisplayId());
961    SetNativeDisplayId(creature->GetNativeDisplayId());
962    SetMaxPower(POWER_HAPPINESS,GetCreatePowers(POWER_HAPPINESS));
963    SetPower(   POWER_HAPPINESS,166500);
964    setPowerType(POWER_FOCUS);
965    SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP,0);
966    SetUInt32Value(UNIT_FIELD_PETEXPERIENCE,0);
967    SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, uint32((Trinity::XP::xp_to_level(creature->getLevel()))/4));
968    SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
969    SetUInt32Value(UNIT_NPC_FLAGS , 0);
970
971    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(creature->GetCreatureInfo()->family);
972    if( char* familyname = cFamily->Name[sWorld.GetDefaultDbcLocale()] )
973        SetName(familyname);
974    else
975        SetName(creature->GetName());
976
977    m_loyaltyPoints = 1000;
978    if(cinfo->type == CREATURE_TYPE_BEAST)
979    {
980        SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
981        SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
982        SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_UNK3 | UNIT_BYTE2_FLAG_AURAS | UNIT_BYTE2_FLAG_UNK5 );
983        SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
984
985        SetUInt32Value(UNIT_MOD_CAST_SPEED, creature->GetUInt32Value(UNIT_MOD_CAST_SPEED) );
986        SetLoyaltyLevel(REBELLIOUS);
987    }
988    return true;
989}
990
991bool Pet::InitStatsForLevel(uint32 petlevel)
992{
993    CreatureInfo const *cinfo = GetCreatureInfo();
994    assert(cinfo);
995
996    Unit* owner = GetOwner();
997    if(!owner)
998    {
999        sLog.outError("ERROR: attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo->Entry);
1000        return false;
1001    }
1002
1003    uint32 creature_ID = (getPetType() == HUNTER_PET) ? 1 : cinfo->Entry;
1004
1005    SetLevel( petlevel);
1006
1007    SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
1008
1009    SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
1010
1011    SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
1012    SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
1013    SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
1014
1015    SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0);
1016
1017    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
1018    if(cFamily && cFamily->minScale > 0.0f)
1019    {
1020        float scale;
1021        if (getLevel() >= cFamily->maxScaleLevel)
1022            scale = cFamily->maxScale;
1023        else if (getLevel() <= cFamily->minScaleLevel)
1024            scale = cFamily->minScale;
1025        else
1026            scale = cFamily->minScale + (getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
1027
1028        SetFloatValue(OBJECT_FIELD_SCALE_X, scale);
1029    }
1030    m_bonusdamage = 0;
1031
1032    int32 createResistance[MAX_SPELL_SCHOOL] = {0,0,0,0,0,0,0};
1033
1034    if(cinfo && getPetType() != HUNTER_PET)
1035    {
1036        createResistance[SPELL_SCHOOL_HOLY]   = cinfo->resistance1;
1037        createResistance[SPELL_SCHOOL_FIRE]   = cinfo->resistance2;
1038        createResistance[SPELL_SCHOOL_NATURE] = cinfo->resistance3;
1039        createResistance[SPELL_SCHOOL_FROST]  = cinfo->resistance4;
1040        createResistance[SPELL_SCHOOL_SHADOW] = cinfo->resistance5;
1041        createResistance[SPELL_SCHOOL_ARCANE] = cinfo->resistance6;
1042    }
1043
1044    switch(getPetType())
1045    {
1046        case SUMMON_PET:
1047        {
1048            if(owner->GetTypeId() == TYPEID_PLAYER)
1049            {
1050                switch(owner->getClass())
1051                {
1052                    case CLASS_WARLOCK:
1053                    {
1054
1055                        //the damage bonus used for pets is either fire or shadow damage, whatever is higher
1056                        uint32 fire  = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
1057                        uint32 shadow = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
1058                        uint32 val  = (fire > shadow) ? fire : shadow;
1059
1060                        SetBonusDamage(int32 (val * 0.15f));
1061                        //bonusAP += val * 0.57;
1062                        break;
1063                    }
1064                    case CLASS_MAGE:
1065                    {
1066                                                            //40% damage bonus of mage's frost damage
1067                        float val = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FROST) * 0.4;
1068                        if(val < 0)
1069                            val = 0;
1070                        SetBonusDamage( int32(val));
1071                        break;
1072                    }
1073                    default:
1074                        break;
1075                }
1076            }
1077
1078            SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
1079            SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
1080
1081            //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
1082
1083            PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
1084            if(pInfo)                                       // exist in DB
1085            {
1086                SetCreateHealth(pInfo->health);
1087                SetCreateMana(pInfo->mana);
1088
1089                if(pInfo->armor > 0)
1090                    SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
1091
1092                for(int stat = 0; stat < MAX_STATS; ++stat)
1093                {
1094                    SetCreateStat(Stats(stat),float(pInfo->stats[stat]));
1095                }
1096            }
1097            else                                            // not exist in DB, use some default fake data
1098            {
1099                sLog.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo->Entry);
1100
1101                // remove elite bonuses included in DB values
1102                SetCreateHealth(uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
1103                SetCreateMana(  uint32(((float(cinfo->maxmana)   / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
1104
1105                SetCreateStat(STAT_STRENGTH,22);
1106                SetCreateStat(STAT_AGILITY,22);
1107                SetCreateStat(STAT_STAMINA,25);
1108                SetCreateStat(STAT_INTELLECT,28);
1109                SetCreateStat(STAT_SPIRIT,27);
1110            }
1111            break;
1112        }
1113        case HUNTER_PET:
1114        {
1115            SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, uint32((Trinity::XP::xp_to_level(petlevel))/4));
1116
1117            //these formula may not be correct; however, it is designed to be close to what it should be
1118            //this makes dps 0.5 of pets level
1119            SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
1120            //damage range is then petlevel / 2
1121            SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
1122            //damage is increased afterwards as strength and pet scaling modify attack power
1123
1124            //stored standard pet stats are entry 1 in pet_levelinfo
1125            PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
1126            if(pInfo)                                       // exist in DB
1127            {
1128                SetCreateHealth(pInfo->health);
1129                SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
1130                //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
1131
1132                for( int i = STAT_STRENGTH; i < MAX_STATS; i++)
1133                {
1134                    SetCreateStat(Stats(i),  float(pInfo->stats[i]));
1135                }
1136            }
1137            else                                            // not exist in DB, use some default fake data
1138            {
1139                sLog.outErrorDb("Hunter pet levelstats missing in DB");
1140
1141                // remove elite bonuses included in DB values
1142                SetCreateHealth( uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
1143
1144                SetCreateStat(STAT_STRENGTH,22);
1145                SetCreateStat(STAT_AGILITY,22);
1146                SetCreateStat(STAT_STAMINA,25);
1147                SetCreateStat(STAT_INTELLECT,28);
1148                SetCreateStat(STAT_SPIRIT,27);
1149            }
1150            break;
1151        }
1152        case GUARDIAN_PET:
1153            SetUInt32Value(UNIT_FIELD_PETEXPERIENCE,0);
1154            SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP,1000);
1155
1156            SetCreateMana(   28 + 10*petlevel );
1157            SetCreateHealth( 28 + 30*petlevel );
1158
1159            // FIXME: this is wrong formula, possible each guardian pet have own damage formula
1160            //these formula may not be correct; however, it is designed to be close to what it should be
1161            //this makes dps 0.5 of pets level
1162            SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
1163            //damage range is then petlevel / 2
1164            SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
1165            break;
1166        default:
1167            sLog.outError("Pet have incorrect type (%u) for levelup.",getPetType());            break;
1168    }
1169
1170    for (int i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
1171        SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(createResistance[i]) );
1172
1173    UpdateAllStats();
1174
1175    SetHealth(GetMaxHealth());
1176    SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
1177
1178    return true;
1179}
1180
1181bool Pet::HaveInDiet(ItemPrototype const* item) const
1182{
1183    if (!item->FoodType)
1184        return false;
1185
1186    CreatureInfo const* cInfo = GetCreatureInfo();
1187    if(!cInfo)
1188        return false;
1189
1190    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1191    if(!cFamily)
1192        return false;
1193
1194    uint32 diet = cFamily->petFoodMask;
1195    uint32 FoodMask = 1 << (item->FoodType-1);
1196    return diet & FoodMask;
1197}
1198
1199uint32 Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel)
1200{
1201    // -5 or greater food level
1202    if(getLevel() <= itemlevel +5)                          //possible to feed level 60 pet with level 55 level food for full effect
1203        return 35000;
1204    // -10..-6
1205    else if(getLevel() <= itemlevel + 10)                   //pure guess, but sounds good
1206        return 17000;
1207    // -14..-11
1208    else if(getLevel() <= itemlevel + 14)                   //level 55 food gets green on 70, makes sense to me
1209        return 8000;
1210    // -15 or less
1211    else
1212        return 0;                                           //food too low level
1213}
1214
1215void Pet::_LoadSpellCooldowns()
1216{
1217    m_CreatureSpellCooldowns.clear();
1218    m_CreatureCategoryCooldowns.clear();
1219
1220    QueryResult *result = CharacterDatabase.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1221
1222    if(result)
1223    {
1224        time_t curTime = time(NULL);
1225
1226        WorldPacket data(SMSG_SPELL_COOLDOWN, (8+1+result->GetRowCount()*8));
1227        data << GetGUID();
1228        data << uint8(0x0);                                 // flags (0x1, 0x2)
1229
1230        do
1231        {
1232            Field *fields = result->Fetch();
1233
1234            uint32 spell_id = fields[0].GetUInt32();
1235            time_t db_time  = (time_t)fields[1].GetUInt64();
1236
1237            if(!sSpellStore.LookupEntry(spell_id))
1238            {
1239                sLog.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo->GetPetNumber(),spell_id);
1240                continue;
1241            }
1242
1243            // skip outdated cooldown
1244            if(db_time <= curTime)
1245                continue;
1246
1247            data << uint32(spell_id);
1248            data << uint32(uint32(db_time-curTime)*1000);   // in m.secs
1249
1250            _AddCreatureSpellCooldown(spell_id,db_time);
1251
1252            sLog.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).",m_charmInfo->GetPetNumber(),spell_id,uint32(db_time-curTime));
1253        }
1254        while( result->NextRow() );
1255
1256        delete result;
1257
1258        if(!m_CreatureSpellCooldowns.empty() && GetOwner())
1259        {
1260            ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1261        }
1262    }
1263}
1264
1265void Pet::_SaveSpellCooldowns()
1266{
1267    CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1268
1269    time_t curTime = time(NULL);
1270
1271    // remove oudated and save active
1272    for(CreatureSpellCooldowns::iterator itr = m_CreatureSpellCooldowns.begin();itr != m_CreatureSpellCooldowns.end();)
1273    {
1274        if(itr->second <= curTime)
1275            m_CreatureSpellCooldowns.erase(itr++);
1276        else
1277        {
1278            CharacterDatabase.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" I64FMTD "')", m_charmInfo->GetPetNumber(), itr->first, uint64(itr->second));
1279            ++itr;
1280        }
1281    }
1282}
1283
1284void Pet::_LoadSpells()
1285{
1286    QueryResult *result = CharacterDatabase.PQuery("SELECT spell,slot,active FROM pet_spell WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1287
1288    if(result)
1289    {
1290        do
1291        {
1292            Field *fields = result->Fetch();
1293
1294            addSpell(fields[0].GetUInt16(), (ActiveStates)fields[2].GetUInt16(), PETSPELL_UNCHANGED, fields[1].GetUInt16());
1295        }
1296        while( result->NextRow() );
1297
1298        delete result;
1299    }
1300}
1301
1302void Pet::_SaveSpells()
1303{
1304    for (PetSpellMap::const_iterator itr = m_spells.begin(), next = m_spells.begin(); itr != m_spells.end(); itr = next)
1305    {
1306        ++next;
1307        if (itr->second->type == PETSPELL_FAMILY) continue; // prevent saving family passives to DB
1308        if (itr->second->state == PETSPELL_REMOVED || itr->second->state == PETSPELL_CHANGED)
1309            CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1310        if (itr->second->state == PETSPELL_NEW || itr->second->state == PETSPELL_CHANGED)
1311            CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,slot,active) VALUES ('%u', '%u', '%u','%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second->slotId,itr->second->active);
1312
1313        if (itr->second->state == PETSPELL_REMOVED)
1314            _removeSpell(itr->first);
1315        else
1316            itr->second->state = PETSPELL_UNCHANGED;
1317    }
1318}
1319
1320void Pet::_LoadAuras(uint32 timediff)
1321{
1322    m_Auras.clear();
1323    for (int i = 0; i < TOTAL_AURAS; i++)
1324        m_modAuras[i].clear();
1325
1326    // all aura related fields
1327    for(int i = UNIT_FIELD_AURA; i <= UNIT_FIELD_AURASTATE; ++i)
1328        SetUInt32Value(i, 0);
1329
1330    QueryResult *result = CharacterDatabase.PQuery("SELECT caster_guid,spell,effect_index,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1331
1332    if(result)
1333    {
1334        do
1335        {
1336            Field *fields = result->Fetch();
1337            uint64 caster_guid = fields[0].GetUInt64();
1338            uint32 spellid = fields[1].GetUInt32();
1339            uint32 effindex = fields[2].GetUInt32();
1340            int32 damage     = (int32)fields[3].GetUInt32();
1341            int32 maxduration = (int32)fields[4].GetUInt32();
1342            int32 remaintime = (int32)fields[5].GetUInt32();
1343            int32 remaincharges = (int32)fields[6].GetUInt32();
1344
1345            SpellEntry const* spellproto = sSpellStore.LookupEntry(spellid);
1346            if(!spellproto)
1347            {
1348                sLog.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid,effindex);
1349                continue;
1350            }
1351
1352            if(effindex >= 3)
1353            {
1354                sLog.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid,effindex);
1355                continue;
1356            }
1357
1358            // negative effects should continue counting down after logout
1359            if (remaintime != -1 && !IsPositiveEffect(spellid, effindex))
1360            {
1361                if(remaintime  <= int32(timediff))
1362                    continue;
1363
1364                remaintime -= timediff;
1365            }
1366
1367            // prevent wrong values of remaincharges
1368            if(spellproto->procCharges)
1369            {
1370                if(remaincharges <= 0 || remaincharges > spellproto->procCharges)
1371                    remaincharges = spellproto->procCharges;
1372            }
1373            else
1374                remaincharges = -1;
1375
1376            /// do not load single target auras (unless they were cast by the player)
1377            if (caster_guid != GetGUID() && IsSingleTargetSpell(spellproto))
1378                continue;
1379
1380            Aura* aura = CreateAura(spellproto, effindex, NULL, this, NULL);
1381
1382            if(!damage)
1383                damage = aura->GetModifier()->m_amount;
1384            aura->SetLoadedState(caster_guid,damage,maxduration,remaintime,remaincharges);
1385            AddAura(aura);
1386        }
1387        while( result->NextRow() );
1388
1389        delete result;
1390    }
1391}
1392
1393void Pet::_SaveAuras()
1394{
1395    CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1396
1397    AuraMap const& auras = GetAuras();
1398    for(AuraMap::const_iterator itr = auras.begin(); itr != auras.end(); ++itr)
1399    {
1400        // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1401        SpellEntry const *spellInfo = itr->second->GetSpellProto();
1402        uint8 i;
1403        for (i = 0; i < 3; i++)
1404            if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_STEALTH ||
1405                spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER ||
1406                spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PET )
1407                break;
1408
1409        if (i != 3)
1410            continue;
1411       
1412        if(itr->second->IsPassive())
1413            continue;
1414
1415        /// do not save single target auras (unless they were cast by the player)
1416        if (itr->second->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo))
1417            continue;
1418
1419        CharacterDatabase.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,amount,maxduration,remaintime,remaincharges) "
1420            "VALUES ('%u', '" I64FMTD "', '%u', '%u', '%d', '%d', '%d', '%d')",
1421            m_charmInfo->GetPetNumber(), itr->second->GetCasterGUID(),(uint32)(*itr).second->GetId(), (uint32)(*itr).second->GetEffIndex(),(*itr).second->GetModifier()->m_amount,int((*itr).second->GetAuraMaxDuration()),int((*itr).second->GetAuraDuration()),int((*itr).second->m_procCharges));
1422    }
1423}
1424
1425bool Pet::addSpell(uint16 spell_id, ActiveStates active, PetSpellState state, uint16 slot_id, PetSpellType type)
1426{
1427    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id);
1428    if (!spellInfo)
1429    {
1430        // do pet spell book cleanup
1431        if(state == PETSPELL_UNCHANGED)                     // spell load case
1432        {
1433            sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id);
1434            CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1435        }
1436        else
1437            sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id);
1438
1439        return false;
1440    }
1441    // same spells don't have autocast option
1442    if (spellInfo->AttributesEx & SPELL_ATTR_EX_PET_NOT_AUTOCAST)
1443        active = ACT_CAST;
1444
1445    PetSpellMap::iterator itr = m_spells.find(spell_id);
1446    if (itr != m_spells.end())
1447    {
1448        if (itr->second->state == PETSPELL_REMOVED)
1449        {
1450            delete itr->second;
1451            m_spells.erase(itr);
1452            state = PETSPELL_CHANGED;
1453        }
1454        else if (state == PETSPELL_UNCHANGED && itr->second->state != PETSPELL_UNCHANGED)
1455        {
1456            // can be in case spell loading but learned at some previous spell loading
1457            itr->second->state = PETSPELL_UNCHANGED;
1458            return false;
1459        }
1460        else
1461            return false;
1462    }
1463
1464    uint32 oldspell_id = 0;
1465
1466    PetSpell *newspell = new PetSpell;
1467    newspell->state = state;
1468    newspell->type = type;
1469
1470    if(active == ACT_DECIDE)                                //active was not used before, so we save it's autocast/passive state here
1471    {
1472        if(IsPassiveSpell(spell_id))
1473            newspell->active = ACT_PASSIVE;
1474        else
1475            newspell->active = ACT_DISABLED;
1476    }
1477    else
1478        newspell->active = active;
1479
1480    uint32 chainstart = spellmgr.GetFirstSpellInChain(spell_id);
1481
1482    for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); itr++)
1483    {
1484        if(itr->second->state == PETSPELL_REMOVED) continue;
1485
1486        if(spellmgr.GetFirstSpellInChain(itr->first) == chainstart)
1487        {
1488            slot_id = itr->second->slotId;
1489            newspell->active = itr->second->active;
1490
1491            if(newspell->active == ACT_ENABLED)
1492                ToggleAutocast(itr->first, false);
1493
1494            oldspell_id = itr->first;
1495            removeSpell(itr->first);
1496        }
1497    }
1498
1499    uint16 tmpslot=slot_id;
1500
1501    if (tmpslot == 0xffff)
1502    {
1503        uint16 maxid = 0;
1504        PetSpellMap::iterator itr;
1505        for (itr = m_spells.begin(); itr != m_spells.end(); ++itr)
1506        {
1507            if(itr->second->state == PETSPELL_REMOVED) continue;
1508            if (itr->second->slotId > maxid) maxid = itr->second->slotId;
1509        }
1510        tmpslot = maxid + 1;
1511    }
1512
1513    newspell->slotId = tmpslot;
1514    m_spells[spell_id] = newspell;
1515
1516    if (IsPassiveSpell(spell_id))
1517        CastSpell(this, spell_id, true);
1518    else if(state == PETSPELL_NEW)
1519        m_charmInfo->AddSpellToAB(oldspell_id, spell_id, active);
1520
1521    if(newspell->active == ACT_ENABLED)
1522        ToggleAutocast(spell_id, true);
1523
1524    return true;
1525}
1526
1527bool Pet::learnSpell(uint16 spell_id)
1528{
1529    // prevent duplicated entires in spell book
1530    if (!addSpell(spell_id))
1531        return false;
1532
1533    Unit* owner = GetOwner();
1534    if(owner->GetTypeId()==TYPEID_PLAYER)
1535        ((Player*)owner)->PetSpellInitialize();
1536    return true;
1537}
1538
1539void Pet::removeSpell(uint16 spell_id)
1540{
1541    PetSpellMap::iterator itr = m_spells.find(spell_id);
1542    if (itr == m_spells.end())
1543        return;
1544
1545    if(itr->second->state == PETSPELL_REMOVED)
1546        return;
1547
1548    if(itr->second->state == PETSPELL_NEW)
1549    {
1550        delete itr->second;
1551        m_spells.erase(itr);
1552    }
1553    else
1554        itr->second->state = PETSPELL_REMOVED;
1555
1556    RemoveAurasDueToSpell(spell_id);
1557}
1558
1559bool Pet::_removeSpell(uint16 spell_id)
1560{
1561    PetSpellMap::iterator itr = m_spells.find(spell_id);
1562    if (itr != m_spells.end())
1563    {
1564        delete itr->second;
1565        m_spells.erase(itr);
1566        return true;
1567    }
1568    return false;
1569}
1570
1571void Pet::InitPetCreateSpells()
1572{
1573    m_charmInfo->InitPetActionBar();
1574
1575    m_spells.clear();
1576    int32 usedtrainpoints = 0, petspellid;
1577    PetCreateSpellEntry const* CreateSpells = objmgr.GetPetCreateSpellEntry(GetEntry());
1578    if(CreateSpells)
1579    {
1580        for(uint8 i = 0; i < 4; i++)
1581        {
1582            if(!CreateSpells->spellid[i])
1583                break;
1584
1585            SpellEntry const *learn_spellproto = sSpellStore.LookupEntry(CreateSpells->spellid[i]);
1586            if(!learn_spellproto)
1587                continue;
1588
1589            if(learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_SPELL || learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_PET_SPELL)
1590            {
1591                petspellid = learn_spellproto->EffectTriggerSpell[0];
1592                Unit* owner = GetOwner();
1593                if(owner->GetTypeId() == TYPEID_PLAYER && !((Player*)owner)->HasSpell(learn_spellproto->Id))
1594                {
1595                    if(IsPassiveSpell(petspellid))          //learn passive skills when tamed, not sure if thats right
1596                        ((Player*)owner)->learnSpell(learn_spellproto->Id);
1597                    else
1598                        AddTeachSpell(learn_spellproto->EffectTriggerSpell[0], learn_spellproto->Id);
1599                }
1600            }
1601            else
1602                petspellid = learn_spellproto->Id;
1603
1604            addSpell(petspellid);
1605
1606            SkillLineAbilityMap::const_iterator lower = spellmgr.GetBeginSkillLineAbilityMap(learn_spellproto->EffectTriggerSpell[0]);
1607            SkillLineAbilityMap::const_iterator upper = spellmgr.GetEndSkillLineAbilityMap(learn_spellproto->EffectTriggerSpell[0]);
1608
1609            for(SkillLineAbilityMap::const_iterator _spell_idx = lower; _spell_idx != upper; ++_spell_idx)
1610            {
1611                usedtrainpoints += _spell_idx->second->reqtrainpoints;
1612                break;
1613            }
1614        }
1615    }
1616
1617    LearnPetPassives();
1618
1619    CastPetAuras(false);
1620
1621    SetTP(-usedtrainpoints);
1622}
1623
1624void Pet::CheckLearning(uint32 spellid)
1625{
1626                                                            //charmed case -> prevent crash
1627    if(GetTypeId() == TYPEID_PLAYER || getPetType() != HUNTER_PET)
1628        return;
1629
1630    Unit* owner = GetOwner();
1631
1632    if(m_teachspells.empty() || !owner || owner->GetTypeId() != TYPEID_PLAYER)
1633        return;
1634
1635    TeachSpellMap::iterator itr = m_teachspells.find(spellid);
1636    if(itr == m_teachspells.end())
1637        return;
1638
1639    if(urand(0, 100) < 10)
1640    {
1641        ((Player*)owner)->learnSpell(itr->second);
1642        m_teachspells.erase(itr);
1643    }
1644}
1645
1646uint32 Pet::resetTalentsCost() const
1647{
1648    uint32 days = (sWorld.GetGameTime() - m_resetTalentsTime)/DAY;
1649
1650    // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1651    if(m_resetTalentsCost < 10*SILVER || days > 0)
1652        return 10*SILVER;
1653    // then 50 silver
1654    else if(m_resetTalentsCost < 50*SILVER)
1655        return 50*SILVER;
1656    // then 1 gold
1657    else if(m_resetTalentsCost < 1*GOLD)
1658        return 1*GOLD;
1659    // then increasing at a rate of 1 gold; cap 10 gold
1660    else
1661        return (m_resetTalentsCost + 1*GOLD > 10*GOLD ? 10*GOLD : m_resetTalentsCost + 1*GOLD);
1662}
1663
1664void Pet::ToggleAutocast(uint32 spellid, bool apply)
1665{
1666    if(IsPassiveSpell(spellid))
1667        return;
1668
1669    /*if(const SpellEntry *tempSpell = GetSpellStore()->LookupEntry(spellid))
1670        if(tempSpell->EffectImplicitTargetA[0] != TARGET_ALL_AROUND_CASTER
1671            && tempSpell->EffectImplicitTargetA[0] != TARGET_CHAIN_DAMAGE)
1672            return;    */
1673
1674    PetSpellMap::const_iterator itr = m_spells.find((uint16)spellid);
1675
1676    int i;
1677
1678    if(apply)
1679    {
1680        for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; i++);
1681        if (i == m_autospells.size())
1682        {
1683            m_autospells.push_back(spellid);
1684            itr->second->active = ACT_ENABLED;
1685            itr->second->state = PETSPELL_CHANGED;
1686        }
1687    }
1688    else
1689    {
1690        AutoSpellList::iterator itr2 = m_autospells.begin();
1691        for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; i++, itr2++);
1692        if (i < m_autospells.size())
1693        {
1694            m_autospells.erase(itr2);
1695            itr->second->active = ACT_DISABLED;
1696            itr->second->state = PETSPELL_CHANGED;
1697        }
1698    }
1699}
1700
1701bool Pet::Create(uint32 guidlow, Map *map, uint32 Entry, uint32 pet_number)
1702{
1703    SetMapId(map->GetId());
1704    SetInstanceId(map->GetInstanceId());
1705
1706    Object::_Create(guidlow, pet_number, HIGHGUID_PET);
1707
1708    m_DBTableGuid = guidlow;
1709    m_originalEntry = Entry;
1710
1711    if(!InitEntry(Entry))
1712        return false;
1713
1714    SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
1715    SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_UNK3 | UNIT_BYTE2_FLAG_AURAS | UNIT_BYTE2_FLAG_UNK5 );
1716
1717    if(getPetType() == MINI_PET)                            // always non-attackable
1718        SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
1719
1720    return true;
1721}
1722
1723bool Pet::HasSpell(uint32 spell) const
1724{
1725    return (m_spells.find(spell) != m_spells.end());
1726}
1727
1728// Get all passive spells in our skill line
1729void Pet::LearnPetPassives()
1730{
1731    CreatureInfo const* cInfo = GetCreatureInfo();
1732    if(!cInfo)
1733        return;
1734
1735    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1736    if(!cFamily)
1737        return;
1738
1739    PetFamilySpellsStore::const_iterator petStore = sPetFamilySpellsStore.find(cFamily->ID);
1740    if(petStore != sPetFamilySpellsStore.end())
1741    {
1742        for(PetFamilySpellsSet::const_iterator petSet = petStore->second.begin(); petSet != petStore->second.end(); ++petSet)
1743            addSpell(*petSet, ACT_DECIDE, PETSPELL_NEW, 0xffff, PETSPELL_FAMILY);
1744    }
1745}
1746
1747void Pet::CastPetAuras(bool current)
1748{
1749    Unit* owner = GetOwner();
1750    if(!owner)
1751        return;
1752
1753    if(getPetType() != HUNTER_PET && (getPetType() != SUMMON_PET || owner->getClass() != CLASS_WARLOCK))
1754        return;
1755
1756    for(PetAuraSet::iterator itr = owner->m_petAuras.begin(); itr != owner->m_petAuras.end(); )
1757    {
1758        PetAura const* pa = *itr;
1759        ++itr;
1760
1761        if(!current && pa->IsRemovedOnChangePet())
1762            owner->RemovePetAura(pa);
1763        else
1764            CastPetAura(pa);
1765    }
1766}
1767
1768void Pet::CastPetAura(PetAura const* aura)
1769{
1770    uint16 auraId = aura->GetAura(GetEntry());
1771    if(!auraId)
1772        return;
1773
1774    if(auraId == 35696)                                       // Demonic Knowledge
1775    {
1776        int32 basePoints = int32(aura->GetDamage() * (GetStat(STAT_STAMINA) + GetStat(STAT_INTELLECT)) / 100);
1777        CastCustomSpell(this,auraId,&basePoints, NULL, NULL, true );
1778    }
1779    else
1780        CastSpell(this, auraId, true);
1781}
Note: See TracBrowser for help on using the browser.