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

Revision 102, 59.0 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

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