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

Revision 218, 59.7 kB (checked in by yumileroy, 17 years ago)

[svn] Fix a typo that makes pet cannot autocast. Thank Elron for pointing out the mistake.

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